lotus/chain/actors/agen/main.go

219 lines
4.4 KiB
Go
Raw Normal View History

2021-04-26 14:48:20 +00:00
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
2021-04-26 14:48:20 +00:00
"path/filepath"
"text/template"
2021-04-30 16:02:57 +00:00
"golang.org/x/xerrors"
2021-04-26 14:48:20 +00:00
)
var latestVersion = 4
var versions = []int{0, 2, 3, latestVersion}
var versionImports = map[int]string{
0: "/",
2: "/v2/",
3: "/v3/",
latestVersion: "/v4/",
}
var actors = map[string][]int{
"account": versions,
"cron": versions,
"init": versions,
"market": versions,
"miner": versions,
"multisig": versions,
"paych": versions,
"power": versions,
"reward": versions,
"verifreg": versions,
}
2021-04-26 14:48:20 +00:00
func main() {
2021-04-30 16:43:06 +00:00
if err := generateAdapters(); err != nil {
2021-04-26 14:48:20 +00:00
fmt.Println(err)
return
}
2021-05-04 17:53:08 +00:00
if err := generatePolicy("chain/actors/policy/policy.go"); err != nil {
fmt.Println(err)
return
}
2021-05-06 04:17:35 +00:00
if err := generateBuiltin("chain/actors/builtin/builtin.go"); err != nil {
fmt.Println(err)
return
}
2021-04-26 14:48:20 +00:00
}
2021-04-30 16:43:06 +00:00
func generateAdapters() error {
2021-04-26 14:48:20 +00:00
for act, versions := range actors {
actDir := filepath.Join("chain/actors/builtin", act)
if err := generateState(actDir); err != nil {
return err
}
2021-04-26 14:48:20 +00:00
if err := generateMessages(actDir); err != nil {
return err
2021-04-26 14:48:20 +00:00
}
{
af, err := ioutil.ReadFile(filepath.Join(actDir, "actor.go.template"))
if err != nil {
return xerrors.Errorf("loading actor template: %w", err)
}
tpl := template.Must(template.New("").Funcs(template.FuncMap{
"import": func(v int) string { return versionImports[v] },
2021-04-26 14:48:20 +00:00
}).Parse(string(af)))
var b bytes.Buffer
err = tpl.Execute(&b, map[string]interface{}{
"versions": versions,
"latestVersion": latestVersion,
2021-04-26 14:48:20 +00:00
})
if err != nil {
return err
}
if err := ioutil.WriteFile(filepath.Join(actDir, fmt.Sprintf("%s.go", act)), b.Bytes(), 0666); err != nil {
return err
}
}
}
return nil
}
func generateState(actDir string) error {
af, err := ioutil.ReadFile(filepath.Join(actDir, "state.go.template"))
if err != nil {
if os.IsNotExist(err) {
return nil // skip
}
return xerrors.Errorf("loading state adapter template: %w", err)
}
for _, version := range versions {
tpl := template.Must(template.New("").Funcs(template.FuncMap{}).Parse(string(af)))
var b bytes.Buffer
err := tpl.Execute(&b, map[string]interface{}{
"v": version,
"import": versionImports[version],
})
if err != nil {
return err
}
if err := ioutil.WriteFile(filepath.Join(actDir, fmt.Sprintf("v%d.go", version)), b.Bytes(), 0666); err != nil {
return err
}
}
return nil
}
func generateMessages(actDir string) error {
af, err := ioutil.ReadFile(filepath.Join(actDir, "message.go.template"))
if err != nil {
if os.IsNotExist(err) {
return nil // skip
}
2021-04-28 14:20:53 +00:00
return xerrors.Errorf("loading message adapter template: %w", err)
}
for _, version := range versions {
tpl := template.Must(template.New("").Funcs(template.FuncMap{}).Parse(string(af)))
var b bytes.Buffer
err := tpl.Execute(&b, map[string]interface{}{
"v": version,
"import": versionImports[version],
})
if err != nil {
return err
}
if err := ioutil.WriteFile(filepath.Join(actDir, fmt.Sprintf("message%d.go", version)), b.Bytes(), 0666); err != nil {
return err
}
}
return nil
}
2021-05-04 17:53:08 +00:00
func generatePolicy(policyPath string) error {
pf, err := ioutil.ReadFile(policyPath + ".template")
if err != nil {
if os.IsNotExist(err) {
return nil // skip
}
2021-05-06 04:17:35 +00:00
return xerrors.Errorf("loading policy template file: %w", err)
2021-05-04 17:53:08 +00:00
}
tpl := template.Must(template.New("").Funcs(template.FuncMap{
"import": func(v int) string { return versionImports[v] },
2021-05-04 17:53:08 +00:00
}).Parse(string(pf)))
var b bytes.Buffer
err = tpl.Execute(&b, map[string]interface{}{
"versions": versions,
"latestVersion": latestVersion,
2021-05-04 17:53:08 +00:00
})
if err != nil {
return err
}
if err := ioutil.WriteFile(policyPath, b.Bytes(), 0666); err != nil {
return err
}
return nil
}
2021-05-06 04:17:35 +00:00
func generateBuiltin(builtinPath string) error {
bf, err := ioutil.ReadFile(builtinPath + ".template")
if err != nil {
if os.IsNotExist(err) {
return nil // skip
}
return xerrors.Errorf("loading builtin template file: %w", err)
}
tpl := template.Must(template.New("").Funcs(template.FuncMap{
"import": func(v int) string { return versionImports[v] },
2021-05-06 04:17:35 +00:00
}).Parse(string(bf)))
var b bytes.Buffer
err = tpl.Execute(&b, map[string]interface{}{
"versions": versions,
"latestVersion": latestVersion,
2021-05-06 04:17:35 +00:00
})
if err != nil {
return err
}
if err := ioutil.WriteFile(builtinPath, b.Bytes(), 0666); err != nil {
return err
}
return nil
}