-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy path_Events.groovy
357 lines (300 loc) · 14.8 KB
/
_Events.groovy
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
import org.apache.commons.io.FileUtils
import org.apache.commons.lang.StringUtils
def build(String baseDir) {
final workdir = new File(baseDir, '')
println 'Starting NPM install within '+workdir
final proc = new ProcessBuilder().inheritIO()
//final proc = new ProcessBuilder().inheritIO().directory(workdir)
final exec = proc.command('npm' , '-dd', 'install').start()
def exitValue = exec.waitFor()
if (exitValue) {
println '*****************************************************'
println '* `npm install` failed - do you have NPM installed? *'
println '* Try `brew install npm` or `apt-get install npm` *'
println "* Exit value: $exitValue *"
println '*****************************************************'
} else {
println 'Completed NPM install.'
}
println 'Copying files to grails-app/assets/node_modules'
new ProcessBuilder().inheritIO().command('rm', '-r', 'grails-app/assets/node_modules').start().waitFor()
def files = ['angular/angular.js', 'angular/angular.min.js', 'angular-animate/angular-animate.min.js', 'angular-aria/angular-aria.min.js',
'angular-leaflet-directive/dist/angular-leaflet-directive.min.js', 'angular-route/angular-route.min.js',
'angular-touch/angular-touch.min.js', 'angular-ui-bootstrap/dist/ui-bootstrap-tpls.js',
'angular-ui-bootstrap/dist/ui-bootstrap-csp.css', 'bootbox/dist/bootbox.min.js', 'jquery/dist/jquery.min.js',
'ng-file-upload/dist/ng-file-upload.js', 'ngbootbox/dist/ngBootbox.min.js',
'bootstrap/dist/', 'leaflet/dist/', 'leaflet-draw/dist/', 'leaflet-measure/dist/', 'proj4/dist/proj4.js',
'proj4leaflet/src/proj4leaflet.js', 'lz-string/libs/lz-string.min.js']
files.each { name ->
def dst = new File(baseDir + '/grails-app/assets/node_modules/' + name)
dst.getParentFile().mkdirs()
if (name.endsWith('/')) {
FileUtils.copyDirectory(new File(baseDir + '/node_modules/' + name), dst)
} else {
FileUtils.copyFile(new File(baseDir + '/node_modules/' + name), dst)
}
}
println 'Making a list of all spApp modules for importing'
def dirs = ['controller', 'directive', 'service']
def moduleList = []
dirs.each { name ->
def list = new File(baseDir + '/grails-app/assets/javascripts/spApp/' + name).listFiles()
list.each { file ->
if (name == 'directive') {
//append -directive to module name
moduleList.push(com.google.common.base.CaseFormat.LOWER_CAMEL.to(com.google.common.base.CaseFormat.LOWER_HYPHEN, file.name) + '-directive')
} else {
moduleList.push(com.google.common.base.CaseFormat.LOWER_CAMEL.to(com.google.common.base.CaseFormat.LOWER_HYPHEN, file.name))
}
}
}
def file = new File(baseDir + '/grails-app/assets/compile/spAppModules.js')
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs()
}
FileUtils.writeStringToFile(file,
'/* Do not edit. This file built at compile by _Events.groovy */\n$spAppModules = ["' + moduleList.join('","').replaceAll("\\.js",'') + '"];')
// jsdoc
println 'Starting JSDoc'
final exec2 = proc.command('npm', 'run', 'jsdoc').start()
def exitValue2 = exec2.waitFor()
if (exitValue2) {
println '*****************************************************'
println '* `npm run jsdoc` failed'
println "* Exit value: $exitValue2 *"
println '*****************************************************'
} else {
println 'Completed jsdoc'
}
//i18n'ify templates
// This extracts text content from angular html template elements for the values,
// creates an attribute 'i18n' for the key,
// and stores the key value pairs in grails-app/i18n/messages.properties
println 'Starting i18n build from templates'
String [] textElements = ["h1", "h2", "h3", "h4", "h5", "h6", "label", "span", "div", "input", "p", "button", "td", "option"]
def start;
def idx = 0;
def newProperties = new StringBuilder()
// load existing defaults
def p = new File(baseDir + '/grails-app/i18n/messages.properties')
def prop = new Properties()
prop.load(new FileReader(p))
// reverse lookup
def all = [:]
prop.each {
k, v -> all.put(v, k)
}
println 'existing i18n keys: ' + prop.size()
// get last idx value from templates. looking for attributes i18n="#"
idx = findLastIndex(baseDir + '/grails-app/assets/javascripts/spApp/templates', "i18n=\"", "\"")
// get last idx value from javascript. looking for function '$i18n(#, "default")'
def jsDirs = ['controller', 'directive', 'leaflet', 'service']
for (String jsDir : jsDirs) {
idx = Math.max(idx, findLastIndex(baseDir + '/grails-app/assets/javascripts/spApp/' + jsDir, "\$i18n(", ", \""))
}
// get max prop id
for (def kv : prop) {
try {
idx = Math.max(idx, Integer.parseInt(kv.key))
} catch (e) {}
}
idx++
println 'i18n next available idx: ' + idx
def totalNewProperties = 0
def dir = baseDir + '/grails-app/assets/javascripts/spApp/templates/'
print 'i18n - find new in templates : ' + dir
for (File f : new File(dir).listFiles()) {
try {
def label = ' ' + f.name
print(label)
def newCount = 0
String input = FileUtils.readFileToString(f)
if (!input.contains("skipI18n")) {
def output = new StringBuilder()
def previousPos = 0
start = input.indexOf('<')
while (start < input.length() && start != -1) {
if (start > previousPos) {
output.append(input.substring(previousPos, start))
previousPos = start
}
def readPos = -1
for (int i = 0; i < textElements.length && readPos < 0; i++) {
String e = textElements[i]
// match <tag...> and <tag.../>
if (input.startsWith(e, start + 1)) {
// break out of for loop
readPos = 0
//very rough matching
def end1 = input.indexOf("/>", start)
def end2 = input.indexOf(">", start)
//skip over attr ng-*=".*" and substitution strings {{*}}
def s = input.substring(start, end2)
while (end2 > 0 && (end1 == -1 || end1 > end2) &&
(StringUtils.countMatches(s, "{{") < StringUtils.countMatches(s, "}}") ||
s.matches(".*\\sng-[^\\s=]+=(('[^']*)|(\"[^\"]*))\$"))) {
end2 = input.indexOf(">", end2 + 1)
}
def tagEnd = "</" + e + ">"
if (end1 == -1 || end1 > end2) {
//get innerText of <tag...></tag>
def txtStart = end2 + 1
def txtEnd = input.indexOf(tagEnd, txtStart)
if (txtEnd > 0 && txtEnd < input.length()) {
def innerText = input.substring(txtStart, txtEnd)
// exclude whitespace, anything with angular substitution or tag start/end characters '<' '>'
if (innerText.length() > 0 && !innerText.contains('>') && !innerText.contains('<') && !StringUtils.isWhitespace(innerText) && !innerText.contains("{{")) {
//insert i18n attr if missing
if (!input.substring(start, end2).contains("i18n")) {
readPos = e.length() + start + 1
output.append(input.substring(start, readPos))
def value = input.substring(txtStart, txtEnd).replaceAll("\\s+", ' ').trim()
if (value != " ") {
def currentIdx = idx
if (all.containsKey(value)) {
currentIdx = all.get(value)
} else {
all.put(value, idx)
idx++
newProperties.append("\n${currentIdx}=${value}")
newCount++
totalNewProperties++
}
output.append(" i18n=\"${currentIdx}\" ")
}
output.append(input.substring(readPos, txtEnd + tagEnd.length()))
previousPos = txtEnd + tagEnd.length()
start = previousPos
}
}
}
}
}
}
start = input.indexOf('<', start + 1)
}
if (previousPos < input.length()) {
output.append(input.substring(previousPos))
}
if (newCount > 0) {
// overwrite template file
FileUtils.writeStringToFile(f, output.toString())
print " - $newCount new keys, "
}
}
} catch (err) {
println f.name
err.printStackTrace()
}
}
for (def jsDir : jsDirs) {
print 'i18n - find new in scripts : ' + dir
dir = baseDir + '/grails-app/assets/javascripts/spApp/'
for (File f : new File(baseDir + '/grails-app/assets/javascripts/spApp/' + jsDir).listFiles()) {
def label = ' ' + f.name
print(label)
def newCount = 0
String input = FileUtils.readFileToString(f)
def output = new StringBuilder()
start = 0
while (start < input.length() && start != -1) {
// indexOf next $i18n call that does not have a messages.properties entry
def quote = "\'"
def dblQuote = "\""
def dblNext = input.indexOf("\$i18n(" + dblQuote, start)
def quoteNext = input.indexOf("\$i18n(" + quote, start)
def next = dblNext
def txtWrapper = dblQuote
if (dblNext < 0 || (quoteNext >= 0 && quoteNext < dblNext)) {
next = quoteNext
txtWrapper = quote
}
if (next > start) {
output.append(input.substring(start, next))
// find end of the default value that is wrapped in " or ' and may contain escaped values \" or \'
def end = input.indexOf(txtWrapper + ")", next + 7)
while (input.indexOf("\\" + txtWrapper, end - 1) == end - 1) {
end = input.indexOf(txtWrapper + ")", end + 1)
}
// find or create the properties idx for this value
if (end > next + 6) {
def value = input.substring(next + 7, end)
def currentIdx = idx
if (all.containsKey(value)) {
currentIdx = all.get(value)
} else {
all.put(value, idx)
idx++
newProperties.append("\n${currentIdx}=${value}")
newCount++
totalNewProperties++
}
output.append("\$i18n(${currentIdx}, \"${value}\")")
start = end + 2
} else {
// write the failed match so it is not found on the next loop
output.append(input.substring(next, next + 6))
start = next + 6
}
} else {
// write the remaining text and exit loop
output.append(input.substring(start, input.length()))
start = -1
}
}
if (newCount > 0) {
// overwrite template file
FileUtils.writeStringToFile(f, output.toString())
print " - $newCount new keys, "
}
}
}
// append new properties to messages.properties
if (totalNewProperties == 0) {
println("\ni18n no new keys")
} else {
println("\ni18n appended ${totalNewProperties} new entries to ${p.path}")
}
FileUtils.writeStringToFile(p, newProperties.toString(), true)
println("i18n done")
}
def findLastIndex(directory, startString, endString) {
int last_idx = 0
int startStringLen = startString.length()
for (File f : new File(directory).listFiles()) {
String input = FileUtils.readFileToString(f)
start = 0
while (start < input.length() && start >= 0) {
def next = input.indexOf(startString, start)
if (next > 0) {
def end = input.indexOf(endString, next + startStringLen)
if (end > 0) {
def value = input.substring(next + startStringLen, end)
if (value.isNumber()) {
last_idx = Math.max(last_idx, Integer.parseInt(value))
}
}
start = next + 1
} else {
start = -1
}
}
}
return last_idx
}
def process(doc, nextIdx, update) {
doc.depthFirst().collect {
for (def a : it.getAt(0).children()) {
if (a instanceof String) {
if (it.getAt(0).attributes().containsKey("i18n")) {
nextIdx = Math.max(nextIdx, Integer.parseInt(it.getAt(0).attributes().get("i18n")))
} else if (update) {
it.getAt(0).attributes().put("i18n", String.valueOf(nextIdx))
nextIdx ++;
}
}
}
}
return nextIdx
}
build(System.properties.get("base.dir"))