Skip to content

Commit 1b4bfc4

Browse files
committed
feat: add --types-dir flag for custom types output directory
- Add TypesDir field to GenConfig - Add --types-dir flag to gen command (default: internal) - Update types.go to use configured TypesDir instead of hardcoded internal/types - Update import paths in generated handler and logic files Closes #406
1 parent ee805c3 commit 1b4bfc4

3 files changed

Lines changed: 15 additions & 6 deletions

File tree

cmd/jzero/internal/command/gen/gen.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ func GetCommand() *cobra.Command {
9696
genCmd.Flags().BoolP("mongo-cache", "", false, " Generate code with cache prefix [optional]")
9797
genCmd.Flags().StringP("mongo-cache-prefix", "", "cache", "mongo cache prefix")
9898
genCmd.Flags().StringSliceP("mongo-cache-type", "", []string{"*"}, "mongo cache type names to enable caching")
99+
genCmd.Flags().StringP("types-dir", "", "internal", "types output directory")
99100
genCmd.Flags().BoolP("rpc-client", "", false, "generate rpc client code")
100101
}
101102

cmd/jzero/internal/command/gen/genapi/types.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/zeromicro/go-zero/tools/goctl/util"
1616
"golang.org/x/tools/go/ast/astutil"
1717

18+
"github.com/jzero-io/jzero/cmd/jzero/internal/config"
1819
"github.com/jzero-io/jzero/cmd/jzero/internal/pkg/templatex"
1920
)
2021

@@ -47,12 +48,12 @@ var (
4748
return err
4849
}
4950

50-
_ = os.MkdirAll(filepath.Join("internal", "types", goPackage), 0o755)
51+
_ = os.MkdirAll(filepath.Join(config.C.Gen.TypesDir, "types", goPackage), 0o755)
5152
process, err := gosimports.Process("", typesGoBytes, nil)
5253
if err != nil {
5354
return err
5455
}
55-
if err = os.WriteFile(filepath.Join("internal", "types", goPackage, "types.go"), process, 0o644); err != nil {
56+
if err = os.WriteFile(filepath.Join(config.C.Gen.TypesDir, "types", goPackage, "types.go"), process, 0o644); err != nil {
5657
return err
5758
}
5859
} else {
@@ -96,27 +97,33 @@ var (
9697
if err != nil {
9798
return err
9899
}
99-
if err = os.WriteFile(filepath.Join("internal", "types", "types.go"), process, 0o644); err != nil {
100+
if err = os.WriteFile(filepath.Join(config.C.Gen.TypesDir, "types", "types.go"), process, 0o644); err != nil {
100101
return err
101102
}
102103
return nil
103104
}
104105

105106
func (ja *JzeroApi) updateHandlerImportedTypesPath(f *ast.File, fset *token.FileSet, file HandlerFile) error {
107+
typesImportPath := fmt.Sprintf("%s/%s", ja.Module, config.C.Gen.TypesDir)
106108
if astutil.UsesImport(f, fmt.Sprintf("%s/internal/types", ja.Module)) {
107109
astutil.DeleteImport(fset, f, fmt.Sprintf("%s/internal/types", ja.Module))
108-
astutil.AddNamedImport(fset, f, "types", fmt.Sprintf("%s/internal/types/%s", ja.Module, file.Package))
109110
}
111+
if astutil.UsesImport(f, typesImportPath) {
112+
return nil
113+
}
114+
astutil.AddNamedImport(fset, f, "types", fmt.Sprintf("%s/%s/%s", ja.Module, config.C.Gen.TypesDir, file.Package))
110115

111116
return nil
112117
}
113118

114119
func (ja *JzeroApi) updateLogicImportedTypesPath(f *ast.File, fset *token.FileSet, file LogicFile) error {
115-
astutil.DeleteImport(fset, f, fmt.Sprintf("%s/internal/types", ja.Module))
120+
typesImportPath := fmt.Sprintf("%s/%s", ja.Module, config.C.Gen.TypesDir)
121+
oldImportPath := fmt.Sprintf("%s/internal/types", ja.Module)
122+
astutil.DeleteImport(fset, f, oldImportPath)
116123
if file.RequestType == nil && file.ResponseType == nil {
117124
return nil
118125
}
119-
astutil.AddNamedImport(fset, f, "types", fmt.Sprintf("%s/internal/types/%s", ja.Module, file.Package))
126+
astutil.AddNamedImport(fset, f, "types", fmt.Sprintf("%s/%s/%s", ja.Module, config.C.Gen.TypesDir, file.Package))
120127
return nil
121128
}
122129

cmd/jzero/internal/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ type GenConfig struct {
137137
MongoCache bool `mapstructure:"mongo-cache"`
138138
MongoCachePrefix string `mapstructure:"mongo-cache-prefix"`
139139
MongoCacheType []string `mapstructure:"mongo-cache-type"`
140+
TypesDir string `mapstructure:"types-dir"`
140141

141142
// Gen Sub Command
142143
Swagger GenSwaggerConfig `mapstructure:"swagger"`

0 commit comments

Comments
 (0)