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.
This commit is contained in:
		
							parent
							
								
									11af956c84
								
							
						
					
					
						commit
						55a9d90690
					
				
					 8 changed files with 93 additions and 77 deletions
				
			
		|  | @ -26,8 +26,14 @@ package net.fabricmc.loom.task; | |||
| 
 | ||||
| import org.gradle.api.DefaultTask; | ||||
| 
 | ||||
| import net.fabricmc.loom.LoomGradleExtension; | ||||
| 
 | ||||
| public abstract class AbstractLoomTask extends DefaultTask { | ||||
| 	public AbstractLoomTask() { | ||||
| 		setGroup("fabric"); | ||||
| 	} | ||||
| 
 | ||||
| 	protected LoomGradleExtension getExtension() { | ||||
| 		return getProject().getExtensions().getByType(LoomGradleExtension.class); | ||||
| 	} | ||||
| } | ||||
|  |  | |||
|  | @ -27,7 +27,6 @@ package net.fabricmc.loom.task; | |||
| import java.io.IOException; | ||||
| 
 | ||||
| import org.apache.commons.io.FileUtils; | ||||
| import org.gradle.api.Project; | ||||
| import org.gradle.api.tasks.TaskAction; | ||||
| 
 | ||||
| import net.fabricmc.loom.LoomGradleExtension; | ||||
|  | @ -35,8 +34,7 @@ import net.fabricmc.loom.LoomGradleExtension; | |||
| public class CleanLoomBinaries extends AbstractLoomTask { | ||||
| 	@TaskAction | ||||
| 	public void run() { | ||||
| 		Project project = this.getProject(); | ||||
| 		LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class); | ||||
| 		LoomGradleExtension extension = getExtension(); | ||||
| 		extension.getMinecraftProvider().getMergedJar().delete(); | ||||
| 		extension.getMinecraftMappedProvider().getIntermediaryJar().delete(); | ||||
| 		extension.getMinecraftMappedProvider().getMappedJar().delete(); | ||||
|  |  | |||
|  | @ -27,7 +27,6 @@ package net.fabricmc.loom.task; | |||
| import java.io.IOException; | ||||
| import java.nio.file.Files; | ||||
| 
 | ||||
| import org.gradle.api.Project; | ||||
| import org.gradle.api.tasks.TaskAction; | ||||
| 
 | ||||
| import net.fabricmc.loom.LoomGradleExtension; | ||||
|  | @ -37,8 +36,7 @@ public class CleanLoomMappings extends AbstractLoomTask { | |||
| 	@TaskAction | ||||
| 	public void run() { | ||||
| 		try { | ||||
| 			Project project = this.getProject(); | ||||
| 			LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class); | ||||
| 			LoomGradleExtension extension = getExtension(); | ||||
| 			extension.getMappingsProvider().clean(); | ||||
| 			extension.getMinecraftMappedProvider().getIntermediaryJar().delete(); | ||||
| 			extension.getMinecraftMappedProvider().getMappedJar().delete(); | ||||
|  |  | |||
|  | @ -37,7 +37,7 @@ public class DownloadAssetsTask extends AbstractLoomTask { | |||
| 	@TaskAction | ||||
| 	public void downloadAssets() throws IOException { | ||||
| 		Project project = this.getProject(); | ||||
| 		LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class); | ||||
| 		LoomGradleExtension extension = getExtension(); | ||||
| 
 | ||||
| 		MinecraftAssetsProvider.provide(extension.getMinecraftProvider(), project); | ||||
| 		MinecraftNativesProvider.provide(extension.getMinecraftProvider(), project); | ||||
|  |  | |||
|  | @ -31,7 +31,6 @@ import java.nio.charset.StandardCharsets; | |||
| import org.apache.commons.io.FileUtils; | ||||
| import org.gradle.api.tasks.TaskAction; | ||||
| 
 | ||||
| import net.fabricmc.loom.LoomGradleExtension; | ||||
| import net.fabricmc.loom.util.RunConfig; | ||||
| 
 | ||||
| public class GenEclipseRunsTask extends AbstractLoomTask { | ||||
|  | @ -51,7 +50,7 @@ public class GenEclipseRunsTask extends AbstractLoomTask { | |||
| 			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()) { | ||||
| 			runDir.mkdirs(); | ||||
|  |  | |||
|  | @ -58,7 +58,7 @@ public class GenIdeaProjectTask extends AbstractLoomTask { | |||
| 			return; | ||||
| 		} | ||||
| 
 | ||||
| 		LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class); | ||||
| 		LoomGradleExtension extension = getExtension(); | ||||
| 		project.getLogger().lifecycle(":Building idea workspace"); | ||||
| 
 | ||||
| 		File file = project.file(project.getName() + ".iws"); | ||||
|  |  | |||
|  | @ -33,6 +33,7 @@ import java.util.List; | |||
| import com.google.gson.Gson; | ||||
| import com.google.gson.GsonBuilder; | ||||
| import org.apache.commons.io.FileUtils; | ||||
| import org.gradle.api.Project; | ||||
| import org.gradle.api.tasks.TaskAction; | ||||
| 
 | ||||
| import net.fabricmc.loom.LoomGradleExtension; | ||||
|  | @ -45,8 +46,9 @@ import net.fabricmc.loom.util.RunConfig; | |||
| public class GenVsCodeProjectTask extends AbstractLoomTask { | ||||
| 	@TaskAction | ||||
| 	public void genRuns() { | ||||
| 		LoomGradleExtension extension = getProject().getExtensions().getByType(LoomGradleExtension.class); | ||||
| 		File projectDir = getProject().file(".vscode"); | ||||
| 		Project project = getProject(); | ||||
| 		LoomGradleExtension extension = getExtension(); | ||||
| 		File projectDir = project.file(".vscode"); | ||||
| 
 | ||||
| 		if (!projectDir.exists()) { | ||||
| 			projectDir.mkdir(); | ||||
|  | @ -59,8 +61,8 @@ public class GenVsCodeProjectTask extends AbstractLoomTask { | |||
| 		} | ||||
| 
 | ||||
| 		VsCodeLaunch launch = new VsCodeLaunch(); | ||||
| 		launch.add(RunConfig.clientRunConfig(getProject())); | ||||
| 		launch.add(RunConfig.serverRunConfig(getProject())); | ||||
| 		launch.add(RunConfig.clientRunConfig(project)); | ||||
| 		launch.add(RunConfig.serverRunConfig(project)); | ||||
| 
 | ||||
| 		Gson gson = new GsonBuilder().setPrettyPrinting().create(); | ||||
| 		String json = gson.toJson(launch); | ||||
|  | @ -71,7 +73,7 @@ public class GenVsCodeProjectTask extends AbstractLoomTask { | |||
| 			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()) { | ||||
| 			runDir.mkdirs(); | ||||
|  |  | |||
|  | @ -27,27 +27,30 @@ package net.fabricmc.loom.task; | |||
| import java.io.BufferedReader; | ||||
| import java.io.File; | ||||
| import java.io.IOException; | ||||
| import java.net.URL; | ||||
| import java.nio.file.FileSystem; | ||||
| import java.nio.file.FileSystems; | ||||
| import java.nio.file.Files; | ||||
| import java.nio.file.Path; | ||||
| import java.nio.file.Paths; | ||||
| import java.nio.file.StandardCopyOption; | ||||
| import java.util.Collection; | ||||
| import java.util.HashMap; | ||||
| import java.util.Map; | ||||
| import java.util.Set; | ||||
| import java.util.function.BiFunction; | ||||
| 
 | ||||
| import com.google.common.net.UrlEscapers; | ||||
| import org.apache.commons.io.FileUtils; | ||||
| import com.google.common.collect.ImmutableMap; | ||||
| import com.google.common.collect.Iterables; | ||||
| import org.cadixdev.lorenz.MappingSet; | ||||
| import org.cadixdev.lorenz.io.MappingsReader; | ||||
| import org.cadixdev.lorenz.model.ClassMapping; | ||||
| import org.cadixdev.lorenz.model.Mapping; | ||||
| import org.cadixdev.mercury.Mercury; | ||||
| 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.tasks.TaskAction; | ||||
| import org.gradle.api.tasks.options.Option; | ||||
| 
 | ||||
| import net.fabricmc.loom.LoomGradleExtension; | ||||
| import net.fabricmc.loom.providers.MappingsProvider; | ||||
|  | @ -61,88 +64,99 @@ import net.fabricmc.mapping.tree.TinyMappingFactory; | |||
| import net.fabricmc.mapping.tree.TinyTree; | ||||
| 
 | ||||
| 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 | ||||
| 	public void doTask() throws Throwable { | ||||
| 		Project project = getProject(); | ||||
| 		LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class); | ||||
| 		Map<String, ?> properties = project.getProperties(); | ||||
| 		LoomGradleExtension extension = getExtension(); | ||||
| 
 | ||||
| 		project.getLogger().lifecycle(":loading mappings"); | ||||
| 
 | ||||
| 		String inputDir = (String) properties.get("inputDir"); | ||||
| 		if (inputDir == null) inputDir = "src/main/java"; | ||||
| 		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 (!Files.exists(inputDir) || !Files.isDirectory(inputDir)) { | ||||
| 			throw new IllegalArgumentException("Could not find input directory: " + inputDir.toAbsolutePath()); | ||||
| 		} | ||||
| 
 | ||||
| 		if (officialMappingsVersion == null && localMappingsPath == null) { | ||||
| 			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); | ||||
| 		Files.createDirectories(outputDir); | ||||
| 
 | ||||
| 		File mappings = loadMappings(); | ||||
| 		MappingsProvider mappingsProvider = extension.getMappingsProvider(); | ||||
| 
 | ||||
| 		try { | ||||
| 			TinyTree currentMappings = mappingsProvider.getMappings(); | ||||
| 			TinyTree targetMappings = getMappings(project, targetMappingsName, useLocalMappings); | ||||
| 			migrateMappings(project, extension.getMinecraftMappedProvider(), inputDirPath, outputDirPath, currentMappings, targetMappings, extension); | ||||
| 			project.getLogger().lifecycle(":remapped project written to " + outputDirPath.toAbsolutePath()); | ||||
| 			TinyTree targetMappings = getMappings(mappings); | ||||
| 			migrateMappings(project, extension.getMinecraftMappedProvider(), inputDir, outputDir, currentMappings, targetMappings); | ||||
| 			project.getLogger().lifecycle(":remapped project written to " + outputDir.toAbsolutePath()); | ||||
| 		} 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 { | ||||
| 		Path migrateMappingsDir = Files.createTempDirectory("migrate"); | ||||
| 		Path localMappingsOfVersion = migrateMappingsDir.resolve(mappingsVersionOrPath + ".tiny"); | ||||
| 	private File loadMappings() { | ||||
| 		Project project = getProject(); | ||||
| 
 | ||||
| 		if (!Files.exists(localMappingsOfVersion)) { | ||||
| 			if (useLocalMappings) { | ||||
| 				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); | ||||
| 		if (mappings == null || mappings.isEmpty()) { | ||||
| 			throw new IllegalArgumentException("No mappings were specified. Use --mappings=\"\" to specify target mappings"); | ||||
| 		} | ||||
| 
 | ||||
| 				try (FileSystem jar = FileSystems.newFileSystem(mappingsJar.toPath(), null)) { | ||||
| 					if (!Files.exists(migrateMappingsDir)) Files.createDirectory(migrateMappingsDir); | ||||
| 					MappingsProvider.extractMappings(jar, localMappingsOfVersion); | ||||
| 				} | ||||
| 		Set<File> files; | ||||
| 
 | ||||
| 		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); | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	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 { | ||||
| 		project.getLogger().lifecycle(":joining mappings"); | ||||
| 		MappingSet mappingSet = new MappingsJoiner(currentMappings, targetMappings, | ||||
|  | @ -166,7 +180,7 @@ public class MigrateMappingsTask extends AbstractLoomTask { | |||
| 		System.gc(); | ||||
| 	} | ||||
| 
 | ||||
| 	public static class MappingsJoiner extends MappingsReader { | ||||
| 	private static class MappingsJoiner extends MappingsReader { | ||||
| 		private final TinyTree sourceMappings, targetMappings; | ||||
| 		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, | ||||
| 		 * 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.targetMappings = targetMappings; | ||||
| 			this.fromNamespace = fromNamespace; | ||||
|  | @ -234,4 +248,3 @@ public class MigrateMappingsTask extends AbstractLoomTask { | |||
| 		} | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
		Reference in a new issue