forked from GregTechCE/GregTech
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle.kts
517 lines (446 loc) · 17.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
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
import com.google.gson.JsonObject
import com.jfrog.bintray.gradle.BintrayExtension
import com.jfrog.bintray.gradle.BintrayExtension.PackageConfig
import com.jfrog.bintray.gradle.BintrayExtension.VersionConfig
import com.matthewprenger.cursegradle.CurseExtension
import com.matthewprenger.cursegradle.CurseProject
import com.matthewprenger.cursegradle.CurseRelation
import net.minecraftforge.gradle.user.UserBaseExtension
import org.apache.commons.lang.StringUtils
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.ContentType
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.HttpClientBuilder
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.lib.Constants
import org.eclipse.jgit.lib.ObjectId
import org.eclipse.jgit.revwalk.RevCommit
import org.eclipse.jgit.revwalk.RevObject
import org.eclipse.jgit.revwalk.RevTag
import org.eclipse.jgit.revwalk.RevWalk
import java.time.ZoneOffset
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.util.*
buildscript {
repositories {
jcenter()
maven {
name = "forge"
setUrl("http://files.minecraftforge.net/maven")
}
}
dependencies {
classpath("net.minecraftforge.gradle:ForgeGradle:2.3-SNAPSHOT")
classpath("org.eclipse.jgit:org.eclipse.jgit:5.5.0.201909110433-r")
}
}
plugins {
id("com.matthewprenger.cursegradle") version "1.1.0"
id("maven-publish")
id("com.jfrog.bintray") version "1.8.4"
}
apply {
plugin("net.minecraftforge.gradle.forge")
}
val config: Properties = file("build.properties").inputStream().let {
val prop = Properties()
prop.load(it)
return@let prop
}
val mcVersion = config["minecraft.version"] as String
val mcFullVersion = "$mcVersion-${config["forge.version"]}"
val shortVersion = mcVersion.substring(0, mcVersion.lastIndexOf("."))
val strippedVersion = shortVersion.replace(".", "") + "0"
val forestryVersion = config["forestry.version"] as String
val chickenasmVersion = config["chickenasm.version"] as String
val cclVersion = config["ccl.version"] as String
val multipartVersion = config["multipart.version"] as String
val crafttweakerVersion = config["crafttweaker.version"] as String
val jeiVersion = config["jei.version"] as String
val topVersion = config["top.version"] as String
val ctmVersion = config["ctm.version"] as String
val git: Git = Git.open(File("."))
val modVersion = getVersionFromJava(file("src/main/java/gregtech/GregTechVersion.java"))
val modVersionNoBuild = modVersion.substring(0, modVersion.lastIndexOf('.'))
version = "$mcVersion-$modVersion"
group = "gregtech"
configure<BasePluginConvention> {
archivesBaseName = "gregtech"
}
fun minecraft(configure: UserBaseExtension.() -> Unit) = project.configure(configure)
minecraft {
version = mcFullVersion
mappings = "snapshot_20170928"
runDir = "run"
isUseDepAts = true
}
repositories {
maven {
name = "ic2, forestry"
setUrl("http://maven.ic2.player.to/")
}
maven { //JEI
name = "Progwml6 maven"
setUrl("http://dvs1.progwml6.com/files/maven/")
}
maven { //JEI fallback
name = "ModMaven"
setUrl("modmaven.k-4u.nl")
}
maven {
name = "tterrag maven"
setUrl("http://maven.tterrag.com/")
}
maven {
name = "ChickenBones maven"
setUrl("http://chickenbones.net/maven/")
}
maven {
name = "CoFH Maven"
setUrl("http://maven.covers1624.net")
}
maven {
name = "tehnut maven"
setUrl("http://tehnut.info/maven/")
}
maven {
name = "CraftTweaker Maven"
setUrl("https://maven.blamejared.com/")
}
}
dependencies {
"deobfCompile"("net.sengir.forestry:forestry_$mcVersion:$forestryVersion") {
isTransitive = false
}
"deobfCompile"("codechicken:ChickenASM:$shortVersion-$chickenasmVersion")
"deobfCompile"("codechicken:CodeChickenLib:$mcVersion-$cclVersion:deobf")
"deobfCompile"("codechicken:ForgeMultipart:$mcVersion-$multipartVersion:deobf")
"deobfCompile"("CraftTweaker2:CraftTweaker2-MC$strippedVersion-Main:$crafttweakerVersion")
"deobfCompile"("mezz.jei:jei_$mcVersion:$jeiVersion")
"deobfCompile"("mcjty.theoneprobe:TheOneProbe-$shortVersion:$shortVersion-$topVersion")
"deobfCompile"("team.chisel.ctm:CTM:MC$mcVersion-$ctmVersion")
}
configure<JavaPluginConvention> {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
val processResources: ProcessResources by tasks
val sourceSets: SourceSetContainer = the<JavaPluginConvention>().sourceSets
processResources.apply {
inputs.property("version", modVersion)
inputs.property("mcversion", mcFullVersion)
from(sourceSets["main"].resources.srcDirs) {
include("mcmod.info")
expand(mapOf("version" to modVersion,
"mcversion" to mcFullVersion))
}
// copy everything else, thats not the mcmod.info
from(sourceSets["main"].resources.srcDirs) {
exclude("mcmod.info")
}
// access transformer
rename("(.+_at.cfg)", "META-INF/$1")
}
val jar: Jar by tasks
jar.apply {
manifest {
attributes(mapOf("FMLAT" to "gregtech_at.cfg",
"FMLCorePlugin" to "gregtech.common.asm.GTCELoadingPlugin",
"FMLCorePluginContainsFMLMod" to "true"))
}
}
val sourceTask: Jar = tasks.create("source", Jar::class.java) {
from(sourceSets["main"].allSource)
classifier = "sources"
}
val devTask: Jar = tasks.create("dev", Jar::class.java) {
from(sourceSets["main"].output)
classifier = "dev"
}
val energyApiTask: Jar = tasks.create("energyApi", Jar::class.java) {
from(sourceSets["main"].allSource)
from(sourceSets["main"].output)
include("gregtech/api/capability/IElectricItem.*")
include("gregtech/api/capability/IEnergyContainer.*")
include("gregtech/api/capability/GregtechCapabilities.*")
classifier = "energy-api"
}
artifacts {
add("archives", jar)
add("archives", sourceTask)
add("archives", energyApiTask)
}
fun Project.idea(configure: org.gradle.plugins.ide.idea.model.IdeaModel.() -> Unit): Unit =
(this as ExtensionAware).extensions.configure("idea", configure)
idea {
module {
inheritOutputDirs = true
}
}
tasks.create("ciWriteBuildNumber") {
doLast {
val file = file("src/main/java/gregtech/GregTechVersion.java")
val bn = getBuildNumber()
val ln = "\n" //Linux line endings because we"re on git!
var outfile = ""
println("Build number: $bn")
file.forEachLine { s ->
var out = s
if (out.matches(Regex("^ {4}public static final int BUILD = [\\d]+;\$"))) {
out = " public static final int BUILD = $bn;"
}
outfile += (out + ln)
}
file.writeText(outfile)
}
}
tasks.create("generateChangelog") {
doLast {
val file = file("CHANGELOG.md")
val fileContents = StringBuilder(file.readText(Charsets.UTF_8))
val versionHeader = "\n### $modVersionNoBuild\n"
if (fileContents.contains(versionHeader)) return@doLast
val firstNewline = fileContents.indexOf('\n')
val changelog = getActualChangeList()
val insertText = "\n$versionHeader$changelog"
fileContents.insert(firstNewline, insertText)
file.writeText(fileContents.toString(), Charsets.UTF_8)
}
}
val curseforgeProject = configureCurseforgeTask()
//just because curseforge task is retarded
curseforgeProject?.uploadTask?.outputs?.upToDateWhen { false }
fun resolveGitLabDownloadLink(): String? {
val gitlabJobId: String = System.getenv("CI_JOB_ID") ?: return null
return "https://gitlab.com/GregTechCE/GregTech/-/jobs/$gitlabJobId/artifacts/raw/build/libs/${jar.archiveName}"
}
fun resolveCurseforgeArtifactId(): String? {
if (curseforgeProject == null) {
return null
}
val artifact = curseforgeProject.additionalArtifacts.first()
return artifact.parentFileID?.toString()
}
fun resolveCurseForgeDownloadLink(): String? {
val cfArtifactId = resolveCurseforgeArtifactId() ?: return null
return "https://www.curseforge.com/minecraft/mc-mods/gregtechce/files/$cfArtifactId"
}
fun resolveVersionChangelog(): String {
val changeLogLines = file("CHANGELOG.md").readLines(Charsets.UTF_8)
val versionHeader = "### $modVersionNoBuild"
val startLineIndex = changeLogLines.indexOf(versionHeader)
if (startLineIndex == -1) {
return "No changelog provided"
}
val changelogBuilder = StringBuilder()
var lineIndex = startLineIndex
while (lineIndex < changeLogLines.size) {
val changelogLine = changeLogLines[lineIndex]
if (changelogLine.isEmpty()) break
changelogBuilder.append(changelogLine).append("\n")
lineIndex++
}
return changelogBuilder.toString()
}
val notificationTask: Task = tasks.create("postDiscordNotification") {
doLast {
val webhookId = System.getenv("DISCORD_WEBHOOK_ID") ?: error("Discord webhook id not set")
val webhookToken = System.getenv("DISCORD_WEBHOOK_TOKEN") ?: error("Discord webhook token not set")
val curseForgeDownloadLink = resolveCurseForgeDownloadLink()
val gitLabDownloadLink = resolveGitLabDownloadLink()
val message = StringBuilder("New GTCE version $modVersionNoBuild is out!\n")
if (gitLabDownloadLink != null) {
message.append("GitLab download:\n")
message.append(gitLabDownloadLink)
message.append("\n")
}
if (curseForgeDownloadLink != null) {
message.append("CurseForge download (may take a while to be approved):\n")
message.append(curseForgeDownloadLink)
message.append("\n")
}
message.append("```${resolveVersionChangelog()}```")
val jsonObject = JsonObject()
jsonObject.addProperty("content", message.toString())
val httpClient = HttpClientBuilder.create().build()
val webhookUrl = "https://discordapp.com/api/webhooks/$webhookId/$webhookToken"
val response = httpClient.execute(HttpPost(webhookUrl).also {
it.entity = StringEntity(jsonObject.toString(), ContentType.APPLICATION_JSON)
})
if (response.statusLine.statusCode !in 200..210) {
val status = response.statusLine
throw IllegalStateException("Failed to send discord notification - ${status.statusCode} - ${status.reasonPhrase}")
}
}
}
if (curseforgeProject != null) {
notificationTask.dependsOn("curseforge")
}
tasks["build"].dependsOn("generateChangelog")
tasks["ciWriteBuildNumber"].dependsOn("generateChangelog")
notificationTask.dependsOn("generateChangelog")
fun getPrettyCommitDescription(commit: RevCommit): String {
val closePattern = Regex("(Closes|Fixes) #[0-9]*\\.?")
val author = commit.authorIdent.name
val message = commit.fullMessage
//need to remove messages that close issues from changelog
.replace(closePattern, "")
//cut multiple newlines, format them all to linux line endings
.replace(Regex("(\r?\n){1,}"), "\n")
//cut squashed commit sub-commits descriptions
.replace(Regex("\\* [^\\n]*\\n"), "")
//split commit message on lines, trim each one
.split('\n').asSequence().map { it.trim() }
.filterNot { it.isBlank() }
//cut out lines that are related to merges
.filter { !it.startsWith("Merge remote-tracking branch") }
//cut lines that carry too little information
.filter { it.length > 3 }
//captialize each line, add . at the end if it's not there
.map { StringUtils.capitalize(it) }
.map { if (it.endsWith('.')) it.substring(0, it.length - 1) else it }
//append author to each line
.map { "* $it - $author" }.toList()
return message.joinToString( separator = "\n")
}
fun getCommitFromTag(revWalk: RevWalk, tagObject: RevObject) : RevCommit? {
return when (tagObject) {
is RevCommit -> tagObject
is RevTag -> getCommitFromTag(revWalk, revWalk.parseAny(tagObject.`object`))
else -> error("Encountered version tag pointing to a non-commit object: $tagObject")
}
}
fun getActualTagName(tagName: String): String {
val latestSlash = tagName.lastIndexOf('/')
return tagName.substring(latestSlash + 1)
}
fun getActualChangeList(): String {
val revWalk = RevWalk(git.repository)
val latestTagCommit = git.tagList().call()
.filter { getActualTagName(it.name).startsWith("v") }
.mapNotNull { getCommitFromTag(revWalk, revWalk.parseAny(it.objectId)) }
.maxBy { it.commitTime } ?: error("No previous release version tag found")
val gitLog = git.log()
val headCommitId = git.repository.resolve(Constants.HEAD)
gitLog.addRange(latestTagCommit, headCommitId)
val commitsBetween = gitLog.call().map { getPrettyCommitDescription(it) }.filterNot { it.isBlank() }
return commitsBetween.joinToString(separator = "\n")
}
fun getBuildNumber(): String {
val gitLog = git.log()
val headCommitId = git.repository.resolve(Constants.HEAD)
val startCommitId = ObjectId.fromString("c795901d796fba8ce8d3cb87d0172c59f56f3c9b")
gitLog.addRange(startCommitId, headCommitId)
return gitLog.call().toList().size.toString()
}
fun getVersionFromJava(file: File): String {
var major = "0"
var minor = "0"
var revision = "0"
val prefix = "public static final int"
file.forEachLine { line ->
var s = line.trim()
if (s.startsWith(prefix)) {
s = s.substring(prefix.length, s.length - 1)
s = s.replace("=", " ").replace(" +", " ").trim()
val pts = s.split(" ")
when {
pts[0] == "MAJOR" -> major = pts[pts.size - 1]
pts[0] == "MINOR" -> minor = pts[pts.size - 1]
pts[0] == "REVISION" -> revision = pts[pts.size - 1]
}
}
}
val branchNameOrTag = System.getenv("CI_COMMIT_REF_NAME")
if (branchNameOrTag != null && !branchNameOrTag.startsWith("v") && branchNameOrTag != "master") {
return "$major.$minor.$revision-$branchNameOrTag"
}
val build = getBuildNumber()
return "$major.$minor.$revision.$build"
}
fun CurseExtension.project(config: CurseProject.() -> Unit) = CurseProject().also {
it.config()
curseProjects.add(it)
}
// has to be called after addArtifact ¯\_(ツ)_/¯
fun CurseProject.relations(config: CurseRelation.() -> Unit) = CurseRelation().also {
it.config()
additionalArtifacts.forEach { artifact ->
artifact.curseRelations = it
}
mainArtifact.curseRelations = it
}
fun configureCurseforgeTask(): CurseProject? {
if (System.getenv("CURSE_API_KEY") != null) {
val extension = curseforge
extension.apiKey = System.getenv("CURSE_API_KEY")
return extension.project {
apiKey = System.getenv("CURSE_API_KEY")
id = "293327"
changelog = file("CHANGELOG.md")
changelogType = "markdown"
releaseType = "release"
mainArtifact(jar)
addArtifact(sourceTask)
addArtifact(energyApiTask)
relations {
requiredDependency("codechicken-lib-1-8")
optionalDependency("forge-multipart-cbe")
optionalDependency("crafttweaker")
optionalDependency("jei")
optionalDependency("the-one-probe")
optionalDependency("ctm")
}
}
} else {
println("Skipping curseforge task as there is no api key in the environment")
return null
}
}
publishing {
publications {
create("GTCEPublication", MavenPublication::class.java) {
groupId = project.group as String
artifactId = the<BasePluginConvention>().archivesBaseName
version = project.version as String
artifact(jar)
artifact(sourceTask)
artifact(energyApiTask)
}
}
}
fun BintrayExtension.pkg(config: PackageConfig.() -> Unit) = PackageConfig().also {
it.config()
this.pkg = it
}
fun BintrayExtension.version(config: VersionConfig.() -> Unit) {
VersionConfig().also {
it.config()
this.pkg.version = it
}
}
bintray {
val bintrayUser = if (project.hasProperty("bintrayUser")) project.property("bintrayUser") as String else System.getenv("BINTRAY_USER")
val bintrayApiKey = if (project.hasProperty("bintrayApiKey")) project.property("bintrayApiKey") as String else System.getenv("BINTRAY_API_KEY")
if (bintrayUser == null || bintrayApiKey == null) {
println("Skipping bintrayUpload task as there is no api key or user in the environment")
return@bintray
}
user = bintrayUser
key = bintrayApiKey
setPublications("GTCEPublication")
publish = true
override = true //not sure why it is needed
pkg {
repo = "dev"
name = "GregTechCE"
userOrg = "gregtech"
setLicenses("LGPL-3.0")
vcsUrl = "https://github.com/GregTechCE/GregTech.git"
version {
name = project.version as String
released = ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT)
}
}
}