From 842e6392db6ef4fe3d81db3c3941000a15e55117 Mon Sep 17 00:00:00 2001 From: KosmX Date: Wed, 4 Jan 2023 15:24:27 +0100 Subject: [PATCH 1/8] Add Kotlin DSL template --- scripts/src/lib/Template.svelte | 13 ++ scripts/src/lib/template/gradlekotlin.ts | 13 ++ scripts/src/lib/template/template.ts | 8 +- .../gradle/kotlin/build.gradle.kts.eta | 116 ++++++++++++++++++ .../gradle/kotlin/settings.gradle.kts | 9 ++ 5 files changed, 158 insertions(+), 1 deletion(-) create mode 100644 scripts/src/lib/template/gradlekotlin.ts create mode 100644 scripts/src/lib/template/templates/gradle/kotlin/build.gradle.kts.eta create mode 100644 scripts/src/lib/template/templates/gradle/kotlin/settings.gradle.kts diff --git a/scripts/src/lib/Template.svelte b/scripts/src/lib/Template.svelte index 104811b0..510c0f78 100644 --- a/scripts/src/lib/Template.svelte +++ b/scripts/src/lib/Template.svelte @@ -10,6 +10,7 @@ let projectName = "Template Mod"; let packageName = "com.example"; let useKotlin = false; + let useKotlinGradle = false; let dataGeneration = false; let splitSources = true; @@ -48,6 +49,7 @@ projectName, packageName, useKotlin, + useKotlinGradle, dataGeneration: dataGeneration && supportsDataGen, splitSources: splitSources && supportsSplitSources, }; @@ -131,6 +133,17 @@

+
+
+ + +
+

+ Use Gradle Kotlin DSL instead of Groovy. + This will generate build.gradle.kts and settings.gradle.kts files. +

