Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

small buildscript improvements #519

Open
wants to merge 2 commits into
base: 1.20/dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 63 additions & 57 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
*/

import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import dev.architectury.plugin.ArchitectPluginExtension
import dev.ithundxr.silk.ChangelogText
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import me.modmuss50.mpp.ModPublishExtension
import net.fabricmc.loom.api.LoomGradleExtensionAPI
import net.fabricmc.loom.task.RemapJarTask
import org.gradle.configurationcache.extensions.capitalized
import org.objectweb.asm.ClassReader
import org.objectweb.asm.ClassWriter
import org.objectweb.asm.tree.AnnotationNode
Expand All @@ -36,26 +36,31 @@ import java.util.jar.JarOutputStream
import java.util.zip.Deflater

plugins {
java
`maven-publish`
id("architectury-plugin") version "3.4-SNAPSHOT"
id("dev.architectury.loom") version "1.6.+" apply false
id("me.modmuss50.mod-publish-plugin") version "0.3.4" apply false // https://github.com/modmuss50/mod-publish-plugin
id("com.github.johnrengelman.shadow") version "8.1.1" apply false
id("dev.ithundxr.silk") version "0.11.15" // https://github.com/IThundxr/silk
id("net.kyori.blossom") version "2.1.0" apply false // https://github.com/KyoriPowered/blossom
id("org.jetbrains.gradle.plugin.idea-ext") version "1.1.8" // https://github.com/JetBrains/gradle-idea-ext-plugin
id("java")
id("maven-publish")
id("dev.ithundxr.silk")
id("architectury-plugin")
id("org.jetbrains.gradle.plugin.idea-ext")

id("net.kyori.blossom") apply false
id("com.gradleup.shadow") apply false
id("dev.architectury.loom") apply false
id("me.modmuss50.mod-publish-plugin") apply false
}

println("Steam 'n' Rails v${"mod_version"()}")

val isRelease = System.getenv("RELEASE_BUILD")?.toBoolean() ?: false
val buildNumber = System.getenv("GITHUB_RUN_NUMBER")?.toInt()
val removeDevMixinAnyway = System.getenv("REMOVE_DEV_MIXIN_ANYWAY")?.toBoolean() ?: false
val gitHash = "\"${calculateGitHash() + (if (hasUnstaged()) "-modified" else "")}\""

extra["gitHash"] = gitHash

idea {
module {
isDownloadSources = true
}
}

architectury {
minecraft = "minecraft_version"()
}
Expand All @@ -80,6 +85,10 @@ allprojects {

java {
withSourcesJar()

toolchain {
languageVersion.set(JavaLanguageVersion.of(17))
}
}
}

