-
Notifications
You must be signed in to change notification settings - Fork 3
/
pack.go
314 lines (297 loc) · 7.46 KB
/
pack.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package main
import (
"bytes"
"encoding/json"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
)
func packJSON(path string) {
out := packDir(path)
var arr []string
arr = append(arr)
path = filepath.Dir(path)
files, err := os.ReadDir(path)
if err != nil {
return
}
for _, f := range files {
fname := f.Name()
fpath := filepath.Join(path, fname)
if debug {
fmt.Println(fpath)
}
sf, err := os.Stat(fpath)
if err != nil {
fmt.Println(err)
return
}
if sf.IsDir() {
dir := packDir(fpath)
switch fname {
case dirSnippet:
out.Snippets = append(out.Snippets, dir.Snippets...)
case dirMenu:
out.Menus = append(out.Menus, dir.Menus...)
case dirLang:
out.Languages = append(out.Languages, dir.Languages...)
case dirTable:
out.Tables = append(out.Tables, dir.Tables...)
case dirParam:
out.Parameters = append(out.Parameters, dir.Parameters...)
case typeParam:
out.Parameters = append(out.Parameters, dir.Parameters...)
case dirData:
out.Data = append(out.Data, dir.Data...)
case dirPage:
out.Pages = append(out.Pages, dir.Pages...)
case dirCon:
for _, cont := range dir.Contracts {
err := ParserGrammarFile(cont.FullPath)
if err != nil {
panic(err)
}
}
out.Contracts = append(out.Contracts, dir.Contracts...)
}
}
}
if countEntries(out) > 0 {
readConfig(&out)
if len(out.Contracts) > 0 {
out.Contracts = sortContracts(out.Contracts)
}
var result []byte
if importNew {
data := dataFile{}
data.Name = out.Name
data.Conditions = out.Conditions
data.Data = append(data.Data, out.Snippets...)
data.Data = append(data.Data, out.Menus...)
data.Data = append(data.Data, out.Languages...)
data.Data = append(data.Data, out.Tables...)
data.Data = append(data.Data, out.Parameters...)
data.Data = append(data.Data, out.Pages...)
data.Data = append(data.Data, out.Contracts...)
result, _ = _JSONMarshal(data, true)
} else {
out.cleaning()
result, _ = _JSONMarshal(&out, true)
}
if !strings.HasSuffix(outputName, eJSON) {
outputName += eJSON
}
outFile, err := os.Create(outputName)
if err != nil {
if debug {
fmt.Println(err)
}
return
}
defer outFile.Close()
outFile.WriteString(string(result))
if abs, err := filepath.Abs(path); err == nil {
abspath := filepath.Join(abs, structFileName)
createGraph(abspath)
}
fmt.Println("pack complete!\noutput file:", outputName)
}
}
func packDir(path string) (out exportFile) {
out.Snippets = []importStruct{}
out.Contracts = []importStruct{}
out.Data = []dataStruct{}
out.Languages = []importStruct{}
out.Menus = []importStruct{}
out.Pages = []importStruct{}
out.Parameters = []importStruct{}
out.Tables = []importStruct{}
files, err := os.ReadDir(path)
if err != nil {
return
}
absdir, _ := filepath.Abs(path)
absdirParts := strings.Split(absdir, separator)
fdir := absdirParts[len(absdirParts)-1]
for _, f := range files {
fname := f.Name()
ext := filepath.Ext(fname)
if debug {
fmt.Println(fname)
}
switch ext {
case ePTL:
switch {
case fdir == dirMenu || fdir == typeMenu:
el := encodeStd(path, fname)
el.Type = typeMenu
out.Menus = append(out.Menus, el)
case fdir == dirSnippet || fdir == typeSnippet:
el := encodeStd(path, fname)
el.Type = typeSnippet
out.Snippets = append(out.Snippets, el)
default:
el := encodePage(path, fname)
el.Type = typePage
out.Pages = append(out.Pages, el)
}
case eJSON:
switch {
case fdir == dirParam || fdir == typeParam:
el := encodeStd(path, fname)
el.Type = typeParam
out.Parameters = append(out.Parameters, el)
case fdir == dirLang || fdir == typeLang:
el := encodeLang(path, fname)
el.Type = typeLang
out.Languages = append(out.Languages, el)
case fdir == dirTable || fdir == typeTable:
el := encodeTable(path, fname)
el.Type = typeTable
out.Tables = append(out.Tables, el)
case fdir == dirData:
el := encodeData(path, fname)
out.Data = append(out.Data, el)
}
case eCSV:
switch {
case fdir == dirParam || fdir == typeParam:
el := encodeStd(path, fname)
el.Type = typeParam
out.Parameters = append(out.Parameters, el)
}
case eSIM:
el := encodeStd(path, fname)
el.Type = typeCon
out.Contracts = append(out.Contracts, el)
}
}
return
}
func encodePage(path, fname string) (result importStruct) {
ext := filepath.Ext(fname)
name := fname[:len(fname)-len(ext)]
fpath := filepath.Join(path, fname)
result.Menu = defaultMenu
result.Name = name
result.Value = file2str(fpath)
result.Conditions = defaultCondition
return
}
func encodeData(path, fname string) (result dataStruct) {
ext := filepath.Ext(fname)
name := fname[:len(fname)-len(ext)]
fpath := filepath.Join(path, fname)
result.Table = name
dataFile := file2data(fpath)
result.Columns = dataFile.Columns
result.Data = dataFile.Data
return
}
func encodeTable(path, fname string) (result importStruct) {
ext := filepath.Ext(fname)
name := fname[:len(fname)-len(ext)]
fpath := filepath.Join(path, fname)
result.Name = name
result.Columns = file2str(fpath)
result.Permissions = defaultPermission
return
}
func encodeLang(path, fname string) (result importStruct) {
ext := filepath.Ext(fname)
name := fname[:len(fname)-len(ext)]
fpath := filepath.Join(path, fname)
result.Name = name
result.Trans = file2str(fpath)
result.Conditions = ""
return
}
func encodeStd(path, fname string) (result importStruct) {
ext := filepath.Ext(fname)
name := fname[:len(fname)-len(ext)]
fpath := filepath.Join(path, fname)
result.FullPath = fpath
result.Name = name
result.Value = file2str(fpath)
result.Conditions = defaultCondition
return
}
func file2str(filename string) (str string) {
bs, err := os.ReadFile(filename)
if err != nil {
return
}
str = string(bs)
return
}
func file2data(filename string) (result dataStruct) {
bs, err := os.ReadFile(filename)
if err != nil {
return
}
json.Unmarshal(bs, &result)
return
}
func _JSONMarshal(v interface{}, unescape bool) ([]byte, error) {
b, err := json.MarshalIndent(v, "", " ")
if unescape {
b = bytes.Replace(b, []byte("\\u003c"), []byte("<"), -1)
b = bytes.Replace(b, []byte("\\u003e"), []byte(">"), -1)
b = bytes.Replace(b, []byte("\\u0026"), []byte("&"), -1)
}
return b, err
}
func sortContracts(c []importStruct) []importStruct {
loops := map[string]string{}
lenC := len(c)
notSwapped := true
for n := 0; n < lenC; n++ {
notSwapped = true
for i := lenC - 1; i > 0; i-- {
for j := i - 1; j >= 0; j-- {
if textContainsContract(c[j].Value, c[i].Name) {
if textContainsContract(c[i].Value, c[j].Name) { // detect call contract loop
if _, ok := loops[c[j].Name]; !ok {
loops[c[i].Name] = c[j].Name
}
}
c[i], c[j] = c[j], c[i]
notSwapped = false
}
}
}
if notSwapped {
break
}
}
if len(loops) > 0 {
fmt.Println("loops:")
for key, val := range loops {
fmt.Printf("%v <=> %v\n", key, val)
}
}
return c
}
func textContainsContract(text, name string) bool {
re := regexp.MustCompile(name + "\\s*\\(")
lines := strings.Split(text, "\n")
for _, l := range lines {
line := strings.Trim(l, " ")
if !strings.HasPrefix(line, "//") && re.MatchString(line) {
return true
}
}
return false
}
func countEntries(file exportFile) (count int) {
return len(file.Snippets) +
len(file.Contracts) +
len(file.Data) +
len(file.Languages) +
len(file.Menus) +
len(file.Pages) +
len(file.Parameters) +
len(file.Tables)
}