Skip to content

Commit

Permalink
Updating the no-internet run to work headlessly
Browse files Browse the repository at this point in the history
  • Loading branch information
madhephaestus committed Jun 19, 2024
1 parent 20447dd commit 227e62a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 40 deletions.
75 changes: 42 additions & 33 deletions lib/src/main/java/com/commonwealthrobotics/JvmManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ public class JvmManager {

public static String getCommandString(String project, String repo, String version, String downloadJsonURL,
long sizeOfJson, ProgressBar progress, String bindir) throws Exception {

File exe = download(version, downloadJsonURL, sizeOfJson, progress, bindir, "jvm.json");
if(version==null)
throw new RuntimeException("Version can not be null");
File exe;

exe= download(version, downloadJsonURL, sizeOfJson, progress, bindir, "jvm.json");
Type TT_mapStringString = new TypeToken<HashMap<String, Object>>() {
}.getType();
// chreat the gson object, this is the parsing factory
Expand Down Expand Up @@ -123,6 +126,7 @@ public static boolean isExecutable(ZipArchiveEntry entry) {
// executable: 0001 (0x01)
return (unixMode & 0x49) != 0;
}

private static void unzip(File path, String dir) throws Exception {
String fileBaseName = FilenameUtils.getBaseName(path.getName().toString());
Path destFolderPath = new File(dir).toPath();
Expand All @@ -139,12 +143,12 @@ private static void unzip(File path, String dir) throws Exception {
Files.createDirectories(entryPath.getParent());
try (InputStream in = zipFile.getInputStream(entry)) {
try {
//ar.setExternalAttributes(entry.extraAttributes);
// ar.setExternalAttributes(entry.extraAttributes);
if (entry.isUnixSymlink()) {
String text = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))
.lines().collect(Collectors.joining("\n"));
Path target = Paths.get(".", text);
System.out.println("Creating symlink "+entryPath+" with "+target);
System.out.println("Creating symlink " + entryPath + " with " + target);

Files.createSymbolicLink(entryPath, target);
continue;
Expand All @@ -155,7 +159,7 @@ private static void unzip(File path, String dir) throws Exception {
try (OutputStream out = new FileOutputStream(entryPath.toFile())) {
IOUtils.copy(in, out);
}
if(isExecutable(entry)) {
if (isExecutable(entry)) {
entryPath.toFile().setExecutable(true);
}
}
Expand Down Expand Up @@ -204,38 +208,43 @@ private static String bits(byte b) {

private static File download(String version, String downloadJsonURL, long sizeOfJson, ProgressBar progress,
String bindir, String filename) throws MalformedURLException, IOException, FileNotFoundException {
URL url = new URL(downloadJsonURL);
URLConnection connection = url.openConnection();
InputStream is = connection.getInputStream();
ProcessInputStream pis = new ProcessInputStream(is, (int) sizeOfJson);
pis.addListener(new Listener() {
@Override
public void process(double percent) {
System.out.println("Download percent " + percent);
Platform.runLater(() -> {
progress.setProgress(percent);
});
}
});
File folder = new File(bindir + version + "/");
File exe = new File(bindir + version + "/" + filename);

if (!folder.exists() || !exe.exists()) {
System.out.println("Start Downloading " + filename);
folder.mkdirs();
exe.createNewFile();
byte dataBuffer[] = new byte[1024];
int bytesRead;
FileOutputStream fileOutputStream = new FileOutputStream(exe.getAbsoluteFile());
while ((bytesRead = pis.read(dataBuffer, 0, 1024)) != -1) {
fileOutputStream.write(dataBuffer, 0, bytesRead);
try {
URL url = new URL(downloadJsonURL);
URLConnection connection = url.openConnection();
InputStream is = connection.getInputStream();
ProcessInputStream pis = new ProcessInputStream(is, (int) sizeOfJson);
pis.addListener(new Listener() {
@Override
public void process(double percent) {
System.out.println("Download percent " + percent);
Platform.runLater(() -> {
progress.setProgress(percent);
});
}
});

if (!folder.exists() || !exe.exists()) {
System.out.println("Start Downloading " + filename);
folder.mkdirs();
exe.createNewFile();
byte dataBuffer[] = new byte[1024];
int bytesRead;
FileOutputStream fileOutputStream = new FileOutputStream(exe.getAbsoluteFile());
while ((bytesRead = pis.read(dataBuffer, 0, 1024)) != -1) {
fileOutputStream.write(dataBuffer, 0, bytesRead);
}
fileOutputStream.close();
pis.close();
System.out.println("Finished downloading " + filename);
} else {
System.out.println("Not downloadeing, it existst " + filename);
}
fileOutputStream.close();
pis.close();
System.out.println("Finished downloading " + filename);
} else {
System.out.println("Not downloadeing, it existst " + filename);
} catch (Throwable t) {
t.printStackTrace();
}
System.out.println("Using JVM "+exe.getAbsolutePath());
return exe;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class LatestFromGithubLaunchUI {
public static Stage stage;

public static String latestVersionString = "";
public static String myVersionString = "None";
public static String myVersionString = null;
public static long sizeOfJar = 0;
public static long sizeOfJson = 0;
@FXML // ResourceBundle that was given to the FXMLLoader
Expand Down Expand Up @@ -142,6 +142,7 @@ public void launchApplication() {
Platform.runLater(() -> {
yesButton.setDisable(true);
noButton.setDisable(true);
stage.close();
});
new Thread(() -> {
String command;
Expand All @@ -153,7 +154,7 @@ public void launchApplication() {
System.exit(1);
return;
}
Platform.runLater(() -> stage.close());

try {
Thread.sleep(100);
} catch (InterruptedException e1) {
Expand Down Expand Up @@ -274,13 +275,14 @@ void initialize() {
assert progress != null : "fx:id=\"progress\" was not injected: check your FXML file 'ui.fxml'.";
assert previousVersion != null : "fx:id=\"previousVersion\" was not injected: check your FXML file 'ui.fxml'.";
assert currentVersion != null : "fx:id=\"currentVersion\" was not injected: check your FXML file 'ui.fxml'.";

boolean noInternet = false;
try {
readCurrentVersion("https://api.github.com/repos/" + project + "/" + repoName + "/releases/latest");
binary.setText(project + "\n" + repoName + "\n" + jarName + "\n" + (sizeOfJar / 1000000) + " Mb");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
noInternet=true;
}
stage.setTitle("Auto-Updater for " + repoName);
currentVersion.setText(latestVersionString);
Expand All @@ -305,10 +307,12 @@ void initialize() {
e.printStackTrace();
}
}

if (myVersionString.contentEquals(latestVersionString)) {
launchApplication();
}
if(!noInternet) {
if (myVersionString.contentEquals(latestVersionString)) {
launchApplication();
}
}else
onNo(null);

}
}

0 comments on commit 227e62a

Please sign in to comment.