From fbfbe4261b7943e0f00fa61e710ef7b6ad5d4115 Mon Sep 17 00:00:00 2001 From: JFronny Date: Fri, 7 Oct 2022 18:23:08 +0200 Subject: [PATCH] Move LomPlugin to gradle.kts --- lom/build.gradle.kts | 10 - .../io/gitlab/jfronny/scripts/LomPlugin.kt | 237 ------------------ lom/src/main/kotlin/lom.gradle.kts | 225 +++++++++++++++++ 3 files changed, 225 insertions(+), 247 deletions(-) delete mode 100644 lom/src/main/kotlin/io/gitlab/jfronny/scripts/LomPlugin.kt create mode 100644 lom/src/main/kotlin/lom.gradle.kts diff --git a/lom/build.gradle.kts b/lom/build.gradle.kts index da55e29..da6e199 100644 --- a/lom/build.gradle.kts +++ b/lom/build.gradle.kts @@ -2,16 +2,6 @@ plugins { id("jf.plugin-conventions") } -gradlePlugin { - // Define the plugin - plugins { - create("lom") { - id = "lom" - implementationClass = "io.gitlab.jfronny.scripts.LomPlugin" - } - } -} - dependencies { api(project(":convention")) api("net.fabricmc:fabric-loom:1.0-SNAPSHOT") diff --git a/lom/src/main/kotlin/io/gitlab/jfronny/scripts/LomPlugin.kt b/lom/src/main/kotlin/io/gitlab/jfronny/scripts/LomPlugin.kt deleted file mode 100644 index f4aac4b..0000000 --- a/lom/src/main/kotlin/io/gitlab/jfronny/scripts/LomPlugin.kt +++ /dev/null @@ -1,237 +0,0 @@ -package io.gitlab.jfronny.scripts - -import net.fabricmc.loom.task.PrepareJarRemapTask -import net.fabricmc.loom.task.RemapJarTask -import org.gradle.api.Plugin -import org.gradle.api.Project -import org.gradle.api.component.SoftwareComponentFactory -import org.gradle.api.publish.maven.MavenPublication -import org.gradle.api.publish.tasks.GenerateModuleMetadata -import org.gradle.api.tasks.bundling.AbstractArchiveTask -import org.gradle.api.tasks.bundling.Jar -import org.gradle.api.tasks.compile.JavaCompile -import org.gradle.kotlin.dsl.* -import java.io.File -import javax.inject.Inject - -class LomPlugin @Inject constructor(private val softwareComponentFactory: SoftwareComponentFactory) : Plugin { - override fun apply(target: Project) { - target.applyLom() - } - - private fun Project.applyLom() { - val devlibs = File(project.buildDir, "devlibs") - val args = extensions.create("lom") - - afterEvaluate { - // Needs to be registered before loom to properly set things up - dependencies { - minecraft("com.mojang:minecraft:${args.minecraftVersion.get()}") - if (args.yarnBuild != null) mappings("net.fabricmc:yarn:${args.minecraftVersion.get()}+${args.yarnBuild}:v2") - else loom.officialMojangMappings() - modImplementation("net.fabricmc:fabric-loader:${args.loaderVersion.get()}") - - testmodImplementation(sourceSets.main.output) - testmodImplementation(sourceSets.client.output) - } - } - - // Apply necessary plugins - apply(plugin = "idea") - apply(plugin = "java-library") - apply(plugin = "fabric-loom") - apply(plugin = "maven-publish") - apply(plugin = "com.github.johnrengelman.shadow") - - // Configure loom for stricter dev env - loom { - runtimeOnlyLog4j.set(true) - splitEnvironmentSourceSets() - } - - // Create testmod source set with access to main and client classpaths - sourceSets { - create("testmod") { - compileClasspath += sourceSets.main.compileClasspath - runtimeClasspath += sourceSets.main.runtimeClasspath - - compileClasspath += sourceSets.client.compileClasspath - runtimeClasspath += sourceSets.client.runtimeClasspath - } - } - - val hasTestmod = !sourceSets.testmod.resources.isEmpty - - // Class path groups (required to allow ultra early init) - loom { - mods { - register(name) { - sourceSet(sourceSets.main) - sourceSet(sourceSets.client) - } - if (hasTestmod) { - register("$name-testmod") { - sourceSet(sourceSets.testmod) - } - } - } - - if (hasTestmod) { - runs { - create("testmodClient") { - client() - ideConfigGenerated(rootProject == project) - name("Testmod Client") - source(sourceSets.testmod) - } - create("testmodServer") { - server() - ideConfigGenerated(rootProject == project) - name("Testmod Server") - source(sourceSets.testmod) - } - } - } - } - - // Common repositories for mods - repositories { - maven { - name = "TerraformersMC" - url = uri("https://maven.terraformersmc.com/") - } - maven { - name = "LibJF" - url = uri("https://gitlab.com/api/v4/projects/25805200/packages/maven") - } - maven { - name = "JF Gson" - url = uri("https://gitlab.com/api/v4/projects/35030495/packages/maven") - } - maven { - name = "JF Commons" - url = uri("https://gitlab.com/api/v4/projects/35745143/packages/maven") - } - maven { - name = "JF FLoader" - url = uri("https://gitlab.com/api/v4/projects/36014652/packages/maven") - } - mavenCentral() - mavenLocal() - } - - // Mark normal jars as -dev - tasks.jar.archiveClassifier.set("dev") - - // Used for referencing the unremapped jars of other projects - artifacts.add(configurations.create("dev").name, tasks.jar.archiveFile.get().asFile) { - type = "jar" - builtBy(tasks.jar) - } - - // configure the shadow task to not shadow by default and output to builds/devlibs - tasks.shadowJar { - val inputTask: Jar = tasks.findByName("injectCompiledConfig") as Jar? ?: tasks.jar // get injectCompiledConfig task if present (-> LibJF) or use normal jar task - dependsOn(inputTask) - configurations.clear() - from(project.configurations.shadow, inputTask.archiveFile.get()) - archiveClassifier.set("shadow") - destinationDirectory.set(devlibs) - } - - // generate sources jar to publish for better debugging with dependents - java { - withSourcesJar() - } - - // attempt to allow reproducible builds by removing unneeded metadata from jars - tasks.withType() { - isPreserveFileTimestamps = false - isReproducibleFileOrder = false - } - - // generate remapped jar without JiJ'd dependencies for maven publish - val remapMavenJar by tasks.registering(RemapJarTask::class) { - dependsOn(tasks.shadowJar.get()) - inputFile.set(tasks.shadowJar.get().archiveFile.get()) - archiveFileName.set("${archiveBaseName.get()}-${project.versionS}-maven.jar") - addNestedDependencies.set(false) - } - tasks.assemble.dependsOn(remapMavenJar) - - // configure remapJar to use the output of shadow - tasks.remapJar { - dependsOn(tasks.shadowJar.get()) - inputFile.set(tasks.shadowJar.get().archiveFile.get()) - archiveFileName.set("${archiveBaseName.get()}-${project.versionS}.jar") - } - - // fill in mod version - tasks.processResources { - filesMatching("fabric.mod.json") { - expand(mapOf("version" to project.versionS)) - } - } - - // publish sources jar and remapped jar without JiJ'd deps - publishing { - publications { - create("lom") { - from(components["java"]) - setArtifacts(listOf(remapMavenJar, tasks.sourcesJar)) - } - } - } - tasks.publish.dependsOn(tasks.build) - tasks.deployDebug.dependsOn(tasks.publish) - - // create testmodInclude configuration - val testmodIncludeConfiguration = configurations.create("testmodInclude") - - if (hasTestmod) { - // generate jar from testmod source set - val testmodJar by tasks.registering(Jar::class) { - from(sourceSets.testmod.output) - destinationDirectory.set(devlibs) - archiveClassifier.set("testmod") - } - - afterEvaluate { - // remap configuration for outputting usable testmod jar - val remapTestmodJar by tasks.registering(RemapJarTask::class) { - dependsOn(testmodJar) - inputFile.set(testmodJar.archiveFile.get()) - archiveClassifier.set("testmod") - // add nested jars from testmodInclude - addNestedDependencies.set(true) - nestedJars.setFrom(*testmodIncludeConfiguration.files.toTypedArray()) - } - tasks.assemble.dependsOn(remapTestmodJar) - } - } - - afterEvaluate { - // from fabric-example-mod, enforces modern java - tasks.withType().configureEach { - options.encoding = "UTF-8" - options.release.set(17) - } - - // otherwise we can't easily overwrite the artifacts to publish while keeping dependency metadata - tasks.withType { - enabled = false - } - - // Fix prepareRemapJar - // Finds remapJar tasks and the corresponding prepareRemapJar tasks and ensures the dependencies of remapJar are run before prepareRemapJar - // This ensures the input files exist when the task is run - tasks.configureEach { - if (this is RemapJarTask) { - this.dependsOn.filterIsInstance().forEach { prepareTask -> - prepareTask.dependsOn(*this.dependsOn.filter { it != prepareTask }.toTypedArray()) - } - } - } - } - } -} diff --git a/lom/src/main/kotlin/lom.gradle.kts b/lom/src/main/kotlin/lom.gradle.kts new file mode 100644 index 0000000..9472de4 --- /dev/null +++ b/lom/src/main/kotlin/lom.gradle.kts @@ -0,0 +1,225 @@ +import io.gitlab.jfronny.scripts.* +import io.gitlab.jfronny.scripts.registering +import net.fabricmc.loom.task.PrepareJarRemapTask +import net.fabricmc.loom.task.RemapJarTask +import org.gradle.api.publish.maven.MavenPublication +import org.gradle.api.publish.tasks.GenerateModuleMetadata +import org.gradle.api.tasks.bundling.AbstractArchiveTask +import org.gradle.api.tasks.bundling.Jar +import org.gradle.api.tasks.compile.JavaCompile +import org.gradle.kotlin.dsl.* +import java.io.File + +val devlibs = File(project.buildDir, "devlibs") +val args = extensions.create("lom") + +afterEvaluate { + // Needs to be registered before loom to properly set things up + dependencies { + minecraft("com.mojang:minecraft:${args.minecraftVersion.get()}") + if (args.yarnBuild != null) mappings("net.fabricmc:yarn:${args.minecraftVersion.get()}+${args.yarnBuild}:v2") + else loom.officialMojangMappings() + modImplementation("net.fabricmc:fabric-loader:${args.loaderVersion.get()}") + + testmodImplementation(sourceSets.main.output) + testmodImplementation(sourceSets.client.output) + } +} + +// Apply necessary plugins +apply(plugin = "idea") +apply(plugin = "java-library") +apply(plugin = "fabric-loom") +apply(plugin = "maven-publish") +apply(plugin = "com.github.johnrengelman.shadow") + +// Configure loom for stricter dev env +loom { + runtimeOnlyLog4j.set(true) + splitEnvironmentSourceSets() +} + +// Create testmod source set with access to main and client classpaths +sourceSets { + create("testmod") { + compileClasspath += sourceSets.main.compileClasspath + runtimeClasspath += sourceSets.main.runtimeClasspath + + compileClasspath += sourceSets.client.compileClasspath + runtimeClasspath += sourceSets.client.runtimeClasspath + } +} + +val hasTestmod = !sourceSets.testmod.resources.isEmpty + +// Class path groups (required to allow ultra early init) +loom { + mods { + register(name) { + sourceSet(sourceSets.main) + sourceSet(sourceSets.client) + } + if (hasTestmod) { + register("$name-testmod") { + sourceSet(sourceSets.testmod) + } + } + } + + if (hasTestmod) { + runs { + create("testmodClient") { + client() + ideConfigGenerated(rootProject == project) + name("Testmod Client") + source(sourceSets.testmod) + } + create("testmodServer") { + server() + ideConfigGenerated(rootProject == project) + name("Testmod Server") + source(sourceSets.testmod) + } + } + } +} + +// Common repositories for mods +repositories { + maven { + name = "TerraformersMC" + url = uri("https://maven.terraformersmc.com/") + } + maven { + name = "LibJF" + url = uri("https://gitlab.com/api/v4/projects/25805200/packages/maven") + } + maven { + name = "JF Gson" + url = uri("https://gitlab.com/api/v4/projects/35030495/packages/maven") + } + maven { + name = "JF Commons" + url = uri("https://gitlab.com/api/v4/projects/35745143/packages/maven") + } + maven { + name = "JF FLoader" + url = uri("https://gitlab.com/api/v4/projects/36014652/packages/maven") + } + mavenCentral() + mavenLocal() +} + +// Mark normal jars as -dev +tasks.jar.archiveClassifier.set("dev") + +// Used for referencing the unremapped jars of other projects +artifacts.add(configurations.create("dev").name, tasks.jar.archiveFile.get().asFile) { + type = "jar" + builtBy(tasks.jar) +} + +// configure the shadow task to not shadow by default and output to builds/devlibs +tasks.shadowJar { + val inputTask: Jar = tasks.findByName("injectCompiledConfig") as Jar? ?: tasks.jar // get injectCompiledConfig task if present (-> LibJF) or use normal jar task + dependsOn(inputTask) + configurations.clear() + from(project.configurations.shadow, inputTask.archiveFile.get()) + archiveClassifier.set("shadow") + destinationDirectory.set(devlibs) +} + +// generate sources jar to publish for better debugging with dependents +java { + withSourcesJar() +} + +// attempt to allow reproducible builds by removing unneeded metadata from jars +tasks.withType() { + isPreserveFileTimestamps = false + isReproducibleFileOrder = false +} + +// generate remapped jar without JiJ'd dependencies for maven publish +val remapMavenJar by tasks.registering(RemapJarTask::class) { + dependsOn(tasks.shadowJar.get()) + inputFile.set(tasks.shadowJar.get().archiveFile.get()) + archiveFileName.set("${archiveBaseName.get()}-${project.versionS}-maven.jar") + addNestedDependencies.set(false) +} +tasks.assemble.dependsOn(remapMavenJar) + +// configure remapJar to use the output of shadow +tasks.remapJar { + dependsOn(tasks.shadowJar.get()) + inputFile.set(tasks.shadowJar.get().archiveFile.get()) + archiveFileName.set("${archiveBaseName.get()}-${project.versionS}.jar") +} + +// fill in mod version +tasks.processResources { + filesMatching("fabric.mod.json") { + expand(mapOf("version" to project.versionS)) + } +} + +// publish sources jar and remapped jar without JiJ'd deps +publishing { + publications { + create("lom") { + from(components["java"]) + setArtifacts(listOf(remapMavenJar, tasks.sourcesJar)) + } + } +} +tasks.publish.dependsOn(tasks.build) +tasks.deployDebug.dependsOn(tasks.publish) + +// create testmodInclude configuration +val testmodIncludeConfiguration = configurations.create("testmodInclude") + +if (hasTestmod) { + // generate jar from testmod source set + val testmodJar by tasks.registering(Jar::class) { + from(sourceSets.testmod.output) + destinationDirectory.set(devlibs) + archiveClassifier.set("testmod") + } + + afterEvaluate { + // remap configuration for outputting usable testmod jar + val remapTestmodJar by tasks.registering(RemapJarTask::class) { + dependsOn(testmodJar) + inputFile.set(testmodJar.archiveFile.get()) + archiveClassifier.set("testmod") + // add nested jars from testmodInclude + addNestedDependencies.set(true) + nestedJars.setFrom(*testmodIncludeConfiguration.files.toTypedArray()) + } + tasks.assemble.dependsOn(remapTestmodJar) + } +} + +afterEvaluate { + // from fabric-example-mod, enforces modern java + tasks.withType().configureEach { + options.encoding = "UTF-8" + options.release.set(17) + } + + // otherwise we can't easily overwrite the artifacts to publish while keeping dependency metadata + tasks.withType { + enabled = false + } + + // Fix prepareRemapJar + // Finds remapJar tasks and the corresponding prepareRemapJar tasks and ensures the dependencies of remapJar are run before prepareRemapJar + // This ensures the input files exist when the task is run + tasks.configureEach { + if (this is RemapJarTask) { + this.dependsOn.filterIsInstance().forEach { prepareTask -> + prepareTask.dependsOn(*this.dependsOn.filter { it != prepareTask }.toTypedArray()) + } + } + } +} \ No newline at end of file