From 235973cc2dc9e83680222b7cc475e5fa7c1dd8fb Mon Sep 17 00:00:00 2001 From: Ariel Mashraki Date: Tue, 15 Sep 2020 16:23:50 +0300 Subject: [PATCH] entc/gen: merge all templates to global --- entc/gen/graph.go | 29 +++++++++++------------------ entc/gen/template.go | 7 +++---- examples/entcpkg/ent/entc.go | 2 +- 3 files changed, 15 insertions(+), 23 deletions(-) diff --git a/entc/gen/graph.go b/entc/gen/graph.go index e9d5f938c..0d9feef40 100644 --- a/entc/gen/graph.go +++ b/entc/gen/graph.go @@ -125,11 +125,7 @@ func (g *Graph) Gen() (err error) { check(os.MkdirAll(path, os.ModePerm), "create dir %q", path) } b := bytes.NewBuffer(nil) - execT := templates - if tmpl.external != nil { - execT = tmpl.external - } - check(execT.ExecuteTemplate(b, tmpl.Name, g), "execute template %q", tmpl.Name) + check(templates.ExecuteTemplate(b, tmpl.Name, g), "execute template %q", tmpl.Name) target := filepath.Join(g.Config.Target, tmpl.Format) check(ioutil.WriteFile(target, b.Bytes(), 0644), "write file %s", target) written = append(written, target) @@ -431,23 +427,20 @@ func (g *Graph) templates() (*template.Template, []GraphTemplate) { for _, rootT := range g.Templates { rootT.Funcs(Funcs) rootT.Funcs(g.Funcs) - // Make sure external-templates have access to the default-templates. - for _, defaultT := range templates.Templates() { - rootT = template.Must(rootT.AddParseTree(defaultT.Name(), defaultT.Tree)) - } for _, tmpl := range rootT.Templates() { - switch name := tmpl.Name(); { - case parse.IsEmptyTree(tmpl.Root): - // If this template overrides or extends one of the default templates. - case templates.Lookup(name) != nil || extendExisting(name): - templates = template.Must(templates.AddParseTree(name, tmpl.Tree)) - default: + if parse.IsEmptyTree(tmpl.Root) { + continue + } + name := tmpl.Name() + // If this template doesn't override or extend one of the + // default templates, generate it in a new file. + if templates.Lookup(name) == nil && !extendExisting(name) { external = append(external, GraphTemplate{ - Name: name, - Format: snake(name) + ".go", - external: rootT, + Name: name, + Format: snake(name) + ".go", }) } + templates = template.Must(templates.AddParseTree(name, tmpl.Tree)) } } return templates, external diff --git a/entc/gen/template.go b/entc/gen/template.go index 78d72e930..13a84686b 100644 --- a/entc/gen/template.go +++ b/entc/gen/template.go @@ -29,10 +29,9 @@ type ( // GraphTemplate specifies a template that is executed with // the Graph object. GraphTemplate struct { - Name string // template name. - Skip func(*Graph) bool // skip condition. - Format string // file name format. - external *template.Template // external template. + Name string // template name. + Skip func(*Graph) bool // skip condition. + Format string // file name format. } ) diff --git a/examples/entcpkg/ent/entc.go b/examples/entcpkg/ent/entc.go index 915650fc5..6e6eb874d 100644 --- a/examples/entcpkg/ent/entc.go +++ b/examples/entcpkg/ent/entc.go @@ -38,6 +38,6 @@ func main() { }, }, opts...) if err != nil { - log.Fatal("running ent codegen:", err) + log.Fatalf("running ent codegen: %v", err) } }