fabric-loom/src/main/java/net/fabricmc/loom/task/RemapJarTask.java

148 lines
5.3 KiB
Java
Raw Normal View History

2016-10-12 09:44:31 +00:00
/*
* This file is part of fabric-loom, licensed under the MIT License (MIT).
*
2018-10-27 14:14:05 +00:00
* Copyright (c) 2016, 2017, 2018 FabricMC
2016-10-12 09:44:31 +00:00
*
* 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;
2016-10-12 09:44:31 +00:00
import net.fabricmc.loom.LoomGradleExtension;
2018-12-09 21:22:35 +00:00
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;
2018-10-31 13:20:50 +00:00
import net.fabricmc.tinyremapper.OutputConsumerPath;
import net.fabricmc.tinyremapper.TinyRemapper;
import net.fabricmc.tinyremapper.TinyUtils;
2016-10-12 09:44:31 +00:00
import org.gradle.api.Project;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.TaskAction;
2016-10-12 09:44:31 +00:00
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
2016-10-12 09:44:31 +00:00
import java.nio.file.Path;
2019-05-18 09:51:34 +00:00
import java.util.*;
2016-10-12 09:44:31 +00:00
public class RemapJarTask extends AbstractLoomTask {
private Object input;
private Object output;
private boolean addNestedDependencies;
2016-10-12 09:44:31 +00:00
@TaskAction
public void doTask() throws Throwable {
Project project = getProject();
2016-10-12 09:44:31 +00:00
LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class);
Path input = getInput().toPath();
Path output = getOutput().toPath();
if (!Files.exists(input)) {
throw new FileNotFoundException(input.toString());
2016-10-12 09:44:31 +00:00
}
2018-12-09 21:22:35 +00:00
MappingsProvider mappingsProvider = extension.getMappingsProvider();
2018-11-02 16:19:57 +00:00
String fromM = "named";
2018-10-31 13:20:50 +00:00
String toM = "intermediary";
2016-10-12 09:44:31 +00:00
2019-05-18 09:51:34 +00:00
Set<File> classpathFiles = new LinkedHashSet<>();
//noinspection CollectionAddAllCanBeReplacedWithConstructor
classpathFiles.addAll(project.getConfigurations().getByName("compileClasspath").getFiles());
Path[] classpath = classpathFiles.stream().map(File::toPath).filter((p) -> !input.equals(p)).toArray(Path[]::new);
2018-12-12 06:10:30 +00:00
2018-12-09 21:22:35 +00:00
File mixinMapFile = mappingsProvider.MAPPINGS_MIXIN_EXPORT;
Path mixinMapPath = mixinMapFile.toPath();
TinyRemapper.Builder remapperBuilder = TinyRemapper.newRemapper();
remapperBuilder = remapperBuilder.withMappings(TinyRemapperMappingsHelper.create(mappingsProvider.getMappings(), fromM, toM));
if (mixinMapFile.exists()) {
remapperBuilder = remapperBuilder.withMappings(TinyUtils.createTinyMappingProvider(mixinMapPath, fromM, toM));
2016-10-12 09:44:31 +00:00
}
project.getLogger().lifecycle(":remapping " + input.getFileName());
2019-05-18 09:51:34 +00:00
StringBuilder rc = new StringBuilder("Remap classpath: ");
for (Path p : classpath) {
rc.append("\n - ").append(p.toString());
}
project.getLogger().debug(rc.toString());
TinyRemapper remapper = remapperBuilder.build();
2016-10-12 09:44:31 +00:00
try (OutputConsumerPath outputConsumer = new OutputConsumerPath(output)) {
outputConsumer.addNonClassFiles(input);
2019-04-21 09:37:16 +00:00
remapper.readClassPath(classpath);
remapper.readInputs(input);
2019-04-21 09:37:16 +00:00
remapper.apply(outputConsumer);
} catch (Exception e) {
throw new RuntimeException("Failed to remap " + input + " to " + output, e);
} finally {
remapper.finish();
2018-08-07 14:56:39 +00:00
}
2016-10-23 16:31:56 +00:00
if (!Files.exists(output)) {
throw new RuntimeException("Failed to remap " + input + " to " + output + " - file missing!");
2018-06-18 16:01:39 +00:00
}
if (MixinRefmapHelper.addRefmapName(extension.getRefmapName(), extension.getMixinJsonVersion(), output)) {
project.getLogger().debug("Transformed mixin reference maps in output JAR!");
}
if (addNestedDependencies) {
if (NestedJars.addNestedJars(project, output)) {
project.getLogger().debug("Added nested jar paths to mod json");
}
2019-04-07 14:18:11 +00:00
}
extension.addUnmappedMod(input);
/**
*
try {
if (modJar.exists()) {
Files.move(modJar, modJarUnmappedCopy);
extension.addUnmappedMod(modJarUnmappedCopy);
}
Files.move(modJarOutput, modJar);
} catch (IOException e) {
throw new RuntimeException(e);
}
*/
2016-10-12 09:44:31 +00:00
}
//@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
2016-10-12 09:44:31 +00:00
}