com.github.stormbit.packer.PackerConfig Maven / Gradle / Ivy
The newest version!
package com.github.stormbit.packer;
import com.eclipsesource.json.JsonArray;
import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.JsonValue;
import org.zeroturnaround.zip.commons.FileUtilsV2_2;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class PackerConfig
{
public Platform platform;
public String jdk;
public Integer jdk_version;
public String executable;
public List classpath;
public List removePlatformLibs;
public String mainClass;
public List vmArgs;
public String minimizeJre;
public File cacheJre;
public List resources;
public File outDir;
public File platformLibsOutDir;
public File iconResource;
public String bundleIdentifier;
public boolean verbose;
public PackerConfig() {
}
public PackerConfig(final Platform platform, final String jdk, final String executable, final List classpath, final String mainClass, final File outDir) throws IOException {
this.platform = platform;
this.jdk = jdk;
this.executable = executable;
this.classpath = classpath;
this.mainClass = mainClass;
this.outDir = outDir;
}
public PackerConfig(final PackerCommandLine commandLine) throws IOException {
this.verbose = commandLine.verbose();
if (commandLine.isConfig()) {
this.readConfigJson(commandLine.config());
}
if (commandLine.platform() != null) {
this.platform = Platform.byDesc(commandLine.platform());
}
if (commandLine.jdk() != null) {
this.jdk = commandLine.jdk();
}
if (commandLine.jdk_version() != null) {
this.jdk_version = Integer.parseInt(commandLine.jdk_version());
}
if (commandLine.executable() != null) {
this.executable = commandLine.executable();
}
this.classpath = this.appendTo(this.classpath, commandLine.classpath());
this.removePlatformLibs = this.appendTo(this.removePlatformLibs, commandLine.removePlatformLibs());
if (commandLine.mainClass() != null) {
this.mainClass = commandLine.mainClass();
}
this.vmArgs = this.appendTo(this.vmArgs, commandLine.vmArgs());
if (commandLine.minimizeJre() != null) {
this.minimizeJre = commandLine.minimizeJre();
}
if (commandLine.cacheJre() != null) {
this.cacheJre = commandLine.cacheJre();
}
this.resources = this.appendTo(this.resources, commandLine.resources());
if (commandLine.outDir() != null) {
this.outDir = commandLine.outDir();
}
if (commandLine.platformLibsOutDir() != null) {
this.platformLibsOutDir = commandLine.platformLibsOutDir();
}
if (commandLine.iconResource() != null) {
this.iconResource = commandLine.iconResource();
}
if (commandLine.bundleIdentifier() != null) {
this.bundleIdentifier = commandLine.bundleIdentifier();
}
}
private void readConfigJson(final File configJson) throws IOException {
final JsonObject json = JsonObject.readFrom(FileUtilsV2_2.readFileToString(configJson));
if (json.get("platform") != null) {
this.platform = Platform.byDesc(json.get("platform").asString());
}
if (json.get("jdk.version") != null) {
this.jdk_version = json.get("jdk.version").asInt();
}
if (json.get("jdk") != null) {
this.jdk = json.get("jdk").asString();
}
if (json.get("executable") != null) {
this.executable = json.get("executable").asString();
}
if (json.get("classpath") != null) {
this.classpath = this.toStringArray(json.get("classpath").asArray());
}
if (json.get("removelibs") != null) {
this.removePlatformLibs = this.toStringArray(json.get("removelibs").asArray());
}
if (json.get("mainclass") != null) {
this.mainClass = json.get("mainclass").asString();
}
if (json.get("vmargs") != null) {
final List vmArgs = this.toStringArray(json.get("vmargs").asArray());
this.vmArgs = new ArrayList();
for (final String vmArg : vmArgs) {
if (vmArg.startsWith("-")) {
this.vmArgs.add(vmArg.substring(1));
}
else {
this.vmArgs.add(vmArg);
}
}
}
if (json.get("minimizejre") != null) {
this.minimizeJre = json.get("minimizejre").asString();
}
if (json.get("cachejre") != null) {
this.cacheJre = new File(json.get("cachejre").asString());
}
if (json.get("resources") != null) {
this.resources = this.toFileArray(json.get("resources").asArray());
}
if (json.get("output") != null) {
this.outDir = new File(json.get("output").asString());
}
if (json.get("libs") != null) {
this.platformLibsOutDir = new File(json.get("libs").asString());
}
if (json.get("icon") != null) {
this.iconResource = new File(json.get("icon").asString());
}
if (json.get("bundle") != null) {
this.bundleIdentifier = json.get("bundle").asString();
}
}
private List appendTo(final List list, final List append) {
if (list == null) {
return (append != null) ? append : new ArrayList();
}
if (append != null) {
for (final T item : append) {
boolean duplicate = false;
for (final T cmp : list) {
if (cmp.equals(item)) {
duplicate = true;
break;
}
}
if (!duplicate) {
list.add(item);
}
}
}
return list;
}
private List toStringArray(final JsonArray array) {
final List result = new ArrayList();
for (final JsonValue value : array) {
result.add(value.asString());
}
return result;
}
private List toFileArray(final JsonArray array) {
final List result = new ArrayList();
for (final JsonValue value : array) {
result.add(new File(value.asString()));
}
return result;
}
void validate() throws IOException {
this.validate(this.platform, "platform");
this.validate(this.jdk, "JDK");
this.validate(this.jdk_version, "jdk.version");
this.validate(this.executable, "executable name");
this.validate(this.mainClass, "main class");
this.validate(this.outDir, "output folder");
if (this.outDir.exists()) {
if (new File(".").equals(this.outDir)) {
throw new IOException("Output directory equals working directory, aborting");
}
if (new File("/").equals(this.outDir)) {
throw new IOException("Output directory points to root folder.");
}
}
if (this.classpath.isEmpty()) {
throw new IOException("Empty class path. Please check your commandline or configuration.");
}
}
private void validate(final T parameter, final String name) throws IOException {
if (parameter == null) {
throw new IOException("No " + name + " specified. Please check your commandline or configuration.");
}
}
public enum Platform
{
Windows32("windows32"),
Windows64("windows64"),
Linux32("linux32"),
Linux64("linux64"),
MacOS("mac");
final String desc;
private Platform(final String desc) {
this.desc = desc;
}
static Platform byDesc(final String desc) throws IOException {
for (final Platform value : values()) {
if (value.desc.equalsIgnoreCase(desc)) {
return value;
}
}
throw new IOException("Invalid platform '" + desc + "'");
}
}
}