Skip to content

Commit

Permalink
feat(new): new command remove style flag (#167)
Browse files Browse the repository at this point in the history
feat(new): new command remove style flag

feat(new): new command remove style flag

feat(new): new command remove style flag
  • Loading branch information
jaronnie committed Dec 31, 2024
1 parent df271b6 commit a0604ac
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 13 deletions.
1 change: 0 additions & 1 deletion cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
1 change: 0 additions & 1 deletion cmd/serverless.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ func init() {
serverlessNewCmd.Flags().StringP("branch", "b", "", "remote templates repo branch")
serverlessNewCmd.Flags().StringP("frame", "", "api", "use frame")
serverlessNewCmd.Flags().StringP("local", "", "", "local templates")
serverlessNewCmd.Flags().StringP("style", "", "gozero", "The file naming format, see [https://github.com/zeromicro/go-zero/blob/master/tools/goctl/config/readme.md]")

serverlessDeleteCmd.Flags().StringSliceP("plugin", "p", nil, "plugin name")
}
2 changes: 0 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"` // 新建项目使用哪些特性, 灵活构建模板
}

Expand Down Expand Up @@ -215,7 +214,6 @@ type ServerlessNewConfig struct {
Frame string `mapstructure:"frame"` // 使用 jzero 内置的框架
Branch string `mapstructure:"branch"` // 使用远程模板仓库的某个分支
Local string `mapstructure:"local"` // 使用本地模板与 branch 对应
Style string `mapstructure:"style"` // 项目代码风格
}

type ServerlessDeleteConfig struct {
Expand Down
4 changes: 4 additions & 0 deletions internal/gen/genapi/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := "."
Expand Down
36 changes: 36 additions & 0 deletions internal/gen/genapi/patch_svc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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")) || filex.FileExists(filepath.Join("internal", "svc", namingFormat)) {
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
}
4 changes: 4 additions & 0 deletions internal/gen/genrpc/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
36 changes: 36 additions & 0 deletions internal/gen/genrpc/patch_svc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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")) || filex.FileExists(filepath.Join("internal", "svc", namingFormat)) {
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
}
8 changes: 0 additions & 8 deletions internal/new/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion internal/serverless/serverlessnew/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ func Run(args []string) error {
Frame: config.C.Serverless.New.Frame,
Branch: config.C.Serverless.New.Branch,
Local: config.C.Serverless.New.Local,
Style: config.C.Serverless.New.Style,
Features: config.C.Serverless.New.Features,
}
if config.C.Serverless.New.Core {
Expand Down

0 comments on commit a0604ac

Please sign in to comment.