Skip to content

Commit 815b1ed

Browse files
committed
Adding styling to the autoupdater
1 parent 1d1f486 commit 815b1ed

File tree

5 files changed

+555
-23
lines changed

5 files changed

+555
-23
lines changed

CaDoodleUpdater/src/main/java/com/commonwealthrobotics/CadoodleUpdater.java

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
import java.io.OutputStream;
2828
import java.nio.charset.Charset;
2929
import java.nio.file.Files;
30+
import java.nio.file.Path;
3031
import java.nio.file.Paths;
32+
import java.nio.file.StandardCopyOption;
3133
import java.lang.reflect.Type;
3234
import java.util.HashMap;
3335
import java.util.List;
@@ -62,7 +64,8 @@ public class CadoodleUpdater {
6264
private Label binary; // Value injected by FXMLLoader
6365
@FXML // fx:id="currentVersion"
6466
private Label currentVersion; // Value injected by FXMLLoader
65-
67+
@FXML // fx:id="currentVersion"
68+
private Label infoBar;
6669
@FXML // fx:id="yesButton"
6770
private Button yesButton; // Value injected by FXMLLoader
6871

@@ -96,9 +99,11 @@ void onYes(ActionEvent event) {
9699
System.out.println("Yes path");
97100
yesButton.setDisable(true);
98101
noButton.setDisable(true);
102+
infoBar.setText("Downloading CaDoodle JAR...");
99103
new Thread(() -> {
100104

101105
try {
106+
102107
String downloadURL2 = downloadJarURL;
103108
System.out.println("Downloading "+downloadJarURL);
104109
URL url = new URL(downloadURL2);
@@ -114,10 +119,13 @@ public void process(double percent) {
114119
}
115120
});
116121
File folder = new File(bindir + latestVersionString + "/");
117-
File exe = new File(bindir + latestVersionString + "/" + jarName);
122+
File exe = new File(bindir + latestVersionString + "/" + jarName+"_TMP");
123+
File exeFinal = new File(bindir + latestVersionString + "/" + jarName);
118124

119-
if (!folder.exists() || !exe.exists() || sizeOfJar != exe.length()) {
125+
if (!folder.exists() || !exeFinal.exists() || sizeOfJar != exeFinal.length()) {
120126
folder.mkdirs();
127+
if(exe.exists())
128+
exe.delete();
121129
exe.createNewFile();
122130
byte dataBuffer[] = new byte[1024];
123131
int bytesRead;
@@ -129,8 +137,12 @@ public void process(double percent) {
129137
pis.close();
130138

131139
}
132-
if (folder.exists() && exe.exists() && sizeOfJar == exe.length())
140+
141+
if(exe.exists())
142+
Files.move(exe.toPath(), exeFinal.toPath(), StandardCopyOption.REPLACE_EXISTING);
143+
if (folder.exists() && exeFinal.exists() && sizeOfJar == exeFinal.length())
133144
myVersionString = latestVersionString;
145+
134146
} catch (Exception e1) {
135147
// TODO Auto-generated catch block
136148
e1.printStackTrace();
@@ -151,7 +163,7 @@ public void launchApplication() {
151163
new Thread(() -> {
152164
String command;
153165
try {
154-
command = JvmManager.getCommandString(project, repoName, myVersionString,downloadJsonURL,sizeOfJson,progress,bindir);
166+
command = JvmManager.getCommandString(project, repoName, myVersionString,downloadJsonURL,sizeOfJson,progress,bindir,infoBar);
155167
} catch (Exception e) {
156168
// TODO Auto-generated catch block
157169
e.printStackTrace();
@@ -198,7 +210,22 @@ public void launchApplication() {
198210
System.out.println("Running:\n\n"+finalCommand+"\n\n");
199211
new Thread(() -> {
200212
try {
201-
Process process = Runtime.getRuntime().exec(finalCommand);
213+
// Get the current environment
214+
Map<String, String> env = new HashMap<>(System.getenv());
215+
216+
// Extract JAVA_HOME from the JVM path
217+
// Assuming your command starts with the full path to java executable
218+
String javaHome = extractJavaHomeFromCommand(command);
219+
if (javaHome != null) {
220+
env.put("JAVA_HOME", javaHome);
221+
}
222+
223+
// Convert environment map to array format
224+
String[] envArray = env.entrySet().stream()
225+
.map(entry -> entry.getKey() + "=" + entry.getValue())
226+
.toArray(String[]::new);
227+
// Execute with modified environment
228+
Process process = Runtime.getRuntime().exec(finalCommand, envArray);
202229
Thread thread = new Thread(()->{
203230
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
204231
String line;
@@ -257,7 +284,31 @@ public void launchApplication() {
257284
}).start();
258285
}).start();
259286
}
260-
287+
private static String extractJavaHomeFromCommand(String command) {
288+
try {
289+
// Split the command to get the java executable path
290+
String[] parts = command.split(" ");
291+
if (parts.length > 0) {
292+
String javaPath = parts[0];
293+
294+
// Remove quotes if present
295+
javaPath = javaPath.replace("\"", "");
296+
297+
// Get the parent directory of the bin folder
298+
Path path = Paths.get(javaPath);
299+
if (path.getFileName().toString().startsWith("java")) {
300+
// Go up from java executable to bin, then to JAVA_HOME
301+
Path binDir = path.getParent();
302+
if (binDir != null && binDir.getFileName().toString().equals("bin")) {
303+
return binDir.getParent().toString();
304+
}
305+
}
306+
}
307+
} catch (Exception e) {
308+
System.err.println("Could not extract JAVA_HOME from command: " + e.getMessage());
309+
}
310+
return null;
311+
}
261312
public static boolean isWin() {
262313
return System.getProperty("os.name").toLowerCase().contains("windows");
263314
}

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.nio.file.Files;
2020
import java.nio.file.Path;
2121
import java.nio.file.Paths;
22+
import java.nio.file.StandardCopyOption;
2223
import java.util.ArrayList;
2324
import java.util.Enumeration;
2425
import java.util.HashMap;
@@ -43,15 +44,18 @@
4344
import com.google.gson.reflect.TypeToken;
4445

4546
import javafx.application.Platform;
47+
import javafx.scene.control.Label;
4648
import javafx.scene.control.ProgressBar;
4749

4850
public class JvmManager {
4951
private static long timeSincePrint = System.currentTimeMillis();
52+
private static Label infoBar;
5053

5154
public static String getCommandString(String project, String repo, String version, String downloadJsonURL,
52-
long sizeOfJson, ProgressBar progress, String bindir) throws Exception {
55+
long sizeOfJson, ProgressBar progress, String bindir, Label info) throws Exception {
5356
if (version == null)
5457
version = "0.0.6";
58+
infoBar=info;
5559
File exe;
5660
File jvmArchive;
5761
File dest;
@@ -87,7 +91,7 @@ public static String getCommandString(String project, String repo, String versio
8791
System.out.println("Failed the extract, erasing and re-downloading");
8892
jvmArchive.delete();
8993
ex.printStackTrace();
90-
return getCommandString(project, repo, version, downloadJsonURL, sizeOfJson, progress, bindir);
94+
return getCommandString(project, repo, version, downloadJsonURL, sizeOfJson, progress, bindir,info);
9195
}
9296
}
9397
if (type.toLowerCase().contains("tar.gz")) {
@@ -146,6 +150,7 @@ public static boolean isExecutable(ZipArchiveEntry entry) {
146150
private static void unzip(File path, String dir) throws Exception {
147151
String fileBaseName = FilenameUtils.getBaseName(path.getName().toString());
148152
Path destFolderPath = new File(dir).toPath();
153+
Platform.runLater(()->infoBar.setText("Inflating Java Runtime..."));
149154

150155
try (ZipFile zipFile = ZipFile.builder().setFile(path).get()) {
151156
Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
@@ -187,6 +192,7 @@ private static void unzip(File path, String dir) throws Exception {
187192

188193
public static void untar(File tarFile, String dir) throws Exception {
189194
System.out.println("Untaring " + tarFile.getName() + " into " + dir);
195+
Platform.runLater(()->infoBar.setText("Inflating Java Runtime..."));
190196

191197
File dest = new File(dir);
192198
dest.mkdir();
@@ -231,7 +237,9 @@ private static String bits(byte b) {
231237
private static File download(String version, String downloadJsonURL, long sizeOfJson, ProgressBar progress,
232238
String bindir, String filename) throws MalformedURLException, IOException, FileNotFoundException {
233239
File folder = new File(bindir + version + "/");
234-
File exe = new File(bindir + version + "/" + filename);
240+
File exe = new File(bindir + version + "/" + filename+"_TMP");
241+
File exeFinal = new File(bindir + version + "/" + filename);
242+
235243
try {
236244
URL url = new URL(downloadJsonURL);
237245
URLConnection connection = url.openConnection();
@@ -252,8 +260,11 @@ public void process(double percent) {
252260
}
253261
});
254262

255-
if (!folder.exists() || !exe.exists()) {
263+
if (!folder.exists() || !exeFinal.exists()) {
264+
if(exe.exists())
265+
exe.delete();
256266
System.out.println("Start Downloading " + filename);
267+
Platform.runLater(()->infoBar.setText("Downloading Java Runtime..."));
257268
folder.mkdirs();
258269
exe.createNewFile();
259270
byte dataBuffer[] = new byte[1024];
@@ -271,7 +282,10 @@ public void process(double percent) {
271282
} catch (Throwable t) {
272283
t.printStackTrace();
273284
}
274-
System.out.println("Using JVM " + exe.getAbsolutePath());
275-
return exe;
285+
System.out.println("Using JVM " + exeFinal.getAbsolutePath());
286+
if(exe.exists())
287+
Files.move(exe.toPath(), exeFinal.toPath(), StandardCopyOption.REPLACE_EXISTING);
288+
return exeFinal;
276289
}
290+
277291
}
62.9 KB
Loading

0 commit comments

Comments
 (0)