2018-11-04 21:28:46 +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.configuration;
|
2018-11-04 21:28:46 +00:00
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2019-04-19 18:14:58 +00:00
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
import com.google.gson.JsonObject;
|
2018-11-04 21:28:46 +00:00
|
|
|
import org.gradle.api.Project;
|
|
|
|
import org.gradle.api.artifacts.Configuration;
|
2020-07-26 20:01:18 +00:00
|
|
|
import org.gradle.api.artifacts.Dependency;
|
|
|
|
import org.gradle.api.artifacts.DependencySet;
|
2018-12-22 09:31:10 +00:00
|
|
|
import org.gradle.api.artifacts.ExternalModuleDependency;
|
|
|
|
import org.gradle.api.artifacts.repositories.MavenArtifactRepository;
|
2018-11-04 21:28:46 +00:00
|
|
|
|
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.build.ModCompileRemapper;
|
|
|
|
import net.fabricmc.loom.configuration.DependencyProvider.DependencyInfo;
|
|
|
|
import net.fabricmc.loom.configuration.mods.ModProcessor;
|
|
|
|
import net.fabricmc.loom.configuration.providers.mappings.MappingsProvider;
|
|
|
|
import net.fabricmc.loom.util.Constants;
|
|
|
|
import net.fabricmc.loom.util.SourceRemapper;
|
2018-11-04 21:28:46 +00:00
|
|
|
|
|
|
|
public class LoomDependencyManager {
|
2019-04-22 11:41:16 +00:00
|
|
|
private static class ProviderList {
|
|
|
|
private final String key;
|
|
|
|
private final List<DependencyProvider> providers = new ArrayList<>();
|
|
|
|
|
|
|
|
ProviderList(String key) {
|
|
|
|
this.key = key;
|
|
|
|
}
|
|
|
|
}
|
2018-11-04 21:28:46 +00:00
|
|
|
|
2020-07-26 20:32:10 +00:00
|
|
|
private final List<DependencyProvider> dependencyProviderList = new ArrayList<>();
|
2018-11-04 21:28:46 +00:00
|
|
|
|
2020-12-21 19:33:23 +00:00
|
|
|
public <T extends DependencyProvider> T addProvider(T provider) {
|
2019-11-02 20:23:27 +00:00
|
|
|
if (dependencyProviderList.contains(provider)) {
|
2018-11-04 21:28:46 +00:00
|
|
|
throw new RuntimeException("Provider is already registered");
|
|
|
|
}
|
2019-11-02 20:23:27 +00:00
|
|
|
|
|
|
|
if (getProvider(provider.getClass()) != null) {
|
2018-11-04 21:28:46 +00:00
|
|
|
throw new RuntimeException("Provider of this type is already registered");
|
|
|
|
}
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2018-11-04 21:28:46 +00:00
|
|
|
provider.register(this);
|
|
|
|
dependencyProviderList.add(provider);
|
2020-12-21 19:33:23 +00:00
|
|
|
return provider;
|
2018-11-04 21:28:46 +00:00
|
|
|
}
|
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
public <T> T getProvider(Class<T> clazz) {
|
|
|
|
for (DependencyProvider provider : dependencyProviderList) {
|
|
|
|
if (provider.getClass() == clazz) {
|
2018-11-04 21:28:46 +00:00
|
|
|
return (T) provider;
|
|
|
|
}
|
|
|
|
}
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2018-11-04 21:28:46 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
public void handleDependencies(Project project) {
|
2018-12-22 14:29:46 +00:00
|
|
|
List<Runnable> afterTasks = new ArrayList<>();
|
|
|
|
|
2019-04-22 11:41:16 +00:00
|
|
|
MappingsProvider mappingsProvider = null;
|
|
|
|
|
2020-12-27 16:25:30 +00:00
|
|
|
project.getLogger().info(":setting up loom dependencies");
|
2018-11-04 21:28:46 +00:00
|
|
|
LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class);
|
2019-04-22 11:41:16 +00:00
|
|
|
Map<String, ProviderList> providerListMap = new HashMap<>();
|
|
|
|
List<ProviderList> targetProviders = new ArrayList<>();
|
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
for (DependencyProvider provider : dependencyProviderList) {
|
2019-04-22 11:41:16 +00:00
|
|
|
providerListMap.computeIfAbsent(provider.getTargetConfig(), (k) -> {
|
|
|
|
ProviderList list = new ProviderList(k);
|
|
|
|
targetProviders.add(list);
|
|
|
|
return list;
|
|
|
|
}).providers.add(provider);
|
|
|
|
|
|
|
|
if (provider instanceof MappingsProvider) {
|
|
|
|
mappingsProvider = (MappingsProvider) provider;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mappingsProvider == null) {
|
|
|
|
throw new RuntimeException("Could not find MappingsProvider instance!");
|
2018-11-04 21:28:46 +00:00
|
|
|
}
|
2019-04-22 11:41:16 +00:00
|
|
|
|
|
|
|
for (ProviderList list : targetProviders) {
|
|
|
|
Configuration configuration = project.getConfigurations().getByName(list.key);
|
2020-07-26 20:01:18 +00:00
|
|
|
DependencySet dependencies = configuration.getDependencies();
|
|
|
|
|
|
|
|
if (dependencies.isEmpty()) {
|
|
|
|
throw new IllegalArgumentException(String.format("No '%s' dependency was specified!", list.key));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dependencies.size() > 1) {
|
|
|
|
throw new IllegalArgumentException(String.format("Only one '%s' dependency should be specified, but %d were!",
|
|
|
|
list.key,
|
|
|
|
dependencies.size())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Dependency dependency : dependencies) {
|
2019-04-22 11:41:16 +00:00
|
|
|
for (DependencyProvider provider : list.providers) {
|
|
|
|
DependencyProvider.DependencyInfo info = DependencyInfo.create(project, dependency, configuration);
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2019-04-22 11:41:16 +00:00
|
|
|
try {
|
0.2.7 refactors (#178)
* Rough work on project based jars, skeleton for AccessEscalators?
* First working draft
* Minor changes
* Add support for mutable, better error checking when parsing file.
Code cleanup
Remap if needed when reading
* Fix inner classes and genSources
* Fix CME
* Caching, only regen jar when input changes
* Some work, untested
* Fix writing, fix checkstyle issues
* More fixes
* Move jars into a maven file structure, cleans up the file structure, and will benefit idea 2020
Add some basic validation to the AccessWidenerRemapper, will present any issues with the mappings when building (May need a way to disable?)
+ Some bugs fixes
* Fix issues with source jars in idea 2020, should be backwards compatible with 2019
* Move to lorenz-tiny
* Build fix + small cleanup
* Remove accesswidener's for now
* Update dev launch injector, should fix all issues with spaces in the path.
2020-03-06 11:15:34 +00:00
|
|
|
provider.provide(info, afterTasks::add);
|
2019-04-22 11:41:16 +00:00
|
|
|
} catch (Exception e) {
|
2021-01-19 21:58:39 +00:00
|
|
|
throw new RuntimeException("Failed to provide " + dependency.getGroup() + ":" + dependency.getName() + ":" + dependency.getVersion() + " : " + e.toString(), e);
|
2018-11-04 21:28:46 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-26 20:01:18 +00:00
|
|
|
}
|
2018-12-22 09:31:10 +00:00
|
|
|
}
|
2018-11-04 21:28:46 +00:00
|
|
|
|
2020-05-13 18:44:45 +00:00
|
|
|
SourceRemapper sourceRemapper = new SourceRemapper(project, true);
|
2020-12-21 19:33:23 +00:00
|
|
|
String mappingsKey = mappingsProvider.getMappingsKey();
|
2020-05-13 18:44:45 +00:00
|
|
|
|
2019-04-20 17:47:32 +00:00
|
|
|
if (extension.getInstallerJson() == null) {
|
|
|
|
//If we've not found the installer JSON we've probably skipped remapping Fabric loader, let's go looking
|
2019-11-12 18:46:57 +00:00
|
|
|
project.getLogger().info("Searching through modCompileClasspath for installer JSON");
|
2020-09-21 19:22:31 +00:00
|
|
|
final Configuration configuration = project.getConfigurations().getByName(Constants.Configurations.MOD_COMPILE_CLASSPATH);
|
2019-04-20 17:47:32 +00:00
|
|
|
|
2019-11-12 18:46:57 +00:00
|
|
|
for (File input : configuration.resolve()) {
|
|
|
|
JsonObject jsonObject = ModProcessor.readInstallerJson(input, project);
|
2019-04-22 11:41:16 +00:00
|
|
|
|
2019-11-12 18:46:57 +00:00
|
|
|
if (jsonObject != null) {
|
|
|
|
if (extension.getInstallerJson() != null) {
|
|
|
|
project.getLogger().info("Found another installer JSON in, ignoring it! " + input);
|
|
|
|
continue;
|
2019-04-22 11:41:16 +00:00
|
|
|
}
|
2019-11-12 18:46:57 +00:00
|
|
|
|
|
|
|
project.getLogger().info("Found installer JSON in " + input);
|
|
|
|
extension.setInstallerJson(jsonObject);
|
2020-12-21 19:34:00 +00:00
|
|
|
handleInstallerJson(extension.getInstallerJson(), project);
|
2019-04-20 17:47:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-21 19:34:00 +00:00
|
|
|
if (extension.getInstallerJson() == null) {
|
2018-12-22 09:31:10 +00:00
|
|
|
project.getLogger().warn("fabric-installer.json not found in classpath!");
|
2018-11-04 21:28:46 +00:00
|
|
|
}
|
2018-12-22 14:29:46 +00:00
|
|
|
|
2020-12-21 19:34:00 +00:00
|
|
|
ModCompileRemapper.remapDependencies(project, mappingsKey, extension, sourceRemapper);
|
|
|
|
|
2020-05-13 18:44:45 +00:00
|
|
|
sourceRemapper.remapAll();
|
|
|
|
|
2018-12-22 14:29:46 +00:00
|
|
|
for (Runnable runnable : afterTasks) {
|
|
|
|
runnable.run();
|
|
|
|
}
|
2018-11-04 21:28:46 +00:00
|
|
|
}
|
2018-12-22 09:31:10 +00:00
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
private static void handleInstallerJson(JsonObject jsonObject, Project project) {
|
2019-12-13 18:24:01 +00:00
|
|
|
LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class);
|
|
|
|
|
2018-12-22 09:31:10 +00:00
|
|
|
JsonObject libraries = jsonObject.get("libraries").getAsJsonObject();
|
2020-12-21 19:34:00 +00:00
|
|
|
Configuration loaderDepsConfig = project.getConfigurations().getByName(Constants.Configurations.LOADER_DEPENDENCIES);
|
2019-05-27 20:14:35 +00:00
|
|
|
Configuration apDepsConfig = project.getConfigurations().getByName("annotationProcessor");
|
|
|
|
|
2018-12-22 09:31:10 +00:00
|
|
|
libraries.get("common").getAsJsonArray().forEach(jsonElement -> {
|
|
|
|
String name = jsonElement.getAsJsonObject().get("name").getAsString();
|
|
|
|
|
|
|
|
ExternalModuleDependency modDep = (ExternalModuleDependency) project.getDependencies().create(name);
|
|
|
|
modDep.setTransitive(false);
|
2020-12-21 19:34:00 +00:00
|
|
|
loaderDepsConfig.getDependencies().add(modDep);
|
2019-12-13 18:24:01 +00:00
|
|
|
|
|
|
|
if (!extension.ideSync()) {
|
|
|
|
apDepsConfig.getDependencies().add(modDep);
|
|
|
|
}
|
2019-05-27 20:14:35 +00:00
|
|
|
|
|
|
|
project.getLogger().debug("Loom adding " + name + " from installer JSON");
|
2018-12-22 09:31:10 +00:00
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
if (jsonElement.getAsJsonObject().has("url")) {
|
2018-12-22 09:31:10 +00:00
|
|
|
String url = jsonElement.getAsJsonObject().get("url").getAsString();
|
2019-11-02 20:23:27 +00:00
|
|
|
long count = project.getRepositories().stream().filter(artifactRepository -> artifactRepository instanceof MavenArtifactRepository)
|
2018-12-22 09:31:10 +00:00
|
|
|
.map(artifactRepository -> (MavenArtifactRepository) artifactRepository)
|
|
|
|
.filter(mavenArtifactRepository -> mavenArtifactRepository.getUrl().toString().equalsIgnoreCase(url)).count();
|
2019-11-02 20:23:27 +00:00
|
|
|
|
|
|
|
if (count == 0) {
|
2018-12-22 09:31:10 +00:00
|
|
|
project.getRepositories().maven(mavenArtifactRepository -> mavenArtifactRepository.setUrl(jsonElement.getAsJsonObject().get("url").getAsString()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-11-04 21:28:46 +00:00
|
|
|
}
|