1+ import dev.kikugie.stonecutter.build.StonecutterBuildExtension
2+ import org.gradle.api.Plugin
3+ import org.gradle.api.Project
4+ import org.gradle.api.tasks.TaskProvider
5+ import org.gradle.jvm.tasks.Jar
6+ import org.gradle.kotlin.dsl.create
7+ import org.gradle.kotlin.dsl.the
8+
9+ open class FabricLoomCompatPlugin : Plugin <Project > {
10+ override fun apply (target : Project ): Unit = with (target) {
11+ val current = the<StonecutterBuildExtension >().current.parsed
12+
13+ if (current > " 26.0" ) {
14+ setupNewLoomFacade()
15+ } else {
16+ setupOldLoomFacade()
17+ }
18+
19+ extensions.create<FabricExtensions >(" fabric" , this , current > " 26.0" )
20+ }
21+
22+ private fun Project.setupNewLoomFacade () {
23+ plugins.apply (" net.fabricmc.fabric-loom" )
24+
25+ val names = listOf (
26+ " api" , " implementation" , " compileOnly" , " runtimeOnly" , " localRuntime"
27+ )
28+
29+ for (baseName in names) {
30+ val loomified = " mod" + baseName.replaceFirstChar(Char ::uppercaseChar)
31+ val modConfiguration = configurations.findByName(loomified) ? : configurations.create(loomified)
32+
33+ configurations.getByName(baseName).extendsFrom(modConfiguration)
34+ }
35+
36+ configurations.findByName(" mappings" ) ? : configurations.register(" mappings" ) {
37+ isCanBeResolved = false
38+ isCanBeConsumed = false
39+ }
40+ }
41+
42+ private fun Project.setupOldLoomFacade () {
43+ plugins.apply (" net.fabricmc.fabric-loom-remap" )
44+ }
45+
46+ open class FabricExtensions (val project : Project , val isNew : Boolean ) {
47+ val modJar: TaskProvider <Jar > by lazy {
48+ val candidate = if (isNew) project.tasks.named(" jar" )
49+ else project.tasks.named(" remapJar" )
50+ candidate as TaskProvider <Jar >
51+ }
52+
53+ val modSourcesJar: TaskProvider <Jar > by lazy {
54+ val candidate = if (isNew) project.tasks.named(" sourcesJar" )
55+ else project.tasks.named(" remapSourcesJar" )
56+ candidate as TaskProvider <Jar >
57+ }
58+ }
59+ }
0 commit comments