Skip to content

Commit

Permalink
Merge pull request #21 from lazygophers/luoxin
Browse files Browse the repository at this point in the history
生成入口文件
  • Loading branch information
Luoxin authored Jun 2, 2024
2 parents e6db018 + 02e2a0d commit 49d38e5
Show file tree
Hide file tree
Showing 13 changed files with 154 additions and 5 deletions.
1 change: 1 addition & 0 deletions cli/gen_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var GenAllHooks = []GenHook{
runGenPb,
runGenMod,
runEditorconfig,
runGenCmd,
runGenState,
runGenImpl,
}
Expand Down
27 changes: 27 additions & 0 deletions cli/gen_cmd.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions cli/gen_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

var GenStateHook = []GenHook{
ranGenConf,
ranGenCache,
runGenTable,
}

Expand Down
2 changes: 1 addition & 1 deletion codegen/generate_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
68 changes: 68 additions & 0 deletions codegen/generate_cmd.go
Original file line number Diff line number Diff line change
@@ -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
}
9 changes: 9 additions & 0 deletions codegen/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ const (
PathTypeImpl
PathTypeImplPath
PathTypeImplRoute

PathTypeCmd
PathTypeCmdMain
)

func GetPath(t PathType, pb *PbPackage) string {
Expand Down Expand Up @@ -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")
}
Expand Down
6 changes: 6 additions & 0 deletions codegen/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const (
TemplateTypeTableName
TemplateTypeTableField

TemplateTypeCmd

TemplateTypeProtoRpc
TemplateTypeProtoRpcName
TemplateTypeProtoRpcReq
Expand Down Expand Up @@ -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")
}
Expand Down
28 changes: 28 additions & 0 deletions codegen/template/cmd.gtpl
Original file line number Diff line number Diff line change
@@ -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
}
}
8 changes: 6 additions & 2 deletions codegen/template/state/config.gtpl
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand Down
2 changes: 1 addition & 1 deletion codegen/template/state/state.gtpl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func Load() (err error) {
return err
}

err = ConnectCache()
err = ConnectDatebase()
if err != nil {
log.Errorf("err:%v", err)
return err
Expand Down
2 changes: 1 addition & 1 deletion codegen/template/state/table.gtpl
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
3 changes: 3 additions & 0 deletions example.codegen.cfg.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ template:
list: "列表数据的模板路径"
"": "默认的模版路径"

# 入口文件
main: "<main.go> 模板文件路径"

# state/table.go 模板文件路径
table: "<state/table.go 模板文件路径,如果不存在则是使用默认>"
# state/conf.go 模板文件路径
Expand Down
2 changes: 2 additions & 0 deletions state/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down

0 comments on commit 49d38e5

Please sign in to comment.