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

Fixes #30 #28 #31 #32 #33 #36 and some of #35 #43

Merged
merged 8 commits into from
Apr 21, 2023
Merged
72 changes: 48 additions & 24 deletions scripts/src/lib/Template.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -39,34 +39,43 @@
$: supportsDataGen = minorMcVersion >= 17;
$: supportsSplitSources = minorMcVersion >= 19;

$: modIdErrors = computeModIdErrors(customModId);
$: modIdErrors = computeModIdErrors(modid);
$: customIdErrors = computeCustomModIdErrors(customModId);

// Ported/adapted from Loader's MetadataVerifier
function computeModIdErrors(id: string | undefined) : string[] | undefined {
if (id == undefined) {
return undefined;
}

let errorList : string[] = [];
function sharedModIdChecks(id: string, isId: boolean): string[] | undefined {
let errorList : string[] = [];

if (id.length == 0) {
return ["Modid is empty!"];
} else if (id.length == 1) {
errorList.push("Modid is only a single character! (It must be at least 2 characters long)!");
} else if (id.length > 64) {
errorList.push("Modid has more than 64 characters!");
const type = isId ? "Modid" : "Mod Name";
if (id.length == 0) {
return [`${type} is empty!`];
} else if (id.length == 1) {
errorList.push(`${type} is only a single character! (It must be at least 2 characters long)!`);
} else if (id.length > 64) {
errorList.push(`${type} has more than 64 characters!`);
}

return errorList.length === 0 ? undefined : errorList;
}

// Ported/adapted from Loader's MetadataVerifier
function computeCustomModIdErrors(id: string | undefined): string[] | undefined {
if (id === undefined) {
return undefined;
}

let errorList = sharedModIdChecks(id, true) ?? [];

const first = id.charAt(0);

if (first < 'a' || first > 'z') {
errorList.push("Modid starts with an invalid character '" + first + "' (it must belowercase a-z)");
}

var invalidChars: string[] | null = null;
let invalidChars: string[] | null = null;

for (var i = 1; i < id.length; i++) {
var c = id.charAt(i);
for (let i = 1; i < id.length; i++) {
let c = id.charAt(i);

if (c == '-' || c == '_' || ('0' <= c && c <= '9') || ('a' <= c && c <= 'z')) {
continue;
Expand All @@ -80,7 +89,7 @@
}

if (invalidChars != null) {
var error = "Modid contains invalid characters: " + invalidChars.map(value => "'" + value + "'").join(", ") + "!";
let error = "Modid contains invalid characters: " + invalidChars.map(value => "'" + value + "'").join(", ") + "!";
errorList.push(error + "!");
}

Expand All @@ -91,8 +100,16 @@
return errorList;
}

function computeModIdErrors(id: string | undefined) : string[] | undefined {
if (id === undefined) {
return undefined;
}

return sharedModIdChecks(id, customModId === undefined);
}

async function generate() {
if (modIdErrors != undefined) {
if (modIdErrors !== undefined || (customModId !== undefined && customIdErrors !== undefined)) {
return;
}

Expand All @@ -114,14 +131,14 @@
await generator.generateTemplate({
config,
writer: {
write: async (path, content) => {
zip.file(path, content);
write: async (path, content, options) => {
zip.file(path, content, options);
},
},
});

FileSaver.saveAs(
await zip.generateAsync({ type: "blob" }),
await zip.generateAsync({ type: "blob", platform: "UNIX" }),
`${modid}-template-${config.minecraftVersion}.zip`
);

Expand Down Expand Up @@ -156,18 +173,25 @@
{/if}

<input id="project-name" bind:value={projectName} />

{#if modIdErrors != undefined}
{#each modIdErrors as error}
<li style="color: red">{error}</li>
{/each}
<br>
{/if}
</div>

{#if customModId != undefined}
<div class="form-line">
<h3>Mod ID:</h3>
<hr />
<p>Enter the modid you wish to use for your mod. <a href={""} on:click|preventDefault={useDefaultModId}>Use default</a></p>
{#if modIdErrors != undefined}
{#each modIdErrors as error}
{#if customIdErrors != undefined}
{#each customIdErrors as error}
<li style="color: red">{error}</li>
{/each}
<br>
<br />
{/if}

<input id="mod-id" bind:value={customModId} />
Expand Down
4 changes: 3 additions & 1 deletion scripts/src/lib/template/gradlewrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import gitignore from './templates/git/gitignore?raw';
import workflow from './templates/git/workflow.yml?raw'

export async function addGradleWrapper({ writer }: Options) {
await writer.write('gradlew', gradlew);
await writer.write('gradlew', gradlew, {
unixPermissions: "774"
});
await writer.write('gradlew.bat', gradlewBat);
await writer.write('gradle/wrapper/gradle-wrapper.properties', gradleWrapperProperties);
await writer.write('gradle/wrapper/gradle-wrapper.jar', decode64(gradleWrapperJar));
Expand Down
5 changes: 3 additions & 2 deletions scripts/src/lib/template/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { addGroovyGradle } from './gradlegroovy';
import { getApiVersionForMinecraft, getKotlinAdapterVersions, getLoaderVersions, getMinecraftYarnVersions } from '../Api';
import { addModJson } from './modjson';
import { addGitFiles } from './git';
import type { JSZipFileOptions } from 'jszip';

export interface Options {
/**
Expand Down Expand Up @@ -45,7 +46,7 @@ export interface TemplateOptions {
}

export interface TemplateWriter {
write(path: string, content: string | ArrayBufferLike): Promise<void>
write(path: string, content: string | ArrayBufferLike, options?: JSZipFileOptions): Promise<void>
}

export async function generateTemplate(options: Options) {
Expand All @@ -58,7 +59,7 @@ export async function generateTemplate(options: Options) {
}

export function nameToModId(name: string) {
return name.toLowerCase().replace(/\s+/g, '-').replace(/[^a-za-z0-9-_]/, "");
return name.toLowerCase().replaceAll(/\s+/g, '-').replaceAll(/[^a-za-z0-9-_]/g, "");
}

async function computeConfig(options: Configuration): Promise<ComputedConfiguration> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package <%= it.package %>;
import net.fabricmc.api.ClientModInitializer;

public class <%= it.className %> implements ClientModInitializer {
@Override
public void onInitializeClient() {
// This entrypoint is suitable for setting up client-specific logic, such as rendering.
}
@Override
public void onInitializeClient() {
// This entrypoint is suitable for setting up client-specific logic, such as rendering.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package <%= it.package %>
import net.fabricmc.api.ClientModInitializer

object <%= it.className %> : ClientModInitializer {
override fun onInitializeClient() {
// This entrypoint is suitable for setting up client-specific logic, such as rendering.
}
override fun onInitializeClient() {
// This entrypoint is suitable for setting up client-specific logic, such as rendering.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import net.fabricmc.fabric.api.datagen.v1.DataGeneratorEntrypoint;
import net.fabricmc.fabric.api.datagen.v1.FabricDataGenerator;

public class <%= it.className %> implements DataGeneratorEntrypoint {
@Override
public void onInitializeDataGenerator(FabricDataGenerator fabricDataGenerator) {
@Override
public void onInitializeDataGenerator(FabricDataGenerator fabricDataGenerator) {

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import net.fabricmc.fabric.api.datagen.v1.DataGeneratorEntrypoint
import net.fabricmc.fabric.api.datagen.v1.FabricDataGenerator

object <%= it.className %> : DataGeneratorEntrypoint {
override fun onInitializeDataGenerator(fabricDataGenerator: FabricDataGenerator) {
}
override fun onInitializeDataGenerator(fabricDataGenerator: FabricDataGenerator) {
}
}
24 changes: 12 additions & 12 deletions scripts/src/lib/template/templates/entrypoint/Entrypoint.java.eta
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
<% } %>
public class <%= it.className %> implements ModInitializer {
// This logger is used to write text to the console and the log file.
// It is considered best practice to use your mod id as the logger's name.
// That way, it's clear which mod wrote info, warnings, and errors.
<% if (it.slf4j) { %> public static final Logger LOGGER = LoggerFactory.getLogger("<%= it.modid %>");
<% } else { %> public static final Logger LOGGER = LogManager.getLogger("<%= it.modid %>");<% } %>
@Override
public void onInitialize() {
// This code runs as soon as Minecraft is in a mod-load-ready state.
// However, some things (like resources) may still be uninitialized.
// Proceed with mild caution.
// This logger is used to write text to the console and the log file.
// It is considered best practice to use your mod id as the logger's name.
// That way, it's clear which mod wrote info, warnings, and errors.
<% if (it.slf4j) { %> public static final Logger LOGGER = LoggerFactory.getLogger("<%= it.modid %>");
<% } else { %> public static final Logger LOGGER = LogManager.getLogger("<%= it.modid %>");<% } %>
@Override
public void onInitialize() {
// This code runs as soon as Minecraft is in a mod-load-ready state.
// However, some things (like resources) may still be uninitialized.
// Proceed with mild caution.

LOGGER.info("Hello Fabric world!");
}
LOGGER.info("Hello Fabric world!");
}
}
16 changes: 8 additions & 8 deletions scripts/src/lib/template/templates/entrypoint/Entrypoint.kt.eta
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import net.fabricmc.api.ModInitializer
<% if (it.slf4j) { %>import org.slf4j.LoggerFactory
<% } else { %>import org.apache.logging.log4j.LogManager<% } %>
object <%= it.className %> : ModInitializer {
<% if (it.slf4j) { %> private val logger = LoggerFactory.getLogger("<%= it.modid %>")
<% } else { %> private val logger = LogManager.getLogger("<%= it.modid %>")<% } %>
override fun onInitialize() {
// This code runs as soon as Minecraft is in a mod-load-ready state.
// However, some things (like resources) may still be uninitialized.
// Proceed with mild caution.
logger.info("Hello Fabric world!")
}
<% if (it.slf4j) { %> private val logger = LoggerFactory.getLogger("<%= it.modid %>")
<% } else { %> private val logger = LogManager.getLogger("<%= it.modid %>")<% } %>
override fun onInitialize() {
// This code runs as soon as Minecraft is in a mod-load-ready state.
// However, some things (like resources) may still be uninitialized.
// Proceed with mild caution.
logger.info("Hello Fabric world!")
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
plugins {
id 'fabric-loom' version '1.1-SNAPSHOT'
id 'maven-publish'
<% if (it.kotlin) { %>id "org.jetbrains.kotlin.jvm" version "<%= it.kotlin.kotlinVersion %>"<% } %>
<%_ if (it.kotlin) { %>
id "org.jetbrains.kotlin.jvm" version "<%= it.kotlin.kotlinVersion %>"
<%_ } %>
}

sourceCompatibility = JavaVersion.<%= it.java.compatibility %>
Expand All @@ -23,7 +25,7 @@ loom {
<% if (it.splitSources) { %> splitEnvironmentSourceSets()

mods {
modid {
"<%= it.modid %>" {
sourceSet sourceSets.main
sourceSet sourceSets.client
}
Expand Down