2019-04-22 11:41:16 +00:00
|
|
|
/*
|
|
|
|
* This file is part of fabric-loom, licensed under the MIT License (MIT).
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016, 2017, 2018 FabricMC
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2020-12-24 20:58:30 +00:00
|
|
|
package net.fabricmc.loom.build;
|
2019-04-22 11:41:16 +00:00
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2020-05-13 18:44:45 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2020-04-27 13:08:37 +00:00
|
|
|
import java.util.zip.ZipFile;
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2019-04-22 11:41:16 +00:00
|
|
|
import org.gradle.api.Project;
|
|
|
|
import org.gradle.api.artifacts.Configuration;
|
|
|
|
import org.gradle.api.artifacts.Dependency;
|
|
|
|
import org.gradle.api.artifacts.ModuleDependency;
|
2019-05-27 20:18:19 +00:00
|
|
|
import org.gradle.api.artifacts.ResolvedArtifact;
|
2019-04-22 11:41:16 +00:00
|
|
|
import org.gradle.api.artifacts.dsl.DependencyHandler;
|
|
|
|
import org.gradle.api.artifacts.query.ArtifactResolutionQuery;
|
|
|
|
import org.gradle.api.artifacts.result.ArtifactResult;
|
|
|
|
import org.gradle.api.artifacts.result.ComponentArtifactsResult;
|
|
|
|
import org.gradle.api.artifacts.result.ResolvedArtifactResult;
|
|
|
|
import org.gradle.api.logging.Logger;
|
|
|
|
import org.gradle.jvm.JvmLibrary;
|
|
|
|
import org.gradle.language.base.artifact.SourcesArtifact;
|
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
import net.fabricmc.loom.LoomGradleExtension;
|
2020-12-24 20:58:30 +00:00
|
|
|
import net.fabricmc.loom.configuration.RemappedConfigurationEntry;
|
|
|
|
import net.fabricmc.loom.configuration.mods.ModProcessor;
|
|
|
|
import net.fabricmc.loom.configuration.processors.dependency.ModDependencyInfo;
|
|
|
|
import net.fabricmc.loom.configuration.processors.dependency.RemapData;
|
|
|
|
import net.fabricmc.loom.util.Constants;
|
|
|
|
import net.fabricmc.loom.util.SourceRemapper;
|
2019-04-22 11:41:16 +00:00
|
|
|
|
2020-12-27 16:25:30 +00:00
|
|
|
@SuppressWarnings("UnstableApiUsage")
|
2019-04-22 11:41:16 +00:00
|
|
|
public class ModCompileRemapper {
|
2020-06-01 11:17:58 +00:00
|
|
|
public static void remapDependencies(Project project, String mappingsSuffix, LoomGradleExtension extension, SourceRemapper sourceRemapper) {
|
2019-04-22 11:41:16 +00:00
|
|
|
Logger logger = project.getLogger();
|
|
|
|
DependencyHandler dependencies = project.getDependencies();
|
2020-06-23 18:22:36 +00:00
|
|
|
boolean refreshDeps = project.getGradle().getStartParameter().isRefreshDependencies();
|
2019-04-22 11:41:16 +00:00
|
|
|
|
2020-05-13 18:44:45 +00:00
|
|
|
final File modStore = extension.getRemappedModCache();
|
|
|
|
final RemapData remapData = new RemapData(mappingsSuffix, modStore);
|
|
|
|
|
2020-06-01 11:17:58 +00:00
|
|
|
for (RemappedConfigurationEntry entry : Constants.MOD_COMPILE_ENTRIES) {
|
|
|
|
Configuration sourceConfig = project.getConfigurations().getByName(entry.getSourceConfiguration());
|
|
|
|
Configuration remappedConfig = project.getConfigurations().getByName(entry.getRemappedConfiguration());
|
|
|
|
Configuration regularConfig = project.getConfigurations().getByName(entry.getTargetConfiguration(project.getConfigurations()));
|
2019-04-22 11:41:16 +00:00
|
|
|
|
2020-06-01 11:17:58 +00:00
|
|
|
List<ModDependencyInfo> modDependencies = new ArrayList<>();
|
2019-04-22 11:41:16 +00:00
|
|
|
|
2020-06-01 11:17:58 +00:00
|
|
|
for (ResolvedArtifact artifact : sourceConfig.getResolvedConfiguration().getResolvedArtifacts()) {
|
2020-08-26 18:32:34 +00:00
|
|
|
// TODO: This collection doesn't appear to include FileCollection dependencies
|
|
|
|
// Might have to go based on the dependencies, rather than their resolved form?
|
|
|
|
// File dependencies use SelfResolvingDependency, which appears to be handled differently
|
|
|
|
String group = artifact.getModuleVersion().getId().getGroup();
|
|
|
|
String name = artifact.getModuleVersion().getId().getName();
|
|
|
|
String version = artifact.getModuleVersion().getId().getVersion();
|
2019-04-22 11:41:16 +00:00
|
|
|
|
2020-08-26 18:32:34 +00:00
|
|
|
if (!isFabricMod(logger, artifact)) {
|
|
|
|
addToRegularCompile(project, regularConfig, artifact);
|
2020-06-01 11:17:58 +00:00
|
|
|
continue;
|
|
|
|
}
|
2019-04-22 11:41:16 +00:00
|
|
|
|
2020-12-30 20:12:33 +00:00
|
|
|
ModDependencyInfo info = new ModDependencyInfo(group, name, version, artifact.getClassifier(), artifact.getFile(), remappedConfig, remapData);
|
2020-06-23 18:22:36 +00:00
|
|
|
|
|
|
|
if (refreshDeps) {
|
|
|
|
info.forceRemap();
|
|
|
|
}
|
|
|
|
|
2020-06-01 11:17:58 +00:00
|
|
|
modDependencies.add(info);
|
2019-04-22 11:41:16 +00:00
|
|
|
|
2020-12-30 20:12:33 +00:00
|
|
|
String remappedLog = group + ":" + name + ":" + version + (artifact.getClassifier() == null ? "" : ":" + artifact.getClassifier()) + " (" + mappingsSuffix + ")";
|
2020-06-01 11:17:58 +00:00
|
|
|
project.getLogger().info(":providing " + remappedLog);
|
2019-04-22 11:41:16 +00:00
|
|
|
|
2020-12-30 20:12:33 +00:00
|
|
|
File remappedSources = info.getRemappedOutput("sources");
|
2020-12-27 16:25:30 +00:00
|
|
|
|
|
|
|
if (!remappedSources.exists() || refreshDeps) {
|
|
|
|
File sources = findSources(dependencies, artifact);
|
|
|
|
|
|
|
|
if (sources != null) {
|
2020-12-30 20:12:33 +00:00
|
|
|
scheduleSourcesRemapping(project, sourceRemapper, sources, info.getRemappedNotation(), remappedSources);
|
2020-12-27 16:25:30 +00:00
|
|
|
}
|
2020-06-01 11:17:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
ModProcessor.processMods(project, modDependencies);
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException("Failed to remap mods", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add all of the remapped mods onto the config
|
|
|
|
for (ModDependencyInfo info : modDependencies) {
|
2020-12-30 20:12:33 +00:00
|
|
|
project.getDependencies().add(info.targetConfig.getName(), info.getRemappedNotation());
|
2020-06-01 11:17:58 +00:00
|
|
|
}
|
2019-05-27 20:18:19 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-22 11:41:16 +00:00
|
|
|
|
2019-05-27 20:18:19 +00:00
|
|
|
/**
|
2019-11-02 20:23:27 +00:00
|
|
|
* Checks if an artifact is a fabric mod, according to the presence of a fabric.mod.json.
|
2019-05-27 20:18:19 +00:00
|
|
|
*/
|
2020-08-26 18:32:34 +00:00
|
|
|
private static boolean isFabricMod(Logger logger, ResolvedArtifact artifact) {
|
2019-05-27 20:18:19 +00:00
|
|
|
File input = artifact.getFile();
|
2020-04-27 13:08:37 +00:00
|
|
|
|
|
|
|
try (ZipFile zipFile = new ZipFile(input)) {
|
|
|
|
if (zipFile.getEntry("fabric.mod.json") != null) {
|
2020-08-26 18:32:34 +00:00
|
|
|
logger.info("Found Fabric mod in modCompile: {}", artifact.getId());
|
2020-04-27 13:08:37 +00:00
|
|
|
return true;
|
2019-05-27 20:18:19 +00:00
|
|
|
}
|
2020-04-27 13:08:37 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
} catch (IOException e) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-27 20:18:19 +00:00
|
|
|
}
|
2019-04-22 11:41:16 +00:00
|
|
|
|
2020-08-26 18:32:34 +00:00
|
|
|
private static void addToRegularCompile(Project project, Configuration regularCompile, ResolvedArtifact artifact) {
|
|
|
|
project.getLogger().info(":providing " + artifact);
|
2019-05-27 20:18:19 +00:00
|
|
|
DependencyHandler dependencies = project.getDependencies();
|
2020-08-26 18:32:34 +00:00
|
|
|
Dependency dep = dependencies.module(artifact.getModuleVersion().toString()
|
|
|
|
+ (artifact.getClassifier() == null ? "" : ':' + artifact.getClassifier())); // the owning module of the artifact
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2019-05-27 20:18:19 +00:00
|
|
|
if (dep instanceof ModuleDependency) {
|
|
|
|
((ModuleDependency) dep).setTransitive(false);
|
|
|
|
}
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2019-05-27 20:18:19 +00:00
|
|
|
dependencies.add(regularCompile.getName(), dep);
|
|
|
|
}
|
|
|
|
|
2019-11-13 12:32:44 +00:00
|
|
|
public static File findSources(DependencyHandler dependencies, ResolvedArtifact artifact) {
|
2020-12-27 16:25:30 +00:00
|
|
|
@SuppressWarnings("unchecked") ArtifactResolutionQuery query = dependencies.createArtifactResolutionQuery()
|
|
|
|
.forComponents(artifact.getId().getComponentIdentifier())
|
2019-05-27 20:18:19 +00:00
|
|
|
.withArtifacts(JvmLibrary.class, SourcesArtifact.class);
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2019-05-27 20:18:19 +00:00
|
|
|
for (ComponentArtifactsResult result : query.execute().getResolvedComponents()) {
|
|
|
|
for (ArtifactResult srcArtifact : result.getArtifacts(SourcesArtifact.class)) {
|
|
|
|
if (srcArtifact instanceof ResolvedArtifactResult) {
|
|
|
|
return ((ResolvedArtifactResult) srcArtifact).getFile();
|
|
|
|
}
|
2019-04-22 11:41:16 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2019-05-27 20:18:19 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-12-30 20:12:33 +00:00
|
|
|
private static void scheduleSourcesRemapping(Project project, SourceRemapper sourceRemapper, File sources, String remappedLog, File remappedSources) {
|
2020-07-26 20:32:10 +00:00
|
|
|
project.getLogger().debug(":providing " + remappedLog + " sources");
|
|
|
|
|
2020-06-23 18:22:36 +00:00
|
|
|
boolean refreshDeps = project.getGradle().getStartParameter().isRefreshDependencies();
|
2019-05-27 20:18:19 +00:00
|
|
|
|
2020-06-23 18:22:36 +00:00
|
|
|
if (!remappedSources.exists() || sources.lastModified() <= 0 || sources.lastModified() > remappedSources.lastModified() || refreshDeps) {
|
2020-05-13 18:44:45 +00:00
|
|
|
try {
|
2020-12-21 19:47:08 +00:00
|
|
|
sourceRemapper.scheduleRemapSources(sources, remappedSources, false, true); // Depenedency sources are used in ide only so don't need to be reproducable
|
2019-05-27 20:18:19 +00:00
|
|
|
|
2020-07-26 20:32:10 +00:00
|
|
|
// Set the remapped sources creation date to match the sources if we're likely succeeded in making it
|
2020-05-13 18:44:45 +00:00
|
|
|
remappedSources.setLastModified(sources.lastModified());
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
2019-05-27 20:18:19 +00:00
|
|
|
}
|
2020-05-13 18:44:45 +00:00
|
|
|
} else {
|
|
|
|
project.getLogger().info(remappedSources.getName() + " is up to date with " + sources.getName());
|
|
|
|
}
|
2019-04-22 11:41:16 +00:00
|
|
|
}
|
|
|
|
}
|