Skip to content

Commit 227e62a

Browse files
committed
Updating the no-internet run to work headlessly
1 parent 20447dd commit 227e62a

File tree

2 files changed

+53
-40
lines changed

2 files changed

+53
-40
lines changed

lib/src/main/java/com/commonwealthrobotics/JvmManager.java

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,11 @@ public class JvmManager {
4949

5050
public static String getCommandString(String project, String repo, String version, String downloadJsonURL,
5151
long sizeOfJson, ProgressBar progress, String bindir) throws Exception {
52-
53-
File exe = download(version, downloadJsonURL, sizeOfJson, progress, bindir, "jvm.json");
52+
if(version==null)
53+
throw new RuntimeException("Version can not be null");
54+
File exe;
55+
56+
exe= download(version, downloadJsonURL, sizeOfJson, progress, bindir, "jvm.json");
5457
Type TT_mapStringString = new TypeToken<HashMap<String, Object>>() {
5558
}.getType();
5659
// chreat the gson object, this is the parsing factory
@@ -123,6 +126,7 @@ public static boolean isExecutable(ZipArchiveEntry entry) {
123126
// executable: 0001 (0x01)
124127
return (unixMode & 0x49) != 0;
125128
}
129+
126130
private static void unzip(File path, String dir) throws Exception {
127131
String fileBaseName = FilenameUtils.getBaseName(path.getName().toString());
128132
Path destFolderPath = new File(dir).toPath();
@@ -139,12 +143,12 @@ private static void unzip(File path, String dir) throws Exception {
139143
Files.createDirectories(entryPath.getParent());
140144
try (InputStream in = zipFile.getInputStream(entry)) {
141145
try {
142-
//ar.setExternalAttributes(entry.extraAttributes);
146+
// ar.setExternalAttributes(entry.extraAttributes);
143147
if (entry.isUnixSymlink()) {
144148
String text = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))
145149
.lines().collect(Collectors.joining("\n"));
146150
Path target = Paths.get(".", text);
147-
System.out.println("Creating symlink "+entryPath+" with "+target);
151+
System.out.println("Creating symlink " + entryPath + " with " + target);
148152

149153
Files.createSymbolicLink(entryPath, target);
150154
continue;
@@ -155,7 +159,7 @@ private static void unzip(File path, String dir) throws Exception {
155159
try (OutputStream out = new FileOutputStream(entryPath.toFile())) {
156160
IOUtils.copy(in, out);
157161
}
158-
if(isExecutable(entry)) {
162+
if (isExecutable(entry)) {
159163
entryPath.toFile().setExecutable(true);
160164
}
161165
}
@@ -204,38 +208,43 @@ private static String bits(byte b) {
204208

205209
private static File download(String version, String downloadJsonURL, long sizeOfJson, ProgressBar progress,
206210
String bindir, String filename) throws MalformedURLException, IOException, FileNotFoundException {
207-
URL url = new URL(downloadJsonURL);
208-
URLConnection connection = url.openConnection();
209-
InputStream is = connection.getInputStream();
210-
ProcessInputStream pis = new ProcessInputStream(is, (int) sizeOfJson);
211-
pis.addListener(new Listener() {
212-
@Override
213-
public void process(double percent) {
214-
System.out.println("Download percent " + percent);
215-
Platform.runLater(() -> {
216-
progress.setProgress(percent);
217-
});
218-
}
219-
});
220211
File folder = new File(bindir + version + "/");
221212
File exe = new File(bindir + version + "/" + filename);
222-
223-
if (!folder.exists() || !exe.exists()) {
224-
System.out.println("Start Downloading " + filename);
225-
folder.mkdirs();
226-
exe.createNewFile();
227-
byte dataBuffer[] = new byte[1024];
228-
int bytesRead;
229-
FileOutputStream fileOutputStream = new FileOutputStream(exe.getAbsoluteFile());
230-
while ((bytesRead = pis.read(dataBuffer, 0, 1024)) != -1) {
231-
fileOutputStream.write(dataBuffer, 0, bytesRead);
213+
try {
214+
URL url = new URL(downloadJsonURL);
215+
URLConnection connection = url.openConnection();
216+
InputStream is = connection.getInputStream();
217+
ProcessInputStream pis = new ProcessInputStream(is, (int) sizeOfJson);
218+
pis.addListener(new Listener() {
219+
@Override
220+
public void process(double percent) {
221+
System.out.println("Download percent " + percent);
222+
Platform.runLater(() -> {
223+
progress.setProgress(percent);
224+
});
225+
}
226+
});
227+
228+
if (!folder.exists() || !exe.exists()) {
229+
System.out.println("Start Downloading " + filename);
230+
folder.mkdirs();
231+
exe.createNewFile();
232+
byte dataBuffer[] = new byte[1024];
233+
int bytesRead;
234+
FileOutputStream fileOutputStream = new FileOutputStream(exe.getAbsoluteFile());
235+
while ((bytesRead = pis.read(dataBuffer, 0, 1024)) != -1) {
236+
fileOutputStream.write(dataBuffer, 0, bytesRead);
237+
}
238+
fileOutputStream.close();
239+
pis.close();
240+
System.out.println("Finished downloading " + filename);
241+
} else {
242+
System.out.println("Not downloadeing, it existst " + filename);
232243
}
233-
fileOutputStream.close();
234-
pis.close();
235-
System.out.println("Finished downloading " + filename);
236-
} else {
237-
System.out.println("Not downloadeing, it existst " + filename);
244+
} catch (Throwable t) {
245+
t.printStackTrace();
238246
}
247+
System.out.println("Using JVM "+exe.getAbsolutePath());
239248
return exe;
240249
}
241250
}

lib/src/main/java/com/commonwealthrobotics/LatestFromGithubLaunchUI.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class LatestFromGithubLaunchUI {
4444
public static Stage stage;
4545

4646
public static String latestVersionString = "";
47-
public static String myVersionString = "None";
47+
public static String myVersionString = null;
4848
public static long sizeOfJar = 0;
4949
public static long sizeOfJson = 0;
5050
@FXML // ResourceBundle that was given to the FXMLLoader
@@ -142,6 +142,7 @@ public void launchApplication() {
142142
Platform.runLater(() -> {
143143
yesButton.setDisable(true);
144144
noButton.setDisable(true);
145+
stage.close();
145146
});
146147
new Thread(() -> {
147148
String command;
@@ -153,7 +154,7 @@ public void launchApplication() {
153154
System.exit(1);
154155
return;
155156
}
156-
Platform.runLater(() -> stage.close());
157+
157158
try {
158159
Thread.sleep(100);
159160
} catch (InterruptedException e1) {
@@ -274,13 +275,14 @@ void initialize() {
274275
assert progress != null : "fx:id=\"progress\" was not injected: check your FXML file 'ui.fxml'.";
275276
assert previousVersion != null : "fx:id=\"previousVersion\" was not injected: check your FXML file 'ui.fxml'.";
276277
assert currentVersion != null : "fx:id=\"currentVersion\" was not injected: check your FXML file 'ui.fxml'.";
277-
278+
boolean noInternet = false;
278279
try {
279280
readCurrentVersion("https://api.github.com/repos/" + project + "/" + repoName + "/releases/latest");
280281
binary.setText(project + "\n" + repoName + "\n" + jarName + "\n" + (sizeOfJar / 1000000) + " Mb");
281282
} catch (IOException e) {
282283
// TODO Auto-generated catch block
283284
e.printStackTrace();
285+
noInternet=true;
284286
}
285287
stage.setTitle("Auto-Updater for " + repoName);
286288
currentVersion.setText(latestVersionString);
@@ -305,10 +307,12 @@ void initialize() {
305307
e.printStackTrace();
306308
}
307309
}
308-
309-
if (myVersionString.contentEquals(latestVersionString)) {
310-
launchApplication();
311-
}
310+
if(!noInternet) {
311+
if (myVersionString.contentEquals(latestVersionString)) {
312+
launchApplication();
313+
}
314+
}else
315+
onNo(null);
312316

313317
}
314318
}

0 commit comments

Comments
 (0)