lotus/gen/inline-gen/main.go

123 lines
2.5 KiB
Go
Raw Permalink Normal View History

2021-10-11 20:25:41 +00:00
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"text/template"
)
const (
stateGlobal = iota
stateTemplate
stateGen
)
func main() {
db, err := os.ReadFile(os.Args[2])
2021-10-11 20:25:41 +00:00
if err != nil {
panic(err)
}
var data map[string]interface{}
if err := json.Unmarshal(db, &data); err != nil {
panic(err)
}
err = filepath.WalkDir(os.Args[1], func(path string, d fs.DirEntry, err error) error {
2021-10-18 15:50:31 +00:00
if err != nil {
return err
}
2021-10-11 20:25:41 +00:00
if d.IsDir() {
return nil
}
if filepath.Ext(path) != ".go" {
return nil
}
fb, err := os.ReadFile(path)
2021-10-11 20:25:41 +00:00
if err != nil {
return err
}
lines := strings.Split(string(fb), "\n")
outLines := make([]string, 0, len(lines))
var templateLines []string
state := stateGlobal
2021-10-11 20:56:29 +00:00
rewrite := false
2021-10-11 20:25:41 +00:00
for i, line := range lines {
2021-10-11 20:56:29 +00:00
ln := i + 1
2021-10-11 20:25:41 +00:00
switch state {
case stateGlobal:
outLines = append(outLines, line)
2021-10-11 20:56:29 +00:00
if strings.TrimSpace(line) == `/* inline-gen template` {
2021-10-11 20:25:41 +00:00
state = stateTemplate
fmt.Printf("template section start %s:%d\n", path, ln)
}
case stateTemplate:
outLines = append(outLines, line) // output all template lines
if strings.TrimSpace(line) == `/* inline-gen start */` {
2021-10-11 20:25:41 +00:00
state = stateGen
fmt.Printf("generated section start %s:%d\n", path, ln)
continue
}
templateLines = append(templateLines, line)
case stateGen:
if strings.TrimSpace(line) != `/* inline-gen end */` {
2021-10-11 20:25:41 +00:00
continue
}
2021-10-11 20:56:29 +00:00
fmt.Printf("generated section end %s:%d\n", path, ln)
2021-10-11 20:25:41 +00:00
state = stateGlobal
2021-10-11 20:56:29 +00:00
rewrite = true
2021-10-11 20:25:41 +00:00
2021-10-11 20:56:29 +00:00
tpl, err := template.New("").Funcs(template.FuncMap{
"import": func(v float64) string {
if v == 0 {
return "/"
}
return fmt.Sprintf("/v%d/", int(v))
},
2021-10-18 15:17:54 +00:00
"add": func(a, b float64) float64 {
return a + b
},
2021-10-11 20:56:29 +00:00
}).Parse(strings.Join(templateLines, "\n"))
2021-10-11 20:25:41 +00:00
if err != nil {
fmt.Printf("%s:%d: parsing template: %s\n", path, ln, err)
os.Exit(1)
}
2021-10-11 20:56:29 +00:00
2021-10-11 20:25:41 +00:00
var b bytes.Buffer
err = tpl.Execute(&b, data)
2021-10-11 20:56:29 +00:00
if err != nil {
fmt.Printf("%s:%d: executing template: %s\n", path, ln, err)
os.Exit(1)
}
2021-10-11 20:25:41 +00:00
outLines = append(outLines, strings.Split(b.String(), "\n")...)
outLines = append(outLines, line)
2021-10-11 20:56:29 +00:00
templateLines = nil
2021-10-11 20:25:41 +00:00
}
}
2021-10-11 20:56:29 +00:00
if rewrite {
fmt.Printf("write %s\n", path)
if err := os.WriteFile(path, []byte(strings.Join(outLines, "\n")), 0664); err != nil {
2021-10-11 20:56:29 +00:00
return err
}
}
2021-10-11 20:25:41 +00:00
return nil
})
if err != nil {
panic(err)
}
}