Skip to content
This repository has been archived by the owner on Apr 22, 2020. It is now read-only.

Commit

Permalink
Run configuration gneration and some minor ui feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
covers1624 committed Feb 28, 2017
1 parent 23909ac commit 1a6664e
Show file tree
Hide file tree
Showing 8 changed files with 534 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,6 @@ else if (e.getButton() == 3) {
}
}


if (moduleTree.getEditingPath() != null) {
String editing = moduleTree.getEditingPath().getLastPathComponent().toString();
if (editing.equals("Modules") || editing.equals("Forge")) {
Expand Down Expand Up @@ -1281,6 +1280,7 @@ private void importSetup(ActionEvent evt) {
return;
}
GuiFields.importSetup(chooser.getSelectedFile());
JOptionPane.showMessageDialog(this, "Successfully imported setup!");
reloadModuleTree();
reloadCorePluginList();
}
Expand All @@ -1300,16 +1300,18 @@ private void exportSetup(ActionEvent evt) {
return;
}
GuiFields.exportSetup(chooser.getSelectedFile());
JOptionPane.showMessageDialog(this, "Successfully exported setup!");
return;
}

String path = chooser.getSelectedFile().getAbsolutePath();

if (chooser.getFileFilter() == filter && !chooser.getSelectedFile().getAbsolutePath().endsWith(".json")) {
GuiFields.exportSetup(new File(path + ".json"));
}
else {
JOptionPane.showMessageDialog(this, "Successfully exported setup!");
} else {
GuiFields.exportSetup(chooser.getSelectedFile());
JOptionPane.showMessageDialog(this, "Successfully exported setup!");
}
reloadModuleTree();
reloadCorePluginList();
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/covers1624/ccintelli/gui/GuiFields.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package covers1624.ccintelli.gui;

