-
Notifications
You must be signed in to change notification settings - Fork 2
/
zdoc.gradle
211 lines (179 loc) · 6.42 KB
/
zdoc.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
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
import java.nio.charset.StandardCharsets
// directory containing generated lua library
project.ext.zDocLuaDir = file("$buildDir/generated/sources/zdoc/").absoluteFile
repositories {
// try to find dependencies locally first
mavenLocal()
}
configurations {
zomboidDoc.extendsFrom zomboidRuntimeOnly
}
dependencies {
// https://search.maven.org/artifact/io.github.cocolabs/pz-zdoc
zomboidDoc 'io.github.cocolabs:pz-zdoc:3.+'
// ZomboidDoc compiled Lua library
if (project.ext.has('mod.pzversion')) {
compileOnly files("lib/zdoc-lua-${project.ext.get('mod.pzversion')}" + '.jar')
}
else if (file('mod.info').exists()) {
logger.warn('WARN: Unable to find mod.pzversion property')
}
// Project Zomboid classes
zomboidDoc files(zomboidClassesDir)
}
tasks.register('zomboidLuaJar', ZDocJar.class) {
it.archiveBaseName.set('zdoc-lua')
it.description 'Assembles a jar containing compiled Lua classes.'
it.from zDocLuaDir
it.destinationDir file('lib')
}
tasks.register('zomboidVersion', JavaExec.class) {
it.description 'Read Project Zomboid game version.'
it.group 'zomboid'
it.main = 'io.cocolabs.pz.zdoc.Main'
it.classpath = configurations.zomboidDoc
it.args 'version'
OutputStream oStream = new ByteArrayOutputStream()
it.setStandardOutput(oStream)
it.doLast {
// get command output from stream
def versionText = oStream.toString(StandardCharsets.UTF_8.name()).split('\r\n|\r|\n')
// ZomboidDoc version
logger.lifecycle(versionText[0])
// get version number and classifier (ex. 41.50-IWBUMS)
project.ext.set('mod.pzversion', versionText[1].substring(12).replaceAll(" ", "").trim())
logger.lifecycle("game version ${project.ext.get('mod.pzversion')}")
}
it.dependsOn(tasks.getByName('zomboidClasses'))
it.finalizedBy(tasks.getByName('saveModInfo'))
}
/**
* <p>Returns {@code File} instance of {@code zdoc.version} file.</p>
* If the file does not exist it will be created.
*/
File getZDocVersionFile() {
def file = file('zdoc.version')
if (!file.exists() && !file.createNewFile()) {
throw new IOException("Unable to create zdoc.version file")
}
return file
}
/**
* Compare the given versions and return a comparator resulting value.
*
* @param versionA semantic version to compare against.
* @param versionB semantic version to compare with.
* @return {@code -1} if {@code versionA} is lower then {@code versionB}, {@code 0} if
* versions are equal and {@code 1} if {@code versionA} is higher then {@code versionB}
*/
static int compareSemanticVersions(Integer[] versionA, Integer[] versionB) {
if (versionA.length != 3 || versionB.length != 3) {
throw new GradleException("Not valid semantic versions [${versionA}, ${versionB}]")
}
for (int i = 0; i < 3; i++) {
// first version is higher then second
if (versionA[i] > versionB[i]) {
return 1
}
// second version is higher then first
else if (versionA[i] < versionB[i]) {
return -1
}
}
// semantic versions are equal
return 0
}
/**
* Returns a series of integers representing {@code ZomboidDoc} dependency version.
*/
@SuppressWarnings('UnnecessaryQualifiedReference')
Integer[] getZDocDependencyVersion() {
def pattern = java.util.regex.Pattern.compile('[\\w\\-]+(\\d+)\\.(\\d+)\\.(\\d+)')
def dependency = configurations.zomboidDoc.files.stream()
.filter({ it.name.startsWith('pz-zdoc') }).findFirst()
if (!dependency.present) {
throw new RuntimeException('Unable to find ZomboidDoc dependency in configuration')
}
def filename = dependency.get().name
java.util.regex.Matcher matcher = pattern.matcher(filename)
if (matcher.find()) {
def result = new Integer[3]
for (int i = 0; i < 3; i++) {
result[i] = Integer.valueOf(matcher.group(i + 1))
}
return result
}
else throw new RuntimeException("Malformed zdoc dependency name ${filename}")
}
/**
* Returns a series of integers representing last {@code ZomboidDoc} dependency version.
*
* @param versionFile {@code file} to read the version information from.
* @param currentVersion series of integers representing current {@code ZomboidDoc} version.
* @return last{@code ZomboidDoc} dependency version or {@code null} if no version found.
*/
Integer[] getZDocLastVersion(versionFile, currentVersion) {
def content = versionFile.readLines()
if (!content.empty) {
String[] elements = content.get(0).split('\\.')
if (elements.length == 3) {
Integer[] result = new Integer[elements.length]
for (int i = 0; i < elements.length; i++) {
result[i] = Integer.valueOf(elements[i])
}
return result
}
else logger.warn("WARN: Malformed semantic version found '${elements}'")
}
versionFile.text = currentVersion.join('.')
return null
}
def annotateZomboidLua = tasks.register('annotateZomboidLua', JavaExec.class) {
it.description 'Annotate vanilla Lua with EmmyLua.'
it.group 'zomboid'
it.main = 'io.cocolabs.pz.zdoc.Main'
it.classpath = configurations.zomboidDoc
it.args('annotate', '-i', "${project.ext.gameDir}/media/lua", '-o', "$zDocLuaDir/media/lua")
it.dependsOn(tasks.getByName('zomboidClasses'))
it.doLast {
def versionFile = getZDocVersionFile()
versionFile.text = getZDocDependencyVersion().join('.')
}
}
def compileZomboidLua = tasks.register('compileZomboidLua', JavaExec.class) {
it.description 'Compile Lua library from modding API.'
it.group 'zomboid'
//noinspection GroovyAssignabilityCheck,GroovyAccessibility
it.javaLauncher = javaToolchains.launcherFor {
languageVersion = JavaLanguageVersion.of(8)
}
it.main = 'io.cocolabs.pz.zdoc.Main'
it.classpath = configurations.zomboidDoc
it.args('compile', '-i', "$gameDir", '-o', "$zDocLuaDir/media/lua/shared/Library")
it.dependsOn(tasks.getByName('zomboidClasses'))
it.shouldRunAfter(annotateZomboidLua)
it.doLast {
def versionFile = getZDocVersionFile()
versionFile.text = getZDocDependencyVersion().join('.')
}
}
def updateZomboidLua = tasks.register('updateZomboidLua') {
it.description 'Run ZomboidDoc to update compiled Lua library.'
it.group 'zomboid'
def versionFile = getZDocVersionFile()
def currentVersion = getZDocDependencyVersion()
def lastVersion = getZDocLastVersion(versionFile, currentVersion)
def compareResult = lastVersion != null ? compareSemanticVersions(lastVersion, currentVersion) : -1
it.onlyIf {
compareResult == -1
}
// current version is higher then last version
if (compareResult == -1) {
it.dependsOn(annotateZomboidLua, compileZomboidLua)
}
it.doLast {
// update zdoc.version data
versionFile.text = currentVersion.join('.')
}
}
classes.dependsOn(updateZomboidLua)