Refactor RemapJarTask to extend from Jar

It may now be used in places that require AbstractArchiveTask such as Maven publications
This commit is contained in:
Mitchell Skaggs 2019-06-10 21:20:57 -05:00
parent a12fd8e6eb
commit 9e690f489f
No known key found for this signature in database
GPG key ID: 4EB0FECB84AE8967
3 changed files with 67 additions and 56 deletions

View file

@ -31,7 +31,10 @@ import net.fabricmc.loom.providers.MinecraftProvider;
import net.fabricmc.loom.task.RemapJarTask;
import net.fabricmc.loom.task.RemapSourcesJarTask;
import net.fabricmc.loom.util.*;
import org.gradle.api.*;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.UnknownTaskException;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.artifacts.component.ModuleComponentIdentifier;
@ -61,6 +64,10 @@ import java.util.function.Predicate;
public class AbstractPlugin implements Plugin<Project> {
protected Project project;
public static boolean isRootProject(Project project) {
return project.getRootProject() == project;
}
private void extendsFrom(String a, String b) {
project.getConfigurations().getByName(a).extendsFrom(project.getConfigurations().getByName(b));
}
@ -121,8 +128,8 @@ public class AbstractPlugin implements Plugin<Project> {
extendsFrom(Constants.MINECRAFT_NAMED, Constants.MINECRAFT_DEPENDENCIES);
extendsFrom(Constants.MINECRAFT_INTERMEDIARY, Constants.MINECRAFT_DEPENDENCIES);
extendsFrom("compile", Constants.MAPPINGS);
extendsFrom("annotationProcessor", Constants.MAPPINGS);
extendsFrom("compile", Constants.MAPPINGS);
extendsFrom("annotationProcessor", Constants.MAPPINGS);
configureIDEs();
configureCompile();
@ -133,7 +140,7 @@ public class AbstractPlugin implements Plugin<Project> {
Set<Task> taskSet = entry.getValue();
for (Task task : taskSet) {
if (task instanceof JavaCompile
&& !(task.getName().contains("Test")) && !(task.getName().contains("test"))) {
&& !(task.getName().contains("Test")) && !(task.getName().contains("test"))) {
JavaCompile javaCompileTask = (JavaCompile) task;
javaCompileTask.doFirst(task1 -> {
project.getLogger().lifecycle(":setting java compiler args");
@ -161,8 +168,8 @@ public class AbstractPlugin implements Plugin<Project> {
* Permit to add a Maven repository to a target project
*
* @param target The garget project
* @param name The name of the repository
* @param url The URL of the repository
* @param name The name of the repository
* @param url The URL of the repository
* @return An object containing the name and the URL of the repository that can be modified later
*/
public MavenArtifactRepository addMavenRepo(Project target, final String name, final String url) {
@ -293,7 +300,7 @@ public class AbstractPlugin implements Plugin<Project> {
project1.getTasks().getByName("idea").finalizedBy(project1.getTasks().getByName("genIdeaWorkspace"));
project1.getTasks().getByName("eclipse").finalizedBy(project1.getTasks().getByName("genEclipseRuns"));
if(extension.autoGenIDERuns && isRootProject(project1)){
if (extension.autoGenIDERuns && isRootProject(project1)) {
SetupIntelijRunConfigs.setup(project1);
}
@ -302,15 +309,15 @@ public class AbstractPlugin implements Plugin<Project> {
AbstractArchiveTask jarTask = (AbstractArchiveTask) project1.getTasks().getByName("jar");
RemapJarTask remapJarTask = (RemapJarTask) project1.getTasks().findByName("remapJar");
if (remapJarTask.getInput() == null) {
remapJarTask.setOutput(jarTask.getArchivePath());
jarTask.setClassifier("dev");
remapJarTask.setInput(jarTask.getArchivePath());
assert remapJarTask != null;
if (!remapJarTask.getInput().isPresent()) {
remapJarTask.getArchiveClassifier().set("remapped");
remapJarTask.getInput().set(jarTask.getArchiveFile());
}
remapJarTask.setAddNestedDependencies(true);
remapJarTask.getAddNestedDependencies().set(true);
remapJarTask.doLast(task -> project1.getArtifacts().add("archives", remapJarTask.getOutput()));
remapJarTask.doLast(task -> project1.getArtifacts().add("archives", remapJarTask.getArchiveFile()));
remapJarTask.dependsOn(project1.getTasks().getByName("jar"));
project1.getTasks().getByName("build").dependsOn(remapJarTask);
@ -318,7 +325,7 @@ public class AbstractPlugin implements Plugin<Project> {
for (Map.Entry<Project, Set<Task>> entry : taskMap.entrySet()) {
Set<Task> taskSet = entry.getValue();
for (Task task : taskSet) {
if (task instanceof RemapJarTask && ((RemapJarTask) task).isAddNestedDependencies()) {
if (task instanceof RemapJarTask && ((RemapJarTask) task).getAddNestedDependencies().get()) {
//Run all the sub project remap jars tasks before the root projects jar, this is to allow us to include projects
NestedJars.getRequiredTasks(project1).forEach(task::dependsOn);
}
@ -394,8 +401,4 @@ public class AbstractPlugin implements Plugin<Project> {
}
});
}
public static boolean isRootProject(Project project){
return project.getRootProject() == project;
}
}

View file

@ -26,7 +26,6 @@ package net.fabricmc.loom.task;
import net.fabricmc.loom.LoomGradleExtension;
import net.fabricmc.loom.providers.MappingsProvider;
import net.fabricmc.loom.util.Constants;
import net.fabricmc.loom.util.MixinRefmapHelper;
import net.fabricmc.loom.util.NestedJars;
import net.fabricmc.loom.util.TinyRemapperMappingsHelper;
@ -34,29 +33,36 @@ import net.fabricmc.tinyremapper.OutputConsumerPath;
import net.fabricmc.tinyremapper.TinyRemapper;
import net.fabricmc.tinyremapper.TinyUtils;
import org.gradle.api.Project;
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.OutputFile;
import org.gradle.api.tasks.TaskAction;
import org.gradle.jvm.tasks.Jar;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.LinkedHashSet;
import java.util.Set;
public class RemapJarTask extends AbstractLoomTask {
private Object input;
private Object output;
private boolean addNestedDependencies;
public class RemapJarTask extends Jar {
private RegularFileProperty input;
private Property<Boolean> addNestedDependencies;
public RemapJarTask() {
super();
input = getProject().getObjects().fileProperty();
addNestedDependencies = getProject().getObjects().property(Boolean.class);
}
@TaskAction
public void doTask() throws Throwable {
Project project = getProject();
LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class);
Path input = getInput().toPath();
Path output = getOutput().toPath();
Path input = this.getInput().getAsFile().get().toPath();
Path output = this.getArchiveFile().get().getAsFile().toPath();
if (!Files.exists(input)) {
throw new FileNotFoundException(input.toString());
@ -67,9 +73,9 @@ public class RemapJarTask extends AbstractLoomTask {
String fromM = "named";
String toM = "intermediary";
Set<File> classpathFiles = new LinkedHashSet<>();
//noinspection CollectionAddAllCanBeReplacedWithConstructor
classpathFiles.addAll(project.getConfigurations().getByName("compileClasspath").getFiles());
Set<File> classpathFiles = new LinkedHashSet<>(
project.getConfigurations().getByName("compileClasspath").getFiles()
);
Path[] classpath = classpathFiles.stream().map(File::toPath).filter((p) -> !input.equals(p)).toArray(Path[]::new);
File mixinMapFile = mappingsProvider.MAPPINGS_MIXIN_EXPORT;
@ -111,7 +117,7 @@ public class RemapJarTask extends AbstractLoomTask {
project.getLogger().debug("Transformed mixin reference maps in output JAR!");
}
if (addNestedDependencies) {
if (getAddNestedDependencies().get()) {
if (NestedJars.addNestedJars(project, output)) {
project.getLogger().debug("Added nested jar paths to mod json");
}
@ -119,29 +125,25 @@ public class RemapJarTask extends AbstractLoomTask {
extension.addUnmappedMod(input);
/**
*
/*try {
if (modJar.exists()) {
Files.move(modJar, modJarUnmappedCopy);
extension.addUnmappedMod(modJarUnmappedCopy);
}
try {
if (modJar.exists()) {
Files.move(modJar, modJarUnmappedCopy);
extension.addUnmappedMod(modJarUnmappedCopy);
}
Files.move(modJarOutput, modJar);
} catch (IOException e) {
throw new RuntimeException(e);
}
*/
Files.move(modJarOutput, modJar);
} catch (IOException e) {
throw new RuntimeException(e);
}*/
}
//@formatter:off
// the null-check in getInput() is done to allow reconfiguration by AbstractPlugin
@InputFile public File getInput() { return input == null ? null : getProject().file(input); }
@OutputFile public File getOutput() { return getProject().file(output); }
@Input public boolean isAddNestedDependencies() { return addNestedDependencies; }
public void setAddNestedDependencies(boolean value) { this.addNestedDependencies = value; }
public void setInput(Object input) { this.input = input; }
public void setOutput(Object output) { this.output = output; }
//@formatter:on
@InputFile
public RegularFileProperty getInput() {
return input;
}
@Input
public Property<Boolean> getAddNestedDependencies() {
return addNestedDependencies;
}
}

View file

@ -28,8 +28,10 @@ import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import net.fabricmc.loom.LoomGradleExtension;
import net.fabricmc.loom.task.RemapJarTask;
import org.apache.commons.io.FileUtils;
import org.gradle.api.Project;
import org.gradle.api.Task;
@ -47,7 +49,11 @@ import org.zeroturnaround.zip.transform.ZipEntryTransformerEntry;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.zip.ZipEntry;
public class NestedJars {
@ -101,7 +107,7 @@ public class NestedJars {
for (Task task : remapJarTasks.isEmpty() ? jarTasks : remapJarTasks) {
if (task instanceof RemapJarTask) {
fileList.add(((RemapJarTask) task).getOutput());
fileList.add(((RemapJarTask) task).getArchiveFile().get().getAsFile());
} else if (task instanceof AbstractArchiveTask) {
fileList.add(((AbstractArchiveTask) task).getArchivePath());
}