Skip to content

Commit

Permalink
WIP: Refactor how sysprop fallback values are done
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrueden committed Nov 21, 2024
1 parent 6cfcad4 commit eb47248
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions src/main/java/net/imagej/ui/swing/updater/LauncherMigrator.java
Original file line number Diff line number Diff line change
Expand Up @@ -316,29 +316,28 @@ private void switchToNewLauncher() {
// In order to trigger it successfully, we need to set various properties:

File appConfigFile = new File(configDir, appSlug + ".toml");
File appUserConfigFile = new File(configDir, appSlug + ".cfg");
List<String> lines;
try {
lines = Files.readAllLines(appConfigFile.toPath());
}
catch (IOException exc) {
log.debug(exc);
// Couldn't read from the config file; define some fallback values.
lines = Arrays.asList(
"-Dscijava.app.java-links='https://downloads.imagej.net/java/jdk-urls.txt',",
"-Dscijava.app.java-version-minimum=8,",
"-Dscijava.app.java-version-recommended=21,",
"-Dscijava.app.config-file='" + appUserConfigFile.getPath() + "',"
);
// Couldn't read from the config file.
lines = new ArrayList<>();
}

setPropertyIfNull("scijava.app.name", appTitle);

Path splashImage = appDir.toPath().resolve("images").resolve("icon.png");
setPropertyIfNull("scijava.app.splash-image", splashImage.toString());
extractAndSetProperty("scijava.app.java-links", lines);
extractAndSetProperty("scijava.app.java-version-minimum", lines);
extractAndSetProperty("scijava.app.java-version-recommended", lines);
extractAndSetProperty("scijava.app.config-file", lines);
setPropertyIfNull("scijava.app.config-file", appUserConfigFile.getPath());

extractAndSetProperty("scijava.app.java-links", lines,
"https://downloads.imagej.net/java/jdk-urls.txt");
extractAndSetProperty("scijava.app.java-version-minimum", lines, "8");
extractAndSetProperty("scijava.app.java-version-recommended", lines, "21");
extractAndSetProperty("scijava.app.config-file", lines,
new File(configDir, appSlug + ".cfg").getPath());

String platform = UpdaterUtil.getPlatform();
// FIXME: I think the macOS platform will be wrong here. It needs -arm64 suffix!
setPropertyIfNull("scijava.app.java-platform", platform);
Expand Down Expand Up @@ -508,13 +507,17 @@ private static void setPropertyIfNull(String name, String value) {
if (System.getProperty(name) == null) System.setProperty(name, value);
}

private static void extractAndSetProperty(String name, List<String> lines) {
private static void extractAndSetProperty(
String name,
List<String> lines,
String fallbackValue)
{
// No, the following replacement does not escape all problematic regex
// characters. But the properties we're working with here are only
// alphameric with dot separators, so it's OK. Hooray for pragmatism!
String escaped = name.replaceAll("\\.", "\\\\.");
Pattern p = Pattern.compile(".*'-D" + escaped + "=['\"]?(.*?)['\"]?,$");
String value = null;
String value = fallbackValue;
for (String line : lines) {
Matcher m = p.matcher(line);
if (m.matches()) {
Expand Down

0 comments on commit eb47248

Please sign in to comment.