patch: save missing templates to disk (#1463)

Co-authored-by: anqiansong <anqiansong@bytedance.com>
master
anqiansong 3 years ago committed by GitHub
parent e57fa8ff53
commit c903966fc7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -45,6 +45,8 @@ func genMiddleware(dir string, cfg *config.Config, api *spec.ApiSpec) error {
subdir: middlewareDir, subdir: middlewareDir,
filename: filename + ".go", filename: filename + ".go",
templateName: "contextTemplate", templateName: "contextTemplate",
category: category,
templateFile: middlewareImplementCodeFile,
builtinTemplate: middlewareImplementCode, builtinTemplate: middlewareImplementCode,
data: map[string]string{ data: map[string]string{
"name": strings.Title(name), "name": strings.Title(name),

@ -73,7 +73,12 @@ func genRoutes(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) error
return err return err
} }
gt := template.Must(template.New("groupTemplate").Parse(routesAdditionTemplate)) templateText, err := pathx.LoadTemplate(category, routesAdditionTemplateFile, routesAdditionTemplate)
if err != nil {
return err
}
gt := template.Must(template.New("groupTemplate").Parse(templateText))
for _, g := range groups { for _, g := range groups {
var gbuilder strings.Builder var gbuilder strings.Builder
gbuilder.WriteString("[]rest.Route{") gbuilder.WriteString("[]rest.Route{")
@ -139,8 +144,8 @@ rest.WithPrefix("%s"),`, g.prefix)
subdir: handlerDir, subdir: handlerDir,
filename: routeFilename, filename: routeFilename,
templateName: "routesTemplate", templateName: "routesTemplate",
category: "", category: category,
templateFile: "", templateFile: routesTemplateFile,
builtinTemplate: routesTemplate, builtinTemplate: routesTemplate,
data: map[string]string{ data: map[string]string{
"importPackages": genRouteImports(rootPkg, api), "importPackages": genRouteImports(rootPkg, api),

@ -63,8 +63,8 @@ func genTypes(dir string, cfg *config.Config, api *spec.ApiSpec) error {
subdir: typesDir, subdir: typesDir,
filename: typeFilename, filename: typeFilename,
templateName: "typesTemplate", templateName: "typesTemplate",
category: "", category: category,
templateFile: "", templateFile: typesTemplateFile,
builtinTemplate: typesTemplate, builtinTemplate: typesTemplate,
data: map[string]interface{}{ data: map[string]interface{}{
"types": val, "types": val,

@ -8,22 +8,30 @@ import (
) )
const ( const (
category = "api" category = "api"
configTemplateFile = "config.tpl" configTemplateFile = "config.tpl"
contextTemplateFile = "context.tpl" contextTemplateFile = "context.tpl"
etcTemplateFile = "etc.tpl" etcTemplateFile = "etc.tpl"
handlerTemplateFile = "handler.tpl" handlerTemplateFile = "handler.tpl"
logicTemplateFile = "logic.tpl" logicTemplateFile = "logic.tpl"
mainTemplateFile = "main.tpl" mainTemplateFile = "main.tpl"
middlewareImplementCodeFile = "middleware.tpl"
routesTemplateFile = "routes.tpl"
routesAdditionTemplateFile = "route-addition.tpl"
typesTemplateFile = "types.tpl"
) )
var templates = map[string]string{ var templates = map[string]string{
configTemplateFile: configTemplate, configTemplateFile: configTemplate,
contextTemplateFile: contextTemplate, contextTemplateFile: contextTemplate,
etcTemplateFile: etcTemplate, etcTemplateFile: etcTemplate,
handlerTemplateFile: handlerTemplate, handlerTemplateFile: handlerTemplate,
logicTemplateFile: logicTemplate, logicTemplateFile: logicTemplate,
mainTemplateFile: mainTemplate, mainTemplateFile: mainTemplate,
middlewareImplementCodeFile: middlewareImplementCode,
routesTemplateFile: routesTemplate,
routesAdditionTemplateFile: routesAdditionTemplate,
typesTemplateFile: typesTemplate,
} }
// Category returns the category of the api files. // Category returns the category of the api files.

Loading…
Cancel
Save