Refactor RemapJarTask to extend from Jar

It may now be used in places that require AbstractArchiveTask such as Maven publications
dev/0.11
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.RemapJarTask;
import net.fabricmc.loom.task.RemapSourcesJarTask; import net.fabricmc.loom.task.RemapSourcesJarTask;
import net.fabricmc.loom.util.*; 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.Configuration;
import org.gradle.api.artifacts.Dependency; import org.gradle.api.artifacts.Dependency;
import org.gradle.api.artifacts.component.ModuleComponentIdentifier; import org.gradle.api.artifacts.component.ModuleComponentIdentifier;
@ -61,6 +64,10 @@ import java.util.function.Predicate;
public class AbstractPlugin implements Plugin<Project> { public class AbstractPlugin implements Plugin<Project> {
protected Project project; protected Project project;
public static boolean isRootProject(Project project) {
return project.getRootProject() == project;
}
private void extendsFrom(String a, String b) { private void extendsFrom(String a, String b) {
project.getConfigurations().getByName(a).extendsFrom(project.getConfigurations().getByName(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_NAMED, Constants.MINECRAFT_DEPENDENCIES);
extendsFrom(Constants.MINECRAFT_INTERMEDIARY, Constants.MINECRAFT_DEPENDENCIES); extendsFrom(Constants.MINECRAFT_INTERMEDIARY, Constants.MINECRAFT_DEPENDENCIES);
extendsFrom("compile", Constants.MAPPINGS); extendsFrom("compile", Constants.MAPPINGS);
extendsFrom("annotationProcessor", Constants.MAPPINGS); extendsFrom("annotationProcessor", Constants.MAPPINGS);
configureIDEs(); configureIDEs();
configureCompile(); configureCompile();
@ -133,7 +140,7 @@ public class AbstractPlugin implements Plugin<Project> {
Set<Task> taskSet = entry.getValue(); Set<Task> taskSet = entry.getValue();
for (Task task : taskSet) { for (Task task : taskSet) {
if (task instanceof JavaCompile if (task instanceof JavaCompile
&& !(task.getName().contains("Test")) && !(task.getName().contains("test"))) { && !(task.getName().contains("Test")) && !(task.getName().contains("test"))) {
JavaCompile javaCompileTask = (JavaCompile) task; JavaCompile javaCompileTask = (JavaCompile) task;
javaCompileTask.doFirst(task1 -> { javaCompileTask.doFirst(task1 -> {
project.getLogger().lifecycle(":setting java compiler args"); 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 * Permit to add a Maven repository to a target project
* *
* @param target The garget project * @param target The garget project
* @param name The name of the repository * @param name The name of the repository
* @param url The URL 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 * @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) { 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("idea").finalizedBy(project1.getTasks().getByName("genIdeaWorkspace"));
project1.getTasks().getByName("eclipse").finalizedBy(project1.getTasks().getByName("genEclipseRuns")); project1.getTasks().getByName("eclipse").finalizedBy(project1.getTasks().getByName("genEclipseRuns"));
if(extension.autoGenIDERuns && isRootProject(project1)){ if (extension.autoGenIDERuns && isRootProject(project1)) {
SetupIntelijRunConfigs.setup(project1); SetupIntelijRunConfigs.setup(project1);
} }
@ -302,15 +309,15 @@ public class AbstractPlugin implements Plugin<Project> {
AbstractArchiveTask jarTask = (AbstractArchiveTask) project1.getTasks().getByName("jar"); AbstractArchiveTask jarTask = (AbstractArchiveTask) project1.getTasks().getByName("jar");
RemapJarTask remapJarTask = (RemapJarTask) project1.getTasks().findByName("remapJar"); RemapJarTask remapJarTask = (RemapJarTask) project1.getTasks().findByName("remapJar");
if (remapJarTask.getInput() == null) { assert remapJarTask != null;
remapJarTask.setOutput(jarTask.getArchivePath()); if (!remapJarTask.getInput().isPresent()) {
jarTask.setClassifier("dev"); remapJarTask.getArchiveClassifier().set("remapped");
remapJarTask.setInput(jarTask.getArchivePath()); 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")); remapJarTask.dependsOn(project1.getTasks().getByName("jar"));
project1.getTasks().getByName("build").dependsOn(remapJarTask); 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()) { for (Map.Entry<Project, Set<Task>> entry : taskMap.entrySet()) {
Set<Task> taskSet = entry.getValue(); Set<Task> taskSet = entry.getValue();
for (Task task : taskSet) { 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 //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); 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.LoomGradleExtension;
import net.fabricmc.loom.providers.MappingsProvider; import net.fabricmc.loom.providers.MappingsProvider;
import net.fabricmc.loom.util.Constants;
import net.fabricmc.loom.util.MixinRefmapHelper; import net.fabricmc.loom.util.MixinRefmapHelper;
import net.fabricmc.loom.util.NestedJars; import net.fabricmc.loom.util.NestedJars;
import net.fabricmc.loom.util.TinyRemapperMappingsHelper; import net.fabricmc.loom.util.TinyRemapperMappingsHelper;
@ -34,29 +33,36 @@ import net.fabricmc.tinyremapper.OutputConsumerPath;
import net.fabricmc.tinyremapper.TinyRemapper; import net.fabricmc.tinyremapper.TinyRemapper;
import net.fabricmc.tinyremapper.TinyUtils; import net.fabricmc.tinyremapper.TinyUtils;
import org.gradle.api.Project; 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.Input;
import org.gradle.api.tasks.InputFile; import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.TaskAction; import org.gradle.api.tasks.TaskAction;
import org.gradle.jvm.tasks.Jar;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.*; import java.util.LinkedHashSet;
import java.util.Set;
public class RemapJarTask extends AbstractLoomTask { public class RemapJarTask extends Jar {
private Object input; private RegularFileProperty input;
private Object output; private Property<Boolean> addNestedDependencies;
private boolean addNestedDependencies;
public RemapJarTask() {
super();
input = getProject().getObjects().fileProperty();
addNestedDependencies = getProject().getObjects().property(Boolean.class);
}
@TaskAction @TaskAction
public void doTask() throws Throwable { public void doTask() throws Throwable {
Project project = getProject(); Project project = getProject();
LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class); LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class);
Path input = getInput().toPath(); Path input = this.getInput().getAsFile().get().toPath();
Path output = getOutput().toPath(); Path output = this.getArchiveFile().get().getAsFile().toPath();
if (!Files.exists(input)) { if (!Files.exists(input)) {
throw new FileNotFoundException(input.toString()); throw new FileNotFoundException(input.toString());
@ -67,9 +73,9 @@ public class RemapJarTask extends AbstractLoomTask {
String fromM = "named"; String fromM = "named";
String toM = "intermediary"; String toM = "intermediary";
Set<File> classpathFiles = new LinkedHashSet<>(); Set<File> classpathFiles = new LinkedHashSet<>(
//noinspection CollectionAddAllCanBeReplacedWithConstructor project.getConfigurations().getByName("compileClasspath").getFiles()
classpathFiles.addAll(project.getConfigurations().getByName("compileClasspath").getFiles()); );
Path[] classpath = classpathFiles.stream().map(File::toPath).filter((p) -> !input.equals(p)).toArray(Path[]::new); Path[] classpath = classpathFiles.stream().map(File::toPath).filter((p) -> !input.equals(p)).toArray(Path[]::new);
File mixinMapFile = mappingsProvider.MAPPINGS_MIXIN_EXPORT; 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!"); project.getLogger().debug("Transformed mixin reference maps in output JAR!");
} }
if (addNestedDependencies) { if (getAddNestedDependencies().get()) {
if (NestedJars.addNestedJars(project, output)) { if (NestedJars.addNestedJars(project, output)) {
project.getLogger().debug("Added nested jar paths to mod json"); project.getLogger().debug("Added nested jar paths to mod json");
} }
@ -119,29 +125,25 @@ public class RemapJarTask extends AbstractLoomTask {
extension.addUnmappedMod(input); extension.addUnmappedMod(input);
/** /*try {
* if (modJar.exists()) {
Files.move(modJar, modJarUnmappedCopy);
extension.addUnmappedMod(modJarUnmappedCopy);
}
try { Files.move(modJarOutput, modJar);
if (modJar.exists()) { } catch (IOException e) {
Files.move(modJar, modJarUnmappedCopy); throw new RuntimeException(e);
extension.addUnmappedMod(modJarUnmappedCopy); }*/
}
Files.move(modJarOutput, modJar);
} catch (IOException e) {
throw new RuntimeException(e);
}
*/
} }
//@formatter:off @InputFile
// the null-check in getInput() is done to allow reconfiguration by AbstractPlugin public RegularFileProperty getInput() {
@InputFile public File getInput() { return input == null ? null : getProject().file(input); } return input;
@OutputFile public File getOutput() { return getProject().file(output); } }
@Input public boolean isAddNestedDependencies() { return addNestedDependencies; }
public void setAddNestedDependencies(boolean value) { this.addNestedDependencies = value; } @Input
public void setInput(Object input) { this.input = input; } public Property<Boolean> getAddNestedDependencies() {
public void setOutput(Object output) { this.output = output; } return addNestedDependencies;
//@formatter:on }
} }

View File

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