Skip to content

Commit

Permalink
组织代码
Browse files Browse the repository at this point in the history
  • Loading branch information
Skyenought committed May 19, 2024
1 parent fa9e4e5 commit ec151a0
Show file tree
Hide file tree
Showing 18 changed files with 310 additions and 310 deletions.
253 changes: 0 additions & 253 deletions cmd/hertz_migrate/internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,8 @@
package cli

import (
"bytes"
"go/ast"
"go/parser"
"go/printer"
"go/token"
"log"
"os"
"strings"
"sync"

"github.com/hertz-contrib/migrate/cmd/hertz_migrate/internal/logic"

"github.com/hertz-contrib/migrate/cmd/hertz_migrate/internal/logic/gin"
"github.com/hertz-contrib/migrate/cmd/hertz_migrate/internal/logs"
"github.com/hertz-contrib/migrate/cmd/hertz_migrate/internal/types"

mapset "github.com/deckarep/golang-set/v2"
"github.com/hertz-contrib/migrate/cmd/hertz_migrate/internal"
"github.com/hertz-contrib/migrate/cmd/hertz_migrate/internal/logic/chi"
nethttp "github.com/hertz-contrib/migrate/cmd/hertz_migrate/internal/logic/netHttp"
"github.com/hertz-contrib/migrate/cmd/hertz_migrate/internal/utils"
"github.com/urfave/cli/v2"

"golang.org/x/tools/go/ast/astutil"
)

func init() {
globalMap = make(map[string]interface{})
}

var (
globalArgs = &Args{}
globalMap map[string]any
fset *token.FileSet
wg sync.WaitGroup

printerConfig = printer.Config{
Mode: printer.UseSpaces,
Tabwidth: 4,
}
)

type Args struct {
Expand Down Expand Up @@ -126,218 +88,3 @@ func Init() *cli.App {
app.Action = Run
return app
}

func Run(c *cli.Context) error {
var (
gofiles []string
goModDirs []string
err error
)

if gofiles, goModDirs, err = preload(c); err != nil {
return err
}

for _, path := range gofiles {
file, err := parser.ParseFile(fset, path, nil, 0)
if err != nil {
logs.Debugf("Parse file fail, error: %v", err)
return internal.ErrParseFile
}

// collect global information
astutil.Apply(file, func(c *astutil.Cursor) bool {
logic.GetHttpServerProps(c)
if globalArgs.UseGin {
gin.GetFuncNameHasGinCtx(c)
}
if globalArgs.UseNetHTTP {
nethttp.FindHandlerFuncName(c, internal.WebCtxSet)
}
return true
}, nil)
}

if err = processFiles(gofiles); err != nil {
return err
}

for _, dir := range goModDirs {
utils.RunGoImports(dir)
utils.RunGoModTidy(dir)
}
logs.Info("everything are ok!")
return nil
}

func processFiles(gofiles []string) error {
for _, path := range gofiles {
var containsGin bool

file, err := parser.ParseFile(fset, path, nil, parser.ParseComments)
if err != nil {
logs.Debugf("Parse file fail, error: %v", err)
return internal.ErrParseFile
}

astutil.AddNamedImport(fset, file, "hzserver", globalArgs.HzRepo+"/pkg/app/server")
astutil.AddNamedImport(fset, file, "hzapp", globalArgs.HzRepo+"/pkg/app")
astutil.AddNamedImport(fset, file, "hzroute", globalArgs.HzRepo+"/pkg/route")
astutil.AddNamedImport(fset, file, "hzerrors", globalArgs.HzRepo+"/pkg/common/errors")
astutil.AddNamedImport(fset, file, "hzutils", globalArgs.HzRepo+"/pkg/common/utils")

for _, importSpec := range file.Imports {
importStr := importSpec.Path.Value

if globalArgs.UseGin {
if strings.Contains(importStr, `github.com/gin-gonic/gin`) {
containsGin = true
}

if strings.Contains(importStr, `"github.com/gin-contrib/cors"`) {
importSpec.Path.Value = `"github.com/hertz-contrib/cors"`
containsGin = true
}

if strings.Contains(importStr, `github.com/swaggo/gin-swagger`) {
importSpec.Path.Value = `"github.com/hertz-contrib/swagger"`
containsGin = true
}
}
}

if !containsGin && globalArgs.UseGin {
continue
}

astutil.Apply(file, func(c *astutil.Cursor) bool {
switch node := c.Node().(type) {
case *ast.StarExpr:
if globalArgs.UseChi {
if utils.CheckPtrPkgAndStructName(node, "chi", "Mux") {
c.Replace(types.StarServerHertz)
}
}
if globalArgs.UseGin {
if sel, ok := node.X.(*ast.SelectorExpr); ok {
if utils.CheckSelPkgAndStruct(sel, "gin", "Engine") {
c.Replace(types.StarServerHertz)
}

if utils.CheckSelPkgAndStruct(sel, "gin", "RouterGroup") {
c.Replace(types.StarRouteGroup)
}
}
}
case *ast.FieldList:
if globalArgs.UseGin {
gin.ReplaceGinCtx(node)
}
case *ast.SelectorExpr:
if globalArgs.UseGin {
if utils.CheckSelPkgAndStruct(node, "route", "IRoutes") {
c.Replace(types.SelIRoutes)
}
}
}
if globalArgs.UseNetHTTP {
nethttp.GetOptionsFromHttpServer(c, globalMap)
nethttp.PackServerHertz(c, globalMap)
nethttp.ReplaceNetHttpHandler(c)
}
return true
}, nil)

astutil.Apply(file, func(c *astutil.Cursor) bool {
netHttpGroup(c, internal.WebCtxSet)

switch node := c.Node().(type) {
case *ast.SelectorExpr:
if globalArgs.UseNetHTTP {
if utils.CheckSelObj(node, "http", "ResponseWriter") {
switch node.Sel.Name {
case "WriteHeader":
c.Replace(types.SelSetStatusCode)
case "Write":
c.Replace(types.SelWrite)
case "Header":
c.Replace(types.SelRespHeader)
}
}

if node.Sel.Name == "HandleFunc" {
node.Sel.Name = "Any"
}
nethttp.ReplaceRequestOp(node, c)
}

if globalArgs.UseGin {
if utils.CheckSelPkgAndStruct(node, "gin", "HandlerFunc") {
c.Replace(types.SelAppHandlerFunc)
}

if utils.CheckSelPkgAndStruct(node, "gin", "H") {
node.X.(*ast.Ident).Name = "hzutils"
}

gin.ReplaceBinding(node, c)
gin.ReplaceRequestOp(node, c)
gin.ReplaceRespOp(node, c)
gin.ReplaceErrorType(node)
}
case *ast.CallExpr:
if globalArgs.UseChi {
chi.PackChiRouterMethod(node)
chi.PackChiNewRouter(node, c)
}
if globalArgs.UseNetHTTP {
nethttp.ReplaceHttpOp(node, c)
nethttp.ReplaceReqOrRespOp(node, c)
nethttp.ReplaceReqURLQuery(node)
if utils.CheckCallPkgAndMethodName(node, "http", "NotFound") {
c.Replace(types.CallNotFound)
}
}

if globalArgs.UseGin {
gin.ReplaceGinNew(node, c)
gin.ReplaceGinRun(node)
gin.ReplaceGinCtxOp(node, c)
gin.ReplaceCallReqOrResp(node, c)
gin.ReplaceStatisFS(node)
}
}
if globalArgs.UseGin {
gin.ReplaceCtxParamList(c)
}
return true
}, nil)

var buf bytes.Buffer

if err = printerConfig.Fprint(&buf, fset, file); err != nil {
logs.Debugf("Fprint fail, error: %v", err)
return internal.ErrSaveChanges
}

if err := os.WriteFile(path, buf.Bytes(), os.ModePerm); err == nil {
log.Println("File updated:", path)
}
}
return nil
}

func netHttpGroup(c *astutil.Cursor, funcSet mapset.Set[string]) {
if globalArgs.UseNetHTTP {
nethttp.PackFprintf(c)
nethttp.ReplaceReqHeader(c)
nethttp.ReplaceReqHeaderOperation(c)
nethttp.ReplaceRespWrite(c)
nethttp.ReplaceReqFormGet(c)
nethttp.ReplaceReqFormValue(c)
nethttp.ReplaceReqMultipartForm(c)
nethttp.PackType2AppHandlerFunc(c)
nethttp.ReplaceReqMultipartFormOperation(c, globalMap)
nethttp.ReplaceFuncBodyHttpHandlerParam(c, funcSet)
}
}
Loading

0 comments on commit ec151a0

Please sign in to comment.