1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
/*
* 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.
*/
package net.fabricmc.loom.util;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.gson.JsonObject;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.artifacts.DependencySet;
import org.gradle.api.artifacts.ExternalModuleDependency;
import org.gradle.api.artifacts.repositories.MavenArtifactRepository;
import net.fabricmc.loom.LoomGradleExtension;
import net.fabricmc.loom.providers.MappingsProvider;
import net.fabricmc.loom.util.DependencyProvider.DependencyInfo;
public class LoomDependencyManager {
private static class ProviderList {
private final String key;
private final List<DependencyProvider> providers = new ArrayList<>();
ProviderList(String key) {
this.key = key;
}
}
private final List<DependencyProvider> dependencyProviderList = new ArrayList<>();
public void addProvider(DependencyProvider provider) {
if (dependencyProviderList.contains(provider)) {
throw new RuntimeException("Provider is already registered");
}
if (getProvider(provider.getClass()) != null) {
throw new RuntimeException("Provider of this type is already registered");
}
provider.register(this);
dependencyProviderList.add(provider);
}
public <T> T getProvider(Class<T> clazz) {
for (DependencyProvider provider : dependencyProviderList) {
if (provider.getClass() == clazz) {
return (T) provider;
}
}
return null;
}
public void handleDependencies(Project project) {
List<Runnable> afterTasks = new ArrayList<>();
MappingsProvider mappingsProvider = null;
project.getLogger().lifecycle(":setting up loom dependencies");
LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class);
Map<String, ProviderList> providerListMap = new HashMap<>();
List<ProviderList> targetProviders = new ArrayList<>();
for (DependencyProvider provider : dependencyProviderList) {
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!");
}
for (ProviderList list : targetProviders) {
Configuration configuration = project.getConfigurations().getByName(list.key);
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) {
for (DependencyProvider provider : list.providers) {
DependencyProvider.DependencyInfo info = DependencyInfo.create(project, dependency, configuration);
try {
provider.provide(info, afterTasks::add);
} catch (Exception e) {
throw new RuntimeException("Failed to provide " + dependency.getGroup() + ":" + dependency.getName() + ":" + dependency.getVersion() + " : " + e.getMessage(), e);
}
}
}
}
SourceRemapper sourceRemapper = new SourceRemapper(project, true);
String mappingsKey = mappingsProvider.mappingsName + "." + mappingsProvider.minecraftVersion.replace(' ', '_').replace('.', '_').replace('-', '_') + "." + mappingsProvider.mappingsVersion;
ModCompileRemapper.remapDependencies(project, mappingsKey, extension, sourceRemapper);
if (extension.getInstallerJson() == null) {
//If we've not found the installer JSON we've probably skipped remapping Fabric loader, let's go looking
project.getLogger().info("Searching through modCompileClasspath for installer JSON");
final Configuration configuration = project.getConfigurations().getByName(Constants.Configurations.MOD_COMPILE_CLASSPATH);
for (File input : configuration.resolve()) {
JsonObject jsonObject = ModProcessor.readInstallerJson(input, project);
if (jsonObject != null) {
if (extension.getInstallerJson() != null) {
project.getLogger().info("Found another installer JSON in, ignoring it! " + input);
continue;
}
project.getLogger().info("Found installer JSON in " + input);
extension.setInstallerJson(jsonObject);
}
}
}
if (extension.getInstallerJson() != null) {
handleInstallerJson(extension.getInstallerJson(), project);
} else if (!extension.isForge()) {
project.getLogger().warn("fabric-installer.json not found in classpath!");
}
sourceRemapper.remapAll();
for (Runnable runnable : afterTasks) {
runnable.run();
}
}
private static void handleInstallerJson(JsonObject jsonObject, Project project) {
LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class);
JsonObject libraries = jsonObject.get("libraries").getAsJsonObject();
Configuration mcDepsConfig = project.getConfigurations().getByName(Constants.Configurations.MINECRAFT_DEPENDENCIES);
Configuration apDepsConfig = project.getConfigurations().getByName("annotationProcessor");
libraries.get("common").getAsJsonArray().forEach(jsonElement -> {
String name = jsonElement.getAsJsonObject().get("name").getAsString();
ExternalModuleDependency modDep = (ExternalModuleDependency) project.getDependencies().create(name);
modDep.setTransitive(false);
mcDepsConfig.getDependencies().add(modDep);
if (!extension.ideSync()) {
apDepsConfig.getDependencies().add(modDep);
}
project.getLogger().debug("Loom adding " + name + " from installer JSON");
if (jsonElement.getAsJsonObject().has("url")) {
String url = jsonElement.getAsJsonObject().get("url").getAsString();
long count = project.getRepositories().stream().filter(artifactRepository -> artifactRepository instanceof MavenArtifactRepository)
.map(artifactRepository -> (MavenArtifactRepository) artifactRepository)
.filter(mavenArtifactRepository -> mavenArtifactRepository.getUrl().toString().equalsIgnoreCase(url)).count();
if (count == 0) {
project.getRepositories().maven(mavenArtifactRepository -> mavenArtifactRepository.setUrl(jsonElement.getAsJsonObject().get("url").getAsString()));
}
}
});
}
}
|