feat(abigen): Add flag let be able to add user tmpl file. (#362)

* Add user tmpl file.

* Update accounts/abi/bind/template.go

Co-authored-by: HAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com>

* Update cmd/abigen/main.go

Co-authored-by: HAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com>

* Update cmd/abigen/main.go

Co-authored-by: HAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com>

* Update cmd/abigen/main.go

Co-authored-by: HAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com>

* fix bug

---------

Co-authored-by: HAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com>
This commit is contained in:
maskpp 2023-06-19 11:32:20 +08:00 committed by GitHub
parent b5e8d12c06
commit 43be9a6450
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 0 deletions

View file

@ -79,6 +79,13 @@ var tmplSource = map[Lang]string{
LangJava: tmplSourceJava,
}
// SetTmplSource supports this func in order to set special template file.
func SetTmplSource(lang Lang, source string) {
if _, ok := tmplSource[lang]; ok {
tmplSource[lang] = source
}
}
// tmplSourceGo is the Go source template that the generated Go contract binding
// is based on.
const tmplSourceGo = `

View file

@ -103,6 +103,10 @@ var (
Name: "contract",
Usage: "Name of the contract to generate the bindings for",
}
tmplFlag = cli.StringFlag{
Name: "tmpl",
Usage: "Template file if a user wants to customize",
}
)
func init() {
@ -122,6 +126,7 @@ func init() {
langFlag,
aliasFlag,
contractFlag,
tmplFlag,
}
app.Action = utils.MigrateFlags(abigen)
cli.CommandHelpTemplate = flags.OriginCommandHelpTemplate
@ -265,6 +270,15 @@ func abigen(c *cli.Context) error {
aliases[match[1]] = match[2]
}
}
// Set customize template file.
if c.GlobalIsSet(tmplFlag.Name) {
tmplFile := c.GlobalString(tmplFlag.Name)
data, err := os.ReadFile(tmplFile)
if err != nil {
utils.Fatalf("Failed to read template file: %v", err)
}
bind.SetTmplSource(lang, string(data))
}
// Generate the contract binding
code, err := bind.Bind(types, abis, bins, sigs, c.GlobalString(pkgFlag.Name), lang, libs, aliases)
if err != nil {