Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(new): new command remove style flag #167

Merged
merged 1 commit into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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
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
34 changes: 34 additions & 0 deletions internal/gen/genapi/patch_svc.go
Original file line number Diff line number Diff line change
@@ -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
}
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
34 changes: 34 additions & 0 deletions internal/gen/genrpc/patch_svc.go
Original file line number Diff line number Diff line change
@@ -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
}
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
Loading