Skip to content

Commit bc365cd

Browse files
committed
Rearrange things so we restart later
1 parent cb2e4aa commit bc365cd

File tree

1 file changed

+26
-32
lines changed

1 file changed

+26
-32
lines changed

src/main/java/net/imagej/ui/swing/updater/LauncherMigrator.java

+26-32
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,7 @@ class LauncherMigrator {
107107
* <ul>
108108
* <li>If launched with the old ImageJ launcher, call
109109
* {@link #switchToNewLauncher()}.</li>
110-
* <li>If launched with the new Jaunch launcher, call
111-
* {@link #migrateShortcuts()} and {@link #migrateUpdateSite()}.</li>
112-
* <li>If launched in some other creative way, do nothing.</li>
110+
* <li>Do nothing if launched with Jaunch or in some other creative way.</li>
113111
* </ul>
114112
*/
115113
void checkLaunchStatus() {
@@ -124,20 +122,9 @@ void checkLaunchStatus() {
124122
// The old launcher does not set the scijava.app.name property.
125123
boolean oldLauncherUsed = System.getProperty("scijava.app.name") == null;
126124
if (oldLauncherUsed) switchToNewLauncher();
127-
else {
128-
// FIXME if we change update shortcuts to be windows-style "warn only"
129-
// then it's not a problem to do it beforehand.
130-
// If we leave as-is then we need to make sure we're instructing the user
131-
// to run Help > Update... a second time after restarting to complete the
132-
// installation
133-
migrateShortcuts();
134-
// FIXME if we are doing the actual site toggling here then they will need
135-
// to restart again to update. I'd prefer to do it in the switchToNewLauncher
136-
// logic. Also, I am curious if having this logic here would *prevent*
137-
// a user from using the new launcher with old update sites, which I think
138-
// is not intended/desired?
139-
migrateUpdateSite();
140-
}
125+
// NB: it is possible and valid to use Jaunch with the old Fiji update sites
126+
// So we do not want to force updating to them just because the new launcher
127+
// is being used.
141128
}
142129

143130
/**
@@ -162,9 +149,8 @@ void checkLaunchStatus() {
162149
* OS level rather than potentially exploding at the application level.
163150
* </p>
164151
*/
165-
private void migrateShortcuts() {
152+
private void warnAboutShortcuts() {
166153
// If no old launchers are present, assume we already did this.
167-
// FIXME: Which launcher(s) should we look for? All platforms? Or current only?
168154

169155
// Fix links within Linux .desktop files.
170156
// ~/.local/share/applications
@@ -395,31 +381,31 @@ private void switchToNewLauncher() {
395381
}
396382
}
397383

398-
// All looks good! We can finally relaunch safely with the new launcher.
384+
// All looks good!
385+
// Switch update sites, and then we can finally relaunch safely with the
386+
// new launcher.
387+
migrateUpdateSite();
388+
warnAboutShortcuts();
399389
File exeFile = exeFile(appSlug, appDir);
400390
try {
401-
Process p = new ProcessBuilder(exeFile.getPath()).start();
402-
boolean terminated = p.waitFor(500, TimeUnit.MILLISECONDS);
403-
if (terminated || !p.isAlive()) {
404-
askForBugReport(log, appTitle, appSlug,
405-
new RuntimeException("New launcher terminated unexpectedly"));
406-
return;
407-
}
408-
// New process seems to be up and running; we are done. Whew!
409-
startExeRenameProcess(appDir.toPath());
391+
startExeRenameAndRestart(appDir.toPath(), exeFile.getAbsolutePath());
410392
appService.getContext().dispose();
411393
System.exit(0);
412394
}
413-
catch (IOException | InterruptedException exc) {
395+
catch (IOException exc) {
414396
askForBugReport(log, appTitle, appSlug, exc);
415397
}
416398
}
417399

418400
/**
419401
* Helper method to rename the current exe to a backup version, to discourage
420402
* accidental use of a launcher that is incompatible with Java 21, for example
403+
*
404+
* After deletion, the given exePath is invoked, starting the post-update app
421405
*/
422-
private static void startExeRenameProcess(Path appDir) throws IOException {
406+
private static void startExeRenameAndRestart(Path appDir,
407+
String exePath) throws IOException
408+
{
423409
Path originalExe;
424410
if (OS_WIN) {
425411
if (ARCH.equals("x32")) {
@@ -464,7 +450,8 @@ private static void startExeRenameProcess(Path appDir) throws IOException {
464450
" Write-Host \"Attempt $tries of " + numTries + " failed...\"; " +
465451
" if ($tries -eq " + numTries + ") { Write-Host 'Max attempts reached. Exiting.'; break; } " +
466452
" Start-Sleep -Milliseconds " + checkIntervalMs + " " +
467-
"} }"
453+
"} } " +
454+
exePath
468455
);
469456
} else {
470457
// Unix/Linux/Mac implementation using bash
@@ -486,9 +473,16 @@ private static void startExeRenameProcess(Path appDir) throws IOException {
486473
" fi; " +
487474
" sleep " + (checkIntervalMs / 1000.0) + "; " +
488475
"done"
476+
+ exePath
489477
);
490478
}
491479

480+
// Write error output to a file if it doesn't exist already
481+
File errFile = appDir.resolve("fiji-restart.err").toFile();
482+
if (!errFile.exists()) {
483+
pb.redirectError(appDir.resolve("fiji-restart.err").toFile());
484+
}
485+
492486
// Redirect process output (optional - for debugging)
493487
// pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
494488
// pb.redirectError(ProcessBuilder.Redirect.INHERIT);

0 commit comments

Comments
 (0)