Skip to content

Commit

Permalink
Merge pull request #5658 from bhaskarvilles/master
Browse files Browse the repository at this point in the history
fix: devtools: main.go
  • Loading branch information
simlecode authored Feb 8, 2023
2 parents 8351e14 + dad5511 commit 95604cc
Showing 1 changed file with 88 additions and 88 deletions.
176 changes: 88 additions & 88 deletions venus-devtool/inline-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/fs"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"text/template"

"github.com/filecoin-project/venus/venus-devtool/util"
)

const (
Expand All @@ -19,110 +18,111 @@ const (
stateGen
)

var data = map[string]interface{}{}

func main() {
db, err := os.ReadFile(os.Args[2])
db, err := ioutil.ReadFile(os.Args[2])
if err != nil {
panic(err)
log.Fatalf("Error reading file: %v", err)
}
var data map[string]interface{}
if err := json.Unmarshal(db, &data); err != nil {
panic(err)
log.Fatalf("Error unmarshalling JSON: %v", 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
}
err = filepath.Walk(os.Args[1], processFile)
if err != nil {
log.Fatalf("Error walking directory: %v", err)
}
}

lines := strings.Split(string(fb), "\n")
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
}

outLines := make([]string, 0, len(lines))
var templateLines []string
fileBytes, err := ioutil.ReadFile(path)
if err != nil {
return err
}

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

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

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)
if len(templateLines) > 0 {
tpl, err := template.New("").Funcs(template.FuncMap{
"import": func(v float64) string {
if v == 0 {
return "/"
}
case stateTemplate:
outLines = append(outLines, line) // output all template lines
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)
}

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)
}
var b bytes.Buffer
err = tpl.Execute(&b, data)
if err != nil {
return fmt.Errorf("executing template: %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)
}
outLines = append(outLines, strings.Split(b.String(), "\n")...)
}

outLines = append(outLines, strings.Split(b.String(), "\n")...)
outLines = append(outLines, line)
templateLines = nil
}
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
}

if rewrite {
fmt.Printf("write %s\n", path)
formatted, err := util.FmtFile("", []byte(strings.Join(outLines, "\n")))
if err != nil {
return err
func processLines(lines []string) ([]string, []string, error) {
outLines := make([]string, 0, len(lines))
templateLines := make([]string, 0)
state := stateGlobal

for _, 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 */` {
state = stateGen
continue
}
if err := os.WriteFile(path, formatted, 0o664); err != nil {
return err
templateLines = append(templateLines, line)
case stateGen:
if strings.TrimSpace(line) != `/* inline-gen end */` {
continue
}
state = stateGlobal
templateLines = append(templateLines, line)
}

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
}

0 comments on commit 95604cc

Please sign in to comment.