forked from MasterJoyHunan/gengin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_main.go
63 lines (51 loc) · 1.33 KB
/
gen_main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package generator
import (
"fmt"
"sort"
"strings"
. "github.com/MasterJoyHunan/gengin/prepare"
"github.com/MasterJoyHunan/gengin/tpl"
"github.com/zeromicro/go-zero/core/collection"
"github.com/zeromicro/go-zero/tools/goctl/util/format"
)
const ginEngineName = "e"
func GenMain() error {
filename, err := format.FileNamingFormat(PluginInfo.Style, PluginInfo.Api.Service.Name)
if err != nil {
return err
}
if strings.HasSuffix(filename, "-api") {
filename = strings.ReplaceAll(filename, "-api", "")
}
return genFile(fileGenConfig{
dir: PluginInfo.Dir,
subDir: "",
filename: filename + ".go",
templateName: "mainTemplate",
builtinTemplate: tpl.MainTemplate,
data: map[string]interface{}{
"importPkg": genMainImportPkg(),
"etcDir": etcDir,
"configName": filename,
"setup": genMainSetup(),
"host": defaultHost,
"port": defaultPort,
"ginEngineName": ginEngineName,
},
})
}
func genMainImportPkg() string {
set := collection.NewSet()
// route
set.AddStr(fmt.Sprintf("\"%s/%s\"", RootPkg, routesDir))
// config
//set.AddStr(fmt.Sprintf())
importArr := set.KeysStr()
sort.Strings(importArr)
return strings.Join(importArr, "\n\t")
}
func genMainSetup() string {
return fmt.Sprintf(`
%s.Setup(%s)
`, routesPacket, ginEngineName)
}