Expand All @@ -89,10 +98,9 @@ subprojects {

setupRepositories()

val capitalizedName = project.name.capitalized()
val capitalizedName = project.name.replaceFirstChar { it.uppercase() }

val loom = project.extensions.getByType<LoomGradleExtensionAPI>()
loom.apply {
loom {
silentMojangMappingsLicense()
runs.configureEach {
vmArg("-XX:+AllowEnhancedClassRedefinition")
Expand Down Expand Up @@ -136,8 +144,7 @@ subprojects {
val mavenToken = System.getenv("MAVEN_TOKEN")
val maven = if (isRelease) "releases" else "snapshots"
if (mavenToken != null && mavenToken.isNotEmpty()) {
maven {
url = uri("https://maven.ithundxr.dev/${maven}")
maven("https://maven.ithundxr.dev/${maven}") {
credentials {
username = "railways-github"
password = mavenToken
Expand All @@ -152,7 +159,7 @@ subprojects {
return@subprojects
}

apply(plugin = "com.github.johnrengelman.shadow")
apply(plugin = "com.gradleup.shadow")
apply(plugin = "me.modmuss50.mod-publish-plugin")

architectury {
Expand All @@ -161,7 +168,7 @@ subprojects {

tasks.named<RemapJarTask>("remapJar") {
from("${rootProject.projectDir}/LICENSE")
val shadowJar = project.tasks.named<ShadowJar>("shadowJar").get()
val shadowJar = project.tasks.getByName<ShadowJar>("shadowJar")
inputFile.set(shadowJar.archiveFile)
injectAccessWidener = true
dependsOn(shadowJar)
Expand All @@ -171,14 +178,12 @@ subprojects {
}
}

val common: Configuration by configurations.creating
val shadowCommon: Configuration by configurations.creating
val development = configurations.maybeCreate("development${capitalizedName}")

configurations {
compileOnly.get().extendsFrom(common)
runtimeOnly.get().extendsFrom(common)
development.extendsFrom(common)
val shadowCommon: Configuration by configurations.creating
val common: Configuration by configurations.creating {
configurations["compileOnly"].extendsFrom(this)
configurations["runtimeOnly"].extendsFrom(this)
development.extendsFrom(this)
}

dependencies {
Expand Down Expand Up @@ -244,6 +249,26 @@ subprojects {
skip()
}
}

publishMods {
file = tasks.getByName<RemapJarTask>("remapJar").archiveFile
version.set(project.version.toString())
changelog = ChangelogText.getChangelogText(rootProject).toString()
type = STABLE
displayName = "Steam 'n' Rails ${"mod_version"()} ${capitalizedName} ${"minecraft_version"()}"

curseforge {
projectId = "curseforge_id"()
accessToken = System.getenv("CURSEFORGE_TOKEN")
minecraftVersions.add("minecraft_version"())
}

modrinth {
projectId = "modrinth_id"()
accessToken = System.getenv("MODRINTH_TOKEN")
minecraftVersions.add("minecraft_version"())
}
}
}

fun transformJar(jar: File) {
Expand Down Expand Up @@ -306,27 +331,6 @@ fun removeIfDevMixin(nodeName: String, visibleAnnotations: List<AnnotationNode>?
return false
}

fun <T> getValueFromAnnotation(annotation: AnnotationNode?, key: String): T? {
var getNextValue = false

if (annotation?.values == null) {
return null
}

// Keys and value are stored in successive pairs, search for the key and if found return the following entry
for (value in annotation.values) {
if (getNextValue) {
@Suppress("UNCHECKED_CAST")
return value as T
}
if (value == key) {
getNextValue = true
}
}

return null
}

tasks.create("railwaysPublish") {
when (val platform = System.getenv("PLATFORM")) {
"both" -> {
Expand Down Expand Up @@ -393,7 +397,11 @@ fun hasUnstaged(): Boolean {
commandLine("git", "status", "--porcelain")
standardOutput = stdout
}
val result = stdout.toString().replace(Regex("M gradlew(\\.bat)?"), "").trimEnd()
val result = stdout.toString()
.replace(Regex("M gradlew(\\.bat)?"), "")
.lineSequence()
.filter { it.isNotBlank() }
.joinToString("\n")
if (result.isNotEmpty())
println("Found stageable results:\n${result}\n")
return result.isNotEmpty()
Expand All @@ -402,10 +410,6 @@ fun hasUnstaged(): Boolean {
}
}

fun Project.architectury(action: Action<ArchitectPluginExtension>) {
action.execute(this.extensions.getByType<ArchitectPluginExtension>())
}

fun RepositoryHandler.exclusiveMaven(url: String, vararg groups: String) {
exclusiveContent {
forRepository { maven(url) }
Expand All @@ -417,8 +421,10 @@ fun RepositoryHandler.exclusiveMaven(url: String, vararg groups: String) {
}
}

operator fun String.invoke(): String {
return rootProject.ext[this] as? String
?: throw IllegalStateException("Property $this is not defined")
}
val Project.loom: LoomGradleExtensionAPI
get() = the()
fun Project.loom(block: Action<in LoomGradleExtensionAPI>) = block.execute(the())
fun Project.publishMods(block: Action<in ModPublishExtension>) = block.execute(the())

operator fun String.invoke(): String = rootProject.ext[this] as? String ?: error("Property $this is not defined")

14 changes: 3 additions & 11 deletions common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,7 @@ loom {
}

architectury {
common {
for(p in rootProject.subprojects) {
if(p != project) {
[email protected](p.name)
}
}
}
common(rootProject.subprojects.map { it.name }.filter { it != "common" })
}

dependencies {
Expand Down Expand Up @@ -67,13 +61,11 @@ sourceSets.main {
exclude(".cache/**")
exclude("assets/create/**")
}

blossom.javaSources {
property("version", "mod_version"())
property("gitCommit", rootProject.extra["gitHash"].toString())
}
}

operator fun String.invoke(): String {
return rootProject.ext[this] as? String
?: throw IllegalStateException("Property $this is not defined")
}
operator fun String.invoke(): String = rootProject.ext[this] as? String ?: error("Property $this is not defined")
23 changes: 2 additions & 21 deletions fabric/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import dev.ithundxr.silk.ChangelogText

architectury.fabric()

loom {
Expand Down Expand Up @@ -106,36 +104,19 @@ dependencies {
}

publishMods {
file = tasks.remapJar.get().archiveFile
version.set(project.version.toString())
changelog = ChangelogText.getChangelogText(rootProject).toString()
type = STABLE
displayName = "Steam 'n' Rails ${"mod_version"()} Fabric ${"minecraft_version"()}"
modLoaders.add("fabric")
modLoaders.add("quilt")
modLoaders.addAll("fabric", "quilt")

curseforge {
projectId = "curseforge_id"()
accessToken = System.getenv("CURSEFORGE_TOKEN")
minecraftVersions.add("minecraft_version"())

requires {
slug = "create-fabric"
}
}

modrinth {
projectId = "modrinth_id"()
accessToken = System.getenv("MODRINTH_TOKEN")
minecraftVersions.add("minecraft_version"())

requires {
slug = "create-fabric"
}
}
}

operator fun String.invoke(): String {
return rootProject.ext[this] as? String
?: throw IllegalStateException("Property $this is not defined")
}
operator fun String.invoke(): String = rootProject.ext[this] as? String ?: error("Property $this is not defined")
23 changes: 2 additions & 21 deletions forge/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import dev.ithundxr.silk.ChangelogText

architectury.forge()

loom {
Expand Down Expand Up @@ -103,36 +101,19 @@ dependencies {
}

publishMods {
file = tasks.remapJar.get().archiveFile
version.set(project.version.toString())
changelog = ChangelogText.getChangelogText(rootProject).toString()
type = STABLE
displayName = "Steam 'n' Rails ${"mod_version"()} Forge ${"minecraft_version"()}"
modLoaders.add("forge")
modLoaders.add("neoforge")
modLoaders.addAll("forge", "neoforge")

curseforge {
projectId = "curseforge_id"()
accessToken = System.getenv("CURSEFORGE_TOKEN")
minecraftVersions.add("minecraft_version"())

requires {
slug = "create"
}
}

modrinth {
projectId = "modrinth_id"()
accessToken = System.getenv("MODRINTH_TOKEN")
minecraftVersions.add("minecraft_version"())

requires {
slug = "create"
}
}
}

operator fun String.invoke(): String {
return rootProject.ext[this] as? String
?: throw IllegalStateException("Property $this is not defined")
}
operator fun String.invoke(): String = rootProject.ext[this] as? String ?: error("Property $this is not defined")
14 changes: 14 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ maven_group = com.railwayteam.railways

minecraft_version = 1.20.1

# Gradle Plugins
loom_version = 1.6.411
architectury_plugin_version = 3.4.159
# https://github.com/modmuss50/mod-publish-plugin
mod_publish_version = 0.7.3
# https://github.com/gradleup/shadow
shadow_version = 8.3.0
# https://github.com/IThundxr/silk
silk_version = 0.11.16
# https://github.com/KyoriPowered/blossom
blossom_version = 2.1.0
# https://github.com/JetBrains/gradle-idea-ext-plugin
idea_ext_version = 1.1.8

# Mappings
# https://lambdaurora.dev/tools/import_quilt.html
qm_version = 23
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
Loading