Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Kotlin DSL template #23

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
13 changes: 13 additions & 0 deletions scripts/src/lib/Template.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
let projectName = "Template Mod";
let packageName = "com.example";
let useKotlin = false;
let useKotlinGradle = false;
let dataGeneration = false;
let splitSources = true;

Expand Down Expand Up @@ -48,6 +49,7 @@
projectName,
packageName,
useKotlin,
useKotlinGradle,
dataGeneration: dataGeneration && supportsDataGen,
splitSources: splitSources && supportsSplitSources,
};
Expand Down Expand Up @@ -131,6 +133,17 @@
</p>
</div>

<div>
<div class="option-container">
<input id="kotlinGradle" type="checkbox" class="option-input" bind:checked={useKotlinGradle} />
<label for="kotlinGradle" class="option-label">Gradle Kotlin DSL</label>
</div>
<p class="option-body">
Use <a href="https://docs.gradle.org/current/userguide/kotlin_dsl.html">Gradle Kotlin DSL</a> instead of Groovy.
This will generate <code>build.gradle.kts</code> and <code>settings.gradle.kts</code> files.
KosmX marked this conversation as resolved.
Show resolved Hide resolved
</p>
</div>

{#if supportsDataGen}
<div>
<div class="option-container">
Expand Down
13 changes: 13 additions & 0 deletions scripts/src/lib/template/gradlekotlin.ts
Original file line number Diff line number Diff line change
@@ -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);
}
8 changes: 7 additions & 1 deletion scripts/src/lib/template/template.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -21,6 +22,7 @@ export interface Configuration {
projectName: string,
packageName: string,
useKotlin: boolean,
useKotlinGradle: boolean,
dataGeneration: boolean,
splitSources: boolean,
}
Expand Down Expand Up @@ -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);
}
Expand Down
116 changes: 116 additions & 0 deletions scripts/src/lib/template/templates/gradle/kotlin/build.gradle.kts.eta
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
plugins {
id("fabric-loom") version "1.0-SNAPSHOT"
id("maven-publish")
<% if (it.kotlin) { %>kotlin("jvm") version "<%= it.kotlin.kotlinVersion %>"<% } %>
}

KosmX marked this conversation as resolved.
Show resolved Hide resolved
base.archivesName.set(project.properties["archives_base_name"] as String)
version = project.properties["mod_version"] as String
group = project.properties["maven_group"] as String
KosmX marked this conversation as resolved.
Show resolved Hide resolved

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") {
KosmX marked this conversation as resolved.
Show resolved Hide resolved
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"]}")
KosmX marked this conversation as resolved.
Show resolved Hide resolved
}

tasks {
processResources {
inputs.property("version", project.version)
filteringCharset = "UTF-8"
KosmX marked this conversation as resolved.
Show resolved Hide resolved

filesMatching("fabric.mod.json") {
expand(mapOf("version" to project.version))
KosmX marked this conversation as resolved.
Show resolved Hide resolved
}
}

// Minecraft 1.18 (1.18-pre2) upwards uses Java 17.
KosmX marked this conversation as resolved.
Show resolved Hide resolved
val targetJavaVersion = <%= it.java.release %>
KosmX marked this conversation as resolved.
Show resolved Hide resolved
withType<JavaCompile> {
options.encoding = "UTF-8"
KosmX marked this conversation as resolved.
Show resolved Hide resolved
options.release.set(targetJavaVersion)
}

<% if (it.kotlin) { %> withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions.jvmTarget = targetJavaVersion.toString()
}<% } %>

java {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
java {
java {

toolchain.languageVersion.set(JavaLanguageVersion.of(JavaVersion.toVersion(targetJavaVersion).toString()))
KosmX marked this conversation as resolved.
Show resolved Hide resolved

// 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}" }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
rename { "${it}_${base.archivesName}" }
rename { "${it}_${base.archivesName.get()}" }

archivesName is a Property<String>

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done. but it worked without get()

}
}
}

// configure the maven publication
publishing {
publications {
create<MavenPublication>("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.
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pluginManagement {
repositories {
maven("https://maven.fabricmc.net/") {
name= "Fabric"
KosmX marked this conversation as resolved.
Show resolved Hide resolved
}
mavenCentral()
gradlePluginPortal()
}
}