refactor genSources into multiple reusable tasks

dev/0.11
asie 2019-05-10 11:48:07 +02:00
parent 7e59a90463
commit eff108344f
5 changed files with 163 additions and 43 deletions

View File

@ -33,6 +33,7 @@ import net.fabricmc.loom.util.LineNumberRemapper;
import net.fabricmc.loom.util.progress.ProgressLogger;
import net.fabricmc.stitch.util.StitchUtil;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.tasks.TaskContainer;
import java.io.File;
@ -65,50 +66,52 @@ public class LoomGradlePlugin extends AbstractPlugin {
tasks.register("remapJar", RemapJar.class);
tasks.register("genSources", FernFlowerTask.class, t -> {
tasks.register("genSourcesDecompile", FernFlowerTask.class, t -> {
t.getOutputs().upToDateWhen((o) -> false);
});
tasks.register("genSourcesRemapLineNumbers", RemapLineNumbersTask.class, t -> {
t.getOutputs().upToDateWhen((o) -> false);
});
tasks.register("genSources", t -> {
t.getOutputs().upToDateWhen((o) -> false);
});
project.afterEvaluate((p) -> {
FernFlowerTask task = (FernFlowerTask) p.getTasks().getByName("genSources");
AbstractDecompileTask decompileTask = (AbstractDecompileTask) p.getTasks().getByName("genSourcesDecompile");
RemapLineNumbersTask remapLineNumbersTask = (RemapLineNumbersTask) p.getTasks().getByName("genSourcesRemapLineNumbers");
Task genSourcesTask = p.getTasks().getByName("genSources");
genSourcesTask.dependsOn(remapLineNumbersTask);
remapLineNumbersTask.dependsOn(decompileTask);
Project project = this.getProject();
LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class);
MinecraftLibraryProvider libraryProvider = extension.getMinecraftProvider().libraryProvider;
MappingsProvider mappingsProvider = extension.getMappingsProvider();
File mappedJar = mappingsProvider.mappedProvider.getMappedJar();
File linemappedJarTmp = getMappedByproduct(project, "-linemapped.jar.tmp");
File linemappedJar = getMappedByproduct(project, "-linemapped.jar");
File sourcesJar = getMappedByproduct(project, "-sources.jar");
File linemapFile = getMappedByproduct(project, "-sources.lmap");
task.setInput(mappedJar);
task.setOutput(sourcesJar);
task.setLineMapFile(linemapFile);
task.setLibraries(libraryProvider.getLibraries());
decompileTask.setInput(mappedJar);
decompileTask.setOutput(sourcesJar);
decompileTask.setLineMapFile(linemapFile);
decompileTask.setLibraries(libraryProvider.getLibraries());
task.doLast((tt) -> {
project.getLogger().lifecycle(":adjusting line numbers");
LineNumberRemapper remapper = new LineNumberRemapper();
remapper.readMappings(linemapFile);
ProgressLogger progressLogger = ProgressLogger.getProgressFactory(project, FernFlowerTask.class.getName());
progressLogger.start("Adjusting line numbers", "linemap");
try (StitchUtil.FileSystemDelegate inFs = StitchUtil.getJarFileSystem(mappedJar, true);
StitchUtil.FileSystemDelegate outFs = StitchUtil.getJarFileSystem(linemappedJarTmp, true)) {
remapper.process(progressLogger, inFs.get().getPath("/"), outFs.get().getPath("/"));
} catch (IOException e) {
throw new RuntimeException(e);
}
progressLogger.completed();
remapLineNumbersTask.setInput(mappedJar);
remapLineNumbersTask.setLineMapFile(linemapFile);
remapLineNumbersTask.setOutput(linemappedJar);
Path mappedJarPath = mappedJar.toPath();
Path linemappedJarTmpPath = linemappedJarTmp.toPath();
Path linemappedJarPath = linemappedJar.toPath();
if (Files.exists(linemappedJarTmpPath)) {
genSourcesTask.doLast((tt) -> {
if (Files.exists(linemappedJarPath)) {
try {
Files.deleteIfExists(mappedJarPath);
Files.move(linemappedJarTmpPath, mappedJarPath);
Files.copy(linemappedJarPath, mappedJarPath);
} catch (IOException e) {
throw new RuntimeException(e);
}

View File

@ -0,0 +1,49 @@
/*
* This file is part of fabric-loom, licensed under the MIT License (MIT).
*
* Copyright (c) 2016, 2017, 2018 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.
*/
package net.fabricmc.loom.task;
import org.gradle.api.file.FileCollection;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.OutputFile;
import java.io.File;
public abstract class AbstractDecompileTask extends DefaultLoomTask {
private Object input;
private Object output;
private Object lineMapFile;
private Object libraries;
//@formatter:off
@Input public File getInput() { return getProject().file(input); }
@OutputFile public File getOutput() { return getProject().file(output); }
@OutputFile public File getLineMapFile() { return getProject().file(lineMapFile); }
@Input public FileCollection getLibraries() { return getProject().files(libraries); }
public void setInput(Object input) { this.input = input; }
public void setOutput(Object output) { this.output = output; }
public void setLineMapFile(Object lineMapFile) { this.lineMapFile = lineMapFile; }
public void setLibraries(Object libraries) { this.libraries = libraries; }
//@formatter:on
}

View File

@ -0,0 +1,77 @@
/*
* This file is part of fabric-loom, licensed under the MIT License (MIT).
*
* Copyright (c) 2016, 2017, 2018 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.
*/
package net.fabricmc.loom.task;
import net.fabricmc.loom.task.fernflower.FernFlowerTask;
import net.fabricmc.loom.util.LineNumberRemapper;
import net.fabricmc.loom.util.progress.ProgressLogger;
import net.fabricmc.stitch.util.StitchUtil;
import org.gradle.api.Project;
import org.gradle.api.file.FileCollection;
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 java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class RemapLineNumbersTask extends DefaultLoomTask {
private Object input;
private Object output;
private Object lineMapFile;
@TaskAction
public void doTask() throws Throwable {
Project project = getProject();
project.getLogger().lifecycle(":adjusting line numbers");
LineNumberRemapper remapper = new LineNumberRemapper();
remapper.readMappings(getLineMapFile());
ProgressLogger progressLogger = ProgressLogger.getProgressFactory(project, FernFlowerTask.class.getName());
progressLogger.start("Adjusting line numbers", "linemap");
try (StitchUtil.FileSystemDelegate inFs = StitchUtil.getJarFileSystem(getInput(), true);
StitchUtil.FileSystemDelegate outFs = StitchUtil.getJarFileSystem(getOutput(), true)) {
remapper.process(progressLogger, inFs.get().getPath("/"), outFs.get().getPath("/"));
} catch (IOException e) {
throw new RuntimeException(e);
}
progressLogger.completed();
}
//@formatter:off
@Input public File getInput() { return getProject().file(input); }
@InputFile public File getLineMapFile() { return getProject().file(lineMapFile); }
@OutputFile public File getOutput() { return getProject().file(output); }
public void setInput(Object input) { this.input = input; }
public void setLineMapFile(Object lineMapFile) { this.lineMapFile = lineMapFile; }
public void setOutput(Object output) { this.output = output; }
//@formatter:on
}

View File

@ -24,7 +24,7 @@
package net.fabricmc.loom.task.fernflower;
import net.fabricmc.loom.task.DefaultLoomTask;
import net.fabricmc.loom.task.AbstractDecompileTask;
import net.fabricmc.loom.task.ForkingJavaExecTask;
import net.fabricmc.loom.util.ConsumingOutputStream;
import org.gradle.api.file.FileCollection;
@ -48,13 +48,9 @@ import static java.text.MessageFormat.format;
/**
* Created by covers1624 on 9/02/19.
*/
public class FernFlowerTask extends DefaultLoomTask implements ForkingJavaExecTask {
public class FernFlowerTask extends AbstractDecompileTask implements ForkingJavaExecTask {
private boolean noFork;
private Object input;
private Object output;
private Object lineMapFile;
private Object libraries;
private boolean noFork = false;
private int numThreads = Runtime.getRuntime().availableProcessors();
@TaskAction
@ -70,7 +66,9 @@ public class FernFlowerTask extends DefaultLoomTask implements ForkingJavaExecTa
options.forEach((k, v) -> args.add(format("-{0}={1}", k, v)));
args.add(getInput().getAbsolutePath());
args.add("-o=" + getOutput().getAbsolutePath());
if (getLineMapFile() != null) {
args.add("-l=" + getLineMapFile().getAbsolutePath());
}
args.add("-t=" + getNumThreads());
//TODO, Decompiler breaks on jemalloc, J9 module-info.class?
@ -135,16 +133,8 @@ public class FernFlowerTask extends DefaultLoomTask implements ForkingJavaExecTa
}
//@formatter:off
@Input public File getInput() { return getProject().file(input); }
@OutputFile public File getOutput() { return getProject().file(output); }
@OutputFile public File getLineMapFile() { return getProject().file(lineMapFile); }
@Input public FileCollection getLibraries() { return getProject().files(libraries); }
@Input public int getNumThreads() { return numThreads; }
public boolean isNoFork() { return noFork; }
public void setInput(Object input) { this.input = input; }
public void setOutput(Object output) { this.output = output; }
public void setLineMapFile(Object lineMapFile) { this.lineMapFile = lineMapFile; }
public void setLibraries(Object libraries) { this.libraries = libraries; }
public void setNoFork(boolean noFork) { this.noFork = noFork; }
public void setNumThreads(int numThreads) { this.numThreads = numThreads; }
//@formatter:on

View File

@ -42,5 +42,6 @@ public class Constants {
public static final String MINECRAFT_DEPENDENCIES = "minecraftLibraries";
public static final String MINECRAFT_INTERMEDIARY = "minecraftIntermediary";
public static final String MINECRAFT_NAMED = "minecraftNamed";
public static final String MINECRAFT_LINEMAPPED = "minecraftLinemapped";
public static final String MAPPINGS = "mappings";
}