Allow for non-yarn mappings for MigrateMappingsTask (#141)

* Add getExtension utility method to AbstractLoomTask

* Support non-yarn mappings in MigrateMappingsTask

* Fix style

* Assume Yarn mappings if correct notation was not supplied

* Move load mappings block into its own its own method

Also added protected to getExtension on AbstractLoomTask

* Use Option instead of environment variables

* Assume V2 Yarn by default

* Some restructuring

All works the same.
dev/0.11
Ramid Khan 2019-11-16 07:16:09 +11:00 committed by modmuss50
parent 11af956c84
commit 55a9d90690
8 changed files with 93 additions and 77 deletions

View File

@ -26,8 +26,14 @@ package net.fabricmc.loom.task;
import org.gradle.api.DefaultTask; import org.gradle.api.DefaultTask;
import net.fabricmc.loom.LoomGradleExtension;
public abstract class AbstractLoomTask extends DefaultTask { public abstract class AbstractLoomTask extends DefaultTask {
public AbstractLoomTask() { public AbstractLoomTask() {
setGroup("fabric"); setGroup("fabric");
} }
protected LoomGradleExtension getExtension() {
return getProject().getExtensions().getByType(LoomGradleExtension.class);
}
} }

View File

@ -27,7 +27,6 @@ package net.fabricmc.loom.task;
import java.io.IOException; import java.io.IOException;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.gradle.api.Project;
import org.gradle.api.tasks.TaskAction; import org.gradle.api.tasks.TaskAction;
import net.fabricmc.loom.LoomGradleExtension; import net.fabricmc.loom.LoomGradleExtension;
@ -35,8 +34,7 @@ import net.fabricmc.loom.LoomGradleExtension;
public class CleanLoomBinaries extends AbstractLoomTask { public class CleanLoomBinaries extends AbstractLoomTask {
@TaskAction @TaskAction
public void run() { public void run() {
Project project = this.getProject(); LoomGradleExtension extension = getExtension();
LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class);
extension.getMinecraftProvider().getMergedJar().delete(); extension.getMinecraftProvider().getMergedJar().delete();
extension.getMinecraftMappedProvider().getIntermediaryJar().delete(); extension.getMinecraftMappedProvider().getIntermediaryJar().delete();
extension.getMinecraftMappedProvider().getMappedJar().delete(); extension.getMinecraftMappedProvider().getMappedJar().delete();

View File

@ -27,7 +27,6 @@ package net.fabricmc.loom.task;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import org.gradle.api.Project;
import org.gradle.api.tasks.TaskAction; import org.gradle.api.tasks.TaskAction;
import net.fabricmc.loom.LoomGradleExtension; import net.fabricmc.loom.LoomGradleExtension;
@ -37,8 +36,7 @@ public class CleanLoomMappings extends AbstractLoomTask {
@TaskAction @TaskAction
public void run() { public void run() {
try { try {
Project project = this.getProject(); LoomGradleExtension extension = getExtension();
LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class);
extension.getMappingsProvider().clean(); extension.getMappingsProvider().clean();
extension.getMinecraftMappedProvider().getIntermediaryJar().delete(); extension.getMinecraftMappedProvider().getIntermediaryJar().delete();
extension.getMinecraftMappedProvider().getMappedJar().delete(); extension.getMinecraftMappedProvider().getMappedJar().delete();

View File

@ -37,7 +37,7 @@ public class DownloadAssetsTask extends AbstractLoomTask {
@TaskAction @TaskAction
public void downloadAssets() throws IOException { public void downloadAssets() throws IOException {
Project project = this.getProject(); Project project = this.getProject();
LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class); LoomGradleExtension extension = getExtension();
MinecraftAssetsProvider.provide(extension.getMinecraftProvider(), project); MinecraftAssetsProvider.provide(extension.getMinecraftProvider(), project);
MinecraftNativesProvider.provide(extension.getMinecraftProvider(), project); MinecraftNativesProvider.provide(extension.getMinecraftProvider(), project);

View File

@ -31,7 +31,6 @@ import java.nio.charset.StandardCharsets;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.gradle.api.tasks.TaskAction; import org.gradle.api.tasks.TaskAction;
import net.fabricmc.loom.LoomGradleExtension;
import net.fabricmc.loom.util.RunConfig; import net.fabricmc.loom.util.RunConfig;
public class GenEclipseRunsTask extends AbstractLoomTask { public class GenEclipseRunsTask extends AbstractLoomTask {
@ -51,7 +50,7 @@ public class GenEclipseRunsTask extends AbstractLoomTask {
FileUtils.writeStringToFile(serverRunConfigs, serverRunConfig, StandardCharsets.UTF_8); FileUtils.writeStringToFile(serverRunConfigs, serverRunConfig, StandardCharsets.UTF_8);
} }
File runDir = new File(getProject().getRootDir(), this.getProject().getExtensions().getByType(LoomGradleExtension.class).runDir); File runDir = new File(getProject().getRootDir(), getExtension().runDir);
if (!runDir.exists()) { if (!runDir.exists()) {
runDir.mkdirs(); runDir.mkdirs();

View File

@ -58,7 +58,7 @@ public class GenIdeaProjectTask extends AbstractLoomTask {
return; return;
} }
LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class); LoomGradleExtension extension = getExtension();
project.getLogger().lifecycle(":Building idea workspace"); project.getLogger().lifecycle(":Building idea workspace");
File file = project.file(project.getName() + ".iws"); File file = project.file(project.getName() + ".iws");

View File

@ -33,6 +33,7 @@ import java.util.List;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.gradle.api.Project;
import org.gradle.api.tasks.TaskAction; import org.gradle.api.tasks.TaskAction;
import net.fabricmc.loom.LoomGradleExtension; import net.fabricmc.loom.LoomGradleExtension;
@ -45,8 +46,9 @@ import net.fabricmc.loom.util.RunConfig;
public class GenVsCodeProjectTask extends AbstractLoomTask { public class GenVsCodeProjectTask extends AbstractLoomTask {
@TaskAction @TaskAction
public void genRuns() { public void genRuns() {
LoomGradleExtension extension = getProject().getExtensions().getByType(LoomGradleExtension.class); Project project = getProject();
File projectDir = getProject().file(".vscode"); LoomGradleExtension extension = getExtension();
File projectDir = project.file(".vscode");
if (!projectDir.exists()) { if (!projectDir.exists()) {
projectDir.mkdir(); projectDir.mkdir();
@ -59,8 +61,8 @@ public class GenVsCodeProjectTask extends AbstractLoomTask {
} }
VsCodeLaunch launch = new VsCodeLaunch(); VsCodeLaunch launch = new VsCodeLaunch();
launch.add(RunConfig.clientRunConfig(getProject())); launch.add(RunConfig.clientRunConfig(project));
launch.add(RunConfig.serverRunConfig(getProject())); launch.add(RunConfig.serverRunConfig(project));
Gson gson = new GsonBuilder().setPrettyPrinting().create(); Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(launch); String json = gson.toJson(launch);
@ -71,7 +73,7 @@ public class GenVsCodeProjectTask extends AbstractLoomTask {
throw new RuntimeException("Failed to write launch.json", e); throw new RuntimeException("Failed to write launch.json", e);
} }
File runDir = new File(getProject().getRootDir(), extension.runDir); File runDir = new File(project.getRootDir(), extension.runDir);
if (!runDir.exists()) { if (!runDir.exists()) {
runDir.mkdirs(); runDir.mkdirs();

View File

@ -27,27 +27,30 @@ package net.fabricmc.loom.task;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.URL;
import java.nio.file.FileSystem; import java.nio.file.FileSystem;
import java.nio.file.FileSystems; import java.nio.file.FileSystems;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.StandardCopyOption;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import com.google.common.net.UrlEscapers; import com.google.common.collect.ImmutableMap;
import org.apache.commons.io.FileUtils; import com.google.common.collect.Iterables;
import org.cadixdev.lorenz.MappingSet; import org.cadixdev.lorenz.MappingSet;
import org.cadixdev.lorenz.io.MappingsReader; import org.cadixdev.lorenz.io.MappingsReader;
import org.cadixdev.lorenz.model.ClassMapping; import org.cadixdev.lorenz.model.ClassMapping;
import org.cadixdev.lorenz.model.Mapping; import org.cadixdev.lorenz.model.Mapping;
import org.cadixdev.mercury.Mercury; import org.cadixdev.mercury.Mercury;
import org.cadixdev.mercury.remapper.MercuryRemapper; import org.cadixdev.mercury.remapper.MercuryRemapper;
import org.gradle.api.GradleException;
import org.gradle.api.IllegalDependencyNotation;
import org.gradle.api.Project; import org.gradle.api.Project;
import org.gradle.api.tasks.TaskAction; import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.options.Option;
import net.fabricmc.loom.LoomGradleExtension; import net.fabricmc.loom.LoomGradleExtension;
import net.fabricmc.loom.providers.MappingsProvider; import net.fabricmc.loom.providers.MappingsProvider;
@ -61,88 +64,99 @@ import net.fabricmc.mapping.tree.TinyMappingFactory;
import net.fabricmc.mapping.tree.TinyTree; import net.fabricmc.mapping.tree.TinyTree;
public class MigrateMappingsTask extends AbstractLoomTask { public class MigrateMappingsTask extends AbstractLoomTask {
private Path inputDir;
private Path outputDir;
private String mappings;
public MigrateMappingsTask() {
inputDir = getProject().file("src/main/java").toPath();
outputDir = getProject().file("remappedSrc").toPath();
}
@Option(option = "input", description = "Java source file directory")
public void setInputDir(String inputDir) {
this.inputDir = getProject().file(inputDir).toPath();
}
@Option(option = "output", description = "Remapped source output directory")
public void setOutputDir(String outputDir) {
this.outputDir = getProject().file(outputDir).toPath();
}
@Option(option = "mappings", description = "Target mappings")
public void setMappings(String mappings) {
this.mappings = mappings;
}
@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 = getExtension();
Map<String, ?> properties = project.getProperties();
project.getLogger().lifecycle(":loading mappings"); project.getLogger().lifecycle(":loading mappings");
String inputDir = (String) properties.get("inputDir"); if (!Files.exists(inputDir) || !Files.isDirectory(inputDir)) {
if (inputDir == null) inputDir = "src/main/java"; throw new IllegalArgumentException("Could not find input directory: " + inputDir.toAbsolutePath());
String outputDir = (String) properties.get("outputDir");
if (outputDir == null) outputDir = "remappedSrc";
String officialMappingsVersion = (String) properties.get("targetMappings");
String localMappingsPath = (String) properties.get("targetLocalMappings");
if (officialMappingsVersion != null && localMappingsPath != null) {
throw new IllegalArgumentException("targetMappings and targetLocalMappings are mutually exclusive;"
+ " you either specify an official yarn mappings version with targetMappings, "
+ "or a local one with targetLocalMappings.");
} }
if (officialMappingsVersion == null && localMappingsPath == null) { Files.createDirectories(outputDir);
throw new IllegalArgumentException("You must specify a new mappings version with -PtargetMappings (or local mappings with -PtargetLocalMappings).");
}
boolean useLocalMappings = localMappingsPath != null;
if (useLocalMappings && !Files.exists(Paths.get(localMappingsPath))) {
throw new IllegalArgumentException("Can't find local mappings in specified location: " + localMappingsPath);
}
String targetMappingsName = useLocalMappings ? localMappingsPath : officialMappingsVersion;
Path inputDirPath = Paths.get(System.getProperty("user.dir"), inputDir);
Path outputDirPath = Paths.get(System.getProperty("user.dir"), outputDir);
if (!Files.exists(inputDirPath) || !Files.isDirectory(inputDirPath)) {
throw new IllegalArgumentException("Could not find input directory: " + inputDirPath.toAbsolutePath());
}
Files.createDirectories(outputDirPath);
File mappings = loadMappings();
MappingsProvider mappingsProvider = extension.getMappingsProvider(); MappingsProvider mappingsProvider = extension.getMappingsProvider();
try { try {
TinyTree currentMappings = mappingsProvider.getMappings(); TinyTree currentMappings = mappingsProvider.getMappings();
TinyTree targetMappings = getMappings(project, targetMappingsName, useLocalMappings); TinyTree targetMappings = getMappings(mappings);
migrateMappings(project, extension.getMinecraftMappedProvider(), inputDirPath, outputDirPath, currentMappings, targetMappings, extension); migrateMappings(project, extension.getMinecraftMappedProvider(), inputDir, outputDir, currentMappings, targetMappings);
project.getLogger().lifecycle(":remapped project written to " + outputDirPath.toAbsolutePath()); project.getLogger().lifecycle(":remapped project written to " + outputDir.toAbsolutePath());
} catch (IOException e) { } catch (IOException e) {
throw new IllegalArgumentException("Could not find mappings for version " + officialMappingsVersion, e); throw new IllegalArgumentException("Error while loading mappings", e);
} }
} }
private TinyTree getMappings(Project project, String mappingsVersionOrPath, boolean useLocalMappings) throws IOException { private File loadMappings() {
Path migrateMappingsDir = Files.createTempDirectory("migrate"); Project project = getProject();
Path localMappingsOfVersion = migrateMappingsDir.resolve(mappingsVersionOrPath + ".tiny");
if (!Files.exists(localMappingsOfVersion)) { if (mappings == null || mappings.isEmpty()) {
if (useLocalMappings) { throw new IllegalArgumentException("No mappings were specified. Use --mappings=\"\" to specify target mappings");
Files.copy(Paths.get(mappingsVersionOrPath), localMappingsOfVersion); }
} else {
String versionRelativePath = UrlEscapers.urlFragmentEscaper().escape(mappingsVersionOrPath);
String artifactUrl = "https://maven.fabricmc.net/net/fabricmc/yarn/" + versionRelativePath + "/yarn-" + versionRelativePath + ".jar";
File mappingsJar = File.createTempFile("migrateMappingsJar", ".jar");
project.getLogger().lifecycle(":downloading new mappings from " + artifactUrl);
FileUtils.copyURLToFile(new URL(artifactUrl), mappingsJar);
try (FileSystem jar = FileSystems.newFileSystem(mappingsJar.toPath(), null)) { Set<File> files;
if (!Files.exists(migrateMappingsDir)) Files.createDirectory(migrateMappingsDir);
MappingsProvider.extractMappings(jar, localMappingsOfVersion); try {
} files = project.getConfigurations().detachedConfiguration(project.getDependencies().create(mappings)).resolve();
} catch (IllegalDependencyNotation ignored) {
project.getLogger().info("Could not locate mappings, presuming V2 Yarn");
try {
files = project.getConfigurations().detachedConfiguration(project.getDependencies().module(ImmutableMap.of("group", "net.fabricmc", "name", "yarn", "version", mappings, "classifier", "v2"))).resolve();
} catch (GradleException ignored2) {
project.getLogger().info("Could not locate mappings, presuming V1 Yarn");
files = project.getConfigurations().detachedConfiguration(project.getDependencies().module(ImmutableMap.of("group", "net.fabricmc", "name", "yarn", "version", mappings))).resolve();
} }
} }
try (BufferedReader reader = Files.newBufferedReader(localMappingsOfVersion)) { if (files.isEmpty()) {
throw new IllegalArgumentException("Mappings could not be found");
}
return Iterables.getOnlyElement(files);
}
private static TinyTree getMappings(File mappings) throws IOException {
Path temp = Files.createTempFile("mappings", ".tiny");
try (FileSystem fileSystem = FileSystems.newFileSystem(mappings.toPath(), null)) {
Files.copy(fileSystem.getPath("mappings/mappings.tiny"), temp, StandardCopyOption.REPLACE_EXISTING);
}
try (BufferedReader reader = Files.newBufferedReader(temp)) {
return TinyMappingFactory.loadWithDetection(reader); return TinyMappingFactory.loadWithDetection(reader);
} }
} }
private static void migrateMappings(Project project, MinecraftMappedProvider minecraftMappedProvider, private static void migrateMappings(Project project, MinecraftMappedProvider minecraftMappedProvider,
Path inputDir, Path outputDir, TinyTree currentMappings, TinyTree targetMappings, LoomGradleExtension extension Path inputDir, Path outputDir, TinyTree currentMappings, TinyTree targetMappings
) throws IOException { ) throws IOException {
project.getLogger().lifecycle(":joining mappings"); project.getLogger().lifecycle(":joining mappings");
MappingSet mappingSet = new MappingsJoiner(currentMappings, targetMappings, MappingSet mappingSet = new MappingsJoiner(currentMappings, targetMappings,
@ -166,7 +180,7 @@ public class MigrateMappingsTask extends AbstractLoomTask {
System.gc(); System.gc();
} }
public static class MappingsJoiner extends MappingsReader { private static class MappingsJoiner extends MappingsReader {
private final TinyTree sourceMappings, targetMappings; private final TinyTree sourceMappings, targetMappings;
private final String fromNamespace, toNamespace; private final String fromNamespace, toNamespace;
@ -179,7 +193,7 @@ public class MigrateMappingsTask extends AbstractLoomTask {
* Since we only use intermediary names (and not descriptors) to match, and intermediary names are unique, * Since we only use intermediary names (and not descriptors) to match, and intermediary names are unique,
* this will migrate methods that have had their signature changed too. * this will migrate methods that have had their signature changed too.
*/ */
public MappingsJoiner(TinyTree sourceMappings, TinyTree targetMappings, String fromNamespace, String toNamespace) { private MappingsJoiner(TinyTree sourceMappings, TinyTree targetMappings, String fromNamespace, String toNamespace) {
this.sourceMappings = sourceMappings; this.sourceMappings = sourceMappings;
this.targetMappings = targetMappings; this.targetMappings = targetMappings;
this.fromNamespace = fromNamespace; this.fromNamespace = fromNamespace;
@ -234,4 +248,3 @@ public class MigrateMappingsTask extends AbstractLoomTask {
} }
} }
} }