-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle.kts
421 lines (374 loc) · 13.5 KB
/
build.gradle.kts
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import com.diffplug.spotless.changelog.Changelog
import com.diffplug.spotless.changelog.ChangelogAndNext
import com.diffplug.spotless.changelog.NextVersionCfg
import com.diffplug.spotless.changelog.NextVersionFunction.NonSemver
import net.minecraftforge.gradle.common.util.RunConfig
import net.minecraftforge.gradle.userdev.DependencyManagementExtension
import net.minecraftforge.gradle.userdev.UserDevExtension
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import java.io.FilterReader
import java.io.PipedReader
import java.io.PipedWriter
import java.io.Reader
import java.io.Writer
import java.time.LocalDateTime
buildscript {
repositories {
jcenter()
mavenCentral()
maven(url = "http://files.minecraftforge.net/maven")
}
dependencies {
classpath(kotlin("gradle-plugin", version = "1.3.41"))
classpath("net.minecraftforge.gradle:ForgeGradle:3.+")
classpath("org.yaml:snakeyaml:1.25")
classpath("com.diffplug.spotless-changelog:spotless-changelog-lib:1.1.0")
}
}
plugins {
kotlin("jvm") version "1.3.41"
`maven-publish`
id("com.github.breadmoirai.github-release") version "2.2.11"
id("com.matthewprenger.cursegradle") version "1.4.0"
}
apply(plugin = "net.minecraftforge.gradle")
// Get a value from either the environment or gradle properties.
fun getProp(key: String): String? {
val value = System.getenv(key) ?: project.findProperty(key) as? String
value ?: println("Cannot find $key in environment or gradle properties.")
return value
}
// Pattern that can break the version number + tag info up into segments.
// Example: 1.14.4-1.0.2.3-alpha1-24-fdf3d3
// mc: 1.14.4
// mod: 1.0.2.3-alpha1-24-fdf3d3
// sem: 1.0.2.3
// pre: -alpha1
// snap: -24-fdf3d3
val VERSION_PATTERN =
"(?<mc>\\d+\\.\\d+\\.\\d+)-(?<mod>(?<sem>\\d+\\.\\d+\\.\\d+\\.\\d+)(?<pre>-[a-z][^-]*)?(?<snap>-[0-9].*)?)"
// Read the changelog. Used for release notes or to determine the snapshot version, depending on which is needed.
val nextVersionFunction = object : NonSemver() {
private val ifFoundBumpMod = listOf("**BREAKING**")
private val ifFoundBumpApi = listOf("**BREAKING API**")
private val ifFoundBumpMinor = listOf("### Added")
private fun isFound(unreleasedChanges: String, lookFor: List<String>) =
lookFor.any { unreleasedChanges.contains(it, false) }
override fun nextVersion(unreleasedChanges: String, lastVersion: String?): String? {
val lastMatch = VERSION_PATTERN.toPattern().matcher(lastVersion ?: "0.0.0-0.0.0.0")
if (!lastMatch.matches()) {
throw GradleException("Unable to parse last version ($lastVersion} from changelog")
}
// If the previous version was marked as prerelease, assume the version number was already incremented correctly.
if (lastMatch.group("pre") != null) {
return lastMatch.group("sem")
}
val parts = lastMatch.group("sem").split(".").map(String::toInt).toMutableList()
if (isFound(unreleasedChanges, ifFoundBumpMod)) {
parts[0] += 1
parts[1] = 0
parts[2] = 0
parts[3] = 0
} else if (isFound(unreleasedChanges, ifFoundBumpApi)) {
parts[1] += 1
parts[2] = 0
parts[3] = 0
} else if (isFound(unreleasedChanges, ifFoundBumpMinor)) {
parts[2] += 1
parts[3] = 0
} else {
parts[3] += 1
}
return "${parts[0]}.${parts[1]}.${parts[2]}.${parts[3]}"
}
}
val nextVersionCfg = NextVersionCfg().also { it.function = nextVersionFunction }
val changelogFile = project.file(ChangelogAndNext.DEFAULT_FILE)
val changelogModel = ChangelogAndNext.calculate(changelogFile, nextVersionCfg)
// Grab & parse tag info. This gives both the name of the last tag, as well as an extra marker if the current commit is
// not the one that is tagged, which is used for the snapshot names.
val gitDescribe = System.getenv("GIT_TAG") ?: "git describe --tags".runCommand()!!.trim()
val match = "^v$VERSION_PATTERN$".toPattern().matcher(gitDescribe)!!
if (!match.matches()) {
throw Exception("Unable to parse version from git tag ($gitDescribe).")
}
// Determine the current version based on the changelog and git tag info collected above.
// If we're on a tag, then this tag is the version, simple as that.
// If we're not, bump the version using the changelog, and add the snapshot info collected from git.
val minecraftVersion = match.group("mc")!!
val isSnapshot = match.group("snap") != null
val isPreRelease = match.group("pre") != null
val modVersion = if (isSnapshot) {
"${changelogModel.versions().next().split("-").last()}-SNAPSHOT${match.group("snap")}"
} else {
match.group("mod")!!
}
println("Version $modVersion for minecraft $minecraftVersion (snapshot: $isSnapshot, prerelease: $isPreRelease)")
// Accessor for the current changelog entry. Uses reflection because the library only exposes unreleased changes.
val currentChangelog by lazy {
val versionsRawField = Changelog::class.java.getDeclaredField("versionsRaw")
versionsRawField.isAccessible = true
val versionsRaw = versionsRawField.get(changelogModel.changelog()) as List<Changelog.VersionEntry>
if (isSnapshot) {
versionsRaw.find { it.isUnreleased } ?: throw GradleException("Changelog should contain an [Unreleased] section")
} else {
versionsRaw.find { !it.isUnreleased && it.version().toString() == version }
?: throw GradleException("Changelog should contain an [$version] section")
}
}
version = "$minecraftVersion-$modVersion"
group = "com.maienm"
base.archivesBaseName = "accessibilitymod"
val forgeVersion = "$minecraftVersion-28.1.107"
val forgeMappingsVersion = arrayOf("snapshot", "20190719-1.14.3") // http://export.mcpbot.bspk.rs
repositories {
mavenLocal()
jcenter()
mavenCentral()
flatDir {
dir("mods")
}
}
configure<UserDevExtension> {
mappings(forgeMappingsVersion[0], forgeMappingsVersion[1])
runs {
val sharedConfig = Action<RunConfig> {
workingDirectory(project.file("run"))
properties["forge.logging.markers"] = "SCAN,REGISTRIES,REGISTRYDUMP,COREMOD"
properties["forge.logging.console.level"] = "debug"
properties["com.maienm.accessibilitymod.debug"] = ""
mods {
create(project.name) {
source(java.sourceSets["main"])
}
}
}
create("client", sharedConfig)
create("server", sharedConfig)
create("data") {
sharedConfig.execute(this)
args("--mod", "accessibilitymod", "--all", "--output", file("src/generated/resources/"))
}
}
}
tasks.withType<ProcessResources> {
// Flatten yaml language files and convert them to json.
// { "foo": { "bar": "Hello", "baz": "World" } } becomes { "foo.bar": "Hello", "foo.baz": "World" }
filesMatching("**/lang/*.yml") {
relativePath = relativePath.replaceLastName(relativePath.lastName.replace("\\.yml$".toRegex(), ".json"))
fun transform(reader: Reader, writer: Writer) {
val config: HashMap<String, *> = org.yaml.snakeyaml.Yaml().load(reader)
val flattenedConfig = LinkedHashMap<String, Any>()
walkNestedMaps(config) { keys: List<String>, value: Any ->
flattenedConfig.put(keys.joinToString("."), value)
}
com.google.gson.Gson().toJson(flattenedConfig, writer)
}
filter(mutableMapOf("transform" to ::transform), TransformFilter::class.java)
}
}
tasks.withType<Jar> {
finalizedBy("reobfJar")
manifest {
attributes["Specification-Title"] = project.name
attributes["Specification-Vendor"] = "MaienM"
attributes["Specification-Version"] = "1"
attributes["Implementation-Title"] = project.name
attributes["Implementation-Vendor"] = "MaienM"
attributes["Implementation-Version"] = modVersion
attributes["Implementation-Timestamp"] = LocalDateTime.now()
}
}
task("changelogCheck") {
group = "documentation"
doFirst {
val errors = changelogModel.changelog().errors()
if (!errors.isEmpty()) {
val path = getProject().getRootDir().toPath().relativize(changelogFile.toPath()).toString()
val allErrors = errors
.map { (index, error) -> "$path${if (index != -1) ":$index" else ""}: $error" }
.joinToString("\n")
throw GradleException(allErrors)
}
}
}
tasks["check"].dependsOn("changelogCheck")
publishing {
publications {
create<MavenPublication>(project.name) {
from(components["java"])
groupId = group.toString()
artifactId = "AccessibilityMod"
pom {
withXml {
val root = asNode()
root.appendNode("name", "AccessibilityMod")
root.appendNode("description", "A Minecraft mod attempting to make some things a bit more accessible.")
root.appendNode("url", "https://github.com/MaienM/AccessibilityMod")
}
licenses {
license {
name.set("MIT License")
url.set("http://www.opensource.org/licenses/mit-license.php")
distribution.set("repo")
}
}
developers {
developer {
id.set("maienm")
name.set("Michon van Dooren")
email.set("[email protected]")
}
}
scm {
url.set("https://github.com/MaienM/AccessibilityMod")
connection.set("scm:git:git://github.com/MaienM/AccessibilityMod.git")
developerConnection.set("scm:git:ssh://github.com/MaienM/AccessibilityMod.git")
}
}
}
}
}
task("prepareArtifactsForGithub") {
group = "publishing"
dependsOn("jar")
doFirst {
val dir = temporaryDir
dir.deleteRecursively()
tasks["jar"].outputs.files.files.forEach {
it.copyTo(dir.resolve(it.name), overwrite = false)
}
println("::set-output name=version::$version")
println("::set-output name=artifacts::${dir.relativeTo(rootDir)}")
}
}
task("checkReadyForRelease") {
group = "publishing"
dependsOn("jar")
doFirst {
if (isSnapshot) {
throw GradleException("Current commit is not properly tagged as release")
}
if (currentChangelog.isUnreleased) {
throw GradleException("Latest entry in changelog is still marked as unreleased")
}
if (currentChangelog.version().toString() != version) {
throw GradleException("Version of last entry in changelog (${currentChangelog.version()}) should be $version")
}
}
}
githubRelease {
repo("AccessibilityMod")
owner("MaienM")
setToken(getProp("GITHUB_TOKEN"))
targetCommitish(gitDescribe)
releaseName(version as String)
prerelease(isPreRelease)
body { PatchedString(currentChangelog.changes().trim().toString()) }
releaseAssets(tasks["jar"].outputs.files.files)
overwrite(true)
}
tasks["githubRelease"].dependsOn("checkReadyForRelease")
curseforge {
this.project(closureOf<com.matthewprenger.cursegradle.CurseProject> {
apiKey = getProp("CURSEFORGE_API_KEY")
id = "363598"
changelog = currentChangelog.changes().trim()
changelogType = "markdown"
releaseType = if (isPreRelease) "beta" else "release"
})
}
tasks["curseforge"].dependsOn("checkReadyForRelease")
task("release") {
group = "publishing"
dependsOn("checkReadyForRelease")
dependsOn("githubRelease")
dependsOn("curseforge")
githubRelease { mustRunAfter("checkReadyForRelease") }
curseforge { mustRunAfter("githubRelease") }
}
dependencies {
"minecraft"("net.minecraftforge:forge:$forgeVersion")
implementation("io.opencubes:boxlin:3.0.1")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.41")
implementation("org.jetbrains.kotlin:kotlin-reflect:1.3.41")
// Include any mods in the mods/ directory when running, to easily test with other mods present.
val fg = project.extensions.findByType(DependencyManagementExtension::class.java)!!
fileTree("mods").also { it.include("*.jar") }.forEach {
val match = "^(?<B>(?<A>[^-]*)(?:-[0-9.]*)?)-(?<C>.*)$".toPattern().toRegex().find(it.nameWithoutExtension)
if (match != null) {
val name = "${match.groups.get("A")!!.value}:${match.groups.get("B")!!.value}:${match.groups.get("C")!!.value}"
implementation(fg.deobf(name))
}
}
}
val compileKotlin: KotlinCompile by tasks
compileKotlin.kotlinOptions.jvmTarget = "1.8"
/**
* A FilterReader that allows transforming the entire file at once, instead of line-by-line.
*/
class TransformFilter(val originalReader: Reader) : FilterReader(PipedReader(65536)) {
fun setTransform(transform: (Reader, Writer) -> Unit) {
val writer = PipedWriter()
(this.`in` as PipedReader).connect(writer)
transform(this.originalReader, writer)
writer.close()
}
}
/**
* Walk over all non-map values in a nested map.
*
* Take the following map:
* { "top1": { "sub1": "foo", "sub2": "bar" }, "top2": "baz" }.
* This will invoke the callable 3 times:
* callable(["top1", "sub1"], "foo")
* callable(["top1", "sub2"], "bar")
* callable(["top2"], "baz")
*
* @param map The map to walk over.
* @param callback A callback that will be invoked with a list of keys and a value for each value in the nested maps.
*/
fun walkNestedMaps(
map: Map<String, Any>,
keys: MutableList<String> = ArrayList(),
callback: (List<String>, Any) -> Unit
) {
map.entries.forEach { entry ->
keys.add(keys.size, entry.key)
val entryMap = entry.value as? Map<String, Any>
if (entryMap != null) {
walkNestedMaps(entryMap, keys, callback)
} else {
callback(keys, entry.value)
}
keys.removeAt(keys.size - 1)
}
}
/**
* Run a command from within the project directory root.
*
* From https://stackoverflow.com/a/41495542.
*/
fun String.runCommand(): String? {
try {
val parts = this.split("\\s".toRegex())
val proc = ProcessBuilder(*parts.toTypedArray())
.directory(projectDir)
.redirectOutput(ProcessBuilder.Redirect.PIPE)
.redirectError(ProcessBuilder.Redirect.PIPE)
.start()
proc.waitFor(10, TimeUnit.SECONDS)
return proc.inputStream.bufferedReader().readText()
} catch (e: java.io.IOException) {
e.printStackTrace()
return null
}
}
/**
* GithubPublish uses a method that doesn't exist, which causes problems. This wrapper class provides this method.
*/
class PatchedString(private val string: String) : CharSequence by string {
fun replace(replacements: Map<String, String>) = replacements.entries.fold(string) { str, entry ->
str.replace(Regex.fromLiteral(entry.key), entry.value)
}
}