-
Notifications
You must be signed in to change notification settings - Fork 0
/
biz.go
67 lines (58 loc) · 1.23 KB
/
biz.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
64
65
66
67
package generator
import (
"bytes"
_ "embed"
"fmt"
"os"
"path"
"strings"
"text/template"
"github.com/iancoleman/strcase"
"github.com/yoogoc/kratos-scaffold/pkg/cli"
"github.com/yoogoc/kratos-scaffold/pkg/util"
"golang.org/x/tools/imports"
)
type Biz struct {
Base
}
func NewBiz(setting *cli.EnvSettings) *Biz {
return &Biz{
Base: NewBase(setting, true),
}
}
//go:embed tmpl/biz.tmpl
var bizTmpl string
func (b *Biz) Generate() error {
fmt.Println("generating biz...")
buf := new(bytes.Buffer)
funcMap := template.FuncMap{
"ToLower": strings.ToLower,
"ToPlural": util.Plural,
"ToCamel": strcase.ToCamel,
}
tmpl, err := template.New("bizTmpl").Funcs(funcMap).Parse(bizTmpl)
if err != nil {
return err
}
err = tmpl.Execute(buf, b)
if err != nil {
return err
}
p := path.Join(b.OutPath(), strcase.ToSnake(b.Name)+".go")
content, err := imports.Process(p, buf.Bytes(), nil)
if err != nil {
return err
}
return os.WriteFile(p, content, 0o644)
}
func (b *Biz) OutPath() string {
wd, err := os.Getwd()
if err != nil {
panic(err)
}
if b.Namespace != "" {
return path.Join(wd, b.AppDirName, b.Namespace, "internal/biz") // TODO
} else {
return path.Join(wd, b.Namespace, "internal/biz") // TODO
}
}