This repository has been archived by the owner on Nov 5, 2023. It is now read-only.
/
project.go
203 lines (176 loc) · 4.71 KB
/
project.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package cmd
import (
"context"
"fmt"
"github.com/gobuffalo/genny/v2"
"github.com/gobuffalo/plush/v4"
"github.com/markbates/pkger"
"github.com/ronaksoft/rony/config"
"github.com/spf13/cobra"
"os"
"os/exec"
"path/filepath"
"strings"
)
/*
Creation Time: 2020 - Aug - 28
Created by: (ehsan)
Maintainers:
1. Ehsan N. Moosa (E2)
Auditor: Ehsan N. Moosa (E2)
Copyright Ronak Software Group 2020
*/
const (
skeletonPath = "github.com/ronaksoft/rony:/internal/templates"
)
func init() {
workingDir, _ := os.Getwd()
_ = config.Init("", workingDir)
config.SetPersistentFlags(RootCmd,
config.BoolFlag("dry-run", false, "don't actually change anything"),
)
config.SetFlags(createCmd,
config.StringFlag("project.dir", workingDir, "the root path of the project"),
config.StringFlag("project.name", "sample-project", "the name of the project"),
config.StringFlag("package.name", "github.com/sample", "the full path of go package in go.mod file"),
)
config.SetFlags(genProtoCmd,
config.StringFlag("project.dir", workingDir, "the root path of the project"),
)
RootCmd.AddCommand(projectCmd)
projectCmd.AddCommand(createCmd, genProtoCmd)
_ = pkger.Walk(skeletonPath, func(path string, info os.FileInfo, err error) error {
return nil
})
}
var projectCmd = &cobra.Command{
Use: "project",
}
var createCmd = &cobra.Command{
Use: "create",
RunE: func(cmd *cobra.Command, args []string) error {
err := config.BindCmdFlags(cmd)
if err != nil {
return err
}
r := genny.WetRunner(context.Background())
if config.GetBool("dry-run") {
r = genny.DryRunner(context.Background())
}
g := genny.New()
setupSkeleton(g)
goModuleInit(g)
goModuleTidy(g)
gofmt(g)
// Create a Runner with the Generator customized by command's arguments
err = r.With(g)
if err != nil {
return err
}
return r.Run()
},
}
var genProtoCmd = &cobra.Command{
Use: "gen-proto",
RunE: func(cmd *cobra.Command, args []string) error {
r := genny.WetRunner(context.Background())
if config.GetBool("dry-run") {
r = genny.DryRunner(context.Background())
}
g := genny.New()
compileProto(g)
gofmt(g)
goModuleTidy(g)
// Create a Runner with the Generator customized by command's arguments
err := r.With(g)
if err != nil {
return err
}
return r.Run()
},
}
func setupSkeleton(g *genny.Generator) {
projectPath := config.GetString("project.dir")
projectName := config.GetString("project.name")
_ = os.Mkdir(projectPath, os.ModePerm)
pathPrefix := skeletonPath + "/skel"
err := pkger.Walk(pathPrefix, func(path string, info os.FileInfo, err error) error {
realPath := strings.TrimSuffix(strings.TrimPrefix(path, pathPrefix), ".tpl")
if info.IsDir() {
g.File(genny.NewDir(filepath.Join(projectPath, realPath), os.ModeDir|0744))
} else {
f, err := pkger.Open(path)
if err != nil {
return err
}
tCtx := plush.NewContext()
tCtx.Set("projectName", func() string {
return projectName
})
s, err := plush.RenderR(f, tCtx)
if err != nil {
return err
}
g.File(genny.NewFileS(filepath.Join(projectPath, realPath), s))
_ = f.Close()
}
return nil
})
if err != nil {
panic(err)
}
}
func goModuleInit(g *genny.Generator) {
projectPath := config.GetString("project.dir")
packageName := config.GetString("package.name")
cmd := exec.Command("go", "mod", "init", packageName)
cmd.Env = os.Environ()
cmd.Dir = projectPath
g.Command(cmd)
}
func goModuleTidy(g *genny.Generator) {
cmd := exec.Command("go", "mod", "tidy")
cmd.Env = os.Environ()
cmd.Dir = config.GetString("project.dir")
g.Command(cmd)
}
func gofmt(g *genny.Generator) {
cmd := exec.Command("go", "fmt", "./...")
cmd.Env = os.Environ()
cmd.Dir = config.GetString("project.dir")
g.Command(cmd)
}
func compileProto(g *genny.Generator) {
projectPath := config.GetString("project.dir")
// Compile proto files
folders := []string{"service", "model"}
for _, f := range folders {
_ = filepath.Walk(filepath.Join(projectPath, f), func(path string, info os.FileInfo, err error) error {
if info == nil || info.IsDir() {
return nil
}
if filepath.Ext(info.Name()) == ".proto" {
projectPathAbs, _ := filepath.Abs(projectPath)
cmd1 := exec.Command(
"protoc",
fmt.Sprintf("-I=%s", projectPathAbs),
fmt.Sprintf("--go_out=paths=source_relative:%s", projectPathAbs),
path,
)
cmd1.Env = os.Environ()
cmd1.Dir = filepath.Dir(projectPathAbs)
g.Command(cmd1)
cmd2 := exec.Command(
"protoc",
fmt.Sprintf("-I=%s", projectPathAbs),
fmt.Sprintf("--gorony_out=paths=source_relative,plugin=server:%s", projectPathAbs),
path,
)
cmd2.Env = os.Environ()
cmd2.Dir = filepath.Dir(projectPathAbs)
g.Command(cmd2)
}
return nil
})
}
}