From 045b065166805baaf1c5e8a5b733ffd3513fc526 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Sun, 12 Jan 2025 19:03:32 -0800 Subject: [PATCH] I do declare - Move source set bundling/common source set sharing logic into transitiveSourceSets and make it more flexible - Remove commonProject field from platform extension --- .../gradle/platform/PlatformExtension.kt | 61 +------------ .../TransitiveSourceSetConfigurator.kt | 90 ++++++++++++++++++- common/build.gradle.kts | 8 +- fabric/build.gradle.kts | 40 ++++++--- forge/build.gradle.kts | 40 ++++++--- vanillinFabric/build.gradle.kts | 45 +++------- vanillinForge/build.gradle.kts | 45 +++------- 7 files changed, 170 insertions(+), 159 deletions(-) diff --git a/buildSrc/src/main/kotlin/dev/engine_room/gradle/platform/PlatformExtension.kt b/buildSrc/src/main/kotlin/dev/engine_room/gradle/platform/PlatformExtension.kt index 1c03dad14..95ed30384 100644 --- a/buildSrc/src/main/kotlin/dev/engine_room/gradle/platform/PlatformExtension.kt +++ b/buildSrc/src/main/kotlin/dev/engine_room/gradle/platform/PlatformExtension.kt @@ -1,25 +1,18 @@ package dev.engine_room.gradle.platform -import dev.engine_room.gradle.jarset.JarTaskSet import net.fabricmc.loom.api.LoomGradleExtensionAPI import net.fabricmc.loom.task.RemapJarTask import org.gradle.api.Project import org.gradle.api.Task import org.gradle.api.tasks.SourceSet -import org.gradle.api.tasks.SourceSetContainer -import org.gradle.api.tasks.compile.JavaCompile -import org.gradle.api.tasks.javadoc.Javadoc import org.gradle.jvm.tasks.Jar -import org.gradle.kotlin.dsl.* -import org.gradle.language.jvm.tasks.ProcessResources +import org.gradle.kotlin.dsl.assign +import org.gradle.kotlin.dsl.named +import org.gradle.kotlin.dsl.register +import org.gradle.kotlin.dsl.the import java.io.File -import kotlin.properties.Delegates open class PlatformExtension(val project: Project) { - var commonProject: Project by Delegates.notNull() - - val commonSourceSets: SourceSetContainer by lazy { commonProject.the() } - fun setupLoomMod(vararg sourceSets: SourceSet) { project.the().mods.maybeCreate("main").apply { sourceSets.forEach(::sourceSet) @@ -51,52 +44,6 @@ open class PlatformExtension(val project: Project) { } } - fun compileWithCommonSourceSets(vararg sourceSets: SourceSet) { - project.tasks.apply { - withType().configureEach { - JarTaskSet.excludeDuplicatePackageInfos(this) - } - - sourceSets.forEach { - val commonSourceSet = commonSourceSets.named(it.name).get() - - named(it.compileJavaTaskName).configure { - source(commonSourceSet.allJava) - } - named(it.processResourcesTaskName).configure { - from(commonSourceSet.resources) - } - } - } - } - - fun setupFatJar(vararg sourceSets: SourceSet) { - project.tasks.apply { - val extraSourceSets = sourceSets.filter { it.name != "main" }.toList() - val commonSources = sourceSets.map { commonSourceSets.named(it.name).get() } - - named("jar").configure { - extraSourceSets.forEach { from(it.output) } - - JarTaskSet.excludeDuplicatePackageInfos(this) - } - - named("javadoc").configure { - commonSources.forEach { source(it.allJava) } - extraSourceSets.forEach { source(it.allJava) } - - JarTaskSet.excludeDuplicatePackageInfos(this) - } - - named("sourcesJar").configure { - commonSources.forEach { from(it.allJava) } - extraSourceSets.forEach { from(it.allJava) } - - JarTaskSet.excludeDuplicatePackageInfos(this) - } - } - } - fun setupTestMod(sourceSet: SourceSet) { project.tasks.apply { val testModJar = register("testModJar") { diff --git a/buildSrc/src/main/kotlin/dev/engine_room/gradle/transitive/TransitiveSourceSetConfigurator.kt b/buildSrc/src/main/kotlin/dev/engine_room/gradle/transitive/TransitiveSourceSetConfigurator.kt index 7d043ebf1..d197bfccc 100644 --- a/buildSrc/src/main/kotlin/dev/engine_room/gradle/transitive/TransitiveSourceSetConfigurator.kt +++ b/buildSrc/src/main/kotlin/dev/engine_room/gradle/transitive/TransitiveSourceSetConfigurator.kt @@ -1,8 +1,14 @@ package dev.engine_room.gradle.transitive +import dev.engine_room.gradle.jarset.JarTaskSet +import org.gradle.api.Project import org.gradle.api.tasks.SourceSet +import org.gradle.api.tasks.SourceSetContainer import org.gradle.api.tasks.compile.JavaCompile +import org.gradle.api.tasks.javadoc.Javadoc +import org.gradle.jvm.tasks.Jar import org.gradle.kotlin.dsl.named +import org.gradle.kotlin.dsl.the import org.gradle.language.jvm.tasks.ProcessResources class TransitiveSourceSetConfigurator(private val parent: TransitiveSourceSetsExtension, private val sourceSet: SourceSet) { @@ -22,14 +28,21 @@ class TransitiveSourceSetConfigurator(private val parent: TransitiveSourceSetsEx rootRuntime() } - fun compile(vararg sourceSets: SourceSet) { + fun compileClasspath(vararg sourceSets: SourceSet) { compileSourceSets += sourceSets for (sourceSet in sourceSets) { this.sourceSet.compileClasspath += sourceSet.output } } - fun runtime(vararg sourceSets: SourceSet) { + fun compileClasspath(project: Project, vararg sourceSets: String) { + val externalSourceSets = project.the() + for (name in sourceSets) { + this.sourceSet.compileClasspath += externalSourceSets.getByName(name).output + } + } + + fun runtimeClasspath(vararg sourceSets: SourceSet) { runtimeSourceSets += sourceSets for (sourceSet in sourceSets) { this.sourceSet.runtimeClasspath += sourceSet.output @@ -37,8 +50,77 @@ class TransitiveSourceSetConfigurator(private val parent: TransitiveSourceSetsEx } fun implementation(vararg sourceSets: SourceSet) { - compile(*sourceSets) - runtime(*sourceSets) + compileClasspath(*sourceSets) + runtimeClasspath(*sourceSets) + } + + fun from(otherProject: Project) { + from(otherProject, sourceSet.name) + } + + fun from(otherProject: Project, vararg names: String) { + + val otherSourceSets = otherProject.the() + + from(*names.map { otherSourceSets.getByName(it) }.toTypedArray()) + } + + fun from(vararg sourceSets: SourceSet) { + parent.project.tasks.apply { + named(sourceSet.compileJavaTaskName).configure { + sourceSets.forEach { source(it.allJava) } + + JarTaskSet.excludeDuplicatePackageInfos(this) + } + named(sourceSet.processResourcesTaskName).configure { + sourceSets.forEach { from(it.resources) } + } + } + } + + fun bundleFrom(otherProject: Project) { + bundleFrom(otherProject, sourceSet.name) + } + + fun bundleFrom(otherProject: Project, vararg names: String) { + val otherSourceSets = otherProject.the() + + bundleFrom(*names.map { otherSourceSets.getByName(it) }.toTypedArray()) + } + + fun bundleFrom(vararg sourceSets: SourceSet) { + from(*sourceSets) + // The external sourceSets will be included in the jar by default since we bring it into the java compile task, + // however we need to make sure that the javadoc and sources jars also include the external sourceSets + bundleJavadocAndSources(*sourceSets) + } + + fun bundleOutput(vararg sourceSets: SourceSet) { + bundleJavadocAndSources(*sourceSets) + + parent.project.tasks.apply { + named(sourceSet.jarTaskName).configure { + sourceSets.forEach { from(it.output) } + + JarTaskSet.excludeDuplicatePackageInfos(this) + } + } + } + + private fun bundleJavadocAndSources(vararg sourceSets: SourceSet) { + parent.project.tasks.apply { + named(sourceSet.javadocTaskName).configure { + sourceSets.forEach { source(it.allJava) } + + JarTaskSet.excludeDuplicatePackageInfos(this) + } + + named(sourceSet.sourcesJarTaskName).configure { + sourceSets.forEach { from(it.allJava) } + + JarTaskSet.excludeDuplicatePackageInfos(this) + } + } } fun outgoing() { diff --git a/common/build.gradle.kts b/common/build.gradle.kts index a2877f83b..4efe08229 100644 --- a/common/build.gradle.kts +++ b/common/build.gradle.kts @@ -24,12 +24,12 @@ transitiveSourceSets { } sourceSet(lib) { rootCompile() - compile(api) + compileClasspath(api) outgoing() } sourceSet(backend) { rootCompile() - compile(api, lib) + compileClasspath(api, lib) outgoing() } sourceSet(stubs) { @@ -37,7 +37,7 @@ transitiveSourceSets { outgoingClasses() } sourceSet(main) { - compile(api, lib, backend, stubs) + compileClasspath(api, lib, backend, stubs) outgoing() } sourceSet(sourceSets.getByName("test")) { @@ -45,7 +45,7 @@ transitiveSourceSets { } sourceSet(vanillin) { rootCompile() - compile(api, lib) + compileClasspath(api, lib) outgoing() } } diff --git a/fabric/build.gradle.kts b/fabric/build.gradle.kts index 67b44b5a0..e9edf9108 100644 --- a/fabric/build.gradle.kts +++ b/fabric/build.gradle.kts @@ -7,6 +7,9 @@ plugins { id("flywheel.platform") } +val common = ":common" +val commonProject = project(common) + subproject.init("flywheel-fabric", "flywheel_group", "flywheel_version") val api = sourceSets.create("api") @@ -21,22 +24,34 @@ transitiveSourceSets { sourceSet(api) { rootCompile() + + from(commonProject) } sourceSet(lib) { rootCompile() - compile(api) + compileClasspath(api) + + from(commonProject) } sourceSet(backend) { rootCompile() - compile(api, lib) + compileClasspath(api, lib) + + from(commonProject) } sourceSet(stubs) { rootCompile() + + from(commonProject) } sourceSet(main) { // Don't want stubs at runtime - compile(stubs) + compileClasspath(stubs) implementation(api, lib, backend) + + bundleFrom(commonProject) + + bundleOutput(api, lib, backend) } sourceSet(testMod) { rootCompile() @@ -46,11 +61,8 @@ transitiveSourceSets { } platform { - commonProject = project(":common") - compileWithCommonSourceSets(api, lib, backend, stubs, main) setupLoomMod(api, lib, backend, main) setupLoomRuns() - setupFatJar(api, lib, backend, main) setupTestMod(testMod) } @@ -88,13 +100,13 @@ dependencies { modCompileOnly("maven.modrinth:sodium:${property("sodium_version")}") - "forApi"(project(path = ":common", configuration = "apiClasses")) - "forLib"(project(path = ":common", configuration = "libClasses")) - "forBackend"(project(path = ":common", configuration = "backendClasses")) - "forStubs"(project(path = ":common", configuration = "stubsClasses")) - "forMain"(project(path = ":common", configuration = "mainClasses")) + "forApi"(project(path = common, configuration = "apiClasses")) + "forLib"(project(path = common, configuration = "libClasses")) + "forBackend"(project(path = common, configuration = "backendClasses")) + "forStubs"(project(path = common, configuration = "stubsClasses")) + "forMain"(project(path = common, configuration = "mainClasses")) - "forLib"(project(path = ":common", configuration = "libResources")) - "forBackend"(project(path = ":common", configuration = "backendResources")) - "forMain"(project(path = ":common", configuration = "mainResources")) + "forLib"(project(path = common, configuration = "libResources")) + "forBackend"(project(path = common, configuration = "backendResources")) + "forMain"(project(path = common, configuration = "mainResources")) } diff --git a/forge/build.gradle.kts b/forge/build.gradle.kts index a12ea2c76..fdd825e1e 100644 --- a/forge/build.gradle.kts +++ b/forge/build.gradle.kts @@ -7,6 +7,9 @@ plugins { id("flywheel.platform") } +val common = ":common" +val commonProject = project(common) + subproject.init("flywheel-forge", "flywheel_group", "flywheel_version") val api = sourceSets.create("api") @@ -21,20 +24,32 @@ transitiveSourceSets { sourceSet(api) { rootCompile() + + from(commonProject) } sourceSet(lib) { rootCompile() - compile(api) + compileClasspath(api) + + from(commonProject) } sourceSet(backend) { rootCompile() - compile(api, lib) + compileClasspath(api, lib) + + from(commonProject) } sourceSet(stubs) { rootCompile() + + from(commonProject) } sourceSet(main) { - compile(api, lib, backend, stubs) + compileClasspath(api, lib, backend, stubs) + + bundleFrom(commonProject) + + bundleOutput(api, lib, backend) } sourceSet(testMod) { rootCompile() @@ -44,11 +59,8 @@ transitiveSourceSets { } platform { - commonProject = project(":common") - compileWithCommonSourceSets(api, lib, backend, stubs, main) setupLoomMod(api, lib, backend, main) setupLoomRuns() - setupFatJar(api, lib, backend, main) setupTestMod(testMod) } @@ -97,13 +109,13 @@ dependencies { modCompileOnly("maven.modrinth:embeddium:${property("embeddium_version")}") - "forApi"(project(path = ":common", configuration = "apiClasses")) - "forLib"(project(path = ":common", configuration = "libClasses")) - "forBackend"(project(path = ":common", configuration = "backendClasses")) - "forStubs"(project(path = ":common", configuration = "stubsClasses")) - "forMain"(project(path = ":common", configuration = "mainClasses")) + "forApi"(project(path = common, configuration = "apiClasses")) + "forLib"(project(path = common, configuration = "libClasses")) + "forBackend"(project(path = common, configuration = "backendClasses")) + "forStubs"(project(path = common, configuration = "stubsClasses")) + "forMain"(project(path = common, configuration = "mainClasses")) - "forLib"(project(path = ":common", configuration = "libResources")) - "forBackend"(project(path = ":common", configuration = "backendResources")) - "forMain"(project(path = ":common", configuration = "mainResources")) + "forLib"(project(path = common, configuration = "libResources")) + "forBackend"(project(path = common, configuration = "backendResources")) + "forMain"(project(path = common, configuration = "mainResources")) } diff --git a/vanillinFabric/build.gradle.kts b/vanillinFabric/build.gradle.kts index 96883fd3c..455da11d1 100644 --- a/vanillinFabric/build.gradle.kts +++ b/vanillinFabric/build.gradle.kts @@ -1,6 +1,3 @@ -import dev.engine_room.gradle.jarset.JarTaskSet -import org.gradle.jvm.tasks.Jar - plugins { idea java @@ -10,41 +7,23 @@ plugins { id("flywheel.platform") } +val common = ":common" +val platform = ":fabric" + subproject.init("vanillin-fabric", "vanillin_group", "vanillin_version") val main = sourceSets.getByName("main") platform { - commonProject = project(":common") setupLoomRuns() } -listOf("api", "lib") - .map { project(":fabric").sourceSets.named(it).get() } - .forEach { main.compileClasspath += it.output } - -val commonSourceSet = platform.commonSourceSets.named("vanillin").get() - -tasks.named("javadoc").configure { - source(commonSourceSet.allJava) - - JarTaskSet.excludeDuplicatePackageInfos(this) -} - -tasks.named("sourcesJar").configure { - from(commonSourceSet.allJava) - - JarTaskSet.excludeDuplicatePackageInfos(this) -} +transitiveSourceSets { + sourceSet(main) { + compileClasspath(project(platform), "api", "lib") -tasks.withType().configureEach { - JarTaskSet.excludeDuplicatePackageInfos(this) -} -tasks.named(main.compileJavaTaskName).configure { - source(commonSourceSet.allJava) -} -tasks.named(main.processResourcesTaskName).configure { - from(commonSourceSet.resources) + bundleFrom(project(common), "vanillin") + } } jarSets { @@ -68,10 +47,10 @@ dependencies { modCompileOnly("maven.modrinth:sodium:${property("sodium_version")}") - compileOnly(project(path = ":common", configuration = "vanillinClasses")) - compileOnly(project(path = ":common", configuration = "vanillinResources")) + compileOnly(project(path = common, configuration = "vanillinClasses")) + compileOnly(project(path = common, configuration = "vanillinResources")) // JiJ flywheel proper - include(project(path = ":fabric", configuration = "flywheelRemap")) - runtimeOnly(project(path = ":fabric", configuration = "flywheelDev")) + include(project(path = platform, configuration = "flywheelRemap")) + runtimeOnly(project(path = platform, configuration = "flywheelDev")) } diff --git a/vanillinForge/build.gradle.kts b/vanillinForge/build.gradle.kts index e16475411..b7f1a8170 100644 --- a/vanillinForge/build.gradle.kts +++ b/vanillinForge/build.gradle.kts @@ -1,6 +1,3 @@ -import dev.engine_room.gradle.jarset.JarTaskSet -import org.gradle.jvm.tasks.Jar - plugins { idea java @@ -10,41 +7,23 @@ plugins { id("flywheel.platform") } +val common = ":common" +val platform = ":forge" + subproject.init("vanillin-forge", "vanillin_group", "vanillin_version") val main = sourceSets.getByName("main") platform { - commonProject = project(":common") setupLoomRuns() } -listOf("api", "lib") - .map { project(":forge").sourceSets.named(it).get() } - .forEach { main.compileClasspath += it.output } - -val commonSourceSet = platform.commonSourceSets.named("vanillin").get() - -tasks.named("javadoc").configure { - source(commonSourceSet.allJava) - - JarTaskSet.excludeDuplicatePackageInfos(this) -} - -tasks.named("sourcesJar").configure { - from(commonSourceSet.allJava) - - JarTaskSet.excludeDuplicatePackageInfos(this) -} +transitiveSourceSets { + sourceSet(main) { + compileClasspath(project(platform), "api", "lib") -tasks.withType().configureEach { - JarTaskSet.excludeDuplicatePackageInfos(this) -} -tasks.named(main.compileJavaTaskName).configure { - source(commonSourceSet.allJava) -} -tasks.named(main.processResourcesTaskName).configure { - from(commonSourceSet.resources) + bundleFrom(project(common), "vanillin") + } } jarSets { @@ -79,10 +58,10 @@ dependencies { modCompileOnly("maven.modrinth:embeddium:${property("embeddium_version")}") - compileOnly(project(path = ":common", configuration = "vanillinClasses")) - compileOnly(project(path = ":common", configuration = "vanillinResources")) + compileOnly(project(path = common, configuration = "vanillinClasses")) + compileOnly(project(path = common, configuration = "vanillinResources")) // JiJ flywheel proper - include(project(path = ":forge", configuration = "flywheelRemap")) - runtimeOnly(project(path = ":forge", configuration = "flywheelDev")) + include(project(path = platform, configuration = "flywheelRemap")) + runtimeOnly(project(path = platform, configuration = "flywheelDev")) }