From 228115e7a2ba4accf99689ae1e6dc655e362baad Mon Sep 17 00:00:00 2001 From: jaron Date: Tue, 31 Dec 2024 14:43:59 +0800 Subject: [PATCH] feat(new): new command remove style flag --- cmd/new.go | 1 - config/config.go | 1 - internal/gen/genapi/gen.go | 4 ++++ internal/gen/genapi/patch_svc.go | 34 ++++++++++++++++++++++++++++++++ internal/gen/genrpc/gen.go | 4 ++++ internal/gen/genrpc/patch_svc.go | 34 ++++++++++++++++++++++++++++++++ internal/new/new.go | 8 -------- 7 files changed, 76 insertions(+), 10 deletions(-) create mode 100644 internal/gen/genapi/patch_svc.go create mode 100644 internal/gen/genrpc/patch_svc.go diff --git a/cmd/new.go b/cmd/new.go index ef5fca33..68102850 100644 --- a/cmd/new.go +++ b/cmd/new.go @@ -106,7 +106,6 @@ func init() { newCmd.Flags().StringP("remote", "r", "https://github.com/jzero-io/templates", "remote templates repo") newCmd.Flags().StringP("branch", "b", "", "use remote template repo branch") newCmd.Flags().StringP("local", "", "", "use local template") - newCmd.Flags().StringP("style", "", "gozero", "The file naming format, see [https://github.com/zeromicro/go-zero/blob/master/tools/goctl/config/readme.md]") newCmd.Flags().StringSliceP("features", "", []string{}, "select features") newCmd.Flags().BoolP("mono", "", false, "mono project under go mod project") } diff --git a/config/config.go b/config/config.go index 045ea2cb..1b68e601 100644 --- a/config/config.go +++ b/config/config.go @@ -56,7 +56,6 @@ type NewConfig struct { Frame string `mapstructure:"frame"` // 使用 jzero 内置的框架 Branch string `mapstructure:"branch"` // 使用远程模板仓库的某个分支 Local string `mapstructure:"local"` // 使用本地模板与 branch 对应 - Style string `mapstructure:"style"` // 项目代码风格 Features []string `mapstructure:"features"` // 新建项目使用哪些特性, 灵活构建模板 } diff --git a/internal/gen/genapi/gen.go b/internal/gen/genapi/gen.go index ec7c8f10..ece4fa29 100644 --- a/internal/gen/genapi/gen.go +++ b/internal/gen/genapi/gen.go @@ -223,6 +223,10 @@ func (ja *JzeroApi) generateApiCode() error { } } + if err := ja.patchSvc(); err != nil { + return err + } + for _, v := range ja.GenCodeApiFiles { if len(ja.ApiSpecMap[v].Service.Routes()) > 0 { dir := "." diff --git a/internal/gen/genapi/patch_svc.go b/internal/gen/genapi/patch_svc.go new file mode 100644 index 00000000..b6a3bffb --- /dev/null +++ b/internal/gen/genapi/patch_svc.go @@ -0,0 +1,34 @@ +package genapi + +import ( + "os" + "path/filepath" + "strings" + + "github.com/jzero-io/jzero-contrib/filex" + "github.com/zeromicro/go-zero/tools/goctl/util/format" +) + +func (ja *JzeroApi) patchSvc() error { + namingFormat, err := format.FileNamingFormat(ja.Style, "service_context.go") + if err != nil { + return err + } + if filex.FileExists(filepath.Join("internal", "svc")) { + return nil + } + dir, err := os.ReadDir(filepath.Join("internal", "svc")) + if err != nil { + return err + } + for _, v := range dir { + if !v.IsDir() { + if strings.HasPrefix(v.Name(), "service") && strings.HasSuffix(v.Name(), "context.go") { + if err = os.Rename(filepath.Join("internal", "svc", v.Name()), filepath.Join("internal", "svc", namingFormat)); err != nil { + return err + } + } + } + } + return nil +} diff --git a/internal/gen/genrpc/gen.go b/internal/gen/genrpc/gen.go index 2eb25964..a5338a6c 100644 --- a/internal/gen/genrpc/gen.go +++ b/internal/gen/genrpc/gen.go @@ -130,6 +130,10 @@ func (jr *JzeroRpc) Gen() error { } fmt.Printf("%s to generate proto code. \n", color.WithColor("Start", color.FgGreen)) + if err := jr.patchSvc(); err != nil { + return err + } + for _, v := range jr.ProtoFiles { allLogicFiles, err := jr.GetAllLogicFiles(v, jr.ProtoSpecMap[v]) if err != nil { diff --git a/internal/gen/genrpc/patch_svc.go b/internal/gen/genrpc/patch_svc.go new file mode 100644 index 00000000..eacb74b8 --- /dev/null +++ b/internal/gen/genrpc/patch_svc.go @@ -0,0 +1,34 @@ +package genrpc + +import ( + "os" + "path/filepath" + "strings" + + "github.com/jzero-io/jzero-contrib/filex" + "github.com/zeromicro/go-zero/tools/goctl/util/format" +) + +func (jr *JzeroRpc) patchSvc() error { + namingFormat, err := format.FileNamingFormat(jr.Style, "service_context.go") + if err != nil { + return err + } + if filex.FileExists(filepath.Join("internal", "svc")) { + return nil + } + dir, err := os.ReadDir(filepath.Join("internal", "svc")) + if err != nil { + return err + } + for _, v := range dir { + if !v.IsDir() { + if strings.HasPrefix(v.Name(), "service") && strings.HasSuffix(v.Name(), "context.go") { + if err = os.Rename(filepath.Join("internal", "svc", v.Name()), filepath.Join("internal", "svc", namingFormat)); err != nil { + return err + } + } + } + } + return nil +} diff --git a/internal/new/new.go b/internal/new/new.go index 1a162842..9a385cd2 100644 --- a/internal/new/new.go +++ b/internal/new/new.go @@ -9,7 +9,6 @@ import ( "github.com/pkg/errors" "github.com/rinchsan/gosimports" "github.com/spf13/cobra" - "github.com/zeromicro/go-zero/tools/goctl/util/format" "github.com/zeromicro/go-zero/tools/goctl/util/pathx" "github.com/jzero-io/jzero/config" @@ -123,13 +122,6 @@ func (jn *JzeroNew) New(dirname string) ([]*GeneratedFile, error) { } stylePath := filepath.Join(filepath.Dir(rel), filename) - if filepath.Ext(filename) == ".go" && !bytes.Contains(fileBytes, []byte("DO NOT EDIT")) { - formatFilename, err := format.FileNamingFormat(jn.nc.Style, filename[0:len(filename)-len(filepath.Ext(filename))]) - if err != nil { - return nil, err - } - stylePath = filepath.Join(filepath.Dir(rel), formatFilename+filepath.Ext(filename)) - } // specify if filename == "go.mod" && jn.nc.Mono {