|
| 1 | +import io.papermc.hangarpublishplugin.model.Platforms |
| 2 | + |
| 3 | +plugins { |
| 4 | + id("com.gradleup.shadow") |
| 5 | + id("com.modrinth.minotaur") |
| 6 | + id("io.papermc.hangar-publish-plugin") |
| 7 | +} |
| 8 | + |
| 9 | +val buildNumber = providers.environmentVariable("GITHUB_RUN_NUMBER").orNull |
| 10 | +val isRelease = providers.environmentVariable("GITHUB_EVENT_NAME").orNull == "release" |
| 11 | + |
| 12 | +if (buildNumber != null && !isRelease) { |
| 13 | + var offset = "0" |
| 14 | + |
| 15 | + try { |
| 16 | + val describeResult = providers.exec { |
| 17 | + commandLine("git", "describe", "--tags", "--long") |
| 18 | + isIgnoreExitValue = true |
| 19 | + } |
| 20 | + val exitCode = describeResult.result.get().exitValue |
| 21 | + |
| 22 | + if (exitCode == 0) { |
| 23 | + val description = describeResult.standardOutput.asText.get().trim() |
| 24 | + val parts = description.split("-") |
| 25 | + |
| 26 | + if (parts.size >= 3) { |
| 27 | + offset = parts[parts.size - 2] |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + catch (exception: Exception) { |
| 32 | + } |
| 33 | + |
| 34 | + if (offset == "0") { |
| 35 | + try { |
| 36 | + val revListResult = providers.exec { |
| 37 | + commandLine("git", "rev-list", "--count", "HEAD") |
| 38 | + isIgnoreExitValue = true |
| 39 | + } |
| 40 | + |
| 41 | + if (revListResult.result.get().exitValue == 0) { |
| 42 | + offset = revListResult.standardOutput.asText.get().trim() |
| 43 | + } |
| 44 | + } |
| 45 | + catch (exception: Exception) { |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + val baseVersion = project.version.toString().replace("-SNAPSHOT", "") |
| 50 | + version = "$baseVersion-SNAPSHOT+$offset" |
| 51 | +} |
| 52 | + |
| 53 | +val changelogText = providers.environmentVariable("CHANGELOG").orElse(providers.exec { |
| 54 | + commandLine("git", "log", "-1", "--format=%B") |
| 55 | + isIgnoreExitValue = true |
| 56 | +}.standardOutput.asText) |
| 57 | + |
| 58 | +logger.lifecycle("Building version: $version") |
| 59 | + |
| 60 | +val paperVersions = (property("paperVersion") as String) |
| 61 | + .split(",") |
| 62 | + .map { it.trim() } |
| 63 | + |
| 64 | +val hangarToken = providers.environmentVariable("HANGAR_API_TOKEN") |
| 65 | + .orElse(providers.gradleProperty("hangarToken")) |
| 66 | + |
| 67 | +val projectVersion = project.version.toString() |
| 68 | +val releaseChannel = if (isRelease) "Release" else "Snapshot" |
| 69 | + |
| 70 | +afterEvaluate { |
| 71 | + val shadowJarTask = tasks.named<com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar>("shadowJar") |
| 72 | + |
| 73 | + modrinth { |
| 74 | + token.set(providers.environmentVariable("MODRINTH_TOKEN")) |
| 75 | + projectId.set(project.findProperty("modrinthProjectId") as String? ?: project.name) |
| 76 | + versionNumber.set(projectVersion) |
| 77 | + versionType.set(if (isRelease) "release" else "beta") |
| 78 | + uploadFile.set(shadowJarTask) |
| 79 | + gameVersions.addAll(paperVersions) |
| 80 | + loaders.addAll(listOf("paper", "folia", "purpur")) |
| 81 | + changelog.set(changelogText) |
| 82 | + |
| 83 | + val readmeFile = rootProject.file("README.md") |
| 84 | + |
| 85 | + if (readmeFile.exists()) { |
| 86 | + syncBodyFrom.set(readmeFile.readText()) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + hangarPublish { |
| 91 | + publications.register("plugin") { |
| 92 | + version.set(projectVersion) |
| 93 | + channel.set(releaseChannel) |
| 94 | + changelog.set(changelogText) |
| 95 | + id.set(project.findProperty("hangarProjectId") as String? ?: project.name) |
| 96 | + apiKey.set(hangarToken) |
| 97 | + |
| 98 | + platforms { |
| 99 | + register(Platforms.PAPER) { |
| 100 | + jar.set(shadowJarTask.flatMap { it.archiveFile }) |
| 101 | + platformVersions.set(paperVersions) |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments