/
command.go
239 lines (201 loc) · 6.99 KB
/
command.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
package generate
import (
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/FINTLabs/fint-model/common/config"
"github.com/FINTLabs/fint-model/common/document"
"github.com/FINTLabs/fint-model/common/github"
"github.com/FINTLabs/fint-model/common/parser"
"github.com/FINTLabs/fint-model/common/types"
"github.com/codegangsta/cli"
)
func CmdGenerate(c *cli.Context) {
var tag string
if c.GlobalString("tag") == config.DEFAULT_TAG {
tag = github.GetLatest(c.GlobalString("owner"), c.GlobalString("repo"))
} else {
tag = c.GlobalString("tag")
}
force := c.GlobalBool("force")
owner := c.GlobalString("owner")
repo := c.GlobalString("repo")
filename := c.GlobalString("filename")
resource := c.Bool("resource")
if c.String("lang") == "JAVA" {
generateJavaCode(owner, repo, tag, filename, force, resource)
}
if c.String("lang") == "CS" {
generateCSCode(owner, repo, tag, filename, force, resource)
}
if c.String("lang") == "ALL" {
generateCSCode(owner, repo, tag, filename, force, resource)
generateJavaCode(owner, repo, tag, filename, force, resource)
}
}
func writeFile(path string, filename string, content []byte) error {
if _, err := os.Stat(path); os.IsNotExist(err) {
err = os.MkdirAll(path, 0777)
if err != nil {
return err
}
}
return ioutil.WriteFile(path+"/"+filename, content, 0777)
}
func writeJavaClass(pkg string, class string, content []byte) error {
path := fmt.Sprintf("%s/%s", config.JAVA_BASE_PATH, strings.Replace(pkg, ".", "/", -1))
return writeFile(removeJavaPackagePathFromFilePath(path), class+".java", []byte(content))
}
func generateJavaCode(owner string, repo string, tag string, filename string, force bool, resource bool) {
document.Get(owner, repo, tag, filename, force)
fmt.Println("Generating Java code:")
setupJavaDirStructure(owner, repo, tag, filename, force)
classes, _, packageClassMap, _ := parser.GetClasses(owner, repo, tag, filename, force)
for _, c := range classes {
if resource {
if c.Resource || len(c.Resources) > 0 || c.Identifiable {
fmt.Printf(" > Creating resource class: %sResource.java\n", c.Name)
class := GetJavaResourceClass(c)
pkg := strings.Replace(c.Package, "model", "model.resource", -1)
err := writeJavaClass(pkg, c.Name+"Resource", []byte(class))
if err != nil {
fmt.Printf("Unable to write file: %s", err)
}
fmt.Printf(" > Creating resources class: %sResources.java\n", c.Name)
class = GetJavaResourcesClass(c)
err = writeJavaClass(pkg, c.Name+"Resources", []byte(class))
if err != nil {
fmt.Printf("Unable to write file: %s", err)
}
}
}
fmt.Printf(" > Creating class: %s.java\n", c.Name)
class := GetJavaClass(c)
err := writeJavaClass(c.Package, c.Name, []byte(class))
if err != nil {
fmt.Printf("Unable to write file: %s", err)
}
}
for p, cl := range packageClassMap {
action := getAction(p, cl, tag)
fmt.Printf(" > Creating action: %s.java\n", action.Name)
actionEnum := GetJavaActionEnum(action)
path := fmt.Sprintf("%s/%s", config.JAVA_BASE_PATH, strings.Replace(p, ".", "/", -1))
err := writeFile(removeJavaPackagePathFromFilePath(path), action.Name+".java", []byte(actionEnum))
if err != nil {
fmt.Printf("Unable to write file: %s", err)
}
}
fmt.Println("Finish generating Java code!")
}
func removeJavaPackagePathFromFilePath(path string) string {
return strings.Replace(path, "no/fint/model/", "", -1)
}
func getAction(p string, cl []*types.Class, tag string) types.Action {
var action types.Action
packageList := strings.Split(p, ".")
pkg := packageList[len(packageList)-1]
action.Name = fmt.Sprintf("%sActions", strings.Title(pkg))
action.Package = p
action.Namespace = p
action.GitTag = tag
for _, c := range cl {
if c.Identifiable && !c.Abstract {
action.Classes = append(action.Classes, strings.ToUpper(c.Name))
}
}
return action
}
func writeCSClass(namespace string, class string, content []byte) error {
return writeFile(getCSPath(namespace), class+".cs", []byte(content))
}
func generateCSCode(owner string, repo string, tag string, filename string, force bool, resource bool) {
document.Get(owner, repo, tag, filename, force)
fmt.Println("Generating CSharp code:")
setupCSDirStructure(owner, repo, tag, filename, force)
classes, _, _, packageClassMap := parser.GetClasses(owner, repo, tag, filename, force)
for _, c := range classes {
if resource {
if c.Resource || len(c.Resources) > 0 {
fmt.Printf(" > Creating resource class: %sResource.cs\n", c.Name)
class := GetCSResourceClass(c)
err := writeCSClass(c.Namespace, c.Name+"Resource", []byte(class))
if err != nil {
fmt.Printf("Unable to write file: %s", err)
}
fmt.Printf(" > Creating resources class: %sResources.cs\n", c.Name)
class = GetCSResourcesClass(c)
err = writeCSClass(c.Namespace, c.Name+"Resources", []byte(class))
if err != nil {
fmt.Printf("Unable to write file: %s", err)
}
}
} else {
fmt.Printf(" > Creating class: %s.cs\n", c.Name)
class := GetCSClass(c)
err := writeCSClass(c.Namespace, c.Name, []byte(class))
if err != nil {
fmt.Printf("Unable to write file: %s", err)
}
}
}
if !resource {
for p, cl := range packageClassMap {
action := getAction(p, cl, tag)
fmt.Printf(" > Creating action: %s.cs\n", action.Name)
actionEnum := GetCSActionEnum(action)
err := writeCSClass(p, action.Name, []byte(actionEnum))
if err != nil {
fmt.Printf("Unable to write file: %s", err)
}
}
}
fmt.Println("Finish generating CSharp code!")
}
func setupCSDirStructure(owner string, repo string, tag string, filename string, force bool) {
fmt.Println(" > Setup directory structure.")
os.RemoveAll("cs")
err := os.MkdirAll(config.CS_BASE_PATH, 0777)
if err != nil {
fmt.Println("Unable to create base structure")
fmt.Println(err)
}
/*
for _, ns := range namespaces.DistinctNamespaceList(owner, repo, tag, filename, force) {
path := getCSPath(ns)
err := os.MkdirAll(path, 0777)
if err != nil {
fmt.Println("Unable to create namespace structure")
fmt.Println(err)
}
}
*/
}
func getCSPath(ns string) string {
nsList := strings.Split(ns, ".")
projectDir := fmt.Sprintf("%s.%s.%s", nsList[0], nsList[1], nsList[2])
subDirs := strings.Replace(ns, projectDir, "", -1)
subDirs = strings.Replace(subDirs, ".", "/", -1)
path := fmt.Sprintf("%s/%s/%s", config.CS_BASE_PATH, projectDir, subDirs)
return path
}
func setupJavaDirStructure(owner string, repo string, tag string, filename string, force bool) {
fmt.Println(" > Setup directory structure.")
os.RemoveAll("java")
err := os.MkdirAll(config.JAVA_BASE_PATH, 0777)
if err != nil {
fmt.Println("Unable to create base structure")
fmt.Println(err)
}
/*
for _, pkg := range packages.DistinctPackageList(owner, repo, tag, filename, force) {
path := fmt.Sprintf("%s/%s", config.JAVA_BASE_PATH, strings.Replace(pkg, ".", "/", -1))
err := os.MkdirAll(removeJavaPackagePathFromFilePath(path), 0777)
if err != nil {
fmt.Println("Unable to create packages structure")
fmt.Println(err)
}
}
*/
}