Skip to content

Commit

Permalink
I do declare
Browse files Browse the repository at this point in the history
- Move source set bundling/common source set sharing logic into
  transitiveSourceSets and make it more flexible
- Remove commonProject field from platform extension
  • Loading branch information
Jozufozu committed Jan 13, 2025
1 parent 811b0f2 commit 045b065
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 159 deletions.
Original file line number Diff line number Diff line change
@@ -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<SourceSetContainer>() }

fun setupLoomMod(vararg sourceSets: SourceSet) {
project.the<LoomGradleExtensionAPI>().mods.maybeCreate("main").apply {
sourceSets.forEach(::sourceSet)
Expand Down Expand Up @@ -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) {
project.tasks.apply {
val testModJar = register<Jar>("testModJar") {
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -22,23 +28,99 @@ 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<SourceSetContainer>()
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
}
}

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<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() {
Expand Down
8 changes: 4 additions & 4 deletions common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,28 @@ transitiveSourceSets {
}
sourceSet(lib) {
rootCompile()
compile(api)
compileClasspath(api)
outgoing()
}
sourceSet(backend) {
rootCompile()
compile(api, lib)
compileClasspath(api, lib)
outgoing()
}
sourceSet(stubs) {
rootCompile()
outgoingClasses()
}
sourceSet(main) {
compile(api, lib, backend, stubs)
compileClasspath(api, lib, backend, stubs)
outgoing()
}
sourceSet(sourceSets.getByName("test")) {
implementation(api, lib, backend)
}
sourceSet(vanillin) {
rootCompile()
compile(api, lib)
compileClasspath(api, lib)
outgoing()
}
}
Expand Down
40 changes: 26 additions & 14 deletions fabric/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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()
Expand All @@ -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)
}

Expand Down Expand Up @@ -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"))
}
40 changes: 26 additions & 14 deletions forge/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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()
Expand All @@ -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)
}

Expand Down Expand Up @@ -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"))
}
Loading

0 comments on commit 045b065

Please sign in to comment.