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.task;
|
2016-08-15 10:22:14 +00:00
|
|
|
|
2016-08-17 16:38:54 +00:00
|
|
|
import com.google.gson.Gson;
|
2016-08-16 23:30:48 +00:00
|
|
|
import net.fabricmc.loom.LoomGradleExtension;
|
2018-11-04 21:28:46 +00:00
|
|
|
import net.fabricmc.loom.providers.MinecraftProvider;
|
2016-08-16 23:30:48 +00:00
|
|
|
import net.fabricmc.loom.util.Constants;
|
|
|
|
import net.fabricmc.loom.util.IdeaRunConfig;
|
2018-11-04 21:28:46 +00:00
|
|
|
import net.fabricmc.loom.util.MinecraftVersionInfo;
|
2017-05-11 12:02:53 +00:00
|
|
|
import org.gradle.api.DefaultTask;
|
2016-10-12 09:22:08 +00:00
|
|
|
import org.gradle.api.Project;
|
2017-05-11 12:02:53 +00:00
|
|
|
import org.gradle.api.tasks.TaskAction;
|
2016-08-15 10:22:14 +00:00
|
|
|
import org.w3c.dom.Document;
|
|
|
|
import org.w3c.dom.Element;
|
|
|
|
import org.w3c.dom.Node;
|
|
|
|
import org.w3c.dom.NodeList;
|
|
|
|
import org.xml.sax.SAXException;
|
|
|
|
|
|
|
|
import javax.xml.parsers.DocumentBuilder;
|
|
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
|
import javax.xml.parsers.ParserConfigurationException;
|
|
|
|
import javax.xml.transform.OutputKeys;
|
|
|
|
import javax.xml.transform.Transformer;
|
|
|
|
import javax.xml.transform.TransformerException;
|
|
|
|
import javax.xml.transform.TransformerFactory;
|
|
|
|
import javax.xml.transform.dom.DOMSource;
|
|
|
|
import javax.xml.transform.stream.StreamResult;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
|
2017-05-11 12:02:53 +00:00
|
|
|
public class GenIdeaProjectTask extends DefaultTask {
|
2016-08-17 16:38:54 +00:00
|
|
|
|
2017-05-11 12:02:53 +00:00
|
|
|
@TaskAction
|
|
|
|
public void genIdeaRuns() throws IOException, ParserConfigurationException, SAXException, TransformerException {
|
|
|
|
Project project = this.getProject();
|
2016-10-12 09:22:08 +00:00
|
|
|
LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class);
|
2017-05-11 12:02:53 +00:00
|
|
|
project.getLogger().lifecycle(":Building idea workspace");
|
2016-10-12 09:22:08 +00:00
|
|
|
|
|
|
|
File file = new File(project.getName() + ".iml");
|
2016-08-17 16:38:54 +00:00
|
|
|
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
|
|
|
|
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
|
|
|
|
Document doc = docBuilder.parse(file);
|
|
|
|
|
|
|
|
Node component = null;
|
|
|
|
NodeList module = doc.getElementsByTagName("module").item(0).getChildNodes();
|
|
|
|
for (int i = 0; i < module.getLength(); i++) {
|
|
|
|
if (module.item(i).getNodeName().equals("component")) {
|
|
|
|
component = module.item(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (component == null) {
|
2016-10-12 09:22:08 +00:00
|
|
|
project.getLogger().lifecycle(":failed to generate intellij run configurations");
|
2016-08-17 16:38:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node content = null;
|
|
|
|
NodeList moduleList = component.getChildNodes();
|
|
|
|
|
|
|
|
for (int i = 0; i < moduleList.getLength(); i++) {
|
|
|
|
if (moduleList.item(i).getNodeName().equals("content")) {
|
|
|
|
content = moduleList.item(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (content == null) {
|
2016-10-12 09:22:08 +00:00
|
|
|
project.getLogger().lifecycle(":failed to generate intellij run configurations");
|
2016-08-17 16:38:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Element sourceFolder = doc.createElement("sourceFolder");
|
|
|
|
sourceFolder.setAttribute("url", "file://$MODULE_DIR$/minecraft/src/main/java");
|
|
|
|
sourceFolder.setAttribute("isTestSource", "false");
|
|
|
|
content.appendChild(sourceFolder);
|
|
|
|
|
|
|
|
sourceFolder = doc.createElement("sourceFolder");
|
|
|
|
sourceFolder.setAttribute("url", "file://$MODULE_DIR$/minecraft/src/main/resources");
|
|
|
|
sourceFolder.setAttribute("type", "java-resource");
|
|
|
|
content.appendChild(sourceFolder);
|
|
|
|
|
|
|
|
Gson gson = new Gson();
|
|
|
|
|
2018-11-04 21:28:46 +00:00
|
|
|
MinecraftProvider minecraftProvider = extension.getDependencyManager().getProvider(MinecraftProvider.class);
|
|
|
|
MinecraftVersionInfo minecraftVersionInfo = minecraftProvider.versionInfo;
|
2016-08-17 16:38:54 +00:00
|
|
|
|
|
|
|
TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
|
|
|
Transformer transformer = transformerFactory.newTransformer();
|
|
|
|
DOMSource source = new DOMSource(doc);
|
|
|
|
StreamResult result = new StreamResult(file);
|
|
|
|
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
|
|
|
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
|
|
|
|
transformer.transform(source, result);
|
|
|
|
|
2016-10-12 09:22:08 +00:00
|
|
|
file = new File(project.getName() + ".iws");
|
2016-08-17 16:38:54 +00:00
|
|
|
docFactory = DocumentBuilderFactory.newInstance();
|
|
|
|
docBuilder = docFactory.newDocumentBuilder();
|
|
|
|
doc = docBuilder.parse(file);
|
|
|
|
|
|
|
|
NodeList list = doc.getElementsByTagName("component");
|
|
|
|
Element runManager = null;
|
|
|
|
for (int i = 0; i < list.getLength(); i++) {
|
|
|
|
Element element = (Element) list.item(i);
|
|
|
|
if (element.getAttribute("name").equals("RunManager")) {
|
|
|
|
runManager = element;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (runManager == null) {
|
2016-10-12 09:22:08 +00:00
|
|
|
project.getLogger().lifecycle(":failed to generate intellij run configurations");
|
2016-08-17 16:38:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
IdeaRunConfig ideaClient = new IdeaRunConfig();
|
2018-10-22 18:46:42 +00:00
|
|
|
ideaClient.mainClass = "net.minecraft.launchwrapper.Launch";
|
2016-10-12 09:22:08 +00:00
|
|
|
ideaClient.projectName = project.getName();
|
2016-08-17 16:38:54 +00:00
|
|
|
ideaClient.configName = "Minecraft Client";
|
|
|
|
ideaClient.runDir = "file://$PROJECT_DIR$/" + extension.runDir;
|
2018-11-04 21:28:46 +00:00
|
|
|
ideaClient.vmArgs = "-Djava.library.path=" + extension.getMinecraftProvider().libraryProvider.MINECRAFT_NATIVES.getAbsolutePath() + " -Dfabric.development=true";
|
|
|
|
ideaClient.programArgs = "--tweakClass " + Constants.FABRIC_CLIENT_TWEAKER + " --assetIndex " + minecraftVersionInfo.assetIndex.id + " --assetsDir \"" + new File(extension.getUserCache(), "assets-" + minecraftProvider.minecraftVersion).getAbsolutePath() + "\" --fabricMappingFile \"" + minecraftProvider.pomfProvider.MAPPINGS_TINY.getAbsolutePath() + "\"";
|
2016-08-17 16:38:54 +00:00
|
|
|
|
|
|
|
runManager.appendChild(ideaClient.genRuns(runManager));
|
|
|
|
|
2016-08-20 14:16:23 +00:00
|
|
|
IdeaRunConfig ideaServer = new IdeaRunConfig();
|
2018-10-22 18:46:42 +00:00
|
|
|
ideaServer.mainClass = "net.minecraft.launchwrapper.Launch";
|
2016-10-12 09:22:08 +00:00
|
|
|
ideaServer.projectName = project.getName();
|
2016-08-20 14:16:23 +00:00
|
|
|
ideaServer.configName = "Minecraft Server";
|
|
|
|
ideaServer.runDir = "file://$PROJECT_DIR$/" + extension.runDir;
|
2018-10-25 11:40:41 +00:00
|
|
|
ideaServer.vmArgs = "-Dfabric.development=true";
|
2018-11-04 21:28:46 +00:00
|
|
|
ideaServer.programArgs = "--tweakClass " + Constants.FABRIC_SERVER_TWEAKER + " --fabricMappingFile \"" + minecraftProvider.pomfProvider.MAPPINGS_TINY.getAbsolutePath() + "\"";
|
2016-08-20 14:16:23 +00:00
|
|
|
|
|
|
|
runManager.appendChild(ideaServer.genRuns(runManager));
|
|
|
|
|
2016-08-17 16:38:54 +00:00
|
|
|
transformerFactory = TransformerFactory.newInstance();
|
|
|
|
transformer = transformerFactory.newTransformer();
|
|
|
|
source = new DOMSource(doc);
|
|
|
|
result = new StreamResult(file);
|
|
|
|
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
|
|
|
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
|
|
|
|
transformer.transform(source, result);
|
|
|
|
|
|
|
|
File runDir = new File(Constants.WORKING_DIRECTORY, extension.runDir);
|
|
|
|
if (!runDir.exists()) {
|
|
|
|
runDir.mkdirs();
|
|
|
|
}
|
|
|
|
}
|
2016-08-15 10:22:14 +00:00
|
|
|
}
|