Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: devtools: main.go #5658

Merged
merged 3 commits into from
Feb 8, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
199 changes: 99 additions & 100 deletions venus-devtool/inline-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,109 +20,108 @@ const (
)

func main() {
db, err := os.ReadFile(os.Args[2])
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 {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
if filepath.Ext(path) != ".go" {
return nil
}
fb, err := os.ReadFile(path)
if err != nil {
return err
}

lines := strings.Split(string(fb), "\n")

outLines := make([]string, 0, len(lines))
var templateLines []string

state := stateGlobal

rewrite := false

for i, line := range lines {
ln := i + 1
switch state {
case stateGlobal:
outLines = append(outLines, line)
if strings.TrimSpace(line) == `/* inline-gen template` {
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 */` {
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 */` { // empty line for goimports check
continue
}
fmt.Printf("generated section end %s:%d\n", path, ln)

state = stateGlobal
rewrite = true

tpl, err := template.New("").Funcs(template.FuncMap{
"import": func(v float64) string {
if v == 0 {
return "/"
}
return fmt.Sprintf("/v%d/", int(v))
},
"add": func(a, b float64) float64 {
return a + b
},
}).Parse(strings.Join(templateLines, "\n"))
if err != nil {
fmt.Printf("%s:%d: parsing template: %s\n", path, ln, err)
os.Exit(1)
}
db, err := ioutil.ReadFile(os.Args[2])
if err != nil {
log.Fatalf("Error reading file: %v", err)
}
var data map[string]interface{}
bhaskarvilles marked this conversation as resolved.
Show resolved Hide resolved
if err := json.Unmarshal(db, &data); err != nil {
log.Fatalf("Error unmarshalling JSON: %v", err)
}

err = filepath.Walk(os.Args[1], processFile)
if err != nil {
log.Fatalf("Error walking directory: %v", err)
}
}

var b bytes.Buffer
err = tpl.Execute(&b, data)
if err != nil {
fmt.Printf("%s:%d: executing template: %s\n", path, ln, err)
os.Exit(1)
}
func processFile(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
}

fileBytes, err := ioutil.ReadFile(path)
if err != nil {
return err
}

lines := strings.Split(string(fileBytes), "\n")

outLines, templateLines, err := processLines(lines)
if err != nil {
log.Printf("Error processing file %s: %v", path, err)
return nil
}

if len(templateLines) > 0 {
tpl, err := template.New("").Funcs(template.FuncMap{
"import": func(v float64) string {
if v == 0 {
return "/"
}
return fmt.Sprintf("/v%d/", int(v))
},
"add": func(a, b float64) float64 {
return a + b
},
}).Parse(strings.Join(templateLines, "\n"))
if err != nil {
return fmt.Errorf("parsing template: %v", err)
}

var b bytes.Buffer
err = tpl.Execute(&b, data)
bhaskarvilles marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return fmt.Errorf("executing template: %v", err)
}

outLines = append(outLines, strings.Split(b.String(), "\n")...)
}

if len(outLines) != len(lines) {
err = ioutil.WriteFile(path, []byte(strings.Join(outLines, "\n")), 0)
if err != nil {
return fmt.Errorf("writing file: %v", err)
}
}
return nil
}

outLines = append(outLines, strings.Split(b.String(), "\n")...)
outLines = append(outLines, line)
templateLines = nil
}
func processLines(lines []string) ([]string, []string, error) {
outLines := make([]string, 0, len(lines))
templateLines := make([]string, 0)
state := stateGlobal

for i, line := range lines {
switch state {
case stateGlobal:
outLines = append(outLines, line)
if strings.TrimSpace(line) == `/* inline-gen template` {
state = stateTemplate
}
case stateTemplate:
outLines = append(outLines, line)
if strings.TrimSpace(line) == /* inline-gen start */ {
bhaskarvilles marked this conversation as resolved.
Show resolved Hide resolved
state = stateGen
continue
}
templateLines = append(templateLines, line)
case stateGen:
if strings.TrimSpace(line) != /* inline-gen end */ {
bhaskarvilles marked this conversation as resolved.
Show resolved Hide resolved
continue
}

if rewrite {
fmt.Printf("write %s\n", path)
formatted, err := util.FmtFile("", []byte(strings.Join(outLines, "\n")))
if err != nil {
return err
}
if err := os.WriteFile(path, formatted, 0o664); err != nil {
return err
state = stateGlobal
templateLines = nil
}
}

return nil
})
if err != nil {
panic(err)
}
if state != stateGlobal {
return nil, nil, fmt.Errorf("unexpected end of file while in state %d", state)
}
return outLines, templateLines, nil
}