-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodegenerator.go
More file actions
202 lines (194 loc) · 6.55 KB
/
codegenerator.go
File metadata and controls
202 lines (194 loc) · 6.55 KB
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
package codegenerator
import (
"bufio"
"bytes"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"text/template"
"gopkg.in/yaml.v2"
"maps"
)
// var inlineTemplateRegex = regexp.MustCompile(`inline template: data file:`)
var inlineTemplateRegex = regexp.MustCompile(`\/\/ inline template: data file: "(.*)" *\r?\n((.|[\r\n])*?)\/\/ inline template end *\r?\n`)
var fileTemplateRegex = regexp.MustCompile(`\/\/ file template: data file: "(.*)" template file: "(.*)" *\r?\n`)
var globalDataRegex = regexp.MustCompile(`(\/\/|#) global data file: "(.*)" *\r?\n`)
var inlineDataRegex = regexp.MustCompile(`# inline data for template: template file: "(.*)" *\r?\n`)
func ScanFolder(rootFolder string, extension string) error {
return filepath.WalkDir(rootFolder, func(filename string, d os.DirEntry, err error) error {
base := filepath.Base(filename)
if base == "node_modules" || base == ".git" {
return filepath.SkipDir
}
if strings.HasSuffix(filename, extension) {
target := filename[:len(filename)-len(extension)]
log.Println("Generating ", target)
return SingleFile(filename, target)
}
return nil
})
}
func SingleFile(inFilename string, outFilename string) error {
contentsBytes, err := os.ReadFile(inFilename)
if err != nil {
return err
}
globalData := make(map[any]any)
for {
match := globalDataRegex.FindSubmatchIndex(contentsBytes)
if len(match) == 0 {
break
}
upToCommand := contentsBytes[:match[0]]
afterCommand := contentsBytes[match[1]:]
contentsBytes = append(upToCommand, afterCommand...)
dataFilenameGlobExpression := string(contentsBytes[match[4]:match[5]])
dataFilenames, err := filepath.Glob(dataFilenameGlobExpression)
if err != nil {
log.Printf("While attempting to glob expression %s out of %s at position %d", dataFilenameGlobExpression, inFilename, match[0])
return err
}
for _, dataFilename := range dataFilenames {
log.Println("Reading global data file", dataFilename)
dataContents, err := os.ReadFile(dataFilename)
if err != nil {
return err
}
var data map[any]interface{}
err = yaml.Unmarshal(dataContents, &data)
if err != nil {
return err
}
maps.Copy(globalData, data)
}
}
for {
match := fileTemplateRegex.FindSubmatchIndex(contentsBytes)
if len(match) == 0 {
break
}
dataFilenameGlobExpression := string(contentsBytes[match[2]:match[3]])
dataFilenames, err := filepath.Glob(dataFilenameGlobExpression)
if err != nil {
log.Printf("While attempting to glob expression %s out of %s at position %d", dataFilenameGlobExpression, inFilename, match[0])
return err
}
templateFilename := string(contentsBytes[match[4]:match[5]])
templateContents, err := os.ReadFile(templateFilename)
if err != nil {
log.Printf("While attempting to read template %s out of %s at position %d, template contents %s", templateFilename, inFilename, match[0], templateContents)
return err
}
parsedTemplate, err := template.New(fmt.Sprintf("%s:%d", inFilename, match[0])).Funcs(AuxilirayFunctions()).Parse(string(templateContents))
if err != nil {
log.Printf("While attempting to parse %s at position %d, template contents %s", inFilename, match[0], templateContents)
return err
}
outputBuffer := bytes.Buffer{}
outputWriter := bufio.NewWriter(&outputBuffer)
for _, dataFilename := range dataFilenames {
log.Println("Generating using data file", dataFilename)
dataContents, err := os.ReadFile(dataFilename)
if err != nil {
return err
}
var data map[any]interface{}
err = yaml.Unmarshal(dataContents, &data)
if err != nil {
return err
}
finalData := map[any]interface{}{}
maps.Copy(finalData, globalData)
maps.Copy(finalData, data)
err = parsedTemplate.Execute(outputWriter, finalData)
if err != nil {
return err
}
outputWriter.Flush()
}
upToCommand := contentsBytes[:match[0]]
afterCommand := contentsBytes[match[1]:]
contentsBytes = append(upToCommand, append(outputBuffer.Bytes(), afterCommand...)...)
}
for {
match := inlineTemplateRegex.FindSubmatchIndex(contentsBytes)
if len(match) == 0 {
break
}
dataFilename := string(contentsBytes[match[2]:match[3]])
templateContents := string(contentsBytes[match[4]:match[5]])
parsedTemplate, err := template.New(fmt.Sprintf("%s:%d", inFilename, match[0])).Funcs(AuxilirayFunctions()).Parse(templateContents)
if err != nil {
log.Printf("While attempting to parse %s at position %d, template contents %s", inFilename, match[0], templateContents)
return err
}
dataContents, err := os.ReadFile(dataFilename)
if err != nil {
return err
}
var data map[any]interface{}
err = yaml.Unmarshal(dataContents, &data)
if err != nil {
return err
}
outputBuffer := bytes.Buffer{}
outputWriter := bufio.NewWriter(&outputBuffer)
finalData := map[any]interface{}{}
maps.Copy(finalData, globalData)
maps.Copy(finalData, data)
err = parsedTemplate.Execute(outputWriter, finalData)
if err != nil {
return err
}
outputWriter.Flush()
upToCommand := contentsBytes[:match[0]]
afterCommand := contentsBytes[match[1]:]
contentsBytes = append(upToCommand, append(outputBuffer.Bytes(), afterCommand...)...)
}
match := inlineDataRegex.FindSubmatchIndex(contentsBytes)
if len(match) > 0 {
templateFilename := string(contentsBytes[match[2]:match[3]])
templateContents, err := os.ReadFile(templateFilename)
if err != nil {
log.Printf("While attempting to read template2 %s out of %s at position %d, template contents %s", templateFilename, inFilename, match[0], templateContents)
return err
}
parsedTemplate, err := template.New(fmt.Sprintf("%s:%d", inFilename, match[0])).Funcs(AuxilirayFunctions()).Parse(string(templateContents))
if err != nil {
log.Printf("While attempting to parse %s at position %d, template contents %s", inFilename, match[0], templateContents)
return err
}
var data map[any]interface{}
err = yaml.Unmarshal(contentsBytes, &data)
if err != nil {
return err
}
outputBuffer := bytes.Buffer{}
outputWriter := bufio.NewWriter(&outputBuffer)
finalData := map[any]interface{}{}
maps.Copy(finalData, globalData)
maps.Copy(finalData, data)
err = parsedTemplate.Execute(outputWriter, finalData)
if err != nil {
return err
}
outputWriter.Flush()
contentsBytes = outputBuffer.Bytes()
}
err = os.WriteFile(outFilename, contentsBytes, 0644)
if err != nil {
return err
}
if strings.HasSuffix(outFilename, ".go") {
_, err = exec.Command("gofmt", "-w", outFilename).CombinedOutput()
if err != nil {
log.Println("gofmt failed:", outFilename, " error: ", err)
return err
}
}
return nil
}