Skip to content

Commit a01676b

Browse files
committed
feat: Add mod Release function
1 parent cf34ef8 commit a01676b

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.PHONY: build
22

3-
RELEASE_VERSION = v10.2.1
3+
RELEASE_VERSION = v10.2.2
44

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

internal/parser/tpl.go

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ func (a *$$ModuleName$$) Init(ctx context.Context) error {
3030
func (a *$$ModuleName$$) RegisterV1Routers(ctx context.Context, v1 *gin.RouterGroup) error {
3131
return nil
3232
}
33+
34+
func (a *$$ModuleName$$) Release(ctx context.Context) error {
35+
return nil
36+
}
3337
`
3438

3539
var tplModuleWire = `

internal/parser/visitor.go

+55
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,8 @@ func (v *astModsVisitor) Visit(node ast.Node) ast.Visitor {
419419
v.modifyFuncInit(x)
420420
} else if x.Name.Name == "RegisterRouters" {
421421
v.modifyFuncRegisterRouters(x)
422+
} else if x.Name.Name == "Release" {
423+
v.modifyFuncRelease(x)
422424
}
423425
}
424426
return v
@@ -630,3 +632,56 @@ func (v *astModsVisitor) modifyFuncRegisterRouters(x *ast.FuncDecl) {
630632
}
631633
}
632634
}
635+
636+
func (v *astModsVisitor) modifyFuncRelease(x *ast.FuncDecl) {
637+
findIndex := -1
638+
list := x.Body.List
639+
for i, stmt := range list {
640+
if s, ok := stmt.(*ast.IfStmt); ok {
641+
var sb strings.Builder
642+
printer.Fprint(&sb, v.fset, s.Init)
643+
if strings.Contains(sb.String(), fmt.Sprintf("%s.Release", v.args.ModuleName)) {
644+
findIndex = i
645+
break
646+
}
647+
}
648+
}
649+
if v.args.Flag&AstFlagGen != 0 {
650+
if findIndex == -1 {
651+
e, err := parser.ParseExpr(fmt.Sprintf("a.%s.Release(ctx)", v.args.ModuleName))
652+
if err == nil {
653+
list = append(list[:len(list)-1], append([]ast.Stmt{&ast.IfStmt{
654+
Init: &ast.AssignStmt{
655+
Lhs: []ast.Expr{
656+
ast.NewIdent("err"),
657+
},
658+
Tok: token.DEFINE,
659+
Rhs: []ast.Expr{
660+
e,
661+
},
662+
},
663+
Cond: &ast.BinaryExpr{
664+
X: ast.NewIdent("err"),
665+
Op: token.NEQ,
666+
Y: ast.NewIdent("nil"),
667+
},
668+
Body: &ast.BlockStmt{
669+
List: []ast.Stmt{
670+
&ast.ReturnStmt{
671+
Results: []ast.Expr{
672+
ast.NewIdent("err"),
673+
},
674+
},
675+
},
676+
},
677+
}}, list[len(list)-1])...)
678+
x.Body.List = list
679+
}
680+
}
681+
} else if v.args.Flag&AstFlagRem != 0 {
682+
if findIndex != -1 {
683+
list = append(list[:findIndex], list[findIndex+1:]...)
684+
x.Body.List = list
685+
}
686+
}
687+
}

main.go

+1-1
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.2.1"
16+
var VERSION = "v10.2.2"
1717

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

0 commit comments

Comments
 (0)