-
Notifications
You must be signed in to change notification settings - Fork 8
/
Events.groovy
58 lines (48 loc) · 2.42 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
import java.nio.file.Files
import java.nio.file.Paths
import java.nio.file.attribute.BasicFileAttributes
import java.nio.file.attribute.FileTime
import org.apache.commons.io.FileUtils
Boolean doesTemplateNeedCompiling (File templateDirectory, File compiledTemplateFile) {
Boolean compile = false
if (templateDirectory != null) {
BasicFileAttributes attr = Files.readAttributes(Paths.get(compiledTemplateFile.canonicalPath), BasicFileAttributes.class)
FileTime lastModifiedTime = attr.lastModifiedTime()
def templateFiles = templateDirectory.listFiles()
for (templateFile in templateFiles) {
BasicFileAttributes templateFileAttributes = Files.readAttributes(Paths.get(templateFile.canonicalPath), BasicFileAttributes.class)
if (templateFileAttributes.lastModifiedTime().compareTo(lastModifiedTime) > 0 ) {
return true
}
}
}
return compile
}
void generateTemplateFile(File compiledTemplateFile, File templateDirectory) {
if (!compiledTemplateFile.getParentFile().exists()) {
compiledTemplateFile.getParentFile().mkdirs()
}
FileUtils.writeStringToFile(compiledTemplateFile, '/* Do not edit or commit this file. This file built at compile by _Events.groovy */\n')
def templates = templateDirectory.listFiles()
templates.each { File template ->
def fileParts = template.getName().split("\\.")
if (fileParts.length >= 1) {
def componentName = fileParts[0]
if (template.exists()) {
def content = template.getText().replaceAll("[\n\r]", "")
.replaceAll("\"", "\\\\\"")
.replaceAll('\\s+', ' ')
FileUtils.writeStringToFile(compiledTemplateFile,
"componentService.setTemplate(\"${componentName}\", \"" + content + "\");" as String, true)
}
}
}
}
def buildTemplateFile(String templateDirectoryName, String compiledTemplateFileName) {
File templateDir = new File(templateDirectoryName)
File compiledTemplateFile = new File(compiledTemplateFileName)
if (!compiledTemplateFile?.exists() || doesTemplateNeedCompiling(templateDir, compiledTemplateFile)) {
generateTemplateFile(compiledTemplateFile, templateDir)
}
}
buildTemplateFile("${projectDir}/grails-app/assets/components/template", "${projectDir}/grails-app/assets/components/compile/templates.js")