2016-08-16 22:29:10 +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-08-16 22:29:10 +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.
|
|
|
|
*/
|
|
|
|
|
2016-08-16 23:30:48 +00:00
|
|
|
package net.fabricmc.loom;
|
2016-08-15 10:22:14 +00:00
|
|
|
|
2016-08-17 16:38:54 +00:00
|
|
|
import com.google.common.collect.ImmutableMap;
|
2016-08-16 23:30:48 +00:00
|
|
|
import com.google.gson.Gson;
|
2016-08-29 10:08:23 +00:00
|
|
|
import com.google.gson.JsonElement;
|
|
|
|
import com.google.gson.JsonObject;
|
2016-08-16 23:30:48 +00:00
|
|
|
import net.fabricmc.loom.task.DownloadTask;
|
|
|
|
import net.fabricmc.loom.util.Constants;
|
2016-10-20 15:55:46 +00:00
|
|
|
import net.fabricmc.loom.util.ModRemapper;
|
2016-08-16 23:30:48 +00:00
|
|
|
import net.fabricmc.loom.util.Version;
|
2016-08-15 10:22:14 +00:00
|
|
|
import org.gradle.api.Plugin;
|
|
|
|
import org.gradle.api.Project;
|
|
|
|
import org.gradle.api.Task;
|
2018-10-29 11:55:30 +00:00
|
|
|
import org.gradle.api.artifacts.Configuration;
|
2016-08-15 10:22:14 +00:00
|
|
|
import org.gradle.api.artifacts.repositories.MavenArtifactRepository;
|
|
|
|
import org.gradle.api.plugins.JavaPlugin;
|
|
|
|
import org.gradle.api.plugins.JavaPluginConvention;
|
|
|
|
import org.gradle.api.tasks.SourceSet;
|
2018-05-14 10:22:50 +00:00
|
|
|
import org.gradle.api.tasks.compile.JavaCompile;
|
2016-08-15 10:22:14 +00:00
|
|
|
import org.gradle.api.tasks.javadoc.Javadoc;
|
2018-11-01 11:15:09 +00:00
|
|
|
import org.gradle.plugins.ide.api.GeneratorTask;
|
2016-08-15 10:22:14 +00:00
|
|
|
import org.gradle.plugins.ide.eclipse.model.EclipseModel;
|
|
|
|
import org.gradle.plugins.ide.idea.model.IdeaModel;
|
|
|
|
|
2016-08-29 10:08:23 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileNotFoundException;
|
2016-08-16 23:30:48 +00:00
|
|
|
import java.io.FileReader;
|
|
|
|
import java.io.IOException;
|
2018-05-14 10:22:50 +00:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
2016-08-16 23:30:48 +00:00
|
|
|
|
2016-08-15 10:22:14 +00:00
|
|
|
public class AbstractPlugin implements Plugin<Project> {
|
2016-08-17 16:38:54 +00:00
|
|
|
protected Project project;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void apply(Project target) {
|
|
|
|
this.project = target;
|
|
|
|
|
2018-11-03 18:03:43 +00:00
|
|
|
project.getLogger().lifecycle("Fabric Loom: " + AbstractPlugin.class.getPackage().getImplementationVersion());
|
|
|
|
|
2016-08-17 16:38:54 +00:00
|
|
|
// Apply default plugins
|
|
|
|
project.apply(ImmutableMap.of("plugin", "java"));
|
|
|
|
project.apply(ImmutableMap.of("plugin", "eclipse"));
|
|
|
|
project.apply(ImmutableMap.of("plugin", "idea"));
|
|
|
|
|
|
|
|
project.getExtensions().create("minecraft", LoomGradleExtension.class);
|
2016-08-25 11:42:23 +00:00
|
|
|
project.getExtensions().getByType(LoomGradleExtension.class).project = project;
|
2016-08-17 16:38:54 +00:00
|
|
|
|
2016-09-28 15:51:31 +00:00
|
|
|
LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class);
|
|
|
|
readModJson(extension);
|
2016-08-29 10:08:23 +00:00
|
|
|
|
2016-08-17 16:38:54 +00:00
|
|
|
// Force add Mojang repository
|
|
|
|
addMavenRepo(target, "Mojang", "https://libraries.minecraft.net/");
|
|
|
|
|
|
|
|
// Minecraft libraries configuration
|
2018-10-26 22:43:39 +00:00
|
|
|
project.getConfigurations().maybeCreate(Constants.CONFIG_MINECRAFT);
|
2016-08-17 16:38:54 +00:00
|
|
|
project.getConfigurations().maybeCreate(Constants.CONFIG_MC_DEPENDENCIES);
|
|
|
|
project.getConfigurations().maybeCreate(Constants.CONFIG_MC_DEPENDENCIES_CLIENT);
|
|
|
|
project.getConfigurations().maybeCreate(Constants.CONFIG_NATIVES);
|
2018-10-29 11:55:30 +00:00
|
|
|
Configuration compileModsConfig = project.getConfigurations().maybeCreate(Constants.COMPILE_MODS);
|
2016-08-17 16:38:54 +00:00
|
|
|
|
2018-10-29 11:55:30 +00:00
|
|
|
compileModsConfig.setTransitive(false); //Dont get transitive deps of mods
|
2016-10-09 12:18:00 +00:00
|
|
|
|
2016-08-17 16:38:54 +00:00
|
|
|
// Common libraries extends from client libraries, CONFIG_MC_DEPENDENCIES will contains all MC dependencies
|
2018-10-26 22:43:39 +00:00
|
|
|
project.getConfigurations().getByName(Constants.CONFIG_MINECRAFT).extendsFrom(project.getConfigurations().getByName(Constants.CONFIG_MC_DEPENDENCIES).extendsFrom(project.getConfigurations().getByName(Constants.CONFIG_MC_DEPENDENCIES_CLIENT)));
|
2016-08-17 16:38:54 +00:00
|
|
|
|
|
|
|
configureIDEs();
|
|
|
|
configureCompile();
|
2018-05-14 10:17:26 +00:00
|
|
|
|
2018-10-22 18:46:42 +00:00
|
|
|
Map<Project, Set<Task>> taskMap = project.getAllTasks(true);
|
|
|
|
for (Map.Entry<Project, Set<Task>> entry : taskMap.entrySet()) {
|
|
|
|
Project project = entry.getKey();
|
|
|
|
Set<Task> taskSet = entry.getValue();
|
|
|
|
for (Task task : taskSet) {
|
2018-11-01 11:15:09 +00:00
|
|
|
if (task instanceof GeneratorTask) {
|
|
|
|
task.dependsOn("setup");
|
|
|
|
}
|
|
|
|
|
2018-10-22 18:46:42 +00:00
|
|
|
if (task instanceof JavaCompile
|
|
|
|
&& !(task.getName().contains("Test")) && !(task.getName().contains("test"))) {
|
|
|
|
JavaCompile javaCompileTask = (JavaCompile) task;
|
|
|
|
javaCompileTask.doFirst(task1 -> {
|
|
|
|
project.getLogger().lifecycle(":setting java compiler args");
|
|
|
|
try {
|
2018-11-02 09:33:03 +00:00
|
|
|
javaCompileTask.getClasspath().add(project.getConfigurations().getByName(Constants.CONFIG_MINECRAFT));
|
2018-10-22 18:46:42 +00:00
|
|
|
javaCompileTask.getClasspath().add(target.files(this.getClass().getProtectionDomain().getCodeSource().getLocation()));
|
|
|
|
|
2018-11-02 16:19:57 +00:00
|
|
|
javaCompileTask.getOptions().getCompilerArgs().add("-AinMapFileNamedIntermediary=" + Constants.MAPPINGS_TINY.get(extension).getCanonicalPath());
|
|
|
|
javaCompileTask.getOptions().getCompilerArgs().add("-AoutMapFileNamedIntermediary=" + Constants.MAPPINGS_MIXIN_EXPORT.get(extension).getCanonicalPath());
|
2018-10-22 18:46:42 +00:00
|
|
|
if(extension.refmapName == null || extension.refmapName.isEmpty()){
|
|
|
|
project.getLogger().error("Could not find refmap definition, will be using default name: " + project.getName() + "-refmap.json");
|
|
|
|
extension.refmapName = project.getName() + "-refmap.json";
|
|
|
|
}
|
|
|
|
javaCompileTask.getOptions().getCompilerArgs().add("-AoutRefMapFile=" + new File(javaCompileTask.getDestinationDir(), extension.refmapName).getCanonicalPath());
|
2018-11-02 16:19:57 +00:00
|
|
|
javaCompileTask.getOptions().getCompilerArgs().add("-AdefaultObfuscationEnv=named:intermediary");
|
2018-10-22 18:46:42 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-08-17 16:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Permit to create a Task instance of the type in the project
|
|
|
|
*
|
|
|
|
* @param name The name of the task
|
|
|
|
* @param type The type of the task that will be used to create an instance
|
|
|
|
* @return The created task object for the project
|
|
|
|
*/
|
|
|
|
public <T extends Task> T makeTask(String name, Class<T> type) {
|
|
|
|
return makeTask(project, name, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Permit to create a Task instance of the type in a project
|
|
|
|
*
|
|
|
|
* @param target The target project
|
|
|
|
* @param name The name of the task
|
|
|
|
* @param type The type of the task that will be used to create an instance
|
|
|
|
* @return The created task object for the specified project
|
|
|
|
*/
|
|
|
|
public static <T extends Task> T makeTask(Project target, String name, Class<T> type) {
|
|
|
|
return target.getTasks().create(name, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 MavenArtifactRepository addMavenRepo(Project target, final String name, final String url) {
|
|
|
|
return target.getRepositories().maven(repo -> {
|
|
|
|
repo.setName(name);
|
|
|
|
repo.setUrl(url);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add Minecraft dependencies to IDE dependencies
|
|
|
|
*/
|
|
|
|
protected void configureIDEs() {
|
|
|
|
// IDEA
|
2018-11-01 11:15:09 +00:00
|
|
|
IdeaModel ideaModel = (IdeaModel) project.getExtensions().getByName("idea");
|
2016-08-17 16:38:54 +00:00
|
|
|
|
2018-11-01 11:15:09 +00:00
|
|
|
ideaModel.getModule().getExcludeDirs().addAll(project.files(".gradle", "build", ".idea", "out").getFiles());
|
|
|
|
ideaModel.getModule().setDownloadJavadoc(true);
|
|
|
|
ideaModel.getModule().setDownloadSources(true);
|
|
|
|
ideaModel.getModule().setInheritOutputDirs(true);
|
|
|
|
ideaModel.getModule().getScopes().get("COMPILE").get("plus").add(project.getConfigurations().getByName(Constants.CONFIG_MINECRAFT));
|
2016-08-17 16:38:54 +00:00
|
|
|
|
|
|
|
// ECLIPSE
|
2018-11-01 11:15:09 +00:00
|
|
|
EclipseModel eclipseModel = (EclipseModel) project.getExtensions().getByName("eclipse");
|
|
|
|
eclipseModel.getClasspath().getPlusConfigurations().add(project.getConfigurations().getByName(Constants.CONFIG_MINECRAFT));
|
2016-08-17 16:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
|
2018-10-26 22:43:39 +00:00
|
|
|
main.setCompileClasspath(main.getCompileClasspath().plus(project.getConfigurations().getByName(Constants.CONFIG_MINECRAFT)));
|
|
|
|
test.setCompileClasspath(test.getCompileClasspath().plus(project.getConfigurations().getByName(Constants.CONFIG_MINECRAFT)));
|
|
|
|
main.setRuntimeClasspath(main.getCompileClasspath().plus(project.getConfigurations().getByName(Constants.CONFIG_MINECRAFT)));
|
|
|
|
test.setCompileClasspath(test.getCompileClasspath().plus(project.getConfigurations().getByName(Constants.CONFIG_MINECRAFT)));
|
2016-08-17 16:38:54 +00:00
|
|
|
|
|
|
|
Javadoc javadoc = (Javadoc) project.getTasks().getByName(JavaPlugin.JAVADOC_TASK_NAME);
|
|
|
|
javadoc.setClasspath(main.getOutput().plus(main.getCompileClasspath()));
|
|
|
|
|
|
|
|
project.afterEvaluate(project1 -> {
|
|
|
|
LoomGradleExtension extension = project1.getExtensions().getByType(LoomGradleExtension.class);
|
|
|
|
|
2016-08-25 11:42:23 +00:00
|
|
|
project1.getRepositories().flatDir(flatDirectoryArtifactRepository -> {
|
2018-05-13 01:43:40 +00:00
|
|
|
flatDirectoryArtifactRepository.dir(extension.getUserCache());
|
2016-08-25 11:42:23 +00:00
|
|
|
flatDirectoryArtifactRepository.setName("UserCacheFiles");
|
|
|
|
});
|
|
|
|
|
2016-08-29 10:08:23 +00:00
|
|
|
project1.getRepositories().flatDir(flatDirectoryArtifactRepository -> {
|
|
|
|
flatDirectoryArtifactRepository.dir(Constants.CACHE_FILES);
|
|
|
|
flatDirectoryArtifactRepository.setName("UserLocalCacheFiles");
|
|
|
|
});
|
|
|
|
|
2018-06-18 15:16:03 +00:00
|
|
|
project1.getRepositories().maven(mavenArtifactRepository -> {
|
|
|
|
mavenArtifactRepository.setName("modmuss50");
|
2018-08-18 01:00:48 +00:00
|
|
|
mavenArtifactRepository.setUrl("https://maven.modmuss50.me/");
|
2018-06-18 15:16:03 +00:00
|
|
|
});
|
|
|
|
|
2018-10-22 18:46:42 +00:00
|
|
|
project1.getRepositories().maven(mavenArtifactRepository -> {
|
|
|
|
mavenArtifactRepository.setName("SpongePowered");
|
|
|
|
mavenArtifactRepository.setUrl("http://repo.spongepowered.org/maven");
|
|
|
|
});
|
|
|
|
|
2016-08-17 16:38:54 +00:00
|
|
|
project1.getRepositories().maven(mavenArtifactRepository -> {
|
|
|
|
mavenArtifactRepository.setName("Mojang");
|
|
|
|
mavenArtifactRepository.setUrl("https://libraries.minecraft.net/");
|
|
|
|
});
|
|
|
|
|
|
|
|
project1.getRepositories().mavenCentral();
|
|
|
|
project1.getRepositories().jcenter();
|
|
|
|
|
|
|
|
Gson gson = new Gson();
|
|
|
|
try {
|
|
|
|
DownloadTask.downloadMcJson(extension, project1.getLogger());
|
|
|
|
Version version = gson.fromJson(new FileReader(Constants.MINECRAFT_JSON.get(extension)), Version.class);
|
|
|
|
for (Version.Library library : version.libraries) {
|
2016-08-25 11:42:23 +00:00
|
|
|
if (library.allowed() && library.getFile(extension) != null) {
|
2016-08-17 16:38:54 +00:00
|
|
|
String configName = Constants.CONFIG_MC_DEPENDENCIES;
|
2017-03-31 11:27:55 +00:00
|
|
|
if (library.name.contains("java3d") || library.name.contains("paulscode") || library.name.contains("lwjgl") || library.name.contains("twitch") || library.name.contains("jinput") || library.name.contains("text2speech") || library.name.contains("objc")) {
|
2016-08-17 16:38:54 +00:00
|
|
|
configName = Constants.CONFIG_MC_DEPENDENCIES_CLIENT;
|
|
|
|
}
|
|
|
|
project1.getDependencies().add(configName, library.getArtifactName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2018-10-26 22:43:39 +00:00
|
|
|
project1.getDependencies().add(Constants.CONFIG_MINECRAFT, "net.minecraft:" + Constants.MINECRAFT_FINAL_JAR.get(extension).getName().replace(".jar", ""));
|
2016-08-17 16:38:54 +00:00
|
|
|
|
2018-08-18 01:00:48 +00:00
|
|
|
if (extension.isModWorkspace()) {
|
|
|
|
//only add this when not in a dev env
|
2018-11-02 20:16:14 +00:00
|
|
|
project1.getDependencies().add(Constants.COMPILE_MODS, "net.fabricmc:fabric-loader:" + extension.getVersionString());
|
2016-08-17 16:38:54 +00:00
|
|
|
}
|
2018-10-29 11:55:30 +00:00
|
|
|
|
|
|
|
|
2016-08-17 16:38:54 +00:00
|
|
|
});
|
2016-10-12 09:22:08 +00:00
|
|
|
|
2018-06-18 15:41:50 +00:00
|
|
|
project.getTasks().getByName("jar").doLast(task -> {
|
2016-10-12 09:22:08 +00:00
|
|
|
project.getLogger().lifecycle(":remapping mods");
|
2017-02-09 18:21:42 +00:00
|
|
|
LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class);
|
2016-10-12 09:44:31 +00:00
|
|
|
try {
|
2017-02-09 18:21:42 +00:00
|
|
|
if(extension.hasPomf()){
|
|
|
|
ModRemapper.remap(project);
|
|
|
|
}
|
2016-10-12 09:44:31 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2016-10-12 09:22:08 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
project.afterEvaluate(project12 -> {
|
2018-05-11 20:57:42 +00:00
|
|
|
project12.getTasks().getByName("idea").dependsOn(project12.getTasks().getByName("cleanIdea")).dependsOn(project12.getTasks().getByName("setup")).dependsOn(project12.getTasks().getByName("extractNatives"));
|
2017-05-11 12:02:53 +00:00
|
|
|
project12.getTasks().getByName("idea").finalizedBy(project12.getTasks().getByName("genIdeaWorkspace"));
|
2016-10-12 09:22:08 +00:00
|
|
|
});
|
|
|
|
|
2016-08-17 16:38:54 +00:00
|
|
|
}
|
2016-08-29 10:08:23 +00:00
|
|
|
|
2016-09-10 12:09:15 +00:00
|
|
|
protected void readModJson(LoomGradleExtension extension) {
|
2016-08-29 10:08:23 +00:00
|
|
|
File resDir = new File(project.getProjectDir(), "src" + File.separator + "main" + File.separator + "resources");
|
|
|
|
File modJson = new File(resDir, "mod.json");
|
2016-09-10 12:09:15 +00:00
|
|
|
if (modJson.exists()) {
|
2016-08-29 10:08:23 +00:00
|
|
|
Gson gson = new Gson();
|
|
|
|
try {
|
|
|
|
JsonElement jsonElement = gson.fromJson(new FileReader(modJson), JsonElement.class);
|
2018-10-25 11:32:39 +00:00
|
|
|
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
|
|
|
if ((extension.version == null || extension.version.isEmpty()) && jsonObject.has("version")) {
|
|
|
|
project.setVersion(jsonObject.get("version").getAsString());
|
|
|
|
}
|
|
|
|
if (jsonObject.has("group")) {
|
|
|
|
project.setGroup(jsonObject.get("group").getAsString());
|
|
|
|
}
|
|
|
|
if (jsonObject.has("description")) {
|
|
|
|
project.setDescription(jsonObject.get("description").getAsString());
|
2016-08-29 10:08:23 +00:00
|
|
|
}
|
2018-10-25 11:32:39 +00:00
|
|
|
//TODO load deps
|
2016-08-29 10:08:23 +00:00
|
|
|
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
//This wont happen as we have checked for it
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-15 10:22:14 +00:00
|
|
|
}
|