-
Notifications
You must be signed in to change notification settings - Fork 0
/
genServices.go
249 lines (199 loc) · 5.8 KB
/
genServices.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package gen
import (
"fmt"
"html/template"
"io/ioutil"
"os"
"path"
"regexp"
"strings"
"github.com/macinnir/dvc/core/lib"
)
// GenerateServiceInterfaces scans the services directory and outputs 2 files
// 1. A services bootstrap file in the services directory
// 2. A services definition file in the definitions directory
func (g *Gen) GenerateServiceInterfaces(definitionsDir string, servicesDir string) error {
lib.EnsureDir(servicesDir)
var data = struct {
BasePackage string
Imports []string
Services map[string][]string
}{
BasePackage: g.Config.BasePackage,
Imports: []string{
fmt.Sprintf("%s/definitions/models", g.Config.BasePackage),
"github.com/macinnir/dvc/core/lib/utils",
},
Services: map[string][]string{},
}
definitionsPath := fmt.Sprintf("%s/viewmodels", definitionsDir)
if lib.DirExists(definitionsPath) && !lib.DirIsEmpty(definitionsPath) {
data.Imports = append(data.Imports, fmt.Sprintf("%s/definitions/viewmodels", g.Config.BasePackage))
}
// fmt.Sprintf("%s/definitions/viewmodels", g.Config.BasePackage),
// g.EnsureDir("definitions/viewmodels")
var serviceNames []string
var e error
serviceNames, e = getServiceNames(servicesDir)
if e != nil {
return e
}
var fileBytes []byte
for _, serviceName := range serviceNames {
data.Services[serviceName] = []string{}
// Get the service file
fileBytes, e = ioutil.ReadFile(path.Join(servicesDir, serviceName+".go"))
if e != nil {
return e
}
fileString := string(fileBytes)
fileLines := strings.Split(fileString, "\n")
funcSig := fmt.Sprintf(`^func \(s \*%s\) [A-Z].*$`, serviceName)
var validSignature = regexp.MustCompile(funcSig)
funcPrefix := fmt.Sprintf("func (s *%s) ", serviceName)
for _, line := range fileLines {
if validSignature.Match([]byte(line)) {
// Remove the prefix and the ending space and open bracket
funcLine := line[len(funcPrefix) : len(line)-2]
data.Services[serviceName] = append(data.Services[serviceName], funcLine)
}
}
}
t := template.New("service-interfaces")
tpl := `
// Package definitions outlines objects and functionality used in the {{.BasePackage}} application
package definitions
import ({{range .Imports}}
"{{.}}"{{end}}
)
// Services defines the container for all service layer structs
type Services struct {
{{range $serviceName, $service := .Services}} {{$serviceName}} I{{$serviceName}}Service
{{end}}}
{{range $serviceName, $service := .Services}}
// I{{$serviceName}}Service outlines the service methods for the {{$serviceName}} service
type I{{$serviceName}}Service interface {
{{range $service}} {{.}}
{{end}}}
{{end}}
// #genEnd
`
p := path.Join(definitionsDir, "services.go")
f, _ := os.Create(p)
t, _ = t.Parse(tpl)
e = t.Execute(f, data)
if e != nil {
fmt.Println("Execute Error: ", e.Error())
}
f.Close()
if e = lib.FmtGoCode(p); e != nil {
return e
}
return nil
}
// @deprecated
func (g *Gen) GenerateServiceBootstrapFile(servicesDir string) (e error) {
t := template.New("service-bootstrap")
var serviceNames []string
serviceNames, e = getServiceNames(servicesDir)
if e != nil {
return
}
var data = struct {
BasePackage string
Services []string
}{
BasePackage: g.Config.BasePackage,
Services: serviceNames,
}
tpl := `
// Package services provides the service methods objects and functionality used in the {{.BasePackage}} application
package services
import (
"{{.BasePackage}}/definitions/models"
"{{.BasePackage}}/definitions"
"github.com/macinnir/dvc/core/lib/utils"
)
// Bootstrap instantiates a new Services instance and all of its members
func Bootstrap(config *models.Config, repos *definitions.Repos, store utils.IStore) *definitions.Services {
services := &definitions.Services{}
{{range .Services}}services.{{.}} = &{{.}}{config, repos, store}
{{end}}
return services
}
// #genEnd
`
p := path.Join(servicesDir, "bootstrap.go")
f, _ := os.Create(p)
t, _ = t.Parse(tpl)
e = t.Execute(f, data)
if e != nil {
fmt.Println("Execute Error: ", e.Error())
}
f.Close()
if e = lib.FmtGoCode(p); e != nil {
return e
}
return nil
}
// GetServiceNames gets a list of services in the services directory
func getServiceNames(dir string) (serviceNames []string, e error) {
serviceNames = []string{}
dirFileNames := []string{}
var dirHandle *os.File
dirHandle, e = os.Open(dir)
if e != nil {
return
}
defer dirHandle.Close()
dirFileNames, e = dirHandle.Readdirnames(-1)
if e != nil {
return
}
for _, fileName := range dirFileNames {
var fileInfo os.FileInfo
p := path.Join(dir, fileName)
// Skip directories
if fileInfo, e = os.Stat(p); e != nil {
fmt.Printf("CODEGEN.Services> SKIP: File Not Found: `%s`\n ", p)
continue
}
if fileInfo.IsDir() {
fmt.Printf("CODEGEN.Services> SKIP: File is directory: `%s`\n", p)
continue
}
if !isGeneratableFile(fileName) {
continue
}
fmt.Printf("CODEGEN.Services> GENERATE: %s\n", fileName[0:len(fileName)-3])
serviceNames = append(serviceNames, fileName[0:len(fileName)-3])
}
return
}
func isGeneratableFile(fileName string) bool {
fileLen := len(fileName)
if fileLen < 4 {
fmt.Printf("CODEGEN.Services> SKIP: FileName too short: `%s`\n", fileName)
return false
}
if fileName[fileLen-3:] != ".go" {
fmt.Printf("CODEGEN.Services> SKIP: Not a go file: `%s`\n", fileName)
return false
}
if fileLen > 8 && fileName[fileLen-8:] == "_test.go" {
fmt.Printf("CODEGEN.Services> SKIP: Test file: `%s`\n", fileName)
return false
}
// https://yourbasic.org/golang/regexp-cheat-sheet/
// https://regex-golang.appspot.com/assets/html/index.html
r, e := regexp.MatchString("^[A-Z]{1}.+", fileName)
if e != nil {
panic(e)
}
// Skip bootstrap file, test files and anything not a go file
if !r {
fmt.Printf("CODEGEN.Services> SKIP: Invalid file format: %s\n", fileName)
return false
}
return true
}