Skip to content

Commit

Permalink
feat: Add mod Release function
Browse files Browse the repository at this point in the history
  • Loading branch information
LyricTian committed Dec 19, 2023
1 parent cf34ef8 commit a01676b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.PHONY: build

RELEASE_VERSION = v10.2.1
RELEASE_VERSION = v10.2.2

APP = gin-admin-cli
BIN = ${APP}
Expand Down
4 changes: 4 additions & 0 deletions internal/parser/tpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ func (a *$$ModuleName$$) Init(ctx context.Context) error {
func (a *$$ModuleName$$) RegisterV1Routers(ctx context.Context, v1 *gin.RouterGroup) error {
return nil
}
func (a *$$ModuleName$$) Release(ctx context.Context) error {
return nil
}
`

var tplModuleWire = `
Expand Down
55 changes: 55 additions & 0 deletions internal/parser/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,8 @@ func (v *astModsVisitor) Visit(node ast.Node) ast.Visitor {
v.modifyFuncInit(x)
} else if x.Name.Name == "RegisterRouters" {
v.modifyFuncRegisterRouters(x)
} else if x.Name.Name == "Release" {
v.modifyFuncRelease(x)
}
}
return v
Expand Down Expand Up @@ -630,3 +632,56 @@ func (v *astModsVisitor) modifyFuncRegisterRouters(x *ast.FuncDecl) {
}
}
}

func (v *astModsVisitor) modifyFuncRelease(x *ast.FuncDecl) {
findIndex := -1
list := x.Body.List
for i, stmt := range list {
if s, ok := stmt.(*ast.IfStmt); ok {
var sb strings.Builder
printer.Fprint(&sb, v.fset, s.Init)
if strings.Contains(sb.String(), fmt.Sprintf("%s.Release", v.args.ModuleName)) {
findIndex = i
break
}
}
}
if v.args.Flag&AstFlagGen != 0 {
if findIndex == -1 {
e, err := parser.ParseExpr(fmt.Sprintf("a.%s.Release(ctx)", v.args.ModuleName))
if err == nil {
list = append(list[:len(list)-1], append([]ast.Stmt{&ast.IfStmt{
Init: &ast.AssignStmt{
Lhs: []ast.Expr{
ast.NewIdent("err"),
},
Tok: token.DEFINE,
Rhs: []ast.Expr{
e,
},
},
Cond: &ast.BinaryExpr{
X: ast.NewIdent("err"),
Op: token.NEQ,
Y: ast.NewIdent("nil"),
},
Body: &ast.BlockStmt{
List: []ast.Stmt{
&ast.ReturnStmt{
Results: []ast.Expr{
ast.NewIdent("err"),
},
},
},
},
}}, list[len(list)-1])...)
x.Body.List = list
}
}
} else if v.args.Flag&AstFlagRem != 0 {
if findIndex != -1 {
list = append(list[:findIndex], list[findIndex+1:]...)
x.Body.List = list
}
}
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
//go:embed tpls
var f embed.FS

var VERSION = "v10.2.1"
var VERSION = "v10.2.2"

func main() {
defer func() {
Expand Down

0 comments on commit a01676b

Please sign in to comment.