+
+ {#if supportsDataGen}
diff --git a/scripts/src/lib/template/gradlekotlin.ts b/scripts/src/lib/template/gradlekotlin.ts new file mode 100644 index 00000000..3ac5ccbe --- /dev/null +++ b/scripts/src/lib/template/gradlekotlin.ts @@ -0,0 +1,13 @@ +import type { ComputedConfiguration, TemplateWriter } from './template'; +import { renderTemplate } from './eta'; + +import gradlePropertiesTemplate from './templates/gradle/groovy/gradle.properties.eta?raw'; +import buildGradleTemplate from './templates/gradle/kotlin/build.gradle.kts.eta?raw'; +import settingsGradle from './templates/gradle/kotlin/settings.gradle.kts?raw'; +import { getJavaVersion } from './java'; + +export async function addKotlinGradle(writer: TemplateWriter, config: ComputedConfiguration) { + await writer.write('gradle.properties', renderTemplate(gradlePropertiesTemplate, config)); + await writer.write('build.gradle.kts', renderTemplate(buildGradleTemplate, {...config, java: getJavaVersion(config.minecraftVersion)})); + await writer.write('settings.gradle.kts', settingsGradle); +} diff --git a/scripts/src/lib/template/template.ts b/scripts/src/lib/template/template.ts index 5d9f7b6f..a4ae5569 100644 --- a/scripts/src/lib/template/template.ts +++ b/scripts/src/lib/template/template.ts @@ -1,5 +1,6 @@ import { addGradleWrapper } from './gradlewrapper'; import { addGroovyGradle } from './gradlegroovy'; +import { addKotlinGradle } from './gradlekotlin'; import { getApiVersionForMinecraft, getKotlinAdapterVersions, getLoaderVersions, getMinecraftYarnVersions } from '../Api'; import { addModJson } from './modjson'; import { addGitFiles } from './git'; @@ -21,6 +22,7 @@ export interface Configuration { projectName: string, packageName: string, useKotlin: boolean, + useKotlinGradle: boolean, dataGeneration: boolean, splitSources: boolean, } @@ -51,7 +53,11 @@ export async function generateTemplate(options: Options) { const computedConfig = await computeConfig(options.config); await addGradleWrapper(options); - await addGroovyGradle(options.writer, computedConfig); + if (options.config.useKotlinGradle) { + await addKotlinGradle(options.writer, computedConfig); + } else { + await addGroovyGradle(options.writer, computedConfig); + } await addModJson(options.writer, computedConfig); await addGitFiles(options.writer, computedConfig); } diff --git a/scripts/src/lib/template/templates/gradle/kotlin/build.gradle.kts.eta b/scripts/src/lib/template/templates/gradle/kotlin/build.gradle.kts.eta new file mode 100644 index 00000000..e44b961d --- /dev/null +++ b/scripts/src/lib/template/templates/gradle/kotlin/build.gradle.kts.eta @@ -0,0 +1,116 @@ +plugins { + id("fabric-loom") version "1.0-SNAPSHOT" + id("maven-publish") + <% if (it.kotlin) { %>kotlin("jvm") version "<%= it.kotlin.kotlinVersion %>"<% } %> +} + +base.archivesName.set(project.properties["archives_base_name"] as String) +version = project.properties["mod_version"] as String +group = project.properties["maven_group"] as String + +repositories { + // Add repositories to retrieve artifacts from in here. + // You should only use this when depending on other mods because + // Loom adds the essential maven repositories to download Minecraft and libraries from automatically. + // See https://docs.gradle.org/current/userguide/declaring_repositories.html + // for more information about repositories. +} +<% if (it.dataGeneration || it.splitSources) { %> +loom { +<% if (it.splitSources) { %> splitEnvironmentSourceSets() + + mods { + create("modid") { + sourceSet(sourceSets["main"]) + sourceSet(sourceSets["client"]) + } + } +<% } %><% if (it.dataGeneration) { %> runs { + create("datagen") { + inherit(runConfigs["server"]) + name("Data Generation") + vmArg("-Dfabric-api.datagen") + vmArg("-Dfabric-api.datagen.output-dir=${file("src/main/generated")}") + vmArg("-Dfabric-api.datagen.modid=<%= it.modid %>") + + runDir("build/datagen") + } + }<% } %> +} +<% } %><% if (it.dataGeneration) { %> +sourceSets { + main { + resources { + srcDirs += file("src/main/generated") + } + } +}<% } %> + +dependencies { + // To change the versions see the gradle.properties file + minecraft("com.mojang:minecraft:${project.properties["minecraft_version"]}") + mappings("net.fabricmc:yarn:${project.properties["yarn_mappings"]}:v2") + modImplementation("net.fabricmc:fabric-loader:${project.properties["loader_version"]}") + + // Fabric API. This is technically optional, but you probably want it anyway. + modImplementation("net.fabricmc.fabric-api:fabric-api:${project.properties["fabric_version"]}") +<% if (it.kotlin) { %> modImplementation("net.fabricmc:fabric-language-kotlin:${project.properties["fabric_kotlin_version"]}")<% } %> + // Uncomment the following line to enable the deprecated Fabric API modules. + // These are included in the Fabric API production distribution and allow you to update your mod to the latest modules at a later more convenient time. + + // modImplementation("net.fabricmc.fabric-api:fabric-api-deprecated:${project.properties["fabric_version"]}") +} + +tasks { + processResources { + inputs.property("version", project.version) + filteringCharset = "UTF-8" + + filesMatching("fabric.mod.json") { + expand(mapOf("version" to project.version)) + } + } + + // Minecraft 1.18 (1.18-pre2) upwards uses Java 17. + val targetJavaVersion = <%= it.java.release %> + withType { + options.encoding = "UTF-8" + options.release.set(targetJavaVersion) + } + +<% if (it.kotlin) { %> withType { + kotlinOptions.jvmTarget = targetJavaVersion.toString() + }<% } %> + + java { + toolchain.languageVersion.set(JavaLanguageVersion.of(JavaVersion.toVersion(targetJavaVersion).toString())) + + // Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task + // if it is present. + // If you remove this line, sources will not be generated. + withSourcesJar() + } + + jar { + from("LICENSE") { + rename { "${it}_${base.archivesName}" } + } + } +} + +// configure the maven publication +publishing { + publications { + create("mavenJava") { + from(components["java"]) + } + } + + // See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing. + repositories { + // Add repositories to publish to here. + // Notice: This block does NOT have the same function as the block in the top level. + // The repositories here will be used for publishing your artifact, not for + // retrieving dependencies. + } +} diff --git a/scripts/src/lib/template/templates/gradle/kotlin/settings.gradle.kts b/scripts/src/lib/template/templates/gradle/kotlin/settings.gradle.kts new file mode 100644 index 00000000..86640271 --- /dev/null +++ b/scripts/src/lib/template/templates/gradle/kotlin/settings.gradle.kts @@ -0,0 +1,9 @@ +pluginManagement { + repositories { + maven("https://maven.fabricmc.net/") { + name= "Fabric" + } + mavenCentral() + gradlePluginPortal() + } +} From 7bd0823c459efabd09d83929e7b085475f75efab Mon Sep 17 00:00:00 2001 From: KosmX Date: Wed, 4 Jan 2023 19:09:49 +0100 Subject: [PATCH 2/8] Change option description --- scripts/src/lib/Template.svelte | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/scripts/src/lib/Template.svelte b/scripts/src/lib/Template.svelte index 510c0f78..53e1db67 100644 --- a/scripts/src/lib/Template.svelte +++ b/scripts/src/lib/Template.svelte @@ -139,8 +139,7 @@

- Use Gradle Kotlin DSL instead of Groovy. - This will generate build.gradle.kts and settings.gradle.kts files. + Use the Kotlin DSL for the Gradle build script. This option is only reccomended for advanced users familar with Kotlin and Gradle. This may result in slower build peformance and compability issues with some IDEs.

From 76bc7b0b3beda255f3f7d5a1b546f250a8518ec4 Mon Sep 17 00:00:00 2001 From: KosmX Date: Wed, 4 Jan 2023 19:11:37 +0100 Subject: [PATCH 3/8] Apply requested changes --- .../gradle/kotlin/build.gradle.kts.eta | 25 ++++++++----------- .../gradle/kotlin/settings.gradle.kts | 2 +- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/scripts/src/lib/template/templates/gradle/kotlin/build.gradle.kts.eta b/scripts/src/lib/template/templates/gradle/kotlin/build.gradle.kts.eta index e44b961d..cddf0f30 100644 --- a/scripts/src/lib/template/templates/gradle/kotlin/build.gradle.kts.eta +++ b/scripts/src/lib/template/templates/gradle/kotlin/build.gradle.kts.eta @@ -1,9 +1,12 @@ plugins { id("fabric-loom") version "1.0-SNAPSHOT" - id("maven-publish") - <% if (it.kotlin) { %>kotlin("jvm") version "<%= it.kotlin.kotlinVersion %>"<% } %> + id("maven-publish")<% if (it.kotlin) { %> + kotlin("jvm") version "<%= it.kotlin.kotlinVersion %>"<% } %> } +java.sourceCompatibility = JavaVersion.<%= it.java.compatibility %> +java.targetCompatibility = JavaVersion.<%= it.java.compatibility %> + base.archivesName.set(project.properties["archives_base_name"] as String) version = project.properties["mod_version"] as String group = project.properties["maven_group"] as String @@ -64,27 +67,21 @@ dependencies { tasks { processResources { inputs.property("version", project.version) - filteringCharset = "UTF-8" filesMatching("fabric.mod.json") { expand(mapOf("version" to project.version)) } } - // Minecraft 1.18 (1.18-pre2) upwards uses Java 17. - val targetJavaVersion = <%= it.java.release %> withType { - options.encoding = "UTF-8" - options.release.set(targetJavaVersion) + options.release.set(java.targetCompatibility.majorVersion.toInt()) } - -<% if (it.kotlin) { %> withType { - kotlinOptions.jvmTarget = targetJavaVersion.toString() - }<% } %> - +<% if (it.kotlin) { %> + withType { + kotlinOptions.jvmTarget = java.targetCompatibility.toString() + } +<% } %> java { - toolchain.languageVersion.set(JavaLanguageVersion.of(JavaVersion.toVersion(targetJavaVersion).toString())) - // Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task // if it is present. // If you remove this line, sources will not be generated. diff --git a/scripts/src/lib/template/templates/gradle/kotlin/settings.gradle.kts b/scripts/src/lib/template/templates/gradle/kotlin/settings.gradle.kts index 86640271..e1d9b9c4 100644 --- a/scripts/src/lib/template/templates/gradle/kotlin/settings.gradle.kts +++ b/scripts/src/lib/template/templates/gradle/kotlin/settings.gradle.kts @@ -1,7 +1,7 @@ pluginManagement { repositories { maven("https://maven.fabricmc.net/") { - name= "Fabric" + name = "Fabric" } mavenCentral() gradlePluginPortal() From 465f280b9edc4ca0a43a86b709baecfff7ec8e5f Mon Sep 17 00:00:00 2001 From: KosmX Date: Wed, 4 Jan 2023 19:51:21 +0100 Subject: [PATCH 4/8] Use some of the advices from Juuxel --- .../lib/template/templates/gradle/kotlin/build.gradle.kts.eta | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/src/lib/template/templates/gradle/kotlin/build.gradle.kts.eta b/scripts/src/lib/template/templates/gradle/kotlin/build.gradle.kts.eta index cddf0f30..07515de6 100644 --- a/scripts/src/lib/template/templates/gradle/kotlin/build.gradle.kts.eta +++ b/scripts/src/lib/template/templates/gradle/kotlin/build.gradle.kts.eta @@ -69,7 +69,7 @@ tasks { inputs.property("version", project.version) filesMatching("fabric.mod.json") { - expand(mapOf("version" to project.version)) + expand("version" to project.version) } } @@ -90,7 +90,7 @@ tasks { jar { from("LICENSE") { - rename { "${it}_${base.archivesName}" } + rename { "${it}_${base.archivesName.get()}" } } } } From 9729a4711824ce6cef949fe9b40c0b0734c7727a Mon Sep 17 00:00:00 2001 From: KosmX Date: Wed, 4 Jan 2023 20:03:59 +0100 Subject: [PATCH 5/8] Replace modid with modid val in kotlin example. --- .../lib/template/templates/gradle/kotlin/build.gradle.kts.eta | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/src/lib/template/templates/gradle/kotlin/build.gradle.kts.eta b/scripts/src/lib/template/templates/gradle/kotlin/build.gradle.kts.eta index 07515de6..13297c8d 100644 --- a/scripts/src/lib/template/templates/gradle/kotlin/build.gradle.kts.eta +++ b/scripts/src/lib/template/templates/gradle/kotlin/build.gradle.kts.eta @@ -23,7 +23,7 @@ loom { <% if (it.splitSources) { %> splitEnvironmentSourceSets() mods { - create("modid") { + create("<%= it.modid %>") { sourceSet(sourceSets["main"]) sourceSet(sourceSets["client"]) } From 732d3ece96e642343810d55aeb829d322a515c00 Mon Sep 17 00:00:00 2001 From: KosmX Date: Wed, 4 Jan 2023 22:40:57 +0100 Subject: [PATCH 6/8] Use property(...) instead of properties[...] --- .../gradle/kotlin/build.gradle.kts.eta | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/scripts/src/lib/template/templates/gradle/kotlin/build.gradle.kts.eta b/scripts/src/lib/template/templates/gradle/kotlin/build.gradle.kts.eta index 13297c8d..da597cf1 100644 --- a/scripts/src/lib/template/templates/gradle/kotlin/build.gradle.kts.eta +++ b/scripts/src/lib/template/templates/gradle/kotlin/build.gradle.kts.eta @@ -7,9 +7,9 @@ plugins { java.sourceCompatibility = JavaVersion.<%= it.java.compatibility %> java.targetCompatibility = JavaVersion.<%= it.java.compatibility %> -base.archivesName.set(project.properties["archives_base_name"] as String) -version = project.properties["mod_version"] as String -group = project.properties["maven_group"] as String +base.archivesName.set(project.property("archives_base_name") as String) +version = project.property("mod_version") as String +group = project.property("maven_group") as String repositories { // Add repositories to retrieve artifacts from in here. @@ -51,17 +51,17 @@ sourceSets { dependencies { // To change the versions see the gradle.properties file - minecraft("com.mojang:minecraft:${project.properties["minecraft_version"]}") - mappings("net.fabricmc:yarn:${project.properties["yarn_mappings"]}:v2") - modImplementation("net.fabricmc:fabric-loader:${project.properties["loader_version"]}") + minecraft("com.mojang:minecraft:${project.property("minecraft_version")}") + mappings("net.fabricmc:yarn:${project.property("yarn_mappings")}:v2") + modImplementation("net.fabricmc:fabric-loader:${project.property("loader_version")}") // Fabric API. This is technically optional, but you probably want it anyway. - modImplementation("net.fabricmc.fabric-api:fabric-api:${project.properties["fabric_version"]}") -<% if (it.kotlin) { %> modImplementation("net.fabricmc:fabric-language-kotlin:${project.properties["fabric_kotlin_version"]}")<% } %> + modImplementation("net.fabricmc.fabric-api:fabric-api:${project.property("fabric_version")}") +<% if (it.kotlin) { %> modImplementation("net.fabricmc:fabric-language-kotlin:${project.property("fabric_kotlin_version")}")<% } %> // Uncomment the following line to enable the deprecated Fabric API modules. // These are included in the Fabric API production distribution and allow you to update your mod to the latest modules at a later more convenient time. - // modImplementation("net.fabricmc.fabric-api:fabric-api-deprecated:${project.properties["fabric_version"]}") + // modImplementation("net.fabricmc.fabric-api:fabric-api-deprecated:${project.property("fabric_version")}") } tasks { From aacc8013f0d0f08130eb2cd1e57baec91ccf1970 Mon Sep 17 00:00:00 2001 From: KosmX Date: Mon, 22 May 2023 19:44:03 +0200 Subject: [PATCH 7/8] Set kotlin JvmToolchain version instead of jvmTarget --- .../templates/gradle/kotlin/build.gradle.kts.eta | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/scripts/src/lib/template/templates/gradle/kotlin/build.gradle.kts.eta b/scripts/src/lib/template/templates/gradle/kotlin/build.gradle.kts.eta index da597cf1..1fb7ca02 100644 --- a/scripts/src/lib/template/templates/gradle/kotlin/build.gradle.kts.eta +++ b/scripts/src/lib/template/templates/gradle/kotlin/build.gradle.kts.eta @@ -1,5 +1,5 @@ plugins { - id("fabric-loom") version "1.0-SNAPSHOT" + id("fabric-loom") version "1.2-SNAPSHOT" id("maven-publish")<% if (it.kotlin) { %> kotlin("jvm") version "<%= it.kotlin.kotlinVersion %>"<% } %> } @@ -64,6 +64,11 @@ dependencies { // modImplementation("net.fabricmc.fabric-api:fabric-api-deprecated:${project.property("fabric_version")}") } +<% if (it.kotlin) { %> +kotlin { + jvmToolchain(java.targetCompatibility.majorVersion.toInt()) +} +<% } %> tasks { processResources { inputs.property("version", project.version) @@ -76,11 +81,6 @@ tasks { withType { options.release.set(java.targetCompatibility.majorVersion.toInt()) } -<% if (it.kotlin) { %> - withType { - kotlinOptions.jvmTarget = java.targetCompatibility.toString() - } -<% } %> java { // Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task // if it is present. From 66851cc289559eb120a8a4c845f1348e5b00241c Mon Sep 17 00:00:00 2001 From: KosmX Date: Mon, 29 May 2023 20:55:02 +0200 Subject: [PATCH 8/8] Update scripts/src/lib/Template.svelte Co-authored-by: Juuz <6596629+Juuxel@users.noreply.github.com> --- scripts/src/lib/Template.svelte | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/src/lib/Template.svelte b/scripts/src/lib/Template.svelte index bdb4afc2..1843f30e 100644 --- a/scripts/src/lib/Template.svelte +++ b/scripts/src/lib/Template.svelte @@ -250,7 +250,7 @@

- Use the Kotlin DSL for the Gradle build script. This option is only reccomended for advanced users familar with Kotlin and Gradle. This may result in slower build peformance and compability issues with some IDEs. + Use the Kotlin DSL for the Gradle build script. This option is only recommended for advanced users familiar with Kotlin and Gradle. This may result in slower build performance and compatibility issues with some IDEs.