-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.gradle.kts
365 lines (305 loc) · 10.1 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
import groovy.xml.XmlSlurper
import org.codehaus.groovy.runtime.ResourceGroovyMethods
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.kohsuke.github.GHReleaseBuilder
import org.kohsuke.github.GitHub
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.net.URL
import java.nio.file.Files
import java.util.*
import kotlin.collections.HashMap
buildscript {
repositories {
gradlePluginPortal()
}
dependencies {
classpath("org.kohsuke:github-api:+")
}
}
plugins {
id("fabric-loom") version("+")
id("org.ajoberstar.grgit") version("+")
id("com.modrinth.minotaur") version("+")
`maven-publish`
eclipse
idea
`java-library`
java
kotlin("jvm") version("2.1.0")
}
val minecraftVersion: String by project
val quiltMappings: String by project
val parchmentMappings: String by project
val loaderVersion: String by project
val modId: String by project
val modVersion: String by project
val modLoader: String by project
val mavenGroup: String by project
val baseName: String by project
val fabricKotlinVersion: String by project
base {
archivesName = baseName
}
version = getVersion()
group = mavenGroup
val supportedMcVersions: List<String> = listOf(
"1.21.4", "1.21.3", "1.21.2", "1.21.1", "1.21",
"1.20.6", "1.20.5", "1.20.4", "1.20.2", "1.20.1", "1.20",
"1.19.4", "1.19.3", "1.19.2", "1.19.1", "1.19",
"1.18.2", "1.18.1", "1.18",
"1.17.1", "1.17",
"1.16.5", "1.16.4", "1.16.3", "1.16.2", "1.16.1", "1.16",
"1.15.2", "1.15.1", "1.15",
"1.14.4", "1.14.3", "1.14.2", "1.14.1", "1.14",
)
val includeApi: Configuration by configurations.creating
configurations {
api {
extendsFrom(includeApi)
}
include {
extendsFrom(includeApi)
}
}
repositories {
maven("https://jitpack.io")
maven("https://maven.parchmentmc.org")
maven("https://maven.quiltmc.org/repository/release")
mavenCentral()
}
dependencies {
minecraft("com.mojang:minecraft:$minecraftVersion")
mappings(loom.layered {
// please annoy treetrain if this doesnt work
mappings("org.quiltmc:quilt-mappings:$quiltMappings:intermediary-v2")
//parchment("org.parchmentmc.data:parchment-$parchmentMappings@zip")
officialMojangMappings {
nameSyntheticMembers = false
}
})
modApi("net.fabricmc:fabric-language-kotlin:$fabricKotlinVersion")
// don't shadow the kotlin libraries because it errors on build time for some reason
// instead use this module that isn't connected to fabric loom whatsoever
includeApi("net.frozenblock:kotlinlibraryextensions:1.0") // fulfilled by includeBuild
}
kotlin {
explicitApi()
}
tasks {
processResources {
val properties = HashMap<String, Any>()
properties["modId"] = modId
properties["version"] = version
properties["fabricKotlinVersion"] = ">=$fabricKotlinVersion"
properties.forEach { (a, b) -> inputs.property(a, b) }
filesNotMatching(
listOf(
"**/*.java",
"**/*.kt",
"**/sounds.json",
"**/lang/*.json",
"**/.cache/*",
"**/*.accesswidener",
"**/*.nbt",
"**/*.png",
"**/*.ogg",
"**/*.mixins.json"
)
) {
expand(properties)
}
}
register("javadocJar", Jar::class) {
dependsOn(javadoc)
archiveClassifier = "javadoc"
from(javadoc.get().destinationDir)
}
register("sourcesJar", Jar::class) {
dependsOn(classes)
archiveClassifier = "sources"
from(sourceSets.main.get().allSource)
}
withType(JavaCompile::class) {
options.encoding = "UTF-8"
// Minecraft 1.20.5 (24w14a) upwards uses Java 21.
options.release.set(21)
options.isFork = true
options.isIncremental = true
}
withType(KotlinCompile::class) {
kotlinOptions {
// Minecraft 1.20.5 (24w14a) upwards uses Java 21.
jvmTarget = "21"
}
}
withType(Test::class) {
maxParallelForks = Runtime.getRuntime().availableProcessors().div(2)
}
}
val remapJar: Task by tasks
val sourcesJar: Task by tasks
val javadocJar: Task by tasks
java {
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21
// Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task
// if it is present.
// If you remove this line, sources will not be generated.
withSourcesJar()
withJavadocJar()
}
tasks {
jar {
from("LICENSE") {
rename { "${it}_${base.archivesName}"}
}
}
}
artifacts {
archives(sourcesJar)
archives(javadocJar)
}
fun getVersion(): String {
return modVersion
//return "$modVersion-$modLoader+$minecraftVersion"
}
val env = System.getenv()
publishing {
val mavenUrl = env["MAVEN_URL"]
val mavenUsername = env["MAVEN_USERNAME"]
val mavenPassword = env["MAVEN_PASSWORD"]
val release = mavenUrl?.contains("release")
val snapshot = mavenUrl?.contains("snapshot")
val publishingValid = rootProject == project && !mavenUrl.isNullOrEmpty() && !mavenUsername.isNullOrEmpty() && !mavenPassword.isNullOrEmpty()
val publishVersion = makeModrinthVersion(modVersion)
val snapshotPublishVersion = publishVersion + if (snapshot == true) "-SNAPSHOT" else ""
val publishGroup = rootProject.group.toString().trim(' ')
val hash = if (grgit.branch != null && grgit.branch.current() != null) grgit.branch.current().fullName else ""
publications {
var publish = true
try {
if (publishingValid) {
try {
val xml = ResourceGroovyMethods.getText(URL("$mavenUrl/${publishGroup.replace('.', '/')}/$snapshotPublishVersion/$publishVersion.pom"))
val metadata = XmlSlurper().parseText(xml)
if (metadata.getProperty("hash").equals(hash)) {
publish = false
}
} catch (ignored: FileNotFoundException) {
// No existing version was published, so we can publish
}
} else {
publish = false
}
} catch (e: Exception) {
publish = false
println("Unable to publish to maven. The maven server may be offline.")
}
if (publish) {
create<MavenPublication>("mavenJava") {
from(components["java"])
artifact(javadocJar)
pom {
groupId = publishGroup
artifactId = rootProject.base.archivesName.get().lowercase()
version = publishVersion
withXml {
asNode().appendNode("properties").appendNode("hash", hash)
}
}
}
}
}
repositories {
if (publishingValid) {
maven {
url = uri(mavenUrl!!)
credentials {
username = mavenUsername
password = mavenPassword
}
}
} else {
mavenLocal()
}
}
}
extra {
val properties = Properties()
properties.load(FileInputStream(file("gradle/publishing.properties")))
properties.forEach { (a, b) ->
project.extra[a as String] = b as String
}
}
val modrinth_id: String by extra
val release_type: String by extra
val changelog_file: String by extra
val modrinthVersion = makeModrinthVersion(modVersion)
val displayName = makeName(modVersion)
val changelogText = getChangelog(file(changelog_file))
fun makeName(version: String): String {
return version
//return "$version (${minecraftVersion})"
}
fun makeModrinthVersion(version: String): String {
return version
//return "$version-mc${minecraftVersion}"
}
fun getChangelog(changelogFile: File): String {
val text = Files.readString(changelogFile.toPath())
val split = text.split("-----------------")
if (split.size != 2)
throw IllegalStateException("Malformed changelog")
return split[1].trim()
}
fun getBranch(): String {
val env = System.getenv()
var branch = env["GITHUB_REF"]
if (branch != null && branch != "") {
return branch.substring(branch.lastIndexOf("/") + 1)
}
if (grgit == null) {
return "unknown"
}
branch = grgit.branch.current().name
return branch.substring(branch.lastIndexOf("/") + 1)
}
modrinth {
token.set(System.getenv("MODRINTH_TOKEN"))
projectId.set(modrinth_id)
versionNumber.set(modrinthVersion)
versionName.set(displayName)
versionType.set(release_type)
changelog.set(changelogText)
uploadFile.set(file("build/libs/${tasks.remapJar.get().archiveBaseName.get()}-${version}.jar"))
gameVersions.set(supportedMcVersions)
loaders.set(listOf("fabric", "quilt"))
}
val github by tasks.register("github") {
dependsOn(remapJar)
val env = System.getenv()
val token = env["GITHUB_TOKEN"]
val repoVar = env["GITHUB_REPOSITORY"]
onlyIf {
token != null && token != ""
}
doLast {
val github = GitHub.connectUsingOAuth(token)
val repository = github.getRepository(repoVar)
val releaseBuilder = GHReleaseBuilder(repository, makeModrinthVersion(modVersion))
releaseBuilder.name(makeName(modVersion))
releaseBuilder.body(changelogText)
releaseBuilder.commitish(getBranch())
releaseBuilder.prerelease(release_type != "release")
val ghRelease = releaseBuilder.create()
ghRelease.uploadAsset(tasks.remapJar.get().archiveFile.get().asFile, "application/java-archive")
ghRelease.uploadAsset(tasks.remapSourcesJar.get().archiveFile.get().asFile, "application/java-archive")
ghRelease.uploadAsset(javadocJar.outputs.files.singleFile, "application/java-archive")
}
}
val publishMod by tasks.register("publishMod") {
dependsOn(tasks.publish)
dependsOn(github)
dependsOn(tasks.modrinth)
}