-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Use version comparison function #41102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -170,11 +170,38 @@ public void RefreshWorkloadManifests() | |
|
||
if (_workloadSet == null && availableWorkloadSets.Any()) | ||
{ | ||
var maxWorkloadSetVersion = availableWorkloadSets.Keys.Select(k => new ReleaseVersion(k)).Max()!; | ||
var maxWorkloadSetVersion = availableWorkloadSets.Keys.Aggregate((s1, s2) => VersionCompare(s1, s2) >= 0 ? s1 : s2); | ||
_workloadSet = availableWorkloadSets[maxWorkloadSetVersion.ToString()]; | ||
} | ||
} | ||
|
||
private static int VersionCompare(string first, string second) | ||
{ | ||
if (first.Equals(second)) | ||
{ | ||
return 0; | ||
} | ||
|
||
var firstDash = first.IndexOf('-'); | ||
var secondDash = second.IndexOf('-'); | ||
firstDash = firstDash < 0 ? first.Length : firstDash; | ||
secondDash = secondDash < 0 ? second.Length : secondDash; | ||
|
||
var firstVersion = new Version(first.Substring(0, firstDash)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what happens if you pass in the string without removing the - part first? The dash is supported in versions but I recall there being a reason you needed to do it this way (comment)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, I tried a fairly standard version with a - in a console project I use for testing, and it threw an exception. The dash is supported in ReleaseVersions, I think, but not Version? Or perhaps there are some strings with dashes that can be parsed into a Version while others can't? |
||
var secondVersion = new Version(second.Substring(0, secondDash)); | ||
|
||
var comparison = firstVersion.CompareTo(secondVersion); | ||
if (comparison != 0) | ||
{ | ||
return comparison; | ||
} | ||
|
||
var modifiedFirst = "1.1.1" + (firstDash == first.Length ? string.Empty : first.Substring(firstDash)); | ||
var modifiedSecond = "1.1.1" + (secondDash == second.Length ? string.Empty : second.Substring(secondDash)); | ||
|
||
return new ReleaseVersion(modifiedFirst).CompareTo(new ReleaseVersion(modifiedSecond)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does release version have any limitations? Curious why you can't just compare the part after the dash directly rather than making it into a release version first? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, it needed some digits before the dash to make a ReleaseVersion, but it couldn't support four '.'-separated numbers. I originally proposed doing a simpler comparison like a string comparison, since my current understanding is that the part after the dash is just compared alphanumerically, but dsplaisted was concerned that there might be more complexity and suggested doing it with a ReleaseVersion. |
||
} | ||
|
||
void ThrowExceptionIfManifestsNotAvailable() | ||
{ | ||
if (_exceptionToThrow != null) | ||
|
@@ -240,10 +267,10 @@ void AddManifest(string manifestId, string manifestDirectory, string featureBand | |
|
||
void ProbeDirectory(string manifestDirectory, string featureBand) | ||
{ | ||
(string? id, string? finalManifestDirectory, ReleaseVersion? version) = ResolveManifestDirectory(manifestDirectory); | ||
(string? id, string? finalManifestDirectory, string? version) = ResolveManifestDirectory(manifestDirectory); | ||
if (id != null && finalManifestDirectory != null) | ||
{ | ||
AddManifest(id, finalManifestDirectory, featureBand, version?.ToString() ?? Path.GetFileName(manifestDirectory)); | ||
AddManifest(id, finalManifestDirectory, featureBand, version ?? Path.GetFileName(manifestDirectory)); | ||
} | ||
} | ||
|
||
|
@@ -348,7 +375,7 @@ void ProbeDirectory(string manifestDirectory, string featureBand) | |
/// Given a folder that may directly include a WorkloadManifest.json file, or may have the workload manifests in version subfolders, choose the directory | ||
/// with the latest workload manifest. | ||
/// </summary> | ||
private (string? id, string? manifestDirectory, ReleaseVersion? version) ResolveManifestDirectory(string manifestDirectory) | ||
private (string? id, string? manifestDirectory, string? version) ResolveManifestDirectory(string manifestDirectory) | ||
{ | ||
string manifestId = Path.GetFileName(manifestDirectory); | ||
if (_outdatedManifestIds.Contains(manifestId) || | ||
|
@@ -361,26 +388,22 @@ void ProbeDirectory(string manifestDirectory, string featureBand) | |
.Where(dir => File.Exists(Path.Combine(dir, "WorkloadManifest.json"))) | ||
.Select(dir => | ||
{ | ||
ReleaseVersion? releaseVersion = null; | ||
ReleaseVersion.TryParse(Path.GetFileName(dir), out releaseVersion); | ||
return (directory: dir, version: releaseVersion); | ||
}) | ||
.Where(t => t.version != null) | ||
.OrderByDescending(t => t.version) | ||
.ToList(); | ||
return (directory: dir, version: Path.GetFileName(dir)); | ||
marcpopMSFT marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
|
||
// Assume that if there are any versioned subfolders, they are higher manifest versions than a workload manifest directly in the specified folder, if it exists | ||
if (manifestVersionDirectories.Any()) | ||
{ | ||
return (manifestId, manifestVersionDirectories.First().directory, manifestVersionDirectories.First().version); | ||
var maxVersionDirectory = manifestVersionDirectories.Aggregate((d1, d2) => VersionCompare(d1.version, d2.version) > 0 ? d1 : d2); | ||
return (manifestId, maxVersionDirectory.directory, maxVersionDirectory.version); | ||
} | ||
else if (File.Exists(Path.Combine(manifestDirectory, "WorkloadManifest.json"))) | ||
{ | ||
var manifestPath = Path.Combine(manifestDirectory, "WorkloadManifest.json"); | ||
try | ||
{ | ||
var manifestContents = WorkloadManifestReader.ReadWorkloadManifest(manifestId, File.OpenRead(manifestPath), manifestPath); | ||
return (manifestId, manifestDirectory, new ReleaseVersion(manifestContents.Version)); | ||
return (manifestId, manifestDirectory, manifestContents.Version); | ||
} | ||
catch | ||
{ } | ||
|
Uh oh!
There was an error while loading. Please reload this page.