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
This commit is contained in:
Jozufozu 2025-01-12 19:03:32 -08:00
parent 811b0f2532
commit 045b065166
7 changed files with 170 additions and 159 deletions

View file

@ -1,25 +1,18 @@
package dev.engine_room.gradle.platform package dev.engine_room.gradle.platform
import dev.engine_room.gradle.jarset.JarTaskSet
import net.fabricmc.loom.api.LoomGradleExtensionAPI import net.fabricmc.loom.api.LoomGradleExtensionAPI
import net.fabricmc.loom.task.RemapJarTask import net.fabricmc.loom.task.RemapJarTask
import org.gradle.api.Project import org.gradle.api.Project
import org.gradle.api.Task import org.gradle.api.Task
import org.gradle.api.tasks.SourceSet 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.jvm.tasks.Jar
import org.gradle.kotlin.dsl.* import org.gradle.kotlin.dsl.assign
import org.gradle.language.jvm.tasks.ProcessResources import org.gradle.kotlin.dsl.named
import org.gradle.kotlin.dsl.register
import org.gradle.kotlin.dsl.the
import java.io.File import java.io.File
import kotlin.properties.Delegates
open class PlatformExtension(val project: Project) { open class PlatformExtension(val project: Project) {
var commonProject: Project by Delegates.notNull()
val commonSourceSets: SourceSetContainer by lazy { commonProject.the<SourceSetContainer>() }
fun setupLoomMod(vararg sourceSets: SourceSet) { fun setupLoomMod(vararg sourceSets: SourceSet) {
project.the<LoomGradleExtensionAPI>().mods.maybeCreate("main").apply { project.the<LoomGradleExtensionAPI>().mods.maybeCreate("main").apply {
sourceSets.forEach(::sourceSet) sourceSets.forEach(::sourceSet)
@ -51,52 +44,6 @@ open class PlatformExtension(val project: Project) {
} }
} }
fun compileWithCommonSourceSets(vararg sourceSets: SourceSet) {
project.tasks.apply {
withType<JavaCompile>().configureEach {
JarTaskSet.excludeDuplicatePackageInfos(this)
}
sourceSets.forEach {
val commonSourceSet = commonSourceSets.named(it.name).get()
named<JavaCompile>(it.compileJavaTaskName).configure {
source(commonSourceSet.allJava)
}
named<ProcessResources>(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>("jar").configure {
extraSourceSets.forEach { from(it.output) }
JarTaskSet.excludeDuplicatePackageInfos(this)
}
named<Javadoc>("javadoc").configure {
commonSources.forEach { source(it.allJava) }
extraSourceSets.forEach { source(it.allJava) }
JarTaskSet.excludeDuplicatePackageInfos(this)
}
named<Jar>("sourcesJar").configure {
commonSources.forEach { from(it.allJava) }
extraSourceSets.forEach { from(it.allJava) }
JarTaskSet.excludeDuplicatePackageInfos(this)
}
}
}
fun setupTestMod(sourceSet: SourceSet) { fun setupTestMod(sourceSet: SourceSet) {
project.tasks.apply { project.tasks.apply {
val testModJar = register<Jar>("testModJar") { val testModJar = register<Jar>("testModJar") {

View file

@ -1,8 +1,14 @@
package dev.engine_room.gradle.transitive 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.SourceSet
import org.gradle.api.tasks.SourceSetContainer
import org.gradle.api.tasks.compile.JavaCompile 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.named
import org.gradle.kotlin.dsl.the
import org.gradle.language.jvm.tasks.ProcessResources import org.gradle.language.jvm.tasks.ProcessResources
class TransitiveSourceSetConfigurator(private val parent: TransitiveSourceSetsExtension, private val sourceSet: SourceSet) { class TransitiveSourceSetConfigurator(private val parent: TransitiveSourceSetsExtension, private val sourceSet: SourceSet) {
@ -22,14 +28,21 @@ class TransitiveSourceSetConfigurator(private val parent: TransitiveSourceSetsEx
rootRuntime() rootRuntime()
} }
fun compile(vararg sourceSets: SourceSet) { fun compileClasspath(vararg sourceSets: SourceSet) {
compileSourceSets += sourceSets compileSourceSets += sourceSets
for (sourceSet in sourceSets) { for (sourceSet in sourceSets) {
this.sourceSet.compileClasspath += sourceSet.output this.sourceSet.compileClasspath += sourceSet.output
} }
} }
fun runtime(vararg sourceSets: SourceSet) { fun compileClasspath(project: Project, vararg sourceSets: String) {
val externalSourceSets = project.the<SourceSetContainer>()
for (name in sourceSets) {
this.sourceSet.compileClasspath += externalSourceSets.getByName(name).output
}
}
fun runtimeClasspath(vararg sourceSets: SourceSet) {
runtimeSourceSets += sourceSets runtimeSourceSets += sourceSets
for (sourceSet in sourceSets) { for (sourceSet in sourceSets) {
this.sourceSet.runtimeClasspath += sourceSet.output this.sourceSet.runtimeClasspath += sourceSet.output
@ -37,8 +50,77 @@ class TransitiveSourceSetConfigurator(private val parent: TransitiveSourceSetsEx
} }
fun implementation(vararg sourceSets: SourceSet) { fun implementation(vararg sourceSets: SourceSet) {
compile(*sourceSets) compileClasspath(*sourceSets)
runtime(*sourceSets) runtimeClasspath(*sourceSets)
}
fun from(otherProject: Project) {
from(otherProject, sourceSet.name)
}
fun from(otherProject: Project, vararg names: String) {
val otherSourceSets = otherProject.the<SourceSetContainer>()
from(*names.map { otherSourceSets.getByName(it) }.toTypedArray())
}
fun from(vararg sourceSets: SourceSet) {
parent.project.tasks.apply {
named<JavaCompile>(sourceSet.compileJavaTaskName).configure {
sourceSets.forEach { source(it.allJava) }
JarTaskSet.excludeDuplicatePackageInfos(this)
}
named<ProcessResources>(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<SourceSetContainer>()
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<Jar>(sourceSet.jarTaskName).configure {
sourceSets.forEach { from(it.output) }
JarTaskSet.excludeDuplicatePackageInfos(this)
}
}
}
private fun bundleJavadocAndSources(vararg sourceSets: SourceSet) {
parent.project.tasks.apply {
named<Javadoc>(sourceSet.javadocTaskName).configure {
sourceSets.forEach { source(it.allJava) }
JarTaskSet.excludeDuplicatePackageInfos(this)
}
named<Jar>(sourceSet.sourcesJarTaskName).configure {
sourceSets.forEach { from(it.allJava) }
JarTaskSet.excludeDuplicatePackageInfos(this)
}
}
} }
fun outgoing() { fun outgoing() {

View file

@ -24,12 +24,12 @@ transitiveSourceSets {
} }
sourceSet(lib) { sourceSet(lib) {
rootCompile() rootCompile()
compile(api) compileClasspath(api)
outgoing() outgoing()
} }
sourceSet(backend) { sourceSet(backend) {
rootCompile() rootCompile()
compile(api, lib) compileClasspath(api, lib)
outgoing() outgoing()
} }
sourceSet(stubs) { sourceSet(stubs) {
@ -37,7 +37,7 @@ transitiveSourceSets {
outgoingClasses() outgoingClasses()
} }
sourceSet(main) { sourceSet(main) {
compile(api, lib, backend, stubs) compileClasspath(api, lib, backend, stubs)
outgoing() outgoing()
} }
sourceSet(sourceSets.getByName("test")) { sourceSet(sourceSets.getByName("test")) {
@ -45,7 +45,7 @@ transitiveSourceSets {
} }
sourceSet(vanillin) { sourceSet(vanillin) {
rootCompile() rootCompile()
compile(api, lib) compileClasspath(api, lib)
outgoing() outgoing()
} }
} }

View file

@ -7,6 +7,9 @@ plugins {
id("flywheel.platform") id("flywheel.platform")
} }
val common = ":common"
val commonProject = project(common)
subproject.init("flywheel-fabric", "flywheel_group", "flywheel_version") subproject.init("flywheel-fabric", "flywheel_group", "flywheel_version")
val api = sourceSets.create("api") val api = sourceSets.create("api")
@ -21,22 +24,34 @@ transitiveSourceSets {
sourceSet(api) { sourceSet(api) {
rootCompile() rootCompile()
from(commonProject)
} }
sourceSet(lib) { sourceSet(lib) {
rootCompile() rootCompile()
compile(api) compileClasspath(api)
from(commonProject)
} }
sourceSet(backend) { sourceSet(backend) {
rootCompile() rootCompile()
compile(api, lib) compileClasspath(api, lib)
from(commonProject)
} }
sourceSet(stubs) { sourceSet(stubs) {
rootCompile() rootCompile()
from(commonProject)
} }
sourceSet(main) { sourceSet(main) {
// Don't want stubs at runtime // Don't want stubs at runtime
compile(stubs) compileClasspath(stubs)
implementation(api, lib, backend) implementation(api, lib, backend)
bundleFrom(commonProject)
bundleOutput(api, lib, backend)
} }
sourceSet(testMod) { sourceSet(testMod) {
rootCompile() rootCompile()
@ -46,11 +61,8 @@ transitiveSourceSets {
} }
platform { platform {
commonProject = project(":common")
compileWithCommonSourceSets(api, lib, backend, stubs, main)
setupLoomMod(api, lib, backend, main) setupLoomMod(api, lib, backend, main)
setupLoomRuns() setupLoomRuns()
setupFatJar(api, lib, backend, main)
setupTestMod(testMod) setupTestMod(testMod)
} }
@ -88,13 +100,13 @@ dependencies {
modCompileOnly("maven.modrinth:sodium:${property("sodium_version")}") modCompileOnly("maven.modrinth:sodium:${property("sodium_version")}")
"forApi"(project(path = ":common", configuration = "apiClasses")) "forApi"(project(path = common, configuration = "apiClasses"))
"forLib"(project(path = ":common", configuration = "libClasses")) "forLib"(project(path = common, configuration = "libClasses"))
"forBackend"(project(path = ":common", configuration = "backendClasses")) "forBackend"(project(path = common, configuration = "backendClasses"))
"forStubs"(project(path = ":common", configuration = "stubsClasses")) "forStubs"(project(path = common, configuration = "stubsClasses"))
"forMain"(project(path = ":common", configuration = "mainClasses")) "forMain"(project(path = common, configuration = "mainClasses"))
"forLib"(project(path = ":common", configuration = "libResources")) "forLib"(project(path = common, configuration = "libResources"))
"forBackend"(project(path = ":common", configuration = "backendResources")) "forBackend"(project(path = common, configuration = "backendResources"))
"forMain"(project(path = ":common", configuration = "mainResources")) "forMain"(project(path = common, configuration = "mainResources"))
} }

View file

@ -7,6 +7,9 @@ plugins {
id("flywheel.platform") id("flywheel.platform")
} }
val common = ":common"
val commonProject = project(common)
subproject.init("flywheel-forge", "flywheel_group", "flywheel_version") subproject.init("flywheel-forge", "flywheel_group", "flywheel_version")
val api = sourceSets.create("api") val api = sourceSets.create("api")
@ -21,20 +24,32 @@ transitiveSourceSets {
sourceSet(api) { sourceSet(api) {
rootCompile() rootCompile()
from(commonProject)
} }
sourceSet(lib) { sourceSet(lib) {
rootCompile() rootCompile()
compile(api) compileClasspath(api)
from(commonProject)
} }
sourceSet(backend) { sourceSet(backend) {
rootCompile() rootCompile()
compile(api, lib) compileClasspath(api, lib)
from(commonProject)
} }
sourceSet(stubs) { sourceSet(stubs) {
rootCompile() rootCompile()
from(commonProject)
} }
sourceSet(main) { sourceSet(main) {
compile(api, lib, backend, stubs) compileClasspath(api, lib, backend, stubs)
bundleFrom(commonProject)
bundleOutput(api, lib, backend)
} }
sourceSet(testMod) { sourceSet(testMod) {
rootCompile() rootCompile()
@ -44,11 +59,8 @@ transitiveSourceSets {
} }
platform { platform {
commonProject = project(":common")
compileWithCommonSourceSets(api, lib, backend, stubs, main)
setupLoomMod(api, lib, backend, main) setupLoomMod(api, lib, backend, main)
setupLoomRuns() setupLoomRuns()
setupFatJar(api, lib, backend, main)
setupTestMod(testMod) setupTestMod(testMod)
} }
@ -97,13 +109,13 @@ dependencies {
modCompileOnly("maven.modrinth:embeddium:${property("embeddium_version")}") modCompileOnly("maven.modrinth:embeddium:${property("embeddium_version")}")
"forApi"(project(path = ":common", configuration = "apiClasses")) "forApi"(project(path = common, configuration = "apiClasses"))
"forLib"(project(path = ":common", configuration = "libClasses")) "forLib"(project(path = common, configuration = "libClasses"))
"forBackend"(project(path = ":common", configuration = "backendClasses")) "forBackend"(project(path = common, configuration = "backendClasses"))
"forStubs"(project(path = ":common", configuration = "stubsClasses")) "forStubs"(project(path = common, configuration = "stubsClasses"))
"forMain"(project(path = ":common", configuration = "mainClasses")) "forMain"(project(path = common, configuration = "mainClasses"))
"forLib"(project(path = ":common", configuration = "libResources")) "forLib"(project(path = common, configuration = "libResources"))
"forBackend"(project(path = ":common", configuration = "backendResources")) "forBackend"(project(path = common, configuration = "backendResources"))
"forMain"(project(path = ":common", configuration = "mainResources")) "forMain"(project(path = common, configuration = "mainResources"))
} }

View file

@ -1,6 +1,3 @@
import dev.engine_room.gradle.jarset.JarTaskSet
import org.gradle.jvm.tasks.Jar
plugins { plugins {
idea idea
java java
@ -10,41 +7,23 @@ plugins {
id("flywheel.platform") id("flywheel.platform")
} }
val common = ":common"
val platform = ":fabric"
subproject.init("vanillin-fabric", "vanillin_group", "vanillin_version") subproject.init("vanillin-fabric", "vanillin_group", "vanillin_version")
val main = sourceSets.getByName("main") val main = sourceSets.getByName("main")
platform { platform {
commonProject = project(":common")
setupLoomRuns() setupLoomRuns()
} }
listOf("api", "lib") transitiveSourceSets {
.map { project(":fabric").sourceSets.named(it).get() } sourceSet(main) {
.forEach { main.compileClasspath += it.output } compileClasspath(project(platform), "api", "lib")
val commonSourceSet = platform.commonSourceSets.named("vanillin").get() bundleFrom(project(common), "vanillin")
}
tasks.named<Javadoc>("javadoc").configure {
source(commonSourceSet.allJava)
JarTaskSet.excludeDuplicatePackageInfos(this)
}
tasks.named<Jar>("sourcesJar").configure {
from(commonSourceSet.allJava)
JarTaskSet.excludeDuplicatePackageInfos(this)
}
tasks.withType<JavaCompile>().configureEach {
JarTaskSet.excludeDuplicatePackageInfos(this)
}
tasks.named<JavaCompile>(main.compileJavaTaskName).configure {
source(commonSourceSet.allJava)
}
tasks.named<ProcessResources>(main.processResourcesTaskName).configure {
from(commonSourceSet.resources)
} }
jarSets { jarSets {
@ -68,10 +47,10 @@ dependencies {
modCompileOnly("maven.modrinth:sodium:${property("sodium_version")}") modCompileOnly("maven.modrinth:sodium:${property("sodium_version")}")
compileOnly(project(path = ":common", configuration = "vanillinClasses")) compileOnly(project(path = common, configuration = "vanillinClasses"))
compileOnly(project(path = ":common", configuration = "vanillinResources")) compileOnly(project(path = common, configuration = "vanillinResources"))
// JiJ flywheel proper // JiJ flywheel proper
include(project(path = ":fabric", configuration = "flywheelRemap")) include(project(path = platform, configuration = "flywheelRemap"))
runtimeOnly(project(path = ":fabric", configuration = "flywheelDev")) runtimeOnly(project(path = platform, configuration = "flywheelDev"))
} }

View file

@ -1,6 +1,3 @@
import dev.engine_room.gradle.jarset.JarTaskSet
import org.gradle.jvm.tasks.Jar
plugins { plugins {
idea idea
java java
@ -10,41 +7,23 @@ plugins {
id("flywheel.platform") id("flywheel.platform")
} }
val common = ":common"
val platform = ":forge"
subproject.init("vanillin-forge", "vanillin_group", "vanillin_version") subproject.init("vanillin-forge", "vanillin_group", "vanillin_version")
val main = sourceSets.getByName("main") val main = sourceSets.getByName("main")
platform { platform {
commonProject = project(":common")
setupLoomRuns() setupLoomRuns()
} }
listOf("api", "lib") transitiveSourceSets {
.map { project(":forge").sourceSets.named(it).get() } sourceSet(main) {
.forEach { main.compileClasspath += it.output } compileClasspath(project(platform), "api", "lib")
val commonSourceSet = platform.commonSourceSets.named("vanillin").get() bundleFrom(project(common), "vanillin")
}
tasks.named<Javadoc>("javadoc").configure {
source(commonSourceSet.allJava)
JarTaskSet.excludeDuplicatePackageInfos(this)
}
tasks.named<Jar>("sourcesJar").configure {
from(commonSourceSet.allJava)
JarTaskSet.excludeDuplicatePackageInfos(this)
}
tasks.withType<JavaCompile>().configureEach {
JarTaskSet.excludeDuplicatePackageInfos(this)
}
tasks.named<JavaCompile>(main.compileJavaTaskName).configure {
source(commonSourceSet.allJava)
}
tasks.named<ProcessResources>(main.processResourcesTaskName).configure {
from(commonSourceSet.resources)
} }
jarSets { jarSets {
@ -79,10 +58,10 @@ dependencies {
modCompileOnly("maven.modrinth:embeddium:${property("embeddium_version")}") modCompileOnly("maven.modrinth:embeddium:${property("embeddium_version")}")
compileOnly(project(path = ":common", configuration = "vanillinClasses")) compileOnly(project(path = common, configuration = "vanillinClasses"))
compileOnly(project(path = ":common", configuration = "vanillinResources")) compileOnly(project(path = common, configuration = "vanillinResources"))
// JiJ flywheel proper // JiJ flywheel proper
include(project(path = ":forge", configuration = "flywheelRemap")) include(project(path = platform, configuration = "flywheelRemap"))
runtimeOnly(project(path = ":forge", configuration = "flywheelDev")) runtimeOnly(project(path = platform, configuration = "flywheelDev"))
} }