2020-07-29 17:11:41 +08:00
|
|
|
package gogen
|
|
|
|
|
|
|
|
import (
|
2022-04-04 13:12:05 +08:00
|
|
|
_ "embed"
|
2020-07-29 17:11:41 +08:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2022-01-25 23:15:07 +08:00
|
|
|
"github.com/zeromicro/go-zero/tools/goctl/api/spec"
|
|
|
|
"github.com/zeromicro/go-zero/tools/goctl/config"
|
|
|
|
"github.com/zeromicro/go-zero/tools/goctl/util/format"
|
|
|
|
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
|
|
|
|
"github.com/zeromicro/go-zero/tools/goctl/vars"
|
2020-07-29 17:11:41 +08:00
|
|
|
)
|
|
|
|
|
2022-04-04 13:12:05 +08:00
|
|
|
//go:embed main.tpl
|
|
|
|
var mainTemplate string
|
2020-07-29 17:11:41 +08:00
|
|
|
|
2021-05-29 23:01:02 +08:00
|
|
|
func genMain(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) error {
|
2020-07-29 17:11:41 +08:00
|
|
|
name := strings.ToLower(api.Service.Name)
|
2020-11-24 15:11:18 +08:00
|
|
|
filename, err := format.FileNamingFormat(cfg.NamingFormat, name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-12 14:58:37 +08:00
|
|
|
|
2021-08-12 14:55:39 +08:00
|
|
|
configName := filename
|
|
|
|
if strings.HasSuffix(filename, "-api") {
|
|
|
|
filename = strings.ReplaceAll(filename, "-api", "")
|
|
|
|
}
|
2020-11-24 15:11:18 +08:00
|
|
|
|
2021-01-09 00:17:23 +08:00
|
|
|
return genFile(fileGenConfig{
|
|
|
|
dir: dir,
|
|
|
|
subdir: "",
|
|
|
|
filename: filename + ".go",
|
|
|
|
templateName: "mainTemplate",
|
|
|
|
category: category,
|
|
|
|
templateFile: mainTemplateFile,
|
|
|
|
builtinTemplate: mainTemplate,
|
|
|
|
data: map[string]string{
|
2021-05-29 23:01:02 +08:00
|
|
|
"importPackages": genMainImports(rootPkg),
|
2021-08-12 14:58:37 +08:00
|
|
|
"serviceName": configName,
|
2021-01-09 00:17:23 +08:00
|
|
|
},
|
2020-07-29 17:11:41 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func genMainImports(parentPkg string) string {
|
2020-08-27 14:40:05 +08:00
|
|
|
var imports []string
|
2022-01-03 21:54:53 +08:00
|
|
|
imports = append(imports, fmt.Sprintf("\"%s\"", pathx.JoinPackages(parentPkg, configDir)))
|
|
|
|
imports = append(imports, fmt.Sprintf("\"%s\"", pathx.JoinPackages(parentPkg, handlerDir)))
|
|
|
|
imports = append(imports, fmt.Sprintf("\"%s\"\n", pathx.JoinPackages(parentPkg, contextDir)))
|
2021-02-20 19:50:03 +08:00
|
|
|
imports = append(imports, fmt.Sprintf("\"%s/core/conf\"", vars.ProjectOpenSourceURL))
|
|
|
|
imports = append(imports, fmt.Sprintf("\"%s/rest\"", vars.ProjectOpenSourceURL))
|
2020-07-29 17:11:41 +08:00
|
|
|
return strings.Join(imports, "\n\t")
|
|
|
|
}
|