From dfe6e885291932ad7ec3d79437827fe976f74419 Mon Sep 17 00:00:00 2001 From: kevin Date: Mon, 19 Oct 2020 23:13:18 +0800 Subject: [PATCH] use goctl template to generate all kinds of templates --- core/errorx/callchain.go | 11 ++++++ core/errorx/callchain_test.go | 27 ++++++++++++++ tools/goctl/api/gogen/genconfig.go | 4 +-- tools/goctl/api/gogen/genetc.go | 4 +-- tools/goctl/api/gogen/genhandlers.go | 3 +- tools/goctl/api/gogen/genlogic.go | 3 +- tools/goctl/api/gogen/genmain.go | 3 +- tools/goctl/api/gogen/gensvc.go | 3 +- tools/goctl/api/gogen/template.go | 4 +-- tools/goctl/goctl.go | 12 +++---- tools/goctl/model/sql/gen/delete.go | 4 +-- tools/goctl/model/sql/gen/field.go | 4 +-- tools/goctl/model/sql/gen/findone.go | 4 +-- tools/goctl/model/sql/gen/findonebyfield.go | 6 ++-- tools/goctl/model/sql/gen/gen.go | 3 +- tools/goctl/model/sql/gen/imports.go | 6 ++-- tools/goctl/model/sql/gen/insert.go | 4 +-- tools/goctl/model/sql/gen/new.go | 4 +-- tools/goctl/model/sql/gen/tag.go | 4 +-- tools/goctl/model/sql/gen/types.go | 4 +-- tools/goctl/model/sql/gen/update.go | 4 +-- tools/goctl/model/sql/gen/vars.go | 4 +-- tools/goctl/rpc/gen/gencall.go | 11 +++--- tools/goctl/rpc/gen/genetc.go | 3 +- tools/goctl/rpc/gen/genlogic.go | 5 ++- tools/goctl/rpc/gen/genmain.go | 5 ++- tools/goctl/rpc/gen/genserver.go | 7 ++-- tools/goctl/rpc/gen/gensvc.go | 4 +-- tools/goctl/rpc/gen/template.go | 4 +-- tools/goctl/rpc/parser/pbast.go | 7 ++-- tools/goctl/rpc/parser/proto.go | 7 ++-- tools/goctl/tpl/templates.go | 33 +++++++++++++++++ tools/goctl/{templatex => util}/files.go | 37 ++++++++------------ tools/goctl/{templatex => util}/head.go | 2 +- tools/goctl/{templatex => util}/templatex.go | 6 ++-- 35 files changed, 152 insertions(+), 104 deletions(-) create mode 100644 core/errorx/callchain.go create mode 100644 core/errorx/callchain_test.go create mode 100644 tools/goctl/tpl/templates.go rename tools/goctl/{templatex => util}/files.go (67%) rename tools/goctl/{templatex => util}/head.go (93%) rename tools/goctl/{templatex => util}/templatex.go (91%) diff --git a/core/errorx/callchain.go b/core/errorx/callchain.go new file mode 100644 index 00000000..fb9226b7 --- /dev/null +++ b/core/errorx/callchain.go @@ -0,0 +1,11 @@ +package errorx + +func Chain(fns ...func() error) error { + for _, fn := range fns { + if err := fn(); err != nil { + return err + } + } + + return nil +} diff --git a/core/errorx/callchain_test.go b/core/errorx/callchain_test.go new file mode 100644 index 00000000..31ebbc0b --- /dev/null +++ b/core/errorx/callchain_test.go @@ -0,0 +1,27 @@ +package errorx + +import ( + "errors" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestChain(t *testing.T) { + var errDummy = errors.New("dummy") + assert.Nil(t, Chain(func() error { + return nil + }, func() error { + return nil + })) + assert.Equal(t, errDummy, Chain(func() error { + return errDummy + }, func() error { + return nil + })) + assert.Equal(t, errDummy, Chain(func() error { + return nil + }, func() error { + return errDummy + })) +} diff --git a/tools/goctl/api/gogen/genconfig.go b/tools/goctl/api/gogen/genconfig.go index 8410cfb9..0e3f0562 100644 --- a/tools/goctl/api/gogen/genconfig.go +++ b/tools/goctl/api/gogen/genconfig.go @@ -8,7 +8,7 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/api/spec" "github.com/tal-tech/go-zero/tools/goctl/api/util" - "github.com/tal-tech/go-zero/tools/goctl/templatex" + ctlutil "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/vars" ) @@ -48,7 +48,7 @@ func genConfig(dir string, api *spec.ApiSpec) error { } var authImportStr = fmt.Sprintf("\"%s/rest\"", vars.ProjectOpenSourceUrl) - text, err := templatex.LoadTemplate(category, configTemplateFile, configTemplate) + text, err := ctlutil.LoadTemplate(category, configTemplateFile, configTemplate) if err != nil { return err } diff --git a/tools/goctl/api/gogen/genetc.go b/tools/goctl/api/gogen/genetc.go index 20dde425..ad6b312f 100644 --- a/tools/goctl/api/gogen/genetc.go +++ b/tools/goctl/api/gogen/genetc.go @@ -8,7 +8,7 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/api/spec" "github.com/tal-tech/go-zero/tools/goctl/api/util" - "github.com/tal-tech/go-zero/tools/goctl/templatex" + ctlutil "github.com/tal-tech/go-zero/tools/goctl/util" ) const ( @@ -40,7 +40,7 @@ func genEtc(dir string, api *spec.ApiSpec) error { port = strconv.Itoa(defaultPort) } - text, err := templatex.LoadTemplate(category, etcTemplateFile, etcTemplate) + text, err := ctlutil.LoadTemplate(category, etcTemplateFile, etcTemplate) if err != nil { return err } diff --git a/tools/goctl/api/gogen/genhandlers.go b/tools/goctl/api/gogen/genhandlers.go index 31d02151..5c7802fc 100644 --- a/tools/goctl/api/gogen/genhandlers.go +++ b/tools/goctl/api/gogen/genhandlers.go @@ -9,7 +9,6 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/api/spec" apiutil "github.com/tal-tech/go-zero/tools/goctl/api/util" - "github.com/tal-tech/go-zero/tools/goctl/templatex" "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/vars" ) @@ -94,7 +93,7 @@ func doGenToFile(dir, handler string, group spec.Group, route spec.Route, handle } defer fp.Close() - text, err := templatex.LoadTemplate(category, handlerTemplateFile, handlerTemplate) + text, err := util.LoadTemplate(category, handlerTemplateFile, handlerTemplate) if err != nil { return err } diff --git a/tools/goctl/api/gogen/genlogic.go b/tools/goctl/api/gogen/genlogic.go index e27d3fd5..2c4e17b2 100644 --- a/tools/goctl/api/gogen/genlogic.go +++ b/tools/goctl/api/gogen/genlogic.go @@ -9,7 +9,6 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/api/spec" "github.com/tal-tech/go-zero/tools/goctl/api/util" - "github.com/tal-tech/go-zero/tools/goctl/templatex" ctlutil "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/vars" ) @@ -94,7 +93,7 @@ func genLogicByRoute(dir string, group spec.Group, route spec.Route) error { requestString = "req " + "types." + strings.Title(route.RequestType.Name) } - text, err := templatex.LoadTemplate(category, logicTemplateFile, logicTemplate) + text, err := ctlutil.LoadTemplate(category, logicTemplateFile, logicTemplate) if err != nil { return err } diff --git a/tools/goctl/api/gogen/genmain.go b/tools/goctl/api/gogen/genmain.go index 76d486c0..9b19af3c 100644 --- a/tools/goctl/api/gogen/genmain.go +++ b/tools/goctl/api/gogen/genmain.go @@ -8,7 +8,6 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/api/spec" "github.com/tal-tech/go-zero/tools/goctl/api/util" - "github.com/tal-tech/go-zero/tools/goctl/templatex" ctlutil "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/vars" ) @@ -61,7 +60,7 @@ func genMain(dir string, api *spec.ApiSpec) error { return err } - text, err := templatex.LoadTemplate(category, mainTemplateFile, mainTemplate) + text, err := ctlutil.LoadTemplate(category, mainTemplateFile, mainTemplate) if err != nil { return err } diff --git a/tools/goctl/api/gogen/gensvc.go b/tools/goctl/api/gogen/gensvc.go index e71c17d1..16ece42a 100644 --- a/tools/goctl/api/gogen/gensvc.go +++ b/tools/goctl/api/gogen/gensvc.go @@ -7,7 +7,6 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/api/spec" "github.com/tal-tech/go-zero/tools/goctl/api/util" - "github.com/tal-tech/go-zero/tools/goctl/templatex" ctlutil "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/vars" ) @@ -52,7 +51,7 @@ func genServiceContext(dir string, api *spec.ApiSpec) error { return err } - text, err := templatex.LoadTemplate(category, contextTemplateFile, contextTemplate) + text, err := ctlutil.LoadTemplate(category, contextTemplateFile, contextTemplate) if err != nil { return err } diff --git a/tools/goctl/api/gogen/template.go b/tools/goctl/api/gogen/template.go index 338e933a..1ce2c15d 100644 --- a/tools/goctl/api/gogen/template.go +++ b/tools/goctl/api/gogen/template.go @@ -1,7 +1,7 @@ package gogen import ( - "github.com/tal-tech/go-zero/tools/goctl/templatex" + "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/urfave/cli" ) @@ -25,5 +25,5 @@ var templates = map[string]string{ } func GenTemplates(_ *cli.Context) error { - return templatex.InitTemplates(category, templates) + return util.InitTemplates(category, templates) } diff --git a/tools/goctl/goctl.go b/tools/goctl/goctl.go index 316dec53..65023051 100644 --- a/tools/goctl/goctl.go +++ b/tools/goctl/goctl.go @@ -102,13 +102,6 @@ var ( }, }, Action: gogen.GoCommand, - Subcommands: []cli.Command{ - { - Name: "template", - Usage: "initialize the api templates", - Action: gogen.GenTemplates, - }, - }, }, { Name: "java", @@ -339,6 +332,11 @@ var ( Usage: "the features of the latest version", Action: feature.Feature, }, + { + Name: "template", + Usage: "initialize the api templates", + Action: gogen.GenTemplates, + }, } ) diff --git a/tools/goctl/model/sql/gen/delete.go b/tools/goctl/model/sql/gen/delete.go index 51e39211..d6c0701e 100644 --- a/tools/goctl/model/sql/gen/delete.go +++ b/tools/goctl/model/sql/gen/delete.go @@ -5,7 +5,7 @@ import ( "github.com/tal-tech/go-zero/core/collection" "github.com/tal-tech/go-zero/tools/goctl/model/sql/template" - "github.com/tal-tech/go-zero/tools/goctl/templatex" + "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/util/stringx" ) @@ -22,7 +22,7 @@ func genDelete(table Table, withCache bool) (string, error) { } camel := table.Name.ToCamel() - output, err := templatex.With("delete"). + output, err := util.With("delete"). Parse(template.Delete). Execute(map[string]interface{}{ "upperStartCamelObject": camel, diff --git a/tools/goctl/model/sql/gen/field.go b/tools/goctl/model/sql/gen/field.go index b29dcae7..6c45c2c4 100644 --- a/tools/goctl/model/sql/gen/field.go +++ b/tools/goctl/model/sql/gen/field.go @@ -5,7 +5,7 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/model/sql/parser" "github.com/tal-tech/go-zero/tools/goctl/model/sql/template" - "github.com/tal-tech/go-zero/tools/goctl/templatex" + "github.com/tal-tech/go-zero/tools/goctl/util" ) func genFields(fields []parser.Field) (string, error) { @@ -25,7 +25,7 @@ func genField(field parser.Field) (string, error) { if err != nil { return "", err } - output, err := templatex.With("types"). + output, err := util.With("types"). Parse(template.Field). Execute(map[string]interface{}{ "name": field.Name.ToCamel(), diff --git a/tools/goctl/model/sql/gen/findone.go b/tools/goctl/model/sql/gen/findone.go index 93ab2e88..8f48f873 100644 --- a/tools/goctl/model/sql/gen/findone.go +++ b/tools/goctl/model/sql/gen/findone.go @@ -2,13 +2,13 @@ package gen import ( "github.com/tal-tech/go-zero/tools/goctl/model/sql/template" - "github.com/tal-tech/go-zero/tools/goctl/templatex" + "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/util/stringx" ) func genFindOne(table Table, withCache bool) (string, error) { camel := table.Name.ToCamel() - output, err := templatex.With("findOne"). + output, err := util.With("findOne"). Parse(template.FindOne). Execute(map[string]interface{}{ "withCache": withCache, diff --git a/tools/goctl/model/sql/gen/findonebyfield.go b/tools/goctl/model/sql/gen/findonebyfield.go index d094436d..8bd1d7ba 100644 --- a/tools/goctl/model/sql/gen/findonebyfield.go +++ b/tools/goctl/model/sql/gen/findonebyfield.go @@ -5,12 +5,12 @@ import ( "strings" "github.com/tal-tech/go-zero/tools/goctl/model/sql/template" - "github.com/tal-tech/go-zero/tools/goctl/templatex" + "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/util/stringx" ) func genFindOneByField(table Table, withCache bool) (string, string, error) { - t := templatex.With("findOneByField").Parse(template.FindOneByField) + t := util.With("findOneByField").Parse(template.FindOneByField) var list []string camelTableName := table.Name.ToCamel() for _, field := range table.Fields { @@ -36,7 +36,7 @@ func genFindOneByField(table Table, withCache bool) (string, string, error) { list = append(list, output.String()) } if withCache { - out, err := templatex.With("findOneByFieldExtraMethod").Parse(template.FindOneByFieldExtraMethod).Execute(map[string]interface{}{ + out, err := util.With("findOneByFieldExtraMethod").Parse(template.FindOneByFieldExtraMethod).Execute(map[string]interface{}{ "upperStartCamelObject": camelTableName, "primaryKeyLeft": table.CacheKey[table.PrimaryKey.Name.Source()].Left, "lowerStartCamelObject": stringx.From(camelTableName).UnTitle(), diff --git a/tools/goctl/model/sql/gen/gen.go b/tools/goctl/model/sql/gen/gen.go index 6a7f0c57..f351676c 100644 --- a/tools/goctl/model/sql/gen/gen.go +++ b/tools/goctl/model/sql/gen/gen.go @@ -9,7 +9,6 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/model/sql/parser" "github.com/tal-tech/go-zero/tools/goctl/model/sql/template" - "github.com/tal-tech/go-zero/tools/goctl/templatex" "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/util/console" "github.com/tal-tech/go-zero/tools/goctl/util/stringx" @@ -120,7 +119,7 @@ type ( ) func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, error) { - t := templatex.With("model"). + t := util.With("model"). Parse(template.Model). GoFmt(true) diff --git a/tools/goctl/model/sql/gen/imports.go b/tools/goctl/model/sql/gen/imports.go index c9c62f59..6d29ca56 100644 --- a/tools/goctl/model/sql/gen/imports.go +++ b/tools/goctl/model/sql/gen/imports.go @@ -2,12 +2,12 @@ package gen import ( "github.com/tal-tech/go-zero/tools/goctl/model/sql/template" - "github.com/tal-tech/go-zero/tools/goctl/templatex" + "github.com/tal-tech/go-zero/tools/goctl/util" ) func genImports(withCache, timeImport bool) (string, error) { if withCache { - buffer, err := templatex.With("import").Parse(template.Imports).Execute(map[string]interface{}{ + buffer, err := util.With("import").Parse(template.Imports).Execute(map[string]interface{}{ "time": timeImport, }) if err != nil { @@ -15,7 +15,7 @@ func genImports(withCache, timeImport bool) (string, error) { } return buffer.String(), nil } else { - buffer, err := templatex.With("import").Parse(template.ImportsNoCache).Execute(map[string]interface{}{ + buffer, err := util.With("import").Parse(template.ImportsNoCache).Execute(map[string]interface{}{ "time": timeImport, }) if err != nil { diff --git a/tools/goctl/model/sql/gen/insert.go b/tools/goctl/model/sql/gen/insert.go index 65341586..b5600b9d 100644 --- a/tools/goctl/model/sql/gen/insert.go +++ b/tools/goctl/model/sql/gen/insert.go @@ -5,7 +5,7 @@ import ( "github.com/tal-tech/go-zero/core/collection" "github.com/tal-tech/go-zero/tools/goctl/model/sql/template" - "github.com/tal-tech/go-zero/tools/goctl/templatex" + "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/util/stringx" ) @@ -34,7 +34,7 @@ func genInsert(table Table, withCache bool) (string, error) { expressionValues = append(expressionValues, "data."+camel) } camel := table.Name.ToCamel() - output, err := templatex.With("insert"). + output, err := util.With("insert"). Parse(template.Insert). Execute(map[string]interface{}{ "withCache": withCache, diff --git a/tools/goctl/model/sql/gen/new.go b/tools/goctl/model/sql/gen/new.go index ab0cf7e6..e5afe0ea 100644 --- a/tools/goctl/model/sql/gen/new.go +++ b/tools/goctl/model/sql/gen/new.go @@ -2,11 +2,11 @@ package gen import ( "github.com/tal-tech/go-zero/tools/goctl/model/sql/template" - "github.com/tal-tech/go-zero/tools/goctl/templatex" + "github.com/tal-tech/go-zero/tools/goctl/util" ) func genNew(table Table, withCache bool) (string, error) { - output, err := templatex.With("new"). + output, err := util.With("new"). Parse(template.New). Execute(map[string]interface{}{ "withCache": withCache, diff --git a/tools/goctl/model/sql/gen/tag.go b/tools/goctl/model/sql/gen/tag.go index 3d46afd8..86f15c19 100644 --- a/tools/goctl/model/sql/gen/tag.go +++ b/tools/goctl/model/sql/gen/tag.go @@ -2,14 +2,14 @@ package gen import ( "github.com/tal-tech/go-zero/tools/goctl/model/sql/template" - "github.com/tal-tech/go-zero/tools/goctl/templatex" + "github.com/tal-tech/go-zero/tools/goctl/util" ) func genTag(in string) (string, error) { if in == "" { return in, nil } - output, err := templatex.With("tag"). + output, err := util.With("tag"). Parse(template.Tag). Execute(map[string]interface{}{ "field": in, diff --git a/tools/goctl/model/sql/gen/types.go b/tools/goctl/model/sql/gen/types.go index 24ac571d..d6801e6b 100644 --- a/tools/goctl/model/sql/gen/types.go +++ b/tools/goctl/model/sql/gen/types.go @@ -2,7 +2,7 @@ package gen import ( "github.com/tal-tech/go-zero/tools/goctl/model/sql/template" - "github.com/tal-tech/go-zero/tools/goctl/templatex" + "github.com/tal-tech/go-zero/tools/goctl/util" ) func genTypes(table Table, withCache bool) (string, error) { @@ -11,7 +11,7 @@ func genTypes(table Table, withCache bool) (string, error) { if err != nil { return "", err } - output, err := templatex.With("types"). + output, err := util.With("types"). Parse(template.Types). Execute(map[string]interface{}{ "withCache": withCache, diff --git a/tools/goctl/model/sql/gen/update.go b/tools/goctl/model/sql/gen/update.go index edc59aac..426f8fa8 100644 --- a/tools/goctl/model/sql/gen/update.go +++ b/tools/goctl/model/sql/gen/update.go @@ -4,7 +4,7 @@ import ( "strings" "github.com/tal-tech/go-zero/tools/goctl/model/sql/template" - "github.com/tal-tech/go-zero/tools/goctl/templatex" + "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/util/stringx" ) @@ -22,7 +22,7 @@ func genUpdate(table Table, withCache bool) (string, error) { } expressionValues = append(expressionValues, "data."+table.PrimaryKey.Name.ToCamel()) camelTableName := table.Name.ToCamel() - output, err := templatex.With("update"). + output, err := util.With("update"). Parse(template.Update). Execute(map[string]interface{}{ "withCache": withCache, diff --git a/tools/goctl/model/sql/gen/vars.go b/tools/goctl/model/sql/gen/vars.go index 4bb79211..64e043e9 100644 --- a/tools/goctl/model/sql/gen/vars.go +++ b/tools/goctl/model/sql/gen/vars.go @@ -4,7 +4,7 @@ import ( "strings" "github.com/tal-tech/go-zero/tools/goctl/model/sql/template" - "github.com/tal-tech/go-zero/tools/goctl/templatex" + "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/util/stringx" ) @@ -14,7 +14,7 @@ func genVars(table Table, withCache bool) (string, error) { keys = append(keys, v.VarExpression) } camel := table.Name.ToCamel() - output, err := templatex.With("var"). + output, err := util.With("var"). Parse(template.Vars). GoFmt(true). Execute(map[string]interface{}{ diff --git a/tools/goctl/rpc/gen/gencall.go b/tools/goctl/rpc/gen/gencall.go index cf960022..feaf534f 100644 --- a/tools/goctl/rpc/gen/gencall.go +++ b/tools/goctl/rpc/gen/gencall.go @@ -7,7 +7,6 @@ import ( "github.com/tal-tech/go-zero/core/collection" "github.com/tal-tech/go-zero/tools/goctl/rpc/parser" - "github.com/tal-tech/go-zero/tools/goctl/templatex" "github.com/tal-tech/go-zero/tools/goctl/util" ) @@ -123,8 +122,8 @@ func (g *defaultRpcGenerator) genCall() error { } filename := filepath.Join(callPath, typesFilename) - head := templatex.GetHead(g.Ctx.ProtoSource) - err = templatex.With("types").GoFmt(true).Parse(callTemplateTypes).SaveTo(map[string]interface{}{ + head := util.GetHead(g.Ctx.ProtoSource) + err = util.With("types").GoFmt(true).Parse(callTemplateTypes).SaveTo(map[string]interface{}{ "head": head, "const": constLit, "filePackage": service.Name.Lower(), @@ -147,7 +146,7 @@ func (g *defaultRpcGenerator) genCall() error { return err } - err = templatex.With("shared").GoFmt(true).Parse(callTemplateText).SaveTo(map[string]interface{}{ + err = util.With("shared").GoFmt(true).Parse(callTemplateText).SaveTo(map[string]interface{}{ "name": service.Name.Lower(), "head": head, "filePackage": service.Name.Lower(), @@ -167,7 +166,7 @@ func (g *defaultRpcGenerator) genFunction(service *parser.RpcService) ([]string, imports.AddStr(fmt.Sprintf(`%v "%v"`, pkgName, g.mustGetPackage(dirPb))) for _, method := range service.Funcs { imports.AddStr(g.ast.Imports[method.ParameterIn.Package]) - buffer, err := templatex.With("sharedFn").Parse(callFunctionTemplate).Execute(map[string]interface{}{ + buffer, err := util.With("sharedFn").Parse(callFunctionTemplate).Execute(map[string]interface{}{ "rpcServiceName": service.Name.Title(), "method": method.Name.Title(), "package": pkgName, @@ -190,7 +189,7 @@ func (g *defaultRpcGenerator) getInterfaceFuncs(service *parser.RpcService) ([]s functions := make([]string, 0) for _, method := range service.Funcs { - buffer, err := templatex.With("interfaceFn").Parse(callInterfaceFunctionTemplate).Execute( + buffer, err := util.With("interfaceFn").Parse(callInterfaceFunctionTemplate).Execute( map[string]interface{}{ "hasComment": method.HaveDoc(), "comment": method.GetDoc(), diff --git a/tools/goctl/rpc/gen/genetc.go b/tools/goctl/rpc/gen/genetc.go index d96ec85d..4a655e42 100644 --- a/tools/goctl/rpc/gen/genetc.go +++ b/tools/goctl/rpc/gen/genetc.go @@ -4,7 +4,6 @@ import ( "fmt" "path/filepath" - "github.com/tal-tech/go-zero/tools/goctl/templatex" "github.com/tal-tech/go-zero/tools/goctl/util" ) @@ -23,7 +22,7 @@ func (g *defaultRpcGenerator) genEtc() error { return nil } - return templatex.With("etc").Parse(etcTemplate).SaveTo(map[string]interface{}{ + return util.With("etc").Parse(etcTemplate).SaveTo(map[string]interface{}{ "serviceName": g.Ctx.ServiceName.Lower(), }, fileName, false) } diff --git a/tools/goctl/rpc/gen/genlogic.go b/tools/goctl/rpc/gen/genlogic.go index c4527a95..b5e76435 100644 --- a/tools/goctl/rpc/gen/genlogic.go +++ b/tools/goctl/rpc/gen/genlogic.go @@ -7,7 +7,6 @@ import ( "github.com/tal-tech/go-zero/core/collection" "github.com/tal-tech/go-zero/tools/goctl/rpc/parser" - "github.com/tal-tech/go-zero/tools/goctl/templatex" "github.com/tal-tech/go-zero/tools/goctl/util" ) @@ -62,7 +61,7 @@ func (g *defaultRpcGenerator) genLogic() error { svcImport := fmt.Sprintf(`"%v"`, g.mustGetPackage(dirSvc)) imports.AddStr(svcImport) imports.AddStr(importList...) - err = templatex.With("logic").GoFmt(true).Parse(logicTemplate).SaveTo(map[string]interface{}{ + err = util.With("logic").GoFmt(true).Parse(logicTemplate).SaveTo(map[string]interface{}{ "logicName": fmt.Sprintf("%sLogic", method.Name.Title()), "functions": functions, "imports": strings.Join(imports.KeysStr(), util.NL), @@ -83,7 +82,7 @@ func (g *defaultRpcGenerator) genLogicFunction(packageName string, method *parse } imports.AddStr(g.ast.Imports[method.ParameterIn.Package]) imports.AddStr(g.ast.Imports[method.ParameterOut.Package]) - buffer, err := templatex.With("fun").Parse(logicFunctionTemplate).Execute(map[string]interface{}{ + buffer, err := util.With("fun").Parse(logicFunctionTemplate).Execute(map[string]interface{}{ "logicName": fmt.Sprintf("%sLogic", method.Name.Title()), "method": method.Name.Title(), "request": method.ParameterIn.StarExpression, diff --git a/tools/goctl/rpc/gen/genmain.go b/tools/goctl/rpc/gen/genmain.go index 277c20ae..94f1b9f7 100644 --- a/tools/goctl/rpc/gen/genmain.go +++ b/tools/goctl/rpc/gen/genmain.go @@ -6,7 +6,6 @@ import ( "strings" "github.com/tal-tech/go-zero/tools/goctl/rpc/parser" - "github.com/tal-tech/go-zero/tools/goctl/templatex" "github.com/tal-tech/go-zero/tools/goctl/util" ) @@ -58,8 +57,8 @@ func (g *defaultRpcGenerator) genMain() error { configImport := fmt.Sprintf(`"%v"`, g.mustGetPackage(dirConfig)) imports = append(imports, configImport, pbImport, remoteImport, svcImport) srv, registers := g.genServer(pkg, file.Service) - head := templatex.GetHead(g.Ctx.ProtoSource) - return templatex.With("main").GoFmt(true).Parse(mainTemplate).SaveTo(map[string]interface{}{ + head := util.GetHead(g.Ctx.ProtoSource) + return util.With("main").GoFmt(true).Parse(mainTemplate).SaveTo(map[string]interface{}{ "head": head, "package": pkg, "serviceName": g.Ctx.ServiceName.Lower(), diff --git a/tools/goctl/rpc/gen/genserver.go b/tools/goctl/rpc/gen/genserver.go index c6051220..c9d61782 100644 --- a/tools/goctl/rpc/gen/genserver.go +++ b/tools/goctl/rpc/gen/genserver.go @@ -7,7 +7,6 @@ import ( "github.com/tal-tech/go-zero/core/collection" "github.com/tal-tech/go-zero/tools/goctl/rpc/parser" - "github.com/tal-tech/go-zero/tools/goctl/templatex" "github.com/tal-tech/go-zero/tools/goctl/util" ) @@ -52,7 +51,7 @@ func (g *defaultRpcGenerator) genHandler() error { imports := collection.NewSet() imports.AddStr(logicImport, svcImport) - head := templatex.GetHead(g.Ctx.ProtoSource) + head := util.GetHead(g.Ctx.ProtoSource) for _, service := range file.Service { filename := fmt.Sprintf("%vserver.go", service.Name.Lower()) serverFile := filepath.Join(serverPath, filename) @@ -61,7 +60,7 @@ func (g *defaultRpcGenerator) genHandler() error { return err } imports.AddStr(importList...) - err = templatex.With("server").GoFmt(true).Parse(serverTemplate).SaveTo(map[string]interface{}{ + err = util.With("server").GoFmt(true).Parse(serverTemplate).SaveTo(map[string]interface{}{ "head": head, "types": fmt.Sprintf(typeFmt, service.Name.Title()), "server": service.Name.Title(), @@ -86,7 +85,7 @@ func (g *defaultRpcGenerator) genFunctions(service *parser.RpcService) ([]string } imports.AddStr(g.ast.Imports[method.ParameterIn.Package]) imports.AddStr(g.ast.Imports[method.ParameterOut.Package]) - buffer, err := templatex.With("func").Parse(functionTemplate).Execute(map[string]interface{}{ + buffer, err := util.With("func").Parse(functionTemplate).Execute(map[string]interface{}{ "server": service.Name.Title(), "logicName": fmt.Sprintf("%sLogic", method.Name.Title()), "method": method.Name.Title(), diff --git a/tools/goctl/rpc/gen/gensvc.go b/tools/goctl/rpc/gen/gensvc.go index 1a0fa2cd..c2315813 100644 --- a/tools/goctl/rpc/gen/gensvc.go +++ b/tools/goctl/rpc/gen/gensvc.go @@ -4,7 +4,7 @@ import ( "fmt" "path/filepath" - "github.com/tal-tech/go-zero/tools/goctl/templatex" + "github.com/tal-tech/go-zero/tools/goctl/util" ) const svcTemplate = `package svc @@ -25,7 +25,7 @@ func NewServiceContext(c config.Config) *ServiceContext { func (g *defaultRpcGenerator) genSvc() error { svcPath := g.dirM[dirSvc] fileName := filepath.Join(svcPath, fileServiceContext) - return templatex.With("svc").GoFmt(true).Parse(svcTemplate).SaveTo(map[string]interface{}{ + return util.With("svc").GoFmt(true).Parse(svcTemplate).SaveTo(map[string]interface{}{ "imports": fmt.Sprintf(`"%v"`, g.mustGetPackage(dirConfig)), }, fileName, false) } diff --git a/tools/goctl/rpc/gen/template.go b/tools/goctl/rpc/gen/template.go index 94cf6cbf..ba4753e9 100644 --- a/tools/goctl/rpc/gen/template.go +++ b/tools/goctl/rpc/gen/template.go @@ -4,7 +4,7 @@ import ( "path/filepath" "strings" - "github.com/tal-tech/go-zero/tools/goctl/templatex" + "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/util/console" "github.com/tal-tech/go-zero/tools/goctl/util/stringx" ) @@ -43,7 +43,7 @@ func (r *rpcTemplate) MustGenerate(showState bool) { r.Info("generating template...") protoFilename := filepath.Base(r.out) serviceName := stringx.From(strings.TrimSuffix(protoFilename, filepath.Ext(protoFilename))) - err := templatex.With("t").Parse(rpcTemplateText).SaveTo(map[string]string{ + err := util.With("t").Parse(rpcTemplateText).SaveTo(map[string]string{ "package": serviceName.UnTitle(), "serviceName": serviceName.Title(), }, r.out, false) diff --git a/tools/goctl/rpc/parser/pbast.go b/tools/goctl/rpc/parser/pbast.go index a57300b5..6a5872f5 100644 --- a/tools/goctl/rpc/parser/pbast.go +++ b/tools/goctl/rpc/parser/pbast.go @@ -12,7 +12,6 @@ import ( "github.com/tal-tech/go-zero/core/lang" sx "github.com/tal-tech/go-zero/core/stringx" - "github.com/tal-tech/go-zero/tools/goctl/templatex" "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/util/console" "github.com/tal-tech/go-zero/tools/goctl/util/stringx" @@ -590,7 +589,7 @@ func (a *PbAst) GenTypesCode() (string, error) { types = append(types, typeCode) } - buffer, err := templatex.With("type").Parse(typeTemplate).Execute(map[string]interface{}{ + buffer, err := util.With("type").Parse(typeTemplate).Execute(map[string]interface{}{ "types": strings.Join(types, util.NL+util.NL), }) if err != nil { @@ -615,7 +614,7 @@ func (s *Struct) genCode(containsTypeStatement bool) (string, error) { comment = f.Comment[0] } doc = strings.Join(f.Document, util.NL) - buffer, err := templatex.With(sx.Rand()).Parse(fieldTemplate).Execute(map[string]interface{}{ + buffer, err := util.With(sx.Rand()).Parse(fieldTemplate).Execute(map[string]interface{}{ "name": f.Name.Title(), "type": f.Type.InvokeTypeExpression, "tag": f.JsonTag, @@ -630,7 +629,7 @@ func (s *Struct) genCode(containsTypeStatement bool) (string, error) { fields = append(fields, buffer.String()) } - buffer, err := templatex.With("struct").Parse(structTemplate).Execute(map[string]interface{}{ + buffer, err := util.With("struct").Parse(structTemplate).Execute(map[string]interface{}{ "type": containsTypeStatement, "name": s.Name.Title(), "fields": strings.Join(fields, util.NL), diff --git a/tools/goctl/rpc/parser/proto.go b/tools/goctl/rpc/parser/proto.go index 8632d7e0..e0e6f947 100644 --- a/tools/goctl/rpc/parser/proto.go +++ b/tools/goctl/rpc/parser/proto.go @@ -10,7 +10,6 @@ import ( "github.com/emicklei/proto" "github.com/tal-tech/go-zero/core/collection" "github.com/tal-tech/go-zero/core/lang" - "github.com/tal-tech/go-zero/tools/goctl/templatex" "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/util/stringx" ) @@ -263,7 +262,7 @@ func (e *Enum) GenEnumCode() (string, error) { } element = append(element, code) } - buffer, err := templatex.With("enum").Parse(enumTemplate).Execute(map[string]interface{}{ + buffer, err := util.With("enum").Parse(enumTemplate).Execute(map[string]interface{}{ "element": strings.Join(element, util.NL), }) if err != nil { @@ -273,7 +272,7 @@ func (e *Enum) GenEnumCode() (string, error) { } func (e *Enum) GenEnumTypeCode() (string, error) { - buffer, err := templatex.With("enumAlias").Parse(enumTypeTemplate).Execute(map[string]interface{}{ + buffer, err := util.With("enumAlias").Parse(enumTypeTemplate).Execute(map[string]interface{}{ "name": e.Name.Source(), }) if err != nil { @@ -283,7 +282,7 @@ func (e *Enum) GenEnumTypeCode() (string, error) { } func (e *EnumField) GenEnumFieldCode(parentName string) (string, error) { - buffer, err := templatex.With("enumField").Parse(enumFiledTemplate).Execute(map[string]interface{}{ + buffer, err := util.With("enumField").Parse(enumFiledTemplate).Execute(map[string]interface{}{ "key": e.Key, "name": parentName, "value": e.Value, diff --git a/tools/goctl/tpl/templates.go b/tools/goctl/tpl/templates.go new file mode 100644 index 00000000..dc647222 --- /dev/null +++ b/tools/goctl/tpl/templates.go @@ -0,0 +1,33 @@ +package tpl + +import ( + "fmt" + + "github.com/logrusorgru/aurora" + "github.com/tal-tech/go-zero/core/errorx" + "github.com/tal-tech/go-zero/tools/goctl/api/gogen" + "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/urfave/cli" +) + +const templateParentPath = "/" + +func GenTemplates(ctx *cli.Context) error { + if err := errorx.Chain( + func() error { + return gogen.GenTemplates(ctx) + }, + ); err != nil { + return err + } + + dir, err := util.GetTemplateDir(templateParentPath) + if err != nil { + return err + } + + fmt.Printf("Templates are generated in %s, %s\n", aurora.Green(dir), + aurora.Red("edit on your risk!")) + + return nil +} diff --git a/tools/goctl/templatex/files.go b/tools/goctl/util/files.go similarity index 67% rename from tools/goctl/templatex/files.go rename to tools/goctl/util/files.go index 0f8e825a..6ac20b23 100644 --- a/tools/goctl/templatex/files.go +++ b/tools/goctl/util/files.go @@ -1,24 +1,29 @@ -package templatex +package util import ( - "fmt" "io/ioutil" "os" "path/filepath" - - "github.com/logrusorgru/aurora" - "github.com/tal-tech/go-zero/tools/goctl/util" ) const goctlDir = ".goctl" +func GetTemplateDir(category string) (string, error) { + home, err := os.UserHomeDir() + if err != nil { + return "", err + } + + return filepath.Join(home, goctlDir, category), nil +} + func InitTemplates(category string, templates map[string]string) error { - dir, err := getTemplateDir(category) + dir, err := GetTemplateDir(category) if err != nil { return err } - if err := util.MkdirIfNotExist(dir); err != nil { + if err := MkdirIfNotExist(dir); err != nil { return err } @@ -28,20 +33,17 @@ func InitTemplates(category string, templates map[string]string) error { } } - fmt.Printf("Templates are generated in %s, %s\n", aurora.Green(dir), - aurora.Red("edit on your risk!")) - return nil } func LoadTemplate(category, file, builtin string) (string, error) { - dir, err := getTemplateDir(category) + dir, err := GetTemplateDir(category) if err != nil { return "", err } file = filepath.Join(dir, file) - if !util.FileExists(file) { + if !FileExists(file) { return builtin, nil } @@ -54,7 +56,7 @@ func LoadTemplate(category, file, builtin string) (string, error) { } func createTemplate(file, content string) error { - if util.FileExists(file) { + if FileExists(file) { println(1) return nil } @@ -68,12 +70,3 @@ func createTemplate(file, content string) error { _, err = f.WriteString(content) return err } - -func getTemplateDir(category string) (string, error) { - home, err := os.UserHomeDir() - if err != nil { - return "", err - } - - return filepath.Join(home, goctlDir, category), nil -} diff --git a/tools/goctl/templatex/head.go b/tools/goctl/util/head.go similarity index 93% rename from tools/goctl/templatex/head.go rename to tools/goctl/util/head.go index ba44dcc0..b7534c71 100644 --- a/tools/goctl/templatex/head.go +++ b/tools/goctl/util/head.go @@ -1,4 +1,4 @@ -package templatex +package util var headTemplate = `// Code generated by goctl. DO NOT EDIT! // Source: {{.source}}` diff --git a/tools/goctl/templatex/templatex.go b/tools/goctl/util/templatex.go similarity index 91% rename from tools/goctl/templatex/templatex.go rename to tools/goctl/util/templatex.go index 3abbaecb..461b2aca 100644 --- a/tools/goctl/templatex/templatex.go +++ b/tools/goctl/util/templatex.go @@ -1,12 +1,10 @@ -package templatex +package util import ( "bytes" goformat "go/format" "io/ioutil" "text/template" - - "github.com/tal-tech/go-zero/tools/goctl/util" ) const regularPerm = 0666 @@ -34,7 +32,7 @@ func (t *defaultTemplate) GoFmt(format bool) *defaultTemplate { } func (t *defaultTemplate) SaveTo(data interface{}, path string, forceUpdate bool) error { - if util.FileExists(path) && !forceUpdate { + if FileExists(path) && !forceUpdate { return nil }