/
main.go
577 lines (432 loc) · 11.1 KB
/
main.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
package main
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"text/template"
"unicode"
"github.com/BurntSushi/toml"
"github.com/codegangsta/cli"
)
var (
Version = "0.0.3"
CommentLine = "//"
CommentBlockStart = "/*"
CommentBlockEnd = "*/"
PrefixConfig = CommentLine + "dico"
PrefixStartGen = CommentLine + "AUTOGENERATE.DICO>>>"
PrefixEndGen = CommentLine + "<<<AUTOGENERATE.DICO"
PrefixStartConfig = CommentLine + "config."
PrefixEndConfig = CommentLine + "config."
HelpMessage = "The text in the section 'AUTOGENERATE.DICO' automatically generated, please do not edit it"
DEBUG = false
)
var HelpfullTemplateFuncs = template.FuncMap{
"regexp": func(s, r string) []string {
// map\\[(?P<key>[a-zA-Z0-9{}]+)\\](?P<item>[a-zA-Z0-9{}]+)
re := regexp.MustCompile(r)
// re.SubexpNames()
return re.FindStringSubmatch(s)
},
"substring": func(s string, ii ...int) string {
if len(ii) == 0 {
return s
}
var start = 0
var end = len(s)
if len(ii) >= 1 {
start = ii[0]
}
if start < 0 || start > len(s) {
start = 0
}
if len(ii) == 2 {
end = ii[1]
}
if end < 0 || end > len(s) {
end = len(s)
}
if start > end && len(ii) == 2 {
start = 0
end = len(s)
}
return s[start:end]
},
"intersection": func(v interface{}, vv ...interface{}) bool {
switch vv[0].(type) {
case func(string, string) bool:
fn := vv[0].(func(string) bool)
// TODO: check type string
for _, _v := range vv {
if fn(_v.(string)) {
return true
}
}
default:
}
for _, _v := range vv {
if _v == v {
return true
}
}
return false
},
"hasPrefix": func(s1, s2 string) bool {
return strings.HasPrefix(s1, s2)
},
"fnHasPrefix": func(prefix string) func(string) bool {
return func(s1 string) bool {
return strings.HasPrefix(s1, prefix)
}
},
"hasSuffix": func(s1, s2 string) bool {
return strings.HasSuffix(s1, s2)
},
"fnHasSuffix": func(suffix string) func(string) bool {
return func(s1 string) bool {
return strings.HasSuffix(s1, suffix)
}
},
"map": func(vv ...interface{}) (res map[interface{}]interface{}) {
res = make(map[interface{}]interface{})
if len(vv) == 0 || len(vv)%2 != 0 {
return
}
for i := 0; i < len(vv); i += 2 {
res[vv[i]] = vv[i+1]
}
return
},
"array": func(vv ...interface{}) []interface{} {
return vv
},
"setter": func(m map[interface{}]interface{}, key, value interface{}) interface{} {
m[key] = value
return value
},
"getter": func(m map[interface{}]interface{}, key interface{}) interface{} {
return m[key]
},
// toUpper transform "str str" to "Str<separator>Str"
"toUpper": func(ss ...string) string {
if len(ss) == 0 {
return ""
}
res := bytes.NewBufferString("")
hasSep := true // first symbol up
replace := ""
if len(ss) == 2 {
replace = ss[1]
}
for index, runeValue := range ss[0] {
if !unicode.IsLetter(runeValue) {
hasSep = true
continue
}
if hasSep {
if index > 0 {
res.WriteString(replace)
}
runeValue = unicode.ToUpper(runeValue)
}
res.WriteRune(runeValue)
hasSep = false
}
return res.String()
},
// toLower transform "str str" to "str<separator>str"
"toLower": func(ss ...string) string {
if len(ss) == 0 {
return ""
}
res := bytes.NewBufferString("")
hasSep := false
replace := ""
if len(ss) == 2 {
replace = ss[1]
}
for index, runeValue := range ss[0] {
if !unicode.IsLetter(runeValue) {
hasSep = true
continue
}
if unicode.IsUpper(runeValue) {
runeValue = unicode.ToLower(runeValue)
hasSep = true
}
if index > 0 && hasSep {
res.WriteString(replace)
}
// IsLower
res.WriteRune(runeValue)
hasSep = false
}
return res.String()
},
"firstLower": func(s string) string {
if len(s) == 0 {
return ""
}
return string(unicode.ToLower([]rune(s)[0]))
},
}
func NewGenerator(args []string, config *Config) (*Generator, error) {
if len(args) == 0 {
return nil, fmt.Errorf("not valid command arguments %v", args)
}
// // TODO: check template errors
var tpl *template.Template
var err error
// TODO: check file
// b, err := ioutil.ReadFile(args[0])
// if err != nil {
// return nil, err
// }
// s := string(b)
// // parse execute file
tpl = template.New(args[0]).Funcs(HelpfullTemplateFuncs)
// tpl, err = template.New(args[0]).Funcs(HelpfullTemplateFuncs).Parse(s)
// if err != nil {
// return nil, err
// }
// parse tempaltes
path := os.Getenv("DICO_TEMPLATES")
if len(path) > 0 {
tpl, err = tpl.ParseGlob(path)
if err != nil {
return nil, err
}
}
pwd, _ := os.Getwd()
for _, tplpath := range args[1:] {
tpl, err = tpl.ParseGlob(pwd + string(os.PathSeparator) + tplpath)
if err != nil {
return nil, err
}
}
tpl = template.Must(tpl, err)
config.TplName = args[0]
if DEBUG {
fmt.Printf("defined tempaltes: %v\n", tpl.DefinedTemplates())
}
return &Generator{config, tpl}, nil
}
type Generator struct {
Config *Config
Tpl *template.Template
}
func (g *Generator) Compile() (string, error) {
var b = bytes.NewBufferString("")
g.Config.Config["ENV"] = g.Config.Env
// if err := g.Tpl.Execute(b, g.Config.Config); err != nil {
// return "", err
// }
if err := g.Tpl.ExecuteTemplate(b, g.Config.TplName, g.Config.Config); err != nil {
return "", err
}
return b.String(), nil
}
type ConfigParser func(raw string, v interface{}) error
var registredConfigTypes = map[string]ConfigParser{
"toml": func(raw string, config interface{}) error {
_, err := toml.Decode(raw, config)
return err
},
}
func NewConfig(typeName string, configRaw string) *Config {
// TODO: Check allowable types
parser, supported := registredConfigTypes[typeName]
return &Config{
Type: typeName,
ConfigRaw: configRaw,
Config: make(map[string]interface{}),
parser: parser,
supportedType: supported,
Env: make(map[interface{}]interface{}), // TODO: values for the application
}
}
type Config struct {
TplName string // output template name
Type string // type config toml or ...
ConfigRaw string
Config map[string]interface{}
Env map[interface{}]interface{}
parser ConfigParser
supportedType bool
}
func (g *Config) IsSupportedType() bool {
return g.supportedType
}
func (g *Config) BuildConfig() error {
if g.IsSupportedType() {
return g.parser(g.ConfigRaw, &g.Config)
}
return fmt.Errorf("not supported type")
}
func (g *Config) String() string {
return "generated text"
}
func removeCommentSymbols(str string) string {
return strings.Replace(str, CommentLine, "", -1)
}
func getTypeConfig(str string) string {
return strings.Replace(str, PrefixStartConfig, "", 1)
}
func isStartDicoConfig(str string) bool {
return strings.HasPrefix(str, PrefixStartConfig)
}
func isEndDicoConfig(str string) bool {
return strings.HasPrefix(str, PrefixEndConfig)
}
func isDicoCommand(str string) bool {
return strings.HasPrefix(str, PrefixConfig)
}
func isStartCodeGenerated(str string) bool {
return strings.HasPrefix(str, PrefixStartGen)
}
func isEndCodeGenerated(str string) bool {
return strings.HasPrefix(str, PrefixEndGen)
}
func analyzeAndGenerate(path string) ([]byte, error) {
buff := bytes.NewBuffer([]byte{})
inFile, err := os.Open(path)
if err != nil {
return buff.Bytes(), err
}
defer inFile.Close()
scanner := bufio.NewScanner(inFile)
scanner.Split(bufio.ScanLines)
lineCounter := 0
for scanner.Scan() {
lineCounter++
NEXT:
if isDicoCommand(scanner.Text()) {
var command = strings.Replace(scanner.Text(), PrefixConfig, "", 1)
var args = strings.Fields(command)
var config = bytes.NewBuffer([]byte{})
fmt.Printf("%s:%d\t%s\n", path, lineCounter, command)
fmt.Fprintln(buff, scanner.Text()) // for reuse
var EmbeddedConfig *Config
// next row
if scanner.Scan() {
// detected embedded config
if isStartDicoConfig(scanner.Text()) {
// first row config
fmt.Fprintln(buff, scanner.Text())
// type config
var typeConfig = getTypeConfig(scanner.Text())
for scanner.Scan() {
fmt.Fprintln(buff, scanner.Text())
if isEndDicoConfig(scanner.Text()) {
break
}
fmt.Fprintln(config, removeCommentSymbols(scanner.Text()))
}
EmbeddedConfig = NewConfig(typeConfig, config.String())
scanner.Scan()
// if !scanner.Scan() {
// return buff.Bytes(), fmt.Errorf("unexpected ending")
// }
}
if isStartCodeGenerated(scanner.Text()) {
for scanner.Scan() {
if isEndCodeGenerated(scanner.Text()) {
// clear the previous generated code
break
}
}
}
}
fmt.Fprintln(buff, PrefixStartGen)
fmt.Fprintln(buff, CommentLine+"\t"+HelpMessage)
fmt.Fprintln(buff, CommentLine+"[DICO.VERSION]:\t", Version)
fmt.Fprintln(buff, CommentLine+"[DICO.COMMAND]:\t", command)
if EmbeddedConfig != nil {
err := EmbeddedConfig.BuildConfig()
// for debug
if DEBUG {
fmt.Fprintln(buff, CommentLine+"[DICO.CONFIG]:\t"+fmt.Sprintf("%+v", EmbeddedConfig.Config))
}
if err != nil && DEBUG {
fmt.Fprintln(buff, CommentLine+"[DICO.ERRORS.COMPLIE_CONFIG]:\t"+err.Error())
}
var g *Generator
g, err = NewGenerator(args, EmbeddedConfig)
if g != nil && err == nil {
gentext, err := g.Compile()
if err == nil {
fmt.Fprintln(buff, gentext)
} else {
fmt.Fprintln(buff, CommentLine+"[DICO.ERRORS.COMPLIE_TEXT]:\t"+err.Error())
}
} else if err != nil {
fmt.Fprintln(buff, CommentLine+"[DICO.ERRORS.COMPLIE_TEXT]:\t"+err.Error())
}
}
fmt.Fprintln(buff, PrefixEndGen)
if !isEndCodeGenerated(scanner.Text()) {
// if the beginning of the next line Config
if isDicoCommand(scanner.Text()) {
goto NEXT
}
fmt.Fprintln(buff, scanner.Text())
}
continue
}
fmt.Fprintln(buff, scanner.Text())
}
return buff.Bytes(), nil
}
func analyzeFile(pattern string) func(fp string, fi os.FileInfo, err error) error {
return func(fp string, fi os.FileInfo, err error) error {
// https://rosettacode.org/wiki/Walk_a_directory/Recursively#Go
if err != nil {
fmt.Println(err) // can't walk here,
return nil // but continue walking elsewhere
}
if fi.IsDir() {
return nil // not a file. ignore.
}
matched, err := filepath.Match(pattern, fi.Name())
if err != nil {
fmt.Println(err) // malformed pattern
return err // this is fatal.
}
if !matched {
return nil
}
if out, err := analyzeAndGenerate(fp); err == nil {
err := ioutil.WriteFile(fp, out, 0644)
if err != nil {
fmt.Println(err)
}
} else {
fmt.Println(err)
}
return nil
}
}
func main() {
app := cli.NewApp()
app.Action = func(c *cli.Context) error {
path := c.Args().Get(0)
return filepath.Walk(path, analyzeFile(c.Args().Get(1)))
}
if DEBUG {
fmt.Printf("%v\n", os.Args)
}
app.Run(os.Args)
}
/*
//dico main templates/golang/*
//AUTOGENERATE.DICO>>>
// The text in the section 'AUTOGENERATE.DICO' automatically generated, please do not edit it
//[DICO.VERSION]: 0.0.1
//[DICO.COMMAND]: main templates/golang/*
//<<<AUTOGENERATE.DICO
*/