mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 15:45:38 +00:00
0f05231c35
Signed-off-by: Elias Naur <mail@eliasnaur.com>
58 lines
1.3 KiB
Go
58 lines
1.3 KiB
Go
// SPDX-License-Identifier: Unlicense OR MIT
|
|
|
|
// Command assets converts data files to Go source code.
|
|
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
func main() {
|
|
if len(os.Args) != 2 {
|
|
fmt.Fprintln(os.Stderr, "please specify a directory to convert")
|
|
os.Exit(1)
|
|
}
|
|
var w bytes.Buffer
|
|
w.WriteString("// Code generated by command assets DO NOT EDIT.\n\n")
|
|
w.WriteString("package assets\n\n")
|
|
w.WriteString("import (\n")
|
|
w.WriteString("\t\"io\"\n")
|
|
w.WriteString("\t\"strings\"\n")
|
|
w.WriteString(")\n\n")
|
|
dir := os.Args[1]
|
|
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if info.IsDir() {
|
|
return nil
|
|
}
|
|
if filepath.Ext(path) == ".go" {
|
|
return nil
|
|
}
|
|
name := path[len(dir)+1:]
|
|
name = strings.ReplaceAll(name, "/", "_")
|
|
name = strings.ReplaceAll(name, ".", "_")
|
|
name = strings.Title(name)
|
|
w.WriteString(fmt.Sprintf("func %s() io.Reader {\n", name))
|
|
content, err := ioutil.ReadFile(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
w.WriteString(fmt.Sprintf("\tcontent := %q\n", content))
|
|
w.WriteString("\treturn strings.NewReader(content)\n")
|
|
w.WriteString("}\n")
|
|
return nil
|
|
})
|
|
err := ioutil.WriteFile(filepath.Join(dir, "assets.go"), w.Bytes(), 0644)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "%v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|