From 93ab29d1c3b0d1a199d8977851f52b3cda9995fa Mon Sep 17 00:00:00 2001 From: Michael Babienco Date: Mon, 1 Jul 2024 17:49:37 +0900 Subject: [PATCH] Fix app cast bug; some versions not read properly Closes #588 --- .../AppCastMakerTests.cs | 6 ++++++ .../AppCastMaker.cs | 16 +++++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/NetSparkle.Tests.AppCastGenerator/AppCastMakerTests.cs b/src/NetSparkle.Tests.AppCastGenerator/AppCastMakerTests.cs index e64fd522..bbaf7c67 100644 --- a/src/NetSparkle.Tests.AppCastGenerator/AppCastMakerTests.cs +++ b/src/NetSparkle.Tests.AppCastGenerator/AppCastMakerTests.cs @@ -131,6 +131,12 @@ public void CanGetVersionFromName() Assert.Equal("99999999999999999999999.999999999999999999.99999999999999999", AppCastMaker.GetVersionFromName("app 99999999999999999999999.999999999999999999.99999999999999999.txt")); Assert.Equal("1.0.0-0A.is.legal", AppCastMaker.GetVersionFromName("app 1.0.0-0A.is.legal.txt")); + // #588 + Assert.Equal("2.10.1", AppCastMaker.GetVersionFromName("appsetup-2.10.1.exe")); + Assert.Equal("2.10.1", AppCastMaker.GetVersionFromName("appsetup_2.10.1.exe")); + Assert.Equal("2.10.1", AppCastMaker.GetVersionFromName("appsetup 2.10.1.exe")); + Assert.Equal("2.10.1", AppCastMaker.GetVersionFromName("appsetup2.10.1.exe")); + // Invalid semantic versions tests Assert.Null(AppCastMaker.GetVersionFromName("app 1.2.3-0123.txt")); Assert.Null(AppCastMaker.GetVersionFromName("app 1.2.3-0123.0123.txt")); diff --git a/src/NetSparkle.Tools.AppCastGenerator/AppCastMaker.cs b/src/NetSparkle.Tools.AppCastGenerator/AppCastMaker.cs index a311342d..0ab9bf74 100644 --- a/src/NetSparkle.Tools.AppCastGenerator/AppCastMaker.cs +++ b/src/NetSparkle.Tools.AppCastGenerator/AppCastMaker.cs @@ -66,7 +66,8 @@ private static bool ContainsValidVersionInfo(string segment) return Regex.IsMatch(segment, simpleVersionPattern) || Regex.IsMatch(segment, semverPattern); } - // This regex finds the first text block that is not preceded by a + or - and is followed by a number (starting from left) + // This regex finds the first text block that is not preceded by a + or - + // and is followed by a number (starting from left) private static string RemoveTextBlockFromLeft(string input) { if (!Regex.IsMatch(input, @"\d")) @@ -94,7 +95,10 @@ private static string RemoveTextBlockFromLeft(string input) } } - // This regex finds the first text block that is not preceded by a + or - and is followed by a number (starting from right) + // This regex finds the first text block that is not preceded + // by a + or - and is followed by a number (starting from right) + // TODO: this func and RemoveTextBlockFromLeft need renaming and + // clarifying; docs are not too great here and may be swapping left/right private static string RemoveTextBlockFromRight(string input) { var match = Regex.Match(input, @"(?= 0; i--) { var segment = segments[i]; + // if segment has text in it at the start and digits later, get rid of text and +/- symbol if (Regex.IsMatch(segment, @"[a-zA-Z]") && Regex.IsMatch(segment, @"\d")) { - var match = Regex.Match(segment, @"[^+-]*[a-zA-Z]"); + var match = Regex.Match(segment, @"[^+-]*[a-zA-Z][+-]?"); if (match.Success) { segment = segment.Substring(match.Index + match.Length); @@ -148,11 +153,8 @@ private static string FindVersionInfoInString(string str, bool removeTextFromLef string lastValidVersion = null; if (!string.IsNullOrEmpty(str)) { - // Remove any text block from left - // For example 0.1foo becomes 0.1, 0.1-foo stays 0.1-foo, 0.1+foo stays 0.1+foo str = removeTextFromLeft ? RemoveTextBlockFromLeft(str) : RemoveTextBlockFromRight(str); - - // Make sure left part has a number + // Make sure part has a number if (Regex.IsMatch(str, @"\d")) { // Check if it has only numeric values and a simple version for quick check