Skip to content

Commit

Permalink
Build script improvements (#3493)
Browse files Browse the repository at this point in the history
* Move more to filament

* Fix build

* Mabye fix

* Update enigma

* Fix config caching warning.

* Update engima again

* Enable configuration cache

* Update gradle

* Update loom

* Fixes and improvements.

* Small cleanup

* Fix typo
  • Loading branch information
modmuss50 authored Mar 25, 2023
1 parent 2b9d67d commit bb89009
Show file tree
Hide file tree
Showing 28 changed files with 651 additions and 462 deletions.
438 changes: 162 additions & 276 deletions build.gradle

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions filament/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,18 @@ dependencies {
implementation "org.ow2.asm:asm:${properties.asm_version}"
implementation "org.ow2.asm:asm-tree:${properties.asm_version}"
implementation "cuchaz:enigma:$properties.enigma_version"
implementation "cuchaz:enigma-cli:$properties.enigma_version"
implementation "net.fabricmc.unpick:unpick:$properties.unpick_version"
implementation "net.fabricmc.unpick:unpick-format-utils:$properties.unpick_version"
implementation "net.fabricmc.unpick:unpick-cli:$properties.unpick_version"
implementation "net.fabricmc:tiny-mappings-parser:$properties.tiny_mappings_parser_version"
implementation "net.fabricmc:tiny-remapper:$properties.tiny_remapper_version"
implementation "net.fabricmc:mappingpoet:$properties.mappingpoet_version"
implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.4.2'

// Contains a number of useful utilities we can re-use.
implementation ("net.fabricmc:fabric-loom:1.0.7") {
transitive = true
implementation ("net.fabricmc:fabric-loom:1.1.10") {
transitive = false
}

implementation ('net.fabricmc:stitch:0.6.2') {
Expand Down
2 changes: 1 addition & 1 deletion filament/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
filament_version=0.4.0
filament_version=0.5.0
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
import javax.inject.Inject;

import org.gradle.api.Project;
import org.gradle.api.file.Directory;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.RegularFile;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.Provider;

import net.fabricmc.filament.util.MinecraftVersionMetaHelper;
import net.fabricmc.loom.configuration.providers.minecraft.MinecraftVersionMeta;

public abstract class FilamentExtension {
public static FilamentExtension get(Project project) {
Expand All @@ -19,17 +26,31 @@ public static FilamentExtension get(Project project) {

public abstract Property<String> getMinecraftVersionManifestUrl();

private final MinecraftVersionMetaHelper metaHelper;
private final Provider<MinecraftVersionMeta> metaProvider;

@Inject
public FilamentExtension() {
getMinecraftVersion().finalizeValueOnRead();
getMinecraftVersionManifestUrl().convention("https://piston-meta.mojang.com/mc/game/version_manifest_v2.json").finalizeValueOnRead();

metaHelper = getProject().getObjects().newInstance(MinecraftVersionMetaHelper.class, this);
metaProvider = getProject().provider(metaHelper::setup);
}

public DirectoryProperty getCacheDirectory() {
return getProject().getObjects().directoryProperty().fileValue(new File(getProject().getRootDir(), ".gradle/filament"));
}

public Provider<Directory> getMinecraftDirectory() {
return getCacheDirectory().dir(getMinecraftVersion());
}

public File getCacheDirectory() {
return new File(getProject().getRootDir(), ".gradle/filament");
public Provider<RegularFile> getMinecraftFile(String filename) {
return getMinecraftDirectory().map(directory -> directory.file(filename));
}

public File getMinecraftDirectory() {
return new File(getCacheDirectory(), getMinecraftVersion().get());
public Provider<MinecraftVersionMeta> getMinecraftVersionMetadata() {
return metaProvider;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package net.fabricmc.filament;

import java.io.File;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.artifacts.dsl.DependencyHandler;
import org.gradle.api.file.RegularFile;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.Delete;
import org.gradle.api.tasks.TaskContainer;
import org.gradle.api.tasks.TaskProvider;

Expand All @@ -16,12 +17,11 @@
import net.fabricmc.filament.task.GeneratePackageInfoMappingsTask;
import net.fabricmc.filament.task.JavadocLintTask;
import net.fabricmc.filament.task.RemapUnpickDefinitionsTask;
import net.fabricmc.filament.task.base.FileOutputTask;
import net.fabricmc.filament.task.base.WithFileOutput;
import net.fabricmc.filament.task.minecraft.ExtractBundledServerTask;
import net.fabricmc.filament.task.minecraft.MergeMinecraftTask;
import net.fabricmc.filament.task.minecraft.MinecraftLibrariesTask;
import net.fabricmc.filament.task.minecraft.MinecraftVersionMetaTask;
import net.fabricmc.loom.configuration.providers.minecraft.MinecraftVersionMeta;
import net.fabricmc.loom.util.gradle.GradleUtils;

public final class FilamentGradlePlugin implements Plugin<Project> {
public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Expand All @@ -31,33 +31,32 @@ public void apply(Project project) {
final FilamentExtension extension = project.getExtensions().create("filament", FilamentExtension.class);
final TaskContainer tasks = project.getTasks();

var minecraftMetaTask = tasks.register("downloadMinecraftMeta", MinecraftVersionMetaTask.class);
var metaProvider = MinecraftVersionMetaTask.readMetadata(minecraftMetaTask);
var metaProvider = extension.getMinecraftVersionMetadata();

var minecraftClient = tasks.register("downloadMinecraftClientJar", DownloadTask.class, task -> {
Provider<MinecraftVersionMeta.Download> downloadProvider = metaProvider.map(meta -> meta.download("client"));
task.getUrl().set(downloadProvider.map(MinecraftVersionMeta.Download::url));
task.getSha1().set(downloadProvider.map(MinecraftVersionMeta.Download::sha1));

task.getOutputFile().set(new File(extension.getMinecraftDirectory(), "client.jar"));
task.getOutput().set(extension.getMinecraftFile("client.jar"));
});
var minecraftServer = tasks.register("downloadMinecraftServerJar", DownloadTask.class, task -> {
Provider<MinecraftVersionMeta.Download> downloadProvider = metaProvider.map(meta -> meta.download("server"));
task.getUrl().set(downloadProvider.map(MinecraftVersionMeta.Download::url));
task.getSha1().set(downloadProvider.map(MinecraftVersionMeta.Download::sha1));

task.getOutputFile().set(new File(extension.getMinecraftDirectory(), "server_bundle.jar"));
task.getOutput().set(extension.getMinecraftFile("server_bundle.jar"));
});
var extractBundledServer = tasks.register("extractBundledServer", ExtractBundledServerTask.class, task -> {
task.dependsOn(minecraftServer);
task.getServerJar().set(getOutput(minecraftServer));
task.getOutputFile().set(new File(extension.getMinecraftDirectory(), "server.jar"));
task.getInput().set(getOutput(minecraftServer));
task.getOutput().set(extension.getMinecraftFile("server.jar"));
});
tasks.register("mergeMinecraftJars", MergeMinecraftTask.class, task -> {
task.getClientJar().set(getOutput(minecraftClient));
task.getServerJar().set(getOutput(extractBundledServer));

task.getOutputFile().set(new File(extension.getMinecraftDirectory(), "merged.jar"));
task.getOutput().set(extension.getMinecraftFile("merged.jar"));
});
tasks.register("generatePackageInfoMappings", GeneratePackageInfoMappingsTask.class);
tasks.register("javadocLint", JavadocLintTask.class);
Expand All @@ -70,17 +69,28 @@ public void apply(Project project) {
task.getTargetNamespace().set("intermediary");
});

tasks.register("minecraftLibraries", MinecraftLibrariesTask.class, task -> {
task.dependsOn(minecraftMetaTask);
var cleanFilament = tasks.register("cleanFilament", Delete.class, task -> task.delete(extension.getCacheDirectory()));
tasks.named("clean", task -> task.dependsOn(cleanFilament));

var minecraftLibraries = project.getConfigurations().register("minecraftLibraries");

GradleUtils.afterSuccessfulEvaluation(project, () -> {
var name = minecraftLibraries.getName();

var files = MinecraftVersionMetaTask.readMetadata(minecraftMetaTask)
.map(meta -> MinecraftLibrariesTask.getDependencies(project, meta))
.map(dependencies -> project.getConfigurations().detachedConfiguration(dependencies).resolve());
task.getFiles().from(files);
for (Dependency dependency : getDependencies(metaProvider.get(), project.getDependencies())) {
project.getDependencies().add(name, dependency);
}
});
}

private Provider<RegularFile> getOutput(TaskProvider<? extends FileOutputTask> taskProvider) {
return taskProvider.flatMap(FileOutputTask::getOutputFile);
private Provider<? extends RegularFile> getOutput(TaskProvider<? extends WithFileOutput> taskProvider) {
return taskProvider.flatMap(WithFileOutput::getOutput);
}

private Dependency[] getDependencies(MinecraftVersionMeta meta, DependencyHandler dependencyHandler) {
return meta.libraries().stream()
.filter(library -> library.artifact() != null)
.map(library -> dependencyHandler.create(library.name()))
.toArray(Dependency[]::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
import org.gradle.workers.WorkQueue;
import org.gradle.workers.WorkerExecutor;

import net.fabricmc.filament.task.base.FileOutputTask;
import net.fabricmc.filament.task.base.FilamentTask;
import net.fabricmc.filament.task.base.WithFileOutput;
import net.fabricmc.loom.util.download.Download;
import net.fabricmc.loom.util.download.DownloadException;

public abstract class DownloadTask extends FileOutputTask {
public abstract class DownloadTask extends FilamentTask implements WithFileOutput {
@Input
public abstract Property<String> getUrl();
@Input
Expand All @@ -26,13 +27,18 @@ public abstract class DownloadTask extends FileOutputTask {
@Inject
protected abstract WorkerExecutor getWorkerExecutor();

@Inject
public DownloadTask() {
getSha1().convention("");
}

@TaskAction
public void run() {
WorkQueue workQueue = getWorkerExecutor().noIsolation();
workQueue.submit(DownloadAction.class, parameters -> {
parameters.getUrl().set(getUrl());
parameters.getSha1().set(getSha1());
parameters.getOutput().set(getOutputFile());
parameters.getOutput().set(getOutput());
});
}

Expand All @@ -46,9 +52,16 @@ public abstract static class DownloadAction implements WorkAction<DownloadParame
@Override
public void execute() {
try {
Download.create(getParameters().getUrl().get())
.sha1(getParameters().getSha1().get())
.downloadPath(getParameters().getOutput().get().getAsFile().toPath());
var sha1 = getParameters().getSha1().get();
var download = Download.create(getParameters().getUrl().get());

if (!sha1.isEmpty()) {
download.sha1(sha1);
} else {
download.defaultCache();
}

download.downloadPath(getParameters().getOutput().get().getAsFile().toPath());
} catch (DownloadException | URISyntaxException e) {
throw new RuntimeException("Failed to download", e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package net.fabricmc.filament.task;

import java.io.IOException;
import java.nio.file.Files;

import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.TaskAction;

import net.fabricmc.filament.task.base.FilamentTask;
import net.fabricmc.filament.task.base.WithFileInput;
import net.fabricmc.filament.task.base.WithFileOutput;
import net.fabricmc.loom.util.ZipUtils;

public abstract class ExtractZipEntryTask extends FilamentTask implements WithFileInput, WithFileOutput {
@Input
public abstract Property<String> getEntry();

@TaskAction
public void run() throws IOException {
byte[] bytes = ZipUtils.unpack(getInputPath(), getEntry().get());
Files.write(getOutputPath(), bytes);
}
}
27 changes: 10 additions & 17 deletions filament/src/main/java/net/fabricmc/filament/task/MapJarTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,20 @@
import org.gradle.api.tasks.Classpath;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.TaskAction;
import org.gradle.workers.WorkAction;
import org.gradle.workers.WorkParameters;
import org.gradle.workers.WorkQueue;
import org.gradle.workers.WorkerExecutor;

import net.fabricmc.filament.task.base.WithFileInput;
import net.fabricmc.filament.task.base.WithFileOutput;
import net.fabricmc.tinyremapper.OutputConsumerPath;
import net.fabricmc.tinyremapper.TinyRemapper;
import net.fabricmc.tinyremapper.TinyUtils;

public abstract class MapJarTask extends DefaultTask {
@InputFile public abstract RegularFileProperty getInput();
public abstract class MapJarTask extends DefaultTask implements WithFileOutput, WithFileInput {
@InputFile public abstract RegularFileProperty getMappings();
@OutputFile public abstract RegularFileProperty getOutput();
@Classpath public abstract ConfigurableFileCollection getClasspath();
@Input public abstract Property<String> getFrom();
@Input public abstract Property<String> getTo();
Expand All @@ -48,8 +47,6 @@ public MapJarTask() {

@TaskAction
public void remap() {
getProject().getLogger().lifecycle(":remapping {} from {} to {}", getInput().get().getAsFile(), getFrom().get(), getTo().get());

WorkQueue workQueue = getWorkerExecutor().noIsolation();
workQueue.submit(RemapAction.class, parameters -> {
parameters.getInput().set(getInput());
Expand All @@ -63,20 +60,16 @@ public void remap() {
}

public interface RemapParameters extends WorkParameters {
@InputFile RegularFileProperty getInput();
@InputFile RegularFileProperty getMappings();
@OutputFile RegularFileProperty getOutput();
@Classpath ConfigurableFileCollection getClasspath();
@Input Property<String> getFrom();
@Input Property<String> getTo();
@Input MapProperty<String, String> getClassMappings();
RegularFileProperty getInput();
RegularFileProperty getMappings();
RegularFileProperty getOutput();
ConfigurableFileCollection getClasspath();
Property<String> getFrom();
Property<String> getTo();
MapProperty<String, String> getClassMappings();
}

public abstract static class RemapAction implements WorkAction<RemapParameters> {
@Inject
public RemapAction() {
}

private static Path getPath(Provider<? extends FileSystemLocation> provider) {
return provider.get().getAsFile().toPath();
}
Expand Down
Loading

0 comments on commit bb89009

Please sign in to comment.