diff --git a/scripts/src/lib/Template.svelte b/scripts/src/lib/Template.svelte index fb5e620e..b3bb17c8 100644 --- a/scripts/src/lib/Template.svelte +++ b/scripts/src/lib/Template.svelte @@ -9,6 +9,7 @@ let projectName = "Template Mod"; let packageName = "com.example"; let useKotlin = false; + let useKotlinGradle = false; let dataGeneration = false; let splitSources = true; @@ -53,6 +54,7 @@ projectName, packageName, useKotlin, + useKotlinGradle, dataGeneration: dataGeneration && supportsDataGen, splitSources: splitSources && supportsSplitSources, }; @@ -179,6 +181,16 @@

+
+
+ + +
+

+ 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. +

+
+ {#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 a5ba86e8..2d5f676d 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, type GameVersion, getGameVersions } from '../Api'; import { addModJson } from './modjson'; import { addGitFiles } from './git'; @@ -22,6 +23,7 @@ export interface Configuration { projectName: string, packageName: string, useKotlin: boolean, + useKotlinGradle: boolean, dataGeneration: boolean, splitSources: boolean, } @@ -56,7 +58,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..1fb7ca02 --- /dev/null +++ b/scripts/src/lib/template/templates/gradle/kotlin/build.gradle.kts.eta @@ -0,0 +1,113 @@ +plugins { + id("fabric-loom") version "1.2-SNAPSHOT" + 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.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. + // 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("<%= it.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.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.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.property("fabric_version")}") +} + +<% if (it.kotlin) { %> +kotlin { + jvmToolchain(java.targetCompatibility.majorVersion.toInt()) +} +<% } %> +tasks { + processResources { + inputs.property("version", project.version) + + filesMatching("fabric.mod.json") { + expand("version" to project.version) + } + } + + withType { + options.release.set(java.targetCompatibility.majorVersion.toInt()) + } + java { + // 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.get()}" } + } + } +} + +// 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..e1d9b9c4 --- /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() + } +}