import com.google.common.collect.ImmutableList;
import covers1624.ccintelli.launch.SetupSerializer;
import covers1624.ccintelli.module.Module;
import covers1624.ccintelli.module.ModuleEntry;
Expand All @@ -17,12 +18,13 @@
* Created by brandon3055 on 23/01/2017.
*/
public class GuiFields {

//The order of the entries in this list will be the order they show.
//I can apply a comparator to the list before display to sort it.
public static List<Module> modules = new LinkedList<>();
public static Module forgeModule;
public static List<String> fmlCorePlugins = new LinkedList<>();
public static List<String> vmArgs = new LinkedList<>();
public static List<String> vmArgs = new LinkedList<>(ImmutableList.of("-Xmx4G", "-Xms1G"));

public static EnumLanguageLevel projectLangLevel = EnumLanguageLevel.JDK_1_8;
public static EnumLanguageLevel projectBytecodeLevel = EnumLanguageLevel.JDK_1_8;
Expand Down Expand Up @@ -72,9 +74,9 @@ public static void importSetup(File fileToImport) {
public static void exportSetup(File targetFile) {
try {
SetupSerializer.writeSetup(targetFile);
LogHelper.info("Successfully exported %s modules to: %s", modules.size() - 1, targetFile.getAbsolutePath());
LogHelper.info("Successfully exported %s modules to: %s", modules.size() - 1, targetFile.getAbsolutePath());
} catch (IOException e) {
LogHelper.errorError("Exception was thrown whils exporting modules to: %s", e, targetFile.getAbsolutePath());
LogHelper.errorError("Exception was thrown whilst exporting modules to: %s", e, targetFile.getAbsolutePath());
}
}
}
11 changes: 11 additions & 0 deletions src/main/java/covers1624/ccintelli/launch/SetupSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public static void readSetup(File json) throws IOException {
for (JsonElement element : object.getAsJsonArray("core_plugins")) {
GuiFields.fmlCorePlugins.add(element.getAsString());
}
GuiFields.vmArgs.clear();
for (JsonElement element : object.getAsJsonArray("vm_args")) {
GuiFields.vmArgs.add(element.getAsString());
}
}

public static void writeSetup(File json) throws IOException {
Expand All @@ -56,6 +60,13 @@ public static void writeSetup(File json) throws IOException {
corePlugins.add(corePlugin);
}
object.add("core_plugins", corePlugins);

JsonArray vmArgs = new JsonArray();
for (String arg : GuiFields.vmArgs) {
vmArgs.add(arg);
}
object.add("vm_args", vmArgs);

Streams.write(object, writer);
writer.flush();
}
Expand Down
182 changes: 182 additions & 0 deletions src/main/java/covers1624/ccintelli/workspace/RunConfigGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package covers1624.ccintelli.workspace;

import covers1624.ccintelli.gui.GuiFields;
import covers1624.ccintelli.launch.Launch;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import static covers1624.util.XMLUtils.createAndAdd;
import static covers1624.util.XMLUtils.getFirstElementNode;

/**
* Created by covers1624 on 20/02/2017.
*/
public class RunConfigGenerator {

public static void generateRunConfigs(String projectName) {
File workspaceIWS = new File(Launch.WORKSPACE, projectName + ".iws");
try {
List<String> vmArgs = new ArrayList<>();

vmArgs.addAll(GuiFields.vmArgs);

if (!GuiFields.fmlCorePlugins.isEmpty()) {
StringBuilder builder = new StringBuilder("-Dfml.coreMods.load=");
boolean hasFirst = false;
for (String corePlugin : GuiFields.fmlCorePlugins) {
if (hasFirst) {
builder.append(",");
}
hasFirst = true;
builder.append(corePlugin);
}
vmArgs.add(builder.toString());
}

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(RunConfigGenerator.class.getResourceAsStream("/iwsTemplate.xml"));
document.getDocumentElement().normalize();

Element documentElement = document.getDocumentElement();
Element componentElement = getFirstElementNode(documentElement, "component");
{
Element clientRun = createAndAdd(document, componentElement, "configuration");
clientRun.setAttribute("factoryName", "Application");
clientRun.setAttribute("type", "Application");
clientRun.setAttribute("name", "Minecraft Client");
clientRun.setAttribute("default", "false");
Element extension = createAndAdd(document, clientRun, "extension");
extension.setAttribute("runner", "idea");
extension.setAttribute("merge", "false");
extension.setAttribute("enabled", "false");
extension.setAttribute("name", "coverage");
Element mainClassOpt = createAndAdd(document, clientRun, "option");
mainClassOpt.setAttribute("value", "GradleStart");
mainClassOpt.setAttribute("name", "MAIN_CLASS_NAME");
StringBuilder argBuilder = new StringBuilder();
boolean hasFirst = false;
for (String arg : vmArgs) {
if (hasFirst) {
argBuilder.append(" ");
}
hasFirst = true;
argBuilder.append(arg);
}
Element vmArgsOpt = createAndAdd(document, clientRun, "option");
vmArgsOpt.setAttribute("value", argBuilder.toString());
vmArgsOpt.setAttribute("name", "VM_PARAMETERS");
Element progArgsOpt = createAndAdd(document, clientRun, "option");
progArgsOpt.setAttribute("value", "");
progArgsOpt.setAttribute("name", "PROGRAM_PARAMETERS");
Element workingDirOpt = createAndAdd(document, clientRun, "option");
workingDirOpt.setAttribute("value", "file://" + Launch.PROJECT_RUN.getAbsoluteFile().getPath());
workingDirOpt.setAttribute("name", "WORKING_DIRECTORY");
Element altJERpathEnableOpt = createAndAdd(document, clientRun, "option");
altJERpathEnableOpt.setAttribute("value", "false");
altJERpathEnableOpt.setAttribute("name", "ALTERNATIVE_JRE_PATH_ENABLED");
Element aldJrePathOpt = createAndAdd(document, clientRun, "option");
aldJrePathOpt.setAttribute("name", "ALTERNATIVE_JRE_PATH");
Element swingInspecOpt = createAndAdd(document, clientRun, "option");
swingInspecOpt.setAttribute("value", "false");
swingInspecOpt.setAttribute("name", "ENABLE_SWING_INSPECTOR");
Element envVarsOpt = createAndAdd(document, clientRun, "option");
envVarsOpt.setAttribute("name", "ENV_VARIABLES");
Element passParentEnvsOpt = createAndAdd(document, clientRun, "option");
passParentEnvsOpt.setAttribute("value", "true");
passParentEnvsOpt.setAttribute("name", "PASS_PARENT_ENVS");

Element moduleClassPathElement = createAndAdd(document, clientRun, "module");
moduleClassPathElement.setAttribute("name", GuiFields.forgeModule.NAME);
createAndAdd(document, clientRun, "envs");
createAndAdd(document, clientRun, "method");
}
{
Element serverRun = createAndAdd(document, componentElement, "configuration");
serverRun.setAttribute("factoryName", "Application");
serverRun.setAttribute("type", "Application");
serverRun.setAttribute("name", "Minecraft Server");
serverRun.setAttribute("default", "false");
Element extension = createAndAdd(document, serverRun, "extension");
extension.setAttribute("runner", "idea");
extension.setAttribute("merge", "false");
extension.setAttribute("enabled", "false");
extension.setAttribute("name", "coverage");
Element mainClassOpt = createAndAdd(document, serverRun, "option");
mainClassOpt.setAttribute("value", "GradleStartServer");
mainClassOpt.setAttribute("name", "MAIN_CLASS_NAME");
StringBuilder argBuilder = new StringBuilder();
boolean hasFirst = false;
for (String arg : vmArgs) {
if (hasFirst) {
argBuilder.append(" ");
}
hasFirst = true;
argBuilder.append(arg);
}
Element vmArgsOpt = createAndAdd(document, serverRun, "option");
vmArgsOpt.setAttribute("value", argBuilder.toString());
vmArgsOpt.setAttribute("name", "VM_PARAMETERS");
Element progArgsOpt = createAndAdd(document, serverRun, "option");
progArgsOpt.setAttribute("value", "");
progArgsOpt.setAttribute("name", "PROGRAM_PARAMETERS");
Element workingDirOpt = createAndAdd(document, serverRun, "option");
workingDirOpt.setAttribute("value", "file://" + Launch.PROJECT_RUN.getAbsoluteFile().getPath());
workingDirOpt.setAttribute("name", "WORKING_DIRECTORY");
Element altJERpathEnableOpt = createAndAdd(document, serverRun, "option");
altJERpathEnableOpt.setAttribute("value", "false");
altJERpathEnableOpt.setAttribute("name", "ALTERNATIVE_JRE_PATH_ENABLED");
Element aldJrePathOpt = createAndAdd(document, serverRun, "option");
aldJrePathOpt.setAttribute("name", "ALTERNATIVE_JRE_PATH");
Element swingInspecOpt = createAndAdd(document, serverRun, "option");
swingInspecOpt.setAttribute("value", "false");
swingInspecOpt.setAttribute("name", "ENABLE_SWING_INSPECTOR");
Element envVarsOpt = createAndAdd(document, serverRun, "option");
envVarsOpt.setAttribute("name", "ENV_VARIABLES");
Element passParentEnvsOpt = createAndAdd(document, serverRun, "option");
passParentEnvsOpt.setAttribute("value", "true");
passParentEnvsOpt.setAttribute("name", "PASS_PARENT_ENVS");

Element moduleClassPathElement = createAndAdd(document, serverRun, "module");
moduleClassPathElement.setAttribute("name", GuiFields.forgeModule.NAME);
createAndAdd(document, serverRun, "envs");
createAndAdd(document, serverRun, "method");
}

Element listElement = getFirstElementNode(componentElement, "list");
listElement.setAttribute("size", "2");
Element clientChild = createAndAdd(document, listElement, "item");
clientChild.setAttribute("itemvalue", "Application.Minecraft Client");
clientChild.setAttribute("class", "java.lang.String");
clientChild.setAttribute("index", "0");

Element serverChild = createAndAdd(document, listElement, "item");
serverChild.setAttribute("itemvalue", "Application.Minecraft Server");
serverChild.setAttribute("class", "java.lang.String");
serverChild.setAttribute("index", "1");

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(workspaceIWS);
transformer.transform(source, result);

} catch (Exception e) {
e.printStackTrace();
//throw new RuntimeException("Exception thrown whilst generating run configs.", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.base.Strings;
import covers1624.ccintelli.gui.GuiFields;
import covers1624.ccintelli.launch.Launch;
import covers1624.ccintelli.launch.SetupSerializer;
import covers1624.ccintelli.module.Module;
import covers1624.ccintelli.util.ATFileFilter;
import covers1624.ccintelli.util.ResourceWalker;
Expand All @@ -11,7 +12,10 @@
import org.apache.commons.io.IOUtils;

import java.io.*;
import java.util.*;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
Expand All @@ -32,6 +36,8 @@ public static void generateWorkspace(String projectName) {
runForgeSetup();
exportModules();
ProjectGenerator.generateWorkspace(projectName);
RunConfigGenerator.generateRunConfigs(projectName);
exportCurrentSetup(projectName);
LogHelper.info("Done!");
}

Expand Down Expand Up @@ -167,4 +173,14 @@ private static void runProcessAndLog(Process process) throws Exception {
}
}

private static void exportCurrentSetup(String projectName) {
try {
File export = new File(Launch.WORKSPACE, projectName + ".json");
SetupSerializer.writeSetup(export);
LogHelper.info("Your current workspace has been exported to: " + export.getAbsoluteFile().getPath());
} catch (Exception e) {
LogHelper.errorError("Exception was thrown whilst exporting setup..", e);
}
}

}
38 changes: 38 additions & 0 deletions src/main/java/covers1624/util/NodeListIterator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package covers1624.util;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.util.Iterator;

/**
* Created by covers1624 on 28/02/2017.
*/
public class NodeListIterator implements Iterator<Node>, Iterable<Node> {

private final NodeList list;
private int index;

public NodeListIterator(NodeList list) {
this.list = list;
}

@Override
public boolean hasNext() {
return list != null && index < list.getLength();
}

@Override
public Node next() {
Node object = list.item(index);
if (object != null) {
index++;
}
return object;
}

@Override
public Iterator<Node> iterator() {
return this;
}
}
Loading

0 comments on commit 1a6664e

Please sign in to comment.