Skip to content

Commit 3e48b95

Browse files
committed
feat: Support set module name in config
1 parent a7eb84c commit 3e48b95

5 files changed

Lines changed: 38 additions & 26 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.PHONY: build
22

3-
RELEASE_VERSION = v10.3.1
3+
RELEASE_VERSION = v10.3.3
44

55
APP = gin-admin-cli
66
BIN = ${APP}

cmd/generate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func Generate() *cli.Command {
2727
Name: "module",
2828
Aliases: []string{"m"},
2929
Usage: "The module to generate the struct from (like: RBAC)",
30-
Required: true,
30+
Required: false,
3131
},
3232
&cli.StringFlag{
3333
Name: "module-path",
@@ -87,7 +87,7 @@ func Generate() *cli.Command {
8787
gen := actions.Generate(actions.GenerateConfig{
8888
Dir: c.String("dir"),
8989
TplType: c.String("tpl-type"),
90-
ModuleName: c.String("module"),
90+
Module: c.String("module"),
9191
ModulePath: c.String("module-path"),
9292
WirePath: c.String("wire-path"),
9393
SwaggerPath: c.String("swag-path"),

internal/actions/generate.go

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
type GenerateConfig struct {
1717
Dir string
1818
TplType string
19-
ModuleName string
19+
Module string
2020
ModulePath string
2121
WirePath string
2222
SwaggerPath string
@@ -29,7 +29,7 @@ func Generate(cfg GenerateConfig) *GenerateAction {
2929
cfg: &cfg,
3030
fs: tfs.Ins,
3131
rootImportPath: parser.GetRootImportPath(cfg.Dir),
32-
moduleImportPath: parser.GetModuleImportPath(cfg.Dir, cfg.ModulePath, cfg.ModuleName),
32+
moduleImportPath: parser.GetModuleImportPath(cfg.Dir, cfg.ModulePath, cfg.Module),
3333
UtilImportPath: parser.GetUtilImportPath(cfg.Dir, cfg.ModulePath),
3434
}
3535
}
@@ -85,32 +85,45 @@ func (a *GenerateAction) RunWithStruct(ctx context.Context, s *schema.S) error {
8585
}
8686

8787
func (a *GenerateAction) run(ctx context.Context, data []*schema.S) error {
88+
moduleMap := make(map[string]bool)
89+
8890
for _, d := range data {
91+
if d.Module == "" && a.cfg.Module == "" {
92+
return fmt.Errorf("Struct %s module is empty", d.Name)
93+
}
94+
95+
if d.Module == "" {
96+
d.Module = a.cfg.Module
97+
}
98+
if !moduleMap[d.Module] {
99+
moduleMap[d.Module] = true
100+
}
89101
if err := a.generate(ctx, d); err != nil {
90102
return err
91103
}
92-
93104
if d.GenerateFE {
94105
if err := a.generateFE(ctx, d); err != nil {
95106
return err
96107
}
97108
}
98109
}
99110

100-
modsTplData, err := parser.ModifyModsFile(ctx, parser.BasicArgs{
101-
Dir: a.cfg.Dir,
102-
ModuleName: a.cfg.ModuleName,
103-
ModulePath: a.cfg.ModulePath,
104-
Flag: parser.AstFlagGen,
105-
})
106-
if err != nil {
107-
a.logger.Errorf("Failed to modify mods file, err: %s", err)
108-
return err
109-
}
111+
for module := range moduleMap {
112+
modsTplData, err := parser.ModifyModsFile(ctx, parser.BasicArgs{
113+
Dir: a.cfg.Dir,
114+
ModuleName: module,
115+
ModulePath: a.cfg.ModulePath,
116+
Flag: parser.AstFlagGen,
117+
})
118+
if err != nil {
119+
a.logger.Errorf("Failed to modify mods file, err: %s", err)
120+
return err
121+
}
110122

111-
err = a.write(ctx, a.cfg.ModuleName, "", parser.FileForMods, modsTplData, false)
112-
if err != nil {
113-
return err
123+
err = a.write(ctx, module, "", parser.FileForMods, modsTplData, false)
124+
if err != nil {
125+
return err
126+
}
114127
}
115128

116129
return a.execWireAndSwag(ctx)
@@ -200,7 +213,6 @@ func (a *GenerateAction) write(ctx context.Context, moduleName, structName, tpl
200213
func (a *GenerateAction) generate(ctx context.Context, dataItem *schema.S) error {
201214
dataItem = dataItem.Format()
202215
dataItem.RootImportPath = a.rootImportPath
203-
dataItem.ModuleName = a.cfg.ModuleName
204216
dataItem.ModuleImportPath = a.moduleImportPath
205217
dataItem.UtilImportPath = a.UtilImportPath
206218

@@ -217,15 +229,15 @@ func (a *GenerateAction) generate(ctx context.Context, dataItem *schema.S) error
217229
return err
218230
}
219231

220-
err = a.write(ctx, dataItem.ModuleName, dataItem.Name, parser.StructPackageTplPaths[pkgName], tplData, !dataItem.ForceWrite)
232+
err = a.write(ctx, dataItem.Module, dataItem.Name, parser.StructPackageTplPaths[pkgName], tplData, !dataItem.ForceWrite)
221233
if err != nil {
222234
return err
223235
}
224236
}
225237

226238
basicArgs := parser.BasicArgs{
227239
Dir: a.cfg.Dir,
228-
ModuleName: dataItem.ModuleName,
240+
ModuleName: dataItem.Module,
229241
ModulePath: a.cfg.ModulePath,
230242
StructName: dataItem.Name,
231243
GenPackages: genPackages,
@@ -238,7 +250,7 @@ func (a *GenerateAction) generate(ctx context.Context, dataItem *schema.S) error
238250
return err
239251
}
240252

241-
err = a.write(ctx, dataItem.ModuleName, dataItem.Name, parser.FileForModuleMain, moduleMainTplData, false)
253+
err = a.write(ctx, dataItem.Module, dataItem.Name, parser.FileForModuleMain, moduleMainTplData, false)
242254
if err != nil {
243255
return err
244256
}
@@ -249,7 +261,7 @@ func (a *GenerateAction) generate(ctx context.Context, dataItem *schema.S) error
249261
return err
250262
}
251263

252-
err = a.write(ctx, dataItem.ModuleName, dataItem.Name, parser.FileForModuleWire, moduleWireTplData, false)
264+
err = a.write(ctx, dataItem.Module, dataItem.Name, parser.FileForModuleWire, moduleWireTplData, false)
253265
if err != nil {
254266
return err
255267
}

internal/schema/struct.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
type S struct {
1111
RootImportPath string `yaml:"-" json:"-"`
12-
ModuleName string `yaml:"-" json:"-"`
1312
ModuleImportPath string `yaml:"-" json:"-"`
1413
UtilImportPath string `yaml:"-" json:"-"`
1514
Include struct {
@@ -19,6 +18,7 @@ type S struct {
1918
UpdatedAt bool
2019
Sequence bool
2120
} `yaml:"-" json:"-"`
21+
Module string `yaml:"module,omitempty" json:"module,omitempty"`
2222
Name string `yaml:"name,omitempty" json:"name,omitempty"`
2323
TableName string `yaml:"table_name,omitempty" json:"table_name,omitempty"`
2424
Comment string `yaml:"comment,omitempty" json:"comment,omitempty"`

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
//go:embed tpls
1414
var f embed.FS
1515

16-
var VERSION = "v10.3.1"
16+
var VERSION = "v10.3.3"
1717

1818
func main() {
1919
defer func() {

0 commit comments

Comments
 (0)