forked from HaxeFoundation/intellij-haxe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.gradle
172 lines (153 loc) · 5.71 KB
/
template.gradle
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
/*
* Copyright 2019 Eric Bishton
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Builds version-specific java source files into the gen/... directories
// from template files (named *.java.NNN) in the source directories.
import java.util.regex.Matcher
import java.util.regex.Pattern
import org.apache.tools.ant.filters.ReplaceTokens
// Entry point. Called from build.gradle.
task generateVersionSpecificSources(group: 'generate') {
doFirst {
Pattern templatePattern = Pattern.compile("(.*\\.java)\\.\\d\\d\\d\$")
//println "Template source files: " + sourceSets.templates.allSource.size()
FileCollection templateFiles = sourceSets.templates.allSource.filter { File f ->
f.name.matches(templatePattern)
}
println "Templates found: " + templateFiles.size()
templateFiles.each { File f ->
println f.name
}
String currentTemplateName = null
ArrayList<String> relatedTemplates = null
templateFiles.sort().each { f ->
String fileName = normalizePath(f.path)
Matcher parsed = templatePattern.matcher(fileName)
if (parsed.matches()) {
String nextTemplateName = parsed.group(1)
if (nextTemplateName != null && currentTemplateName.equals(nextTemplateName)) {
relatedTemplates.add(fileName)
}
else {
if (null != relatedTemplates) {
doGenerateFromTemplate(relatedTemplates)
}
currentTemplateName = nextTemplateName
relatedTemplates = new ArrayList<String>()
relatedTemplates.add(fileName)
}
} else {
println "File '" + f.path + "' didn't match the pattern."
}
}
if (null != relatedTemplates) {
doGenerateFromTemplate(relatedTemplates)
}
}
}
void doGenerateFromTemplate(ArrayList<String> templateFiles) {
String templateFile = selectProperTemplate(templateFiles)
if (null == templateFile) {
throw new GradleException("Error trying to generate from templates -- no appropriate template found for" + templateFiles.first() + ".")
}
Map<String, String> names = splitTemplateName(templateFile)
String pathSep = getSeparator()
String templateFileName = names.get("templateName")
String outputDir = String.join(pathSep, normalizePath("${generatedSrcDir}"), names.get("packagePath"))
String outputFileName = names.get("javaFileName")
Properties properties = findSdkValuesAndProperties(file("${ideaTargetDir}/build.txt"))
copy {
file(outputDir).mkdirs()
from(templateFile) {
filter(ReplaceTokens, tokens: properties)
}
into outputDir
rename(templateFileName, outputFileName)
}
}
Map<String, String> splitTemplateName(String template) {
String PACKAGE_BASE_NAME = "com"
String PACKAGE_ROOT = "/" + PACKAGE_BASE_NAME + "/"
//String fullName = String.join("/","${haxePluginDir}", template)
String fullName = normalizePath(template)
if (!fullName.contains(PACKAGE_ROOT)) {
throw new GradleException("Cannot find /com/ in template file name")
}
boolean foundPackageRoot = false;
ArrayList<String> prefixPathParts = new ArrayList<>()
ArrayList<String> packagePathParts = new ArrayList<>()
String[] pathParts = fullName.split("/")
String fileName = pathParts.last()
pathParts = pathParts.dropRight(1)
pathParts.each{ pathPart ->
if (!foundPackageRoot) {
if (PACKAGE_BASE_NAME.equals(pathPart)) {
foundPackageRoot = true
packagePathParts.add(pathPart)
}
else {
prefixPathParts.add(pathPart)
}
}
else {
packagePathParts.add(pathPart)
}
}
String pathSep = getSeparator()
HashMap<String, String> output = new HashMap<String, String>()
output.put("templatePrefix", String.join(pathSep, prefixPathParts))
output.put("packagePath", String.join(pathSep, packagePathParts))
output.put("templateName", fileName)
output.put("javaFileName", String.join(".", fileName.split("\\.").dropRight(1)))
return output
}
String getSeparator() {
//return System.getProperty("file.separator")
return "/"
}
String normalizePath(String path) {
// Replace backslash with forward slash.
String s = path.replaceAll("\\\\", "/")
return s.replaceAll("//", "/")
}
String selectProperTemplate(List<String> templateFiles) {
// TODO: Figure out a way to get the properties ONCE (e.g. not in this method) and re-use them.
Properties properties = findSdkValuesAndProperties(file("${ideaTargetDir}/build.txt"))
int codeline = Integer.parseInt(properties.getProperty("idea.sdk.codeline"))
String templateToUse = null
int currentValue = 0
templateFiles.sort().each{ template ->
int templateVersion = extractTemplateVersionId(template)
currentValue = isHigherButLessThan(templateVersion, currentValue, codeline)
if (currentValue == templateVersion) {
templateToUse = template
}
}
return templateToUse
}
int extractTemplateVersionId(String fileName) {
String name = lastPart("/", fileName)
String version = lastPart("\\.", name)
return Integer.parseInt(version)
}
String lastPart(String regexSep, String toSplit) {
return toSplit.split(regexSep).last()
}
int isHigherButLessThan(int value, int current, int max) {
if (value > max) return current
if (value == max) return max
if (value > current) return value
return current
}