Skip to content

Commit

Permalink
WIP: Use ModDevGradle instead of archloom for common and NeoForge
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Oct 1, 2024
1 parent c22538c commit 25298dc
Show file tree
Hide file tree
Showing 11 changed files with 300 additions and 58 deletions.
56 changes: 22 additions & 34 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import me.modmuss50.mpp.ReleaseType

plugins {
id("xyz.jpenilla.quiet-architectury-loom")
id("java-library")
id("net.neoforged.moddev")
id("me.modmuss50.mod-publish-plugin") version "0.7.2" apply false
}

Expand All @@ -18,23 +19,38 @@ def getGitCommit = { ->
return stdout.toString().trim()
}

Aw2AtTask.configureDefault(
getProject(),
layout.projectDirectory.file("src/main/resources/moonrise.accesswidener").getAsFile(),
sourceSets.main
)

neoForge {
neoFormVersion = "1.21.1-20240808.144430"
validateAccessTransformers = true
}

tasks.named("createMinecraftArtifacts") {
dependsOn("copyAt")
}

dependencies {
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"
// todo: check versions
compileOnly "net.fabricmc:sponge-mixin:0.13.4+mixin.0.8.5"
compileOnly "io.github.llamalad7:mixinextras-common:0.4.1"

api("ca.spottedleaf:concurrentutil:${rootProject.concurrentutil_version}")
api("org.yaml:snakeyaml:${rootProject.snakeyaml_version}")

modImplementation "me.shedaniel.cloth:cloth-config:${rootProject.cloth_version}"
// todo: does cloth publish a platform-agnostic jar in mojang mappings?
compileOnly "me.shedaniel.cloth:cloth-config-neoforge:${rootProject.cloth_version}"
}

File awFile = file("src/main/resources/moonrise.accesswidener")

allprojects {
group = rootProject.maven_group
version = rootProject.mod_version + "+" + getGitCommit()

plugins.apply("xyz.jpenilla.quiet-architectury-loom")
plugins.apply("java-library")

java {
withSourcesJar()
Expand All @@ -60,11 +76,6 @@ allprojects {
maven { url "https://maven.terraformersmc.com/releases/" }
}

dependencies {
minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings loom.officialMojangMappings()
}

// make build reproducible
tasks.withType(AbstractArchiveTask).configureEach {
preserveFileTimestamps = false
Expand All @@ -80,31 +91,12 @@ allprojects {
rename { "${it}_${rootProject.base.archivesName.get()}"}
}
}

loom {
accessWidenerPath = awFile
mixin {
useLegacyMixinAp = false
}
}
}

subprojects {
loom.mods {
main {
sourceSet("main")
sourceSet("main", project.rootProject)
}
}
loom.runs.all {
ideConfigGenerated true
// property "mixin.debug", "true"
}

plugins.apply("me.modmuss50.mod-publish-plugin")

publishMods {
file = remapJar.archiveFile
if (project.version.contains("-beta.")) {
type = ReleaseType.BETA
} else {
Expand All @@ -127,7 +119,3 @@ subprojects {
}
}
}

loom.runs.all {
ideConfigGenerated false
}
11 changes: 11 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
repositories {
gradlePluginPortal()
mavenCentral()
maven("https://maven.fabricmc.net/")
maven("https://maven.architectury.dev/")
}

dependencies {
implementation("net.fabricmc:access-widener:2.1.0")
implementation("dev.architectury:at:1.0.1")
}
129 changes: 129 additions & 0 deletions buildSrc/src/main/java/Aw2AtTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import dev.architectury.at.AccessChange;
import dev.architectury.at.AccessTransform;
import dev.architectury.at.AccessTransformSet;
import dev.architectury.at.ModifierChange;
import dev.architectury.at.io.AccessTransformFormats;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import javax.inject.Inject;
import net.fabricmc.accesswidener.AccessWidenerReader;
import net.fabricmc.accesswidener.AccessWidenerVisitor;
import org.cadixdev.bombe.type.signature.MethodSignature;
import org.gradle.api.DefaultTask;
import org.gradle.api.Project;
import org.gradle.api.file.ProjectLayout;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.tasks.CacheableTask;
import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.TaskProvider;

@CacheableTask
public abstract class Aw2AtTask extends DefaultTask {

@InputFile
@PathSensitive(PathSensitivity.NONE)
public abstract RegularFileProperty getInputFile();

@OutputFile
public abstract RegularFileProperty getOutputFile();

@Inject
public abstract ProjectLayout getLayout();

public static TaskProvider<Aw2AtTask> configureDefault(
final Project project,
final File awFile,
final SourceSet sourceSet
) {
final TaskProvider<Aw2AtTask> aw2at = project.getTasks().register("aw2at", Aw2AtTask.class, task -> {
task.getOutputFile().set(project.getLayout().getBuildDirectory().file("aw2at/files/accesstransformer.cfg"));
task.getInputFile().set(awFile);
});

final TaskProvider<CopyTask> copyTask = project.getTasks().register("copyAt", CopyTask.class, copy -> {
copy.getInputFile().set(aw2at.flatMap(Aw2AtTask::getOutputFile));
copy.getOutputDirectory().set(project.getLayout().getBuildDirectory().dir("aw2at/dir"));
copy.getDestination().set("META-INF/accesstransformer.cfg");
});

sourceSet.resources(resources -> {
resources.srcDir(copyTask.flatMap(CopyTask::getOutputDirectory));
});

return aw2at;
}

@TaskAction
public void run() {
try (final BufferedReader reader = Files.newBufferedReader(this.getInputFile().get().getAsFile().toPath())) {
final AccessTransformSet accessTransformSet = toAccessTransformSet(reader);
Files.deleteIfExists(this.getOutputFile().get().getAsFile().toPath());
Files.createDirectories(this.getOutputFile().get().getAsFile().toPath().getParent());
AccessTransformFormats.FML.write(this.getOutputFile().get().getAsFile().toPath(), accessTransformSet);
} catch (final IOException e) {
throw new RuntimeException(e);
}
}

// Below methods are heavily based on architectury-loom Aw2At class (MIT licensed)
/*
MIT License
Copyright (c) 2016 FabricMC
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

public static AccessTransformSet toAccessTransformSet(final BufferedReader reader) throws IOException {
AccessTransformSet atSet = AccessTransformSet.create();

new AccessWidenerReader(new AccessWidenerVisitor() {
@Override
public void visitClass(final String name, final AccessWidenerReader.AccessType access, final boolean transitive) {
atSet.getOrCreateClass(name).merge(toAt(access));
}

@Override
public void visitMethod(final String owner, final String name, final String descriptor, final AccessWidenerReader.AccessType access, final boolean transitive) {
atSet.getOrCreateClass(owner).mergeMethod(MethodSignature.of(name, descriptor), toAt(access));
}

@Override
public void visitField(final String owner, final String name, final String descriptor, final AccessWidenerReader.AccessType access, final boolean transitive) {
atSet.getOrCreateClass(owner).mergeField(name, toAt(access));
}
}).read(reader);

return atSet;
}

public static AccessTransform toAt(final AccessWidenerReader.AccessType access) {
return switch (access) {
case ACCESSIBLE -> AccessTransform.of(AccessChange.PUBLIC);
case EXTENDABLE, MUTABLE -> AccessTransform.of(AccessChange.PUBLIC, ModifierChange.REMOVE);
};
}
}
44 changes: 44 additions & 0 deletions buildSrc/src/main/java/CopyTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.stream.Stream;
import org.gradle.api.DefaultTask;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.OutputDirectory;
import org.gradle.api.tasks.TaskAction;

public abstract class CopyTask extends DefaultTask {

@InputFile
public abstract RegularFileProperty getInputFile();

@Input
public abstract Property<String> getDestination();

@OutputDirectory
public abstract DirectoryProperty getOutputDirectory();

@TaskAction
public void run() {
final Path outputDirPath = this.getOutputDirectory().get().getAsFile().toPath();
try {
try (final Stream<Path> walk = Files.walk(outputDirPath)) {
for (final Path path : walk.sorted(Comparator.reverseOrder()).toList()) {
Files.delete(path);
}
}

final Path destFile = outputDirPath.resolve(this.getDestination().get());
Files.createDirectories(destFile.getParent());

Files.copy(this.getInputFile().get().getAsFile().toPath(), destFile);
} catch (final IOException e) {
throw new RuntimeException(e);
}
}
}
29 changes: 26 additions & 3 deletions fabric/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id("xyz.jpenilla.quiet-architectury-loom")
id("quiet-fabric-loom")
id 'maven-publish'
id 'com.gradleup.shadow'
}
Expand All @@ -13,10 +13,15 @@ configurations.implementation {
}

dependencies {
add('shadow', project([path: ":", configuration: "namedElements"]))
runtimeOnly(project(":").sourceSets.main.output)
minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings loom.officialMojangMappings()
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"

// todo: runs are broken (cannot find aw file)
//add('shadow', project([path: ":", configuration: "namedElements"]))
//runtimeOnly(project(":").sourceSets.main.output)
libs(project(":"))

libs("ca.spottedleaf:concurrentutil:${rootProject.concurrentutil_version}")
libs("org.yaml:snakeyaml:${rootProject.snakeyaml_version}")

Expand Down Expand Up @@ -46,6 +51,7 @@ shadowJar {
}

publishMods {
file = remapJar.archiveFile
modLoaders = ["fabric"]

modrinth {
Expand All @@ -64,6 +70,23 @@ publishMods {
}
}

loom {
accessWidenerPath.set(getRootProject().file("src/main/resources/moonrise.accesswidener"))
mixin {
useLegacyMixinAp = false
}
runs.all {
ideConfigGenerated true
// property "mixin.debug", "true"
}
mods {
main {
sourceSet("main")
sourceSet("main", project.rootProject)
}
}
}

// Setup a run with lithium for compatibility testing
sourceSets.create("lithium")
configurations.create("lithium")
Expand Down
Loading

0 comments on commit 25298dc

Please sign in to comment.