mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-23 09:00:20 +08:00
parent
3022f93b6d
commit
546fcd8bab
@ -54,14 +54,19 @@ func DoGenProject(apiFile, dir, style string) error {
|
||||
}
|
||||
|
||||
logx.Must(util.MkdirIfNotExist(dir))
|
||||
rootPkg, err := getParentPackage(dir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
logx.Must(genEtc(dir, cfg, api))
|
||||
logx.Must(genConfig(dir, cfg, api))
|
||||
logx.Must(genMain(dir, cfg, api))
|
||||
logx.Must(genServiceContext(dir, cfg, api))
|
||||
logx.Must(genMain(dir, rootPkg, cfg, api))
|
||||
logx.Must(genServiceContext(dir, rootPkg, cfg, api))
|
||||
logx.Must(genTypes(dir, cfg, api))
|
||||
logx.Must(genRoutes(dir, cfg, api))
|
||||
logx.Must(genHandlers(dir, cfg, api))
|
||||
logx.Must(genLogic(dir, cfg, api))
|
||||
logx.Must(genRoutes(dir, rootPkg, cfg, api))
|
||||
logx.Must(genHandlers(dir, rootPkg, cfg, api))
|
||||
logx.Must(genLogic(dir, rootPkg, cfg, api))
|
||||
logx.Must(genMiddleware(dir, cfg, api))
|
||||
|
||||
if err := backupAndSweep(apiFile); err != nil {
|
||||
|
@ -49,18 +49,14 @@ type handlerInfo struct {
|
||||
HasRequest bool
|
||||
}
|
||||
|
||||
func genHandler(dir string, cfg *config.Config, group spec.Group, route spec.Route) error {
|
||||
func genHandler(dir, rootPkg string, cfg *config.Config, group spec.Group, route spec.Route) error {
|
||||
handler := getHandlerName(route)
|
||||
if getHandlerFolderPath(group, route) != handlerDir {
|
||||
handler = strings.Title(handler)
|
||||
}
|
||||
parentPkg, err := getParentPackage(dir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return doGenToFile(dir, handler, cfg, group, route, handlerInfo{
|
||||
ImportPackages: genHandlerImports(group, route, parentPkg),
|
||||
ImportPackages: genHandlerImports(group, route, rootPkg),
|
||||
HandlerName: handler,
|
||||
RequestType: util.Title(route.RequestTypeName()),
|
||||
LogicType: strings.Title(getLogicName(route)),
|
||||
@ -89,10 +85,10 @@ func doGenToFile(dir, handler string, cfg *config.Config, group spec.Group,
|
||||
})
|
||||
}
|
||||
|
||||
func genHandlers(dir string, cfg *config.Config, api *spec.ApiSpec) error {
|
||||
func genHandlers(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) error {
|
||||
for _, group := range api.Service.Groups {
|
||||
for _, route := range group.Routes {
|
||||
if err := genHandler(dir, cfg, group, route); err != nil {
|
||||
if err := genHandler(dir, rootPkg, cfg, group, route); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
@ -39,10 +39,10 @@ func (l *{{.logic}}) {{.function}}({{.request}}) {{.responseType}} {
|
||||
}
|
||||
`
|
||||
|
||||
func genLogic(dir string, cfg *config.Config, api *spec.ApiSpec) error {
|
||||
func genLogic(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) error {
|
||||
for _, g := range api.Service.Groups {
|
||||
for _, r := range g.Routes {
|
||||
err := genLogicByRoute(dir, cfg, g, r)
|
||||
err := genLogicByRoute(dir, rootPkg, cfg, g, r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -51,19 +51,14 @@ func genLogic(dir string, cfg *config.Config, api *spec.ApiSpec) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func genLogicByRoute(dir string, cfg *config.Config, group spec.Group, route spec.Route) error {
|
||||
func genLogicByRoute(dir, rootPkg string, cfg *config.Config, group spec.Group, route spec.Route) error {
|
||||
logic := getLogicName(route)
|
||||
goFile, err := format.FileNamingFormat(cfg.NamingFormat, logic)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
parentPkg, err := getParentPackage(dir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
imports := genLogicImports(route, parentPkg)
|
||||
imports := genLogicImports(route, rootPkg)
|
||||
var responseString string
|
||||
var returnString string
|
||||
var requestString string
|
||||
|
@ -39,7 +39,7 @@ func main() {
|
||||
}
|
||||
`
|
||||
|
||||
func genMain(dir string, cfg *config.Config, api *spec.ApiSpec) error {
|
||||
func genMain(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) error {
|
||||
name := strings.ToLower(api.Service.Name)
|
||||
if strings.HasSuffix(name, "-api") {
|
||||
name = strings.ReplaceAll(name, "-api", "")
|
||||
@ -49,11 +49,6 @@ func genMain(dir string, cfg *config.Config, api *spec.ApiSpec) error {
|
||||
return err
|
||||
}
|
||||
|
||||
parentPkg, err := getParentPackage(dir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return genFile(fileGenConfig{
|
||||
dir: dir,
|
||||
subdir: "",
|
||||
@ -63,7 +58,7 @@ func genMain(dir string, cfg *config.Config, api *spec.ApiSpec) error {
|
||||
templateFile: mainTemplateFile,
|
||||
builtinTemplate: mainTemplate,
|
||||
data: map[string]string{
|
||||
"importPackages": genMainImports(parentPkg),
|
||||
"importPackages": genMainImports(rootPkg),
|
||||
"serviceName": api.Service.Name,
|
||||
},
|
||||
})
|
||||
|
@ -62,7 +62,7 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
func genRoutes(dir string, cfg *config.Config, api *spec.ApiSpec) error {
|
||||
func genRoutes(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) error {
|
||||
var builder strings.Builder
|
||||
groups, err := getRoutes(api)
|
||||
if err != nil {
|
||||
@ -116,11 +116,6 @@ func genRoutes(dir string, cfg *config.Config, api *spec.ApiSpec) error {
|
||||
}
|
||||
}
|
||||
|
||||
parentPkg, err := getParentPackage(dir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
routeFilename, err := format.FileNamingFormat(cfg.NamingFormat, routesFilename)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -139,7 +134,7 @@ func genRoutes(dir string, cfg *config.Config, api *spec.ApiSpec) error {
|
||||
templateFile: "",
|
||||
builtinTemplate: routesTemplate,
|
||||
data: map[string]string{
|
||||
"importPackages": genRouteImports(parentPkg, api),
|
||||
"importPackages": genRouteImports(rootPkg, api),
|
||||
"routesAdditions": strings.TrimSpace(builder.String()),
|
||||
},
|
||||
})
|
||||
|
@ -33,7 +33,7 @@ func NewServiceContext(c {{.config}}) *ServiceContext {
|
||||
`
|
||||
)
|
||||
|
||||
func genServiceContext(dir string, cfg *config.Config, api *spec.ApiSpec) error {
|
||||
func genServiceContext(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) error {
|
||||
filename, err := format.FileNamingFormat(cfg.NamingFormat, contextFilename)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -45,11 +45,6 @@ func genServiceContext(dir string, cfg *config.Config, api *spec.ApiSpec) error
|
||||
auths = append(auths, fmt.Sprintf("%s config.AuthConfig", item))
|
||||
}
|
||||
|
||||
parentPkg, err := getParentPackage(dir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var middlewareStr string
|
||||
var middlewareAssignment string
|
||||
middlewares := getMiddleware(api)
|
||||
@ -61,9 +56,9 @@ func genServiceContext(dir string, cfg *config.Config, api *spec.ApiSpec) error
|
||||
fmt.Sprintf("middleware.New%s().%s", strings.Title(name), "Handle"))
|
||||
}
|
||||
|
||||
configImport := "\"" + ctlutil.JoinPackages(parentPkg, configDir) + "\""
|
||||
configImport := "\"" + ctlutil.JoinPackages(rootPkg, configDir) + "\""
|
||||
if len(middlewareStr) > 0 {
|
||||
configImport += "\n\t\"" + ctlutil.JoinPackages(parentPkg, middlewareDir) + "\""
|
||||
configImport += "\n\t\"" + ctlutil.JoinPackages(rootPkg, middlewareDir) + "\""
|
||||
configImport += fmt.Sprintf("\n\t\"%s/rest\"", vars.ProjectOpenSourceURL)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user