forked from FabricMC/fabric-loom
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAbstractPlugin.java
More file actions
409 lines (340 loc) · 17 KB
/
AbstractPlugin.java
File metadata and controls
409 lines (340 loc) · 17 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
* 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;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import com.google.common.collect.ImmutableMap;
import groovy.util.Node;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.UnknownTaskException;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.artifacts.repositories.FlatDirectoryArtifactRepository;
import org.gradle.api.artifacts.repositories.MavenArtifactRepository;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.publish.Publication;
import org.gradle.api.publish.PublishingExtension;
import org.gradle.api.publish.maven.MavenPublication;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.bundling.AbstractArchiveTask;
import org.gradle.api.tasks.compile.JavaCompile;
import org.gradle.api.tasks.javadoc.Javadoc;
import org.gradle.api.tasks.scala.ScalaCompile;
import org.gradle.plugins.ide.eclipse.model.EclipseModel;
import org.gradle.plugins.ide.idea.model.IdeaModel;
import net.fabricmc.loom.dependencies.LoomDependencyManager;
import net.fabricmc.loom.dependencies.RemappedConfigurationEntry;
import net.fabricmc.loom.providers.LaunchProvider;
import net.fabricmc.loom.providers.MappedModsProvider;
import net.fabricmc.loom.providers.MinecraftMappedProvider;
import net.fabricmc.loom.providers.MinecraftProvider;
import net.fabricmc.loom.providers.StackedMappingsProvider;
import net.fabricmc.loom.task.RemapJarTask;
import net.fabricmc.loom.task.RemapSourcesJarTask;
import net.fabricmc.loom.util.AccessTransformerHelper;
import net.fabricmc.loom.util.Constants;
import net.fabricmc.loom.util.GroovyXmlUtil;
import net.fabricmc.loom.util.NestedJars;
import net.fabricmc.loom.util.SetupIntelijRunConfigs;
public class AbstractPlugin implements Plugin<Project> {
protected Project project;
public static boolean isRootProject(Project project) {
return project.getRootProject() == project;
}
private void extendsFrom(String a, String b) {
project.getConfigurations().getByName(a).extendsFrom(project.getConfigurations().getByName(b));
}
@Override
public void apply(Project target) {
this.project = target;
project.getLogger().lifecycle("Fabric Loom: " + AbstractPlugin.class.getPackage().getImplementationVersion());
// Apply default plugins
project.apply(ImmutableMap.of("plugin", "java"));
project.apply(ImmutableMap.of("plugin", "eclipse"));
project.apply(ImmutableMap.of("plugin", "idea"));
LoomGradleExtension extension = project.getExtensions().create("minecraft", LoomGradleExtension.class, project);
// Add default repositories
addDirectoryRepo(target, "UserCacheFiles", extension.getUserCache());
addDirectoryRepo(target, "UserLocalCacheFiles", extension.getRootProjectBuildCache());
addDirectoryRepo(target, "UserLocalRemappedMods", extension.getRemappedModCache());
addMavenRepo(target, "Fabric", "https://maven.fabricmc.net/");
//addMavenRepo(target, "SpongePowered", "http://repo.spongepowered.org/maven/");
addMavenRepo(target, "Mojang", "https://libraries.minecraft.net/");
target.getRepositories().mavenCentral();
target.getRepositories().jcenter();
// Create default configurations
Configuration modCompileClasspathConfig = project.getConfigurations().maybeCreate(Constants.MOD_COMPILE_CLASSPATH);
modCompileClasspathConfig.setTransitive(true);
Configuration modCompileClasspathMappedConfig = project.getConfigurations().maybeCreate(Constants.MOD_COMPILE_CLASSPATH_MAPPED);
modCompileClasspathMappedConfig.setTransitive(false);
Configuration minecraftNamedConfig = project.getConfigurations().maybeCreate(Constants.MINECRAFT_NAMED);
minecraftNamedConfig.setTransitive(false); // The launchers do not recurse dependencies
Configuration minecraftIntermediaryConfig = project.getConfigurations().maybeCreate(Constants.MINECRAFT_INTERMEDIARY);
minecraftIntermediaryConfig.setTransitive(false);
Configuration minecraftDependenciesConfig = project.getConfigurations().maybeCreate(Constants.MINECRAFT_DEPENDENCIES);
minecraftDependenciesConfig.setTransitive(false);
Configuration minecraftConfig = project.getConfigurations().maybeCreate(Constants.MINECRAFT);
minecraftConfig.setTransitive(false);
Configuration includeConfig = project.getConfigurations().maybeCreate(Constants.INCLUDE);
includeConfig.setTransitive(false); // Dont get transitive deps
project.getConfigurations().maybeCreate(Constants.MAPPINGS);
project.getConfigurations().maybeCreate(Constants.MAPPINGS_RAW);
for (RemappedConfigurationEntry entry : Constants.MOD_COMPILE_ENTRIES) {
Configuration compileModsConfig = project.getConfigurations().maybeCreate(entry.getSourceConfiguration());
compileModsConfig.setTransitive(true);
Configuration compileModsMappedConfig = project.getConfigurations().maybeCreate(entry.getRemappedConfiguration());
compileModsMappedConfig.setTransitive(false); // Don't get transitive deps of already remapped mods
extendsFrom(entry.getTargetConfiguration(project.getConfigurations()), entry.getRemappedConfiguration());
if (entry.isOnModCompileClasspath()) {
extendsFrom(Constants.MOD_COMPILE_CLASSPATH, entry.getSourceConfiguration());
extendsFrom(Constants.MOD_COMPILE_CLASSPATH_MAPPED, entry.getRemappedConfiguration());
}
}
extendsFrom("compile", Constants.MINECRAFT_NAMED);
if (!extension.ideSync()) {
extendsFrom("annotationProcessor", Constants.MINECRAFT_NAMED);
extendsFrom("annotationProcessor", Constants.MOD_COMPILE_CLASSPATH_MAPPED);
}
extendsFrom(Constants.MINECRAFT_NAMED, Constants.MINECRAFT_DEPENDENCIES);
extendsFrom(Constants.MINECRAFT_INTERMEDIARY, Constants.MINECRAFT_DEPENDENCIES);
extendsFrom("compile", Constants.MAPPINGS);
if (!extension.ideSync()) {
extendsFrom("annotationProcessor", Constants.MAPPINGS);
}
configureIDEs();
configureCompile();
configureScala();
project.getTasks().withType(JavaCompile.class, javaCompileTask -> {
if (!javaCompileTask.getName().contains("Test") && !javaCompileTask.getName().contains("test")) {
javaCompileTask.doFirst(task -> {
project.getLogger().lifecycle(":setting java compiler args");
try {
Collections.addAll(javaCompileTask.getOptions().getCompilerArgs(),
"-AinMapFileNamedIntermediary=" + extension.getMappingsProvider().MAPPINGS_TINY.getCanonicalPath(),
"-AoutMapFileNamedIntermediary=" + extension.getMappingsProvider().MAPPINGS_MIXIN_EXPORT.getCanonicalPath(),
"-AoutRefMapFile=" + new File(javaCompileTask.getDestinationDir(), extension.getRefmapName(task)).getCanonicalPath(),
"-AdefaultObfuscationEnv=named:intermediary");
} catch (IOException e) {
e.printStackTrace();
}
});
}
});
configureMaven();
}
public Project getProject() {
return project;
}
protected void configureScala() {
project.afterEvaluate(proj -> {
if (project.getPluginManager().hasPlugin("scala")) {
ScalaCompile task = (ScalaCompile) project.getTasks().getByName("compileScala");
LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class);
project.getLogger().warn(":configuring scala compilation processing");
try {
task.getOptions().getCompilerArgs().add("-AinMapFileNamedIntermediary=" + extension.getMappingsProvider().MAPPINGS_TINY.getCanonicalPath());
task.getOptions().getCompilerArgs().add("-AoutMapFileNamedIntermediary=" + extension.getMappingsProvider().MAPPINGS_MIXIN_EXPORT.getCanonicalPath());
task.getOptions().getCompilerArgs().add("-AoutRefMapFile=" + new File(task.getDestinationDir(), extension.getRefmapName(task)).getCanonicalPath());
task.getOptions().getCompilerArgs().add("-AdefaultObfuscationEnv=named:intermediary");
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
/**
* Permit to add a Maven repository to a target project.
*
* @param target The garget project
* @param name The name of the repository
* @param url The URL of the repository
* @return An object containing the name and the URL of the repository that can be modified later
*/
public static MavenArtifactRepository addMavenRepo(Project target, final String name, final String url) {
return target.getRepositories().maven(repo -> {
repo.setName(name);
repo.setUrl(url);
});
}
public static FlatDirectoryArtifactRepository addDirectoryRepo(Project target, final String name, final Object directory) {
return target.getRepositories().flatDir(repo -> {
repo.setName(name);
repo.dir(directory);
});
}
/**
* Add Minecraft dependencies to IDE dependencies.
*/
protected void configureIDEs() {
// IDEA
IdeaModel ideaModel = (IdeaModel) project.getExtensions().getByName("idea");
ideaModel.getModule().getExcludeDirs().addAll(project.files(".gradle", "build", ".idea", "out").getFiles());
ideaModel.getModule().setDownloadJavadoc(true);
ideaModel.getModule().setDownloadSources(true);
ideaModel.getModule().setInheritOutputDirs(true);
// ECLIPSE
EclipseModel eclipseModel = (EclipseModel) project.getExtensions().getByName("eclipse");
}
public static class JarSettings {
public boolean includeAT = true;
public void setInclude(boolean flag) {
includeAT = flag;
}
}
/**
* Add Minecraft dependencies to compile time.
*/
protected void configureCompile() {
JavaPluginConvention javaModule = (JavaPluginConvention) project.getConvention().getPlugins().get("java");
SourceSet main = javaModule.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME);
SourceSet test = javaModule.getSourceSets().getByName(SourceSet.TEST_SOURCE_SET_NAME);
Javadoc javadoc = (Javadoc) project.getTasks().getByName(JavaPlugin.JAVADOC_TASK_NAME);
javadoc.setClasspath(main.getOutput().plus(main.getCompileClasspath()));
project.getTasks().getByName("jar").getExtensions().create("AT", JarSettings.class);
project.getTasks().whenTaskAdded(task -> {
if (task instanceof AbstractArchiveTask) {
task.getExtensions().create("AT", JarSettings.class);
//Only include the AT by default to the main sources task
if (!"sourcesJar".equals(task.getName())) task.getExtensions().getByType(JarSettings.class).setInclude(false);
}
});
if (!project.getExtensions().getByType(LoomGradleExtension.class).ideSync()) {
// Add Mixin dependencies
project.getDependencies().add(JavaPlugin.ANNOTATION_PROCESSOR_CONFIGURATION_NAME, "net.fabricmc:fabric-mixin-compile-extensions:" + Constants.MIXIN_COMPILE_EXTENSIONS_VERSION);
}
project.afterEvaluate(project1 -> {
LoomGradleExtension extension = project1.getExtensions().getByType(LoomGradleExtension.class);
LoomDependencyManager dependencyManager = new LoomDependencyManager();
extension.setDependencyManager(dependencyManager);
dependencyManager.addProvider(new MinecraftProvider());
dependencyManager.addProvider(new StackedMappingsProvider());
dependencyManager.addProvider(new MinecraftMappedProvider());
dependencyManager.addProvider(new MappedModsProvider());
dependencyManager.addProvider(new LaunchProvider());
dependencyManager.handleDependencies(project1);
project1.getTasks().getByName("idea").finalizedBy(project1.getTasks().getByName("genIdeaWorkspace"));
project1.getTasks().getByName("eclipse").finalizedBy(project1.getTasks().getByName("genEclipseRuns"));
if (extension.autoGenIDERuns && isRootProject(project1)) {
SetupIntelijRunConfigs.setup(project1);
}
// Enables the default mod remapper
if (extension.remapMod) {
AbstractArchiveTask jarTask = (AbstractArchiveTask) project1.getTasks().getByName("jar");
RemapJarTask remapJarTask = (RemapJarTask) project1.getTasks().findByName("remapJar");
assert remapJarTask != null;
if (!remapJarTask.getInput().isPresent()) {
jarTask.setClassifier("dev");
remapJarTask.setClassifier("");
remapJarTask.getInput().set(jarTask.getArchivePath());
remapJarTask.setIncludeAT(jarTask.getExtensions().getByType(JarSettings.class).includeAT);
}
extension.addUnmappedMod(jarTask.getArchivePath().toPath());
remapJarTask.setAddNestedDependencies(true);
project1.getArtifacts().add("archives", remapJarTask);
remapJarTask.dependsOn(jarTask);
project1.getTasks().getByName("build").dependsOn(remapJarTask);
Map<Project, Set<Task>> taskMap = project.getAllTasks(true);
for (Map.Entry<Project, Set<Task>> entry : taskMap.entrySet()) {
Set<Task> taskSet = entry.getValue();
for (Task task : taskSet) {
if (task instanceof RemapJarTask && ((RemapJarTask) task).isAddNestedDependencies()) {
//Run all the sub project remap jars tasks before the root projects jar, this is to allow us to include projects
NestedJars.getRequiredTasks(project1).forEach(task::dependsOn);
}
if (task instanceof AbstractArchiveTask && task.getExtensions().findByType(JarSettings.class) != null) {
if (task.getExtensions().getByType(JarSettings.class).includeAT) {
AccessTransformerHelper.copyInAT(extension, (AbstractArchiveTask) task);
}
}
}
}
try {
AbstractArchiveTask sourcesTask = (AbstractArchiveTask) project1.getTasks().getByName("sourcesJar");
RemapSourcesJarTask remapSourcesJarTask = (RemapSourcesJarTask) project1.getTasks().findByName("remapSourcesJar");
remapSourcesJarTask.setInput(sourcesTask.getArchivePath());
remapSourcesJarTask.setOutput(sourcesTask.getArchivePath());
remapSourcesJarTask.doLast(task -> project1.getArtifacts().add("archives", remapSourcesJarTask.getOutput()));
remapSourcesJarTask.dependsOn(project1.getTasks().getByName("sourcesJar"));
project1.getTasks().getByName("build").dependsOn(remapSourcesJarTask);
} catch (UnknownTaskException e) {
// pass
}
} else {
AbstractArchiveTask jarTask = (AbstractArchiveTask) project1.getTasks().getByName("jar");
extension.addUnmappedMod(jarTask.getArchivePath().toPath());
}
});
}
protected void configureMaven() {
project.afterEvaluate((p) -> {
for (RemappedConfigurationEntry entry : Constants.MOD_COMPILE_ENTRIES) {
if (!entry.hasMavenScope()) {
continue;
}
Configuration compileModsConfig = p.getConfigurations().getByName(entry.getSourceConfiguration());
// add modsCompile to maven-publish
PublishingExtension mavenPublish = p.getExtensions().findByType(PublishingExtension.class);
if (mavenPublish != null) {
mavenPublish.publications((publications) -> {
for (Publication publication : publications) {
if (publication instanceof MavenPublication) {
((MavenPublication) publication).pom((pom) -> {
pom.withXml((xml) -> {
Node dependencies = GroovyXmlUtil.getOrCreateNode(xml.asNode(), "dependencies");
Set<String> foundArtifacts = new HashSet<>();
GroovyXmlUtil.childrenNodesStream(dependencies).filter((n) -> "dependency".equals(n.name())).forEach((n) -> {
Optional<Node> groupId = GroovyXmlUtil.getNode(n, "groupId");
Optional<Node> artifactId = GroovyXmlUtil.getNode(n, "artifactId");
if (groupId.isPresent() && artifactId.isPresent()) {
foundArtifacts.add(groupId.get().text() + ":" + artifactId.get().text());
}
});
for (Dependency dependency : compileModsConfig.getAllDependencies()) {
if (foundArtifacts.contains(dependency.getGroup() + ":" + dependency.getName())) {
continue;
}
Node depNode = dependencies.appendNode("dependency");
depNode.appendNode("groupId", dependency.getGroup());
depNode.appendNode("artifactId", dependency.getName());
depNode.appendNode("version", dependency.getVersion());
depNode.appendNode("scope", entry.getMavenScope());
}
});
});
}
}
});
}
}
});
}
}