fix: More robust logic for checking if a new version is available

Compares each digit for each position in the version string instead of calculating a number from all digits.
This commit is contained in:
lantzelot-swe 2022-06-29 23:22:38 +02:00
parent fc341cac63
commit 6f4a6ed2d5
2 changed files with 52 additions and 57 deletions

View File

@ -393,8 +393,7 @@ public abstract class BaseInstallManager implements AWTEventListener
protected boolean isNewVersionAvailable(String installName)
{
gitHubReleaseInformation = fetchLatestVersionFromGithub(installName);
return ManagerVersionChecker.getIntVersion(latestInInstallFolder) < ManagerVersionChecker
.getIntVersion(gitHubReleaseInformation.getInstallFile());
return ManagerVersionChecker.isNewer(latestInInstallFolder, gitHubReleaseInformation.getInstallFile());
}
public String getLatestInInstallFolder()
@ -460,7 +459,6 @@ public abstract class BaseInstallManager implements AWTEventListener
deleteOldInstallFiles(installName);
}
int value = JOptionPane.showConfirmDialog(MainWindow.getInstance(),
"Download completed, do you want to install " + productName + " " +
gitHubReleaseInformation.getLatestVersion() + " now?",

View File

@ -44,6 +44,7 @@ public class ManagerVersionChecker
private static boolean downloadIterrupted = false;
private static ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
private ManagerVersionChecker()
{
//Empty
@ -72,7 +73,8 @@ public class ManagerVersionChecker
JsonObject jsonObject = root.getAsJsonObject();
latestVersion = jsonObject.get("tag_name").getAsString();
tagloadUrl = jsonObject.get("html_url").getAsString();
downloadUrl = jsonObject.get("assets").getAsJsonArray().get(0).getAsJsonObject().get("browser_download_url").getAsString();
downloadUrl =
jsonObject.get("assets").getAsJsonArray().get(0).getAsJsonObject().get("browser_download_url").getAsString();
latestReleaseDescription = jsonObject.get("body").getAsString();
managerMainInstallFile = downloadUrl.substring(downloadUrl.lastIndexOf("/") + 1);
}
@ -132,7 +134,6 @@ public class ManagerVersionChecker
}
}
public static void startDownload(final DownloadDialog downloadDialog)
{
downloadIterrupted = false;
@ -185,38 +186,34 @@ public class ManagerVersionChecker
public static boolean isNewVersionAvailable()
{
logger.debug("Manifest version=" + FileManager.getPcuVersionFromManifest());
//Ignore versions starting with 1, the comparison does not work well with that.
//Once 2.x is available it will be calculated correctly
if (latestVersion.startsWith("1"))
{
return false;
}
return getIntVersion(FileManager.getPcuVersionFromManifest()) < getIntVersion(latestVersion);
return isNewer(FileManager.getPcuVersionFromManifest(), latestVersion);
}
public static int getIntVersion(String versionString)
public static boolean isNewer(String existingVersionString, String latestVersionString)
{
//Regular expression to match digits in a string
//Regular expression to match digits in a string. Each group returns the digit in the current position.
//1 and 10 is handled properly.
String regex = "\\d+";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(versionString);
String numbers = "";
while(matcher.find()) {
numbers = numbers + matcher.group();
}
if (numbers.isEmpty())
Matcher existingMatcher = pattern.matcher(existingVersionString);
Matcher latestMatcher = pattern.matcher(latestVersionString);
String currentExistingNumber = "";
String currentLatestNumber = "";
while (existingMatcher.find())
{
return 0;
}
int number = Integer.parseInt(numbers);
if (number < 1000)
currentExistingNumber = existingMatcher.group();
currentLatestNumber = latestMatcher.find() ? latestMatcher.group() : "0";
if (Integer.parseInt(currentLatestNumber) > Integer.parseInt(currentExistingNumber))
{
//Major releases increase the first digit and may not add any "build" number
//add a zero.
number = number * 10;
return true;
}
return number;
}
//There is an additional digit in the latest version string, consider it newer
if (latestMatcher.find())
{
return true;
}
return false;
}
public static String getLatestVersion()