diff --git a/cli/gen_all.go b/cli/gen_all.go index 56eb362..8d71b2d 100644 --- a/cli/gen_all.go +++ b/cli/gen_all.go @@ -11,6 +11,7 @@ var GenAllHooks = []GenHook{ runGenPb, runGenMod, runEditorconfig, + runGenCmd, runGenState, runGenImpl, } diff --git a/cli/gen_cmd.go b/cli/gen_cmd.go new file mode 100644 index 0000000..5b12c09 --- /dev/null +++ b/cli/gen_cmd.go @@ -0,0 +1,27 @@ +package cli + +import ( + "github.com/lazygophers/codegen/codegen" + "github.com/lazygophers/log" + "github.com/spf13/cobra" +) + +var genCmdCmd = &cobra.Command{ + Use: "cmd", + Short: "Generates cmd folder", + RunE: runGenCmd, +} + +func runGenCmd(cmd *cobra.Command, args []string) (err error) { + err = codegen.GenerateCmd(pb) + if err != nil { + log.Errorf("err:%v", err) + return err + } + + return nil +} + +func init() { + genCmd.AddCommand(genCmdCmd) +} diff --git a/cli/gen_state.go b/cli/gen_state.go index 59f77c0..ac041df 100644 --- a/cli/gen_state.go +++ b/cli/gen_state.go @@ -8,6 +8,7 @@ import ( var GenStateHook = []GenHook{ ranGenConf, + ranGenCache, runGenTable, } diff --git a/codegen/generate_cache.go b/codegen/generate_cache.go index 1c99793..011affb 100644 --- a/codegen/generate_cache.go +++ b/codegen/generate_cache.go @@ -37,7 +37,7 @@ func GenerateCache(pb *PbPackage) (err error) { }, } - tpl, err := GetTemplate(TemplateTypeStateConf) + tpl, err := GetTemplate(TemplateTypeStateCache) if err != nil { log.Errorf("err:%v", err) return err diff --git a/codegen/generate_cmd.go b/codegen/generate_cmd.go new file mode 100644 index 0000000..c1fb478 --- /dev/null +++ b/codegen/generate_cmd.go @@ -0,0 +1,68 @@ +package codegen + +import ( + "github.com/lazygophers/codegen/state" + "github.com/lazygophers/log" + "github.com/lazygophers/utils/osx" + "github.com/pterm/pterm" + "io/fs" + "os" +) + +func initCmdDirectory(pb *PbPackage) error { + if osx.IsDir(GetPath(PathTypeCmd, pb)) { + return nil + } + + err := os.MkdirAll(GetPath(PathTypeCmd, pb), fs.ModePerm) + if err != nil { + log.Errorf("err:%s", err) + return err + } + + return nil +} + +func GenerateCmd(pb *PbPackage) (err error) { + err = initCmdDirectory(pb) + if err != nil { + log.Errorf("err:%v", err) + return err + } + + // cache 文件为覆盖生成单次生成,不会重复读取 + if osx.IsFile(GetPath(PathTypeCmdMain, pb)) { + if !state.Config.Overwrite { + pterm.Warning.Printfln("main is already existing, skip generation") + return nil + } + + pterm.Warning.Printfln("main is already existing, will overwrite") + } + + // table 文件为覆盖生成 + args := map[string]interface{}{ + "PB": pb, + } + + tpl, err := GetTemplate(TemplateTypeCmd) + if err != nil { + log.Errorf("err:%v", err) + return err + } + + file, err := os.OpenFile(GetPath(PathTypeCmdMain, pb), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, fs.FileMode(0666)) + if err != nil { + log.Errorf("err:%v", err) + return err + } + defer file.Close() + + err = tpl.Execute(file, args) + if err != nil { + log.Errorf("err:%v", err) + return err + } + + return nil +} diff --git a/codegen/path.go b/codegen/path.go index 391fb96..aa2df5d 100644 --- a/codegen/path.go +++ b/codegen/path.go @@ -24,6 +24,9 @@ const ( PathTypeImpl PathTypeImplPath PathTypeImplRoute + + PathTypeCmd + PathTypeCmdMain ) func GetPath(t PathType, pb *PbPackage) string { @@ -73,6 +76,12 @@ func GetPath(t PathType, pb *PbPackage) string { case PathTypeImplRoute: return filepath.Join(pb.ProjectRoot(), "rpc_route.gen.go") + case PathTypeCmd: + return filepath.Join(pb.ProjectRoot(), "cmd") + + case PathTypeCmdMain: + return filepath.Join(GetPath(PathTypeCmd, pb), "main.go") + default: panic("unsupported path type") } diff --git a/codegen/resource.go b/codegen/resource.go index 99cd8b9..489eaa3 100644 --- a/codegen/resource.go +++ b/codegen/resource.go @@ -29,6 +29,8 @@ const ( TemplateTypeTableName TemplateTypeTableField + TemplateTypeCmd + TemplateTypeProtoRpc TemplateTypeProtoRpcName TemplateTypeProtoRpcReq @@ -135,6 +137,10 @@ func GetTemplate(t TemplateType, args ...string) (tpl *template.Template, err er systemPath = state.Config.Template.Impl.Route embedPath = "template/rpc_route.gtpl" + case TemplateTypeCmd: + systemPath = state.Config.Template.Main + embedPath = "template/cmd.gtpl" + default: panic("unsupported template type") } diff --git a/codegen/template/cmd.gtpl b/codegen/template/cmd.gtpl new file mode 100644 index 0000000..4e7fa22 --- /dev/null +++ b/codegen/template/cmd.gtpl @@ -0,0 +1,28 @@ +package main + +import ( + "github.com/lazygophers/{{ .PB.GoPackageName }}" + "github.com/lazygophers/{{ .PB.GoPackageName }}/internal/state" + "github.com/lazygophers/log" + "github.com/lazygophers/lrpc" +) + +func main() { + err := state.Load() + if err != nil { + log.Errorf("err:%v", err) + return + } + + app := lrpc.NewApp(&lrpc.Config{ + Name: state.State.Config.Name, + }) + + app.AddRoutes({{ .PB.GoPackageName }}.Routes) + + err = app.ListenAndServe(state.State.Config.Port) + if err != nil { + log.Errorf("err:%v", err) + return + } +} diff --git a/codegen/template/state/config.gtpl b/codegen/template/state/config.gtpl index 689cc22..862dd70 100644 --- a/codegen/template/state/config.gtpl +++ b/codegen/template/state/config.gtpl @@ -2,12 +2,16 @@ package state import ( "github.com/lazygophers/log" - "github.com/lazygophers/utils/cache" + "github.com/lazygophers/lrpc/middleware/cache" + "github.com/lazygophers/lrpc/middleware/db" "github.com/lazygophers/utils/config" - "github.com/lazygophers/utils/db" ) type Config struct { + Name string `json:"name,omitempty" yaml:"name,omitempty" toml:"name,omitempty"` + + Port int `json:"port,omitempty" yaml:"port,omitempty" toml:"port,omitempty"` + Db *db.Config `json:"db,omitempty" yaml:"db,omitempty" toml:"db,omitempty"` Cache *cache.Config `json:"cache,omitempty" yaml:"cache,omitempty" toml:"cache,omitempty"` diff --git a/codegen/template/state/state.gtpl b/codegen/template/state/state.gtpl index 3f2dad5..461494e 100644 --- a/codegen/template/state/state.gtpl +++ b/codegen/template/state/state.gtpl @@ -21,7 +21,7 @@ func Load() (err error) { return err } - err = ConnectCache() + err = ConnectDatebase() if err != nil { log.Errorf("err:%v", err) return err diff --git a/codegen/template/state/table.gtpl b/codegen/template/state/table.gtpl index 55aa3b9..53bacef 100644 --- a/codegen/template/state/table.gtpl +++ b/codegen/template/state/table.gtpl @@ -23,7 +23,7 @@ func ConnectDatebase() (err error) { return err } -{{ range $key, $value := .Models}} {{TrimPrefix $value "Model"}} = db.NewModel[{{ $.PB.GoPackageName }}.{{ $value }}](Db()).SetNotFound(xerror.NewError({{ $.PB.GoPackageName }}.ErrCode_{{TrimPrefix $value "Model"}}NotFound)) +{{ range $key, $value := .Models}} {{TrimPrefix $value "Model"}} = db.NewModel[{{ $.PB.GoPackageName }}.{{ $value }}](Db()).SetNotFound(xerror.NewError(int32({{ $.PB.GoPackageName }}.ErrCode_{{TrimPrefix $value "Model"}}NotFound))) {{ end }} log.Info("connect mysql successfully") diff --git a/example.codegen.cfg.yaml b/example.codegen.cfg.yaml index d78c669..2b8d8db 100644 --- a/example.codegen.cfg.yaml +++ b/example.codegen.cfg.yaml @@ -82,6 +82,9 @@ template: list: "列表数据的模板路径" "": "默认的模版路径" + # 入口文件 + main: " 模板文件路径" + # state/table.go 模板文件路径 table: "" # state/conf.go 模板文件路径 diff --git a/state/config.go b/state/config.go index edb2c92..dcf245d 100644 --- a/state/config.go +++ b/state/config.go @@ -145,6 +145,8 @@ type CfgTemplate struct { Proto *CfgProto `json:"proto,omitempty" yaml:"proto,omitempty" toml:"proto,omitempty"` Impl *CfgImpl `json:"impl,omitempty" yaml:"impl,omitempty" toml:"impl,omitempty"` + Main string `json:"main,omitempty" yaml:"main,omitempty" toml:"main,omitempty"` + Table string `json:"table,omitempty" yaml:"table,omitempty" toml:"table,omitempty"` Conf string `json:"conf,omitempty" yaml:"conf,omitempty" toml:"conf,omitempty"` Cache string `json:"cache,omitempty" yaml:"cache,omitempty" toml:"cache,omitempty"`