From ddef7081786ed746de47f6c50c5a8da6f5d68905 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Mon, 11 Oct 2021 22:25:41 +0200 Subject: [PATCH] Basic inline codegen --- build/params_shared_vals.go | 8 ++++ gen/inline-gen/main.go | 95 +++++++++++++++++++++++++++++++++++++ gen/inlinegen-data.json | 7 +++ 3 files changed, 110 insertions(+) create mode 100644 gen/inline-gen/main.go create mode 100644 gen/inlinegen-data.json diff --git a/build/params_shared_vals.go b/build/params_shared_vals.go index 22d1c30e3..f15cbc35c 100644 --- a/build/params_shared_vals.go +++ b/build/params_shared_vals.go @@ -28,8 +28,16 @@ const UnixfsLinksPerLevel = 1024 const AllowableClockDriftSecs = uint64(1) // TODO: This is still terrible...What's the impact of updating this before mainnet actually upgrades +/* inline-gen template + +const NewestNetworkVersion = network.Version{{.latestNetworkVersion}} + +inline-gen start */ + const NewestNetworkVersion = network.Version14 +//inline-gen end + // Epochs const ForkLengthThreshold = Finality diff --git a/gen/inline-gen/main.go b/gen/inline-gen/main.go new file mode 100644 index 000000000..4e669099f --- /dev/null +++ b/gen/inline-gen/main.go @@ -0,0 +1,95 @@ +package main + +import ( + "bytes" + "encoding/json" + "fmt" + "io/fs" + "io/ioutil" + "os" + "path/filepath" + "strings" + "text/template" +) + +const ( + stateGlobal = iota + stateTemplate + stateGen +) + +func main() { + db, err := ioutil.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 d.IsDir() { + return nil + } + if filepath.Ext(path) != ".go" { + return nil + } + fb, err := ioutil.ReadFile(path) + if err != nil { + return err + } + + lines := strings.Split(string(fb), "\n") + + outLines := make([]string, 0, len(lines)) + var templateLines []string + + state := stateGlobal + + for i, line := range lines { + ln := i+1 + switch state { + case stateGlobal: + outLines = append(outLines, line) + if 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 line == `inline-gen start */` { + state = stateGen + fmt.Printf("generated section start %s:%d\n", path, ln) + continue + } + templateLines = append(templateLines, line) + case stateGen: + if line != `//inline-gen end` { + continue + } + state = stateGlobal + fmt.Printf("inline gen:\n") + fmt.Println(strings.Join(templateLines, "\n")) + + tpl, err := template.New("").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) + + outLines = append(outLines, strings.Split(b.String(), "\n")...) + fmt.Println("inline gen-ed:\n", b.String()) + + outLines = append(outLines, line) + } + } + return nil + }) + if err != nil { + panic(err) + } +} diff --git a/gen/inlinegen-data.json b/gen/inlinegen-data.json new file mode 100644 index 000000000..1c8a4ecd8 --- /dev/null +++ b/gen/inlinegen-data.json @@ -0,0 +1,7 @@ +{ + "actorVersions": [0, 2, 3, 4, 5, 6], + "latestActorsVersion": 6, + + "networkVersions": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], + "latestNetworkVersion": 14 +} \ No newline at end of file