Skip to content

Commit

Permalink
Upgrade gradle, loads of JAR loading magic and command line
Browse files Browse the repository at this point in the history
  • Loading branch information
comp500 committed May 15, 2019
1 parent 77aa770 commit 9933abb
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 11 deletions.
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.0-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
18 changes: 17 additions & 1 deletion gradlew
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
#!/usr/bin/env sh

#
# Copyright 2015 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

##############################################################################
##
## Gradle start up script for UN*X
Expand Down Expand Up @@ -28,7 +44,7 @@ APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`

# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m"'
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"
Expand Down
18 changes: 17 additions & 1 deletion gradlew.bat
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem http://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem

@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
Expand All @@ -14,7 +30,7 @@ set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m"
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"

@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/link/infra/packwiz/installer/bootstrap/LoadJAR.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package link.infra.packwiz.installer.bootstrap;

import java.io.File;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

import org.apache.commons.cli.Options;

public class LoadJAR {

private static Class<?> mainClass = null;

private static void loadClass(String path) throws MalformedURLException, ClassNotFoundException {
if (mainClass != null) {
return;
}

if (path == null) {
path = "./packwiz-installer.jar";
}

URLClassLoader child = new URLClassLoader(new URL[] { new File(path).toURI().toURL() },
LoadJAR.class.getClassLoader());
mainClass = Class.forName("link.infra.packwiz.installer.Main", true, child);
}

public static boolean addOptions(Options options, String path) {
try {
loadClass(path);

Method method = mainClass.getDeclaredMethod("addNonBootstrapOptions", Options.class);
method.invoke(null, options);
} catch (Exception e) {
// Woah that was a lot of errors!
// Just ignore them, it doesn't matter if there aren't any more options added anyway - it's just a help command.
return false;
}
return true;
}

public static void start(String[] args, String path) throws ClassNotFoundException, Exception {
loadClass(path);

// Must be casted to Object (not array) because varargs
mainClass.getConstructor(String[].class).newInstance((Object)args);
}
}
63 changes: 55 additions & 8 deletions src/main/java/link/infra/packwiz/installer/bootstrap/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.io.InputStreamReader;
import java.net.URL;

import javax.swing.JOptionPane;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
Expand All @@ -20,45 +22,90 @@ public class Main {

private static final String UPDATE_URL = "https://api.github.com/repos/comp500/Demagnetize/releases/latest";
private String updateURL = UPDATE_URL;
private boolean skipUpdate = false;
private boolean useGUI = true;
private String jarPath = null;

public static void main(String[] args) {
new Main(args); // Is this bad?
}

public Main(String[] args) {
try {
new Main().parseOptions(args);
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
parseOptions(args);
} catch (ParseException e) {
showError(e, "There was an error parsing command line arguments:");
}

if (skipUpdate) {
try {
LoadJAR.start(args, jarPath);
} catch (ClassNotFoundException e) {
showError(e, "packwiz-installer cannot be found, or there was an error loading it:");
} catch (Exception e) {
showError(e, "There was an error loading packwiz-installer:");
}
return;
}


try {
new Main().requestRelease();
requestRelease();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

private void showError(Exception e, String message) {
if (useGUI) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, message + "\n" + e.getMessage(), "packwiz-installer-bootstrap", JOptionPane.ERROR_MESSAGE);
System.exit(1);
} else {
System.out.println(message);
e.printStackTrace();
System.exit(1);
}
}

private void parseOptions(String[] args) throws ParseException {
Options options = new Options();
options.addOption(null, "bootstrap-update-url", true, "Github API URL for checking for updates");
options.addOption(null, "bootstrap-no-update", false, "Don't update packwiz-installer");
options.addOption(null, "bootstrap-main-jar", true, "Location of the packwiz-installer JAR file");
options.addOption("g", "no-gui", false, "Don't display a GUI to show update progress");
options.addOption("h", "help", false, "Display this message");

CommandLineParser parser = new DefaultParser();
CommandLine cmd = parser.parse(options, args);
CommandLine cmd = parser.parse(options, args, false);

if (cmd.hasOption("bootstrap-main-jar")) {
jarPath = cmd.getOptionValue("bootstrap-main-jar");
}

if (cmd.hasOption("help")) {
HelpFormatter formatter = new HelpFormatter();
// Add options from packwiz-installer JAR, if it is present
boolean jarLoaded = LoadJAR.addOptions(options, jarPath);
formatter.printHelp("java -jar packwiz-installer-bootstrap.jar", options);
// TODO: call method on packwiz-installer to get it's options
if (!jarLoaded) {
System.out.println("Options for packwiz-installer will be visible once it has been downloaded.");
}
System.exit(0);
}

if (cmd.hasOption("bootstrap-update-url")) {
updateURL = cmd.getOptionValue("bootstrap-update-url");
}

if (cmd.hasOption("bootstrap-no-update")) {
skipUpdate = true;
}

if (cmd.hasOption("no-gui")) {
useGUI = false;
}
}

private void requestRelease() throws IOException {
Expand All @@ -70,7 +117,7 @@ private void requestRelease() throws IOException {
throw new RuntimeException("Invalid Github API response");
}
for (JsonValue asset : assets.asArray()) {

System.out.println(asset.toString());
}
}

Expand Down

0 comments on commit 9933abb

Please sign in to comment.