Skip to content

Commit

Permalink
replace MappingTask::getTaskByType with MappingsTask::getTaskNamed; t…
Browse files Browse the repository at this point in the history
…asks should not be retreived by type alone, adding a new task with the same type would break things

(most calls to MappingsTask::getTaskNamed will probably be eliminated when configuration is moved to MappingsPlugin)
the thisening
the abstractening
the finalening
the line wrappening
  • Loading branch information
supersaiyansubtlety committed Sep 28, 2024
1 parent 3158034 commit 0b2b23f
Show file tree
Hide file tree
Showing 45 changed files with 1,000 additions and 900 deletions.
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ clean.doFirst {

tasks.build.dependsOn "compressTiny", "tinyJar", "v2UnmergedMappingsJar", "v2MergedMappingsJar"

task insertAutoGeneratedMappings(dependsOn: [buildMappingsTiny, downloadPerVersionMappings], type: AddProposedMappingsTask) {
inputJar.set mappings.fileConstants.perVersionMappingsJar
inputMappings.set buildMappingsTiny.outputMappings
}
// task insertAutoGeneratedMappings(dependsOn: [buildMappingsTiny, downloadPerVersionMappings], type: AddProposedMappingsTask) {
// inputJar.set mappings.fileConstants.perVersionMappingsJar
// inputMappings.set buildMappingsTiny.outputMappings
// }

combineUnpickDefinitions {
input = mappings.fileConstants.unpickDefinitions
Expand Down
18 changes: 13 additions & 5 deletions buildSrc/src/main/java/quilt/internal/MappingsPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,17 @@ public void apply(Project project) {
tasks.create(MergeJarsTask.TASK_NAME, MergeJarsTask.class);
tasks.create(DownloadMinecraftLibrariesTask.TASK_NAME, DownloadMinecraftLibrariesTask.class);

tasks.create(DownloadPerVersionMappingsTask.TASK_NAME, DownloadPerVersionMappingsTask.class);
final var downloadPerVersionMappings =
tasks.create(DownloadPerVersionMappingsTask.TASK_NAME, DownloadPerVersionMappingsTask.class);
tasks.create(InvertPerVersionMappingsTask.TASK_NAME, InvertPerVersionMappingsTask.class);
tasks.create(BuildMappingsTinyTask.TASK_NAME, BuildMappingsTinyTask.class);
final var buildMappingsTiny = tasks.create(BuildMappingsTinyTask.TASK_NAME, BuildMappingsTinyTask.class);
tasks.create(INSERT_AUTO_GENERATED_MAPPINGS_TASK_NAME, AddProposedMappingsTask.class, task -> {
task.dependsOn(downloadPerVersionMappings);

task.getInputJar().set(ext.getFileConstants().perVersionMappingsJar);
task.getInputMappings().set(buildMappingsTiny.getOutputMappings());
});

tasks.create(MergeTinyTask.TASK_NAME, MergeTinyTask.class);
tasks.create(MergeTinyV2Task.TASK_NAME, MergeTinyV2Task.class);
tasks.create(TinyJarTask.TASK_NAME, TinyJarTask.class);
Expand All @@ -75,10 +83,10 @@ public void apply(Project project) {

tasks.create(GeneratePackageInfoMappingsTask.TASK_NAME, GeneratePackageInfoMappingsTask.class);
tasks.create(DownloadDictionaryFileTask.TASK_NAME, DownloadDictionaryFileTask.class);
final var mappingLintTask = tasks.create(MappingLintTask.TASK_NAME, MappingLintTask.class);
final var mappingLint = tasks.create(MappingLintTask.TASK_NAME, MappingLintTask.class);
tasks.create(FindDuplicateMappingFilesTask.TASK_NAME, FindDuplicateMappingFilesTask.class, task -> {
task.getMappingDirectory().set(mappingLintTask.getMappingDirectory());
mappingLintTask.dependsOn(task);
task.getMappingDirectory().set(mappingLint.getMappingDirectory());
mappingLint.dependsOn(task);
});

tasks.create(CheckIntermediaryMappingsTask.TASK_NAME, CheckIntermediaryMappingsTask.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public abstract class DefaultMappingsTask extends DefaultTask implements Mapping
protected final FileConstants fileConstants;

public DefaultMappingsTask(String group) {
this.fileConstants = mappingsExt().getFileConstants();
this.fileConstants = this.mappingsExt().getFileConstants();
this.setGroup(group);
}
}
33 changes: 25 additions & 8 deletions buildSrc/src/main/java/quilt/internal/tasks/MappingsTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,54 @@
import org.gradle.api.Task;
import org.gradle.api.artifacts.VersionCatalog;
import org.gradle.api.artifacts.VersionCatalogsExtension;
import org.gradle.api.file.Directory;
import org.gradle.api.file.RegularFile;
import org.gradle.api.provider.Provider;
import quilt.internal.MappingsExtension;
import quilt.internal.MappingsPlugin;
import quilt.internal.util.DownloadImmediate;

// TODO check if tasks that depend on other other tasks' outputs have task dependencies that can be eliminated
public interface MappingsTask extends Task {
default DownloadImmediate.Builder startDownload() {
return new DownloadImmediate.Builder(this);
}

@SuppressWarnings("unchecked")
default <T extends Task> T getTaskByName(String taskName) {
return (T) getProject().getTasks().getByName(taskName);
default Task getTaskNamed(String taskName) {
return this.getProject().getTasks().getByName(taskName);
}

default <T extends Task> T getTaskByType(Class<T> taskClass) {
return getProject().getTasks().stream().filter(task -> taskClass.isAssignableFrom(task.getClass())).map(taskClass::cast).findAny().orElseThrow();
default <T extends Task> T getTaskNamed(String name, Class<T> taskClass) {
return this.getProject().getTasks().named(name, taskClass).get();
}

default RegularFile createRegularProjectFile(String path) {
return this.getProjectDirectory().file(path);
}

default Provider<RegularFile> createRegularProjectFile(Provider<? extends CharSequence> path) {
return this.getProjectDirectory().file(path);
}

private Directory getProjectDirectory() {
return this.getProject().getLayout().getProjectDirectory();
}

// TODO: replace usage of this with @DisableCachingByDefault or @UntrackedTask on task classes that use it,
// with explanations
default void outputsNeverUpToDate() {
this.getOutputs().upToDateWhen(task -> false);
}

default MappingsExtension mappingsExt() {
return MappingsPlugin.getExtension(getProject());
return MappingsPlugin.getExtension(this.getProject());
}

default VersionCatalogsExtension versionCatalogs() {
return getProject().getExtensions().getByType(VersionCatalogsExtension.class);
return this.getProject().getExtensions().getByType(VersionCatalogsExtension.class);
}

default VersionCatalog libs() {
return versionCatalogs().named("libs");
return this.versionCatalogs().named("libs");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ public AbstractHashedMergeTask(String outputMappings) {

@Override
public void mergeMappings() throws Exception {
File hashedTinyInput = this.getTaskByType(InvertPerVersionMappingsTask.class).getInvertedTinyFile();
mergeMappings(hashedTinyInput);
final File hashedTinyInput =
this.getTaskNamed(InvertPerVersionMappingsTask.TASK_NAME, InvertPerVersionMappingsTask.class)
.getInvertedTinyFile().get().getAsFile();

this.mergeMappings(hashedTinyInput);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,30 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Map;
import java.util.function.Function;

import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.TaskAction;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.VisibleForTesting;
import quilt.internal.Constants;
import quilt.internal.mappingio.CompleteInitializersVisitor;
import quilt.internal.tasks.DefaultMappingsTask;

import net.fabricmc.mappingio.MappingReader;
import net.fabricmc.mappingio.MappingVisitor;
import net.fabricmc.mappingio.adapter.MappingNsCompleter;
import net.fabricmc.mappingio.adapter.MappingSourceNsSwitch;
import net.fabricmc.mappingio.format.MappingFormat;
import net.fabricmc.mappingio.format.tiny.Tiny2FileWriter;
import net.fabricmc.mappingio.tree.MemoryMappingTree;

public abstract class AbstractTinyMergeTask extends DefaultMappingsTask {
@InputFile
protected final RegularFileProperty input;
protected abstract RegularFileProperty getInput();

@OutputFile
public File outputMappings;
public abstract RegularFileProperty getOutputMappings();

protected final String mergeName;
protected final String fillName;
Expand All @@ -43,10 +38,10 @@ public AbstractTinyMergeTask(String outputMappings, String mergeName) {

public AbstractTinyMergeTask(String outputMappings, String mergeName, String fillName) {
super(Constants.Groups.BUILD_MAPPINGS_GROUP);
this.outputMappings = new File(fileConstants.buildDir, outputMappings);
getOutputs().file(this.outputMappings);
this.getOutputMappings().convention(() -> new File(this.fileConstants.buildDir, outputMappings));
this.getOutputs().file(this.getOutputMappings());

input = getProject().getObjects().fileProperty();
this.getInput().convention(this.getProject().getObjects().fileProperty());

this.mergeName = mergeName;
this.fillName = fillName;
Expand All @@ -56,24 +51,32 @@ public AbstractTinyMergeTask(String outputMappings, String mergeName, String fil
public abstract void mergeMappings() throws Exception;

protected void mergeMappings(File mergeTinyInput) throws Exception {
File mappingsTinyInput = input.get().getAsFile();

getLogger().lifecycle(":merging {} and {}", Constants.MAPPINGS_NAME, this.mergeName);
mergeMappings(mappingsTinyInput.toPath(), mergeTinyInput.toPath(), outputMappings.toPath(),
this::getFirstVisitor, this::getPreWriteVisitor);
final File mappingsTinyInput = this.getInput().get().getAsFile();

this.getLogger().lifecycle(":merging {} and {}", Constants.MAPPINGS_NAME, this.mergeName);
mergeMappings(
mappingsTinyInput.toPath(), mergeTinyInput.toPath(),
this.getOutputMappings().get().getAsFile().toPath(),
this::getFirstVisitor, this::getPreWriteVisitor
);
}

@VisibleForTesting
public static void mergeMappings(Path mappingsTinyInput, Path mergeTinyInput, Path outputMappings,
Function<MappingVisitor, MappingVisitor> firstVisitor,
Function<MappingVisitor, MappingVisitor> preWriteVisitor) throws IOException {
MemoryMappingTree tree = new MemoryMappingTree(false); // hashed is the src namespace
final MemoryMappingTree tree = new MemoryMappingTree(false); // hashed is the src namespace
MappingReader.read(mergeTinyInput, MappingFormat.TINY_2_FILE, tree);
MappingReader.read(mappingsTinyInput, MappingFormat.TINY_2_FILE, tree);
try (Tiny2FileWriter w = new Tiny2FileWriter(Files.newBufferedWriter(outputMappings), false)) {
tree.accept(firstVisitor.apply(
new CompleteInitializersVisitor(
new MappingSourceNsSwitch(preWriteVisitor.apply(w), "official", /*Drop methods not in hashed*/ true)
new MappingSourceNsSwitch(
preWriteVisitor.apply(w),
"official",
// Drop methods not in hashed
true
)
)
));
}
Expand All @@ -86,13 +89,4 @@ protected MappingVisitor getFirstVisitor(MappingVisitor next) {
protected MappingVisitor getPreWriteVisitor(MappingVisitor writer) {
return writer;
}

public RegularFileProperty getInput() {
return input;
}

@NotNull
public File getOutputMappings() {
return outputMappings;
}
}
Loading

0 comments on commit 0b2b23f

Please sign in to comment.