Added package cleanup
This commit is contained in:
97
sh.actions.package-cleanup/Models/SoftwareVersion.cs
Normal file
97
sh.actions.package-cleanup/Models/SoftwareVersion.cs
Normal file
@@ -0,0 +1,97 @@
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace sh.actions.package_cleanup.Models;
|
||||
|
||||
public class SoftwareVersion(
|
||||
int major,
|
||||
int minor,
|
||||
int patch = 0,
|
||||
int build = 0,
|
||||
string? suffix = null,
|
||||
string? input = null,
|
||||
bool isLatest = false)
|
||||
{
|
||||
public int Major { get; set; } = major;
|
||||
public int Minor { get; set; } = minor;
|
||||
public int Patch { get; set; } = patch;
|
||||
public int Build { get; set; } = build;
|
||||
public string? Suffix { get; set; } = suffix;
|
||||
public string? Input { get; set; } = input;
|
||||
public bool IsLatest { get; set; } = isLatest;
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
var versionStr = $"{Major}.{Minor}.{Patch}.{Build}";
|
||||
if (!string.IsNullOrEmpty(Suffix))
|
||||
{
|
||||
versionStr += $"-{Suffix}";
|
||||
}
|
||||
|
||||
return versionStr;
|
||||
}
|
||||
|
||||
public override bool Equals(object? obj)
|
||||
{
|
||||
return obj is SoftwareVersion version && ToString() == version.ToString();
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return ToString().GetHashCode();
|
||||
}
|
||||
|
||||
public static SoftwareVersion Parse(string versionString)
|
||||
{
|
||||
// Try matching format: X.X.X.X or X.X.X.X-suffix
|
||||
var match = Regex.Match(versionString, @"(\d+)\.(\d+)\.(\d+)\.(\d+)(?:-(.*))?");
|
||||
if (match.Success)
|
||||
{
|
||||
var major = int.Parse(match.Groups[1].Value);
|
||||
var minor = int.Parse(match.Groups[2].Value);
|
||||
var patch = int.Parse(match.Groups[3].Value);
|
||||
var build = int.Parse(match.Groups[4].Value);
|
||||
var suffix = match.Groups[5].Value;
|
||||
return new SoftwareVersion(major, minor, patch, build, string.IsNullOrEmpty(suffix) ? null : suffix,
|
||||
versionString);
|
||||
}
|
||||
|
||||
// Try matching format: X.X.X or X.X.X-suffix
|
||||
match = Regex.Match(versionString, @"(\d+)\.(\d+)\.(\d+)(?:-(.*))?");
|
||||
if (match.Success)
|
||||
{
|
||||
var major = int.Parse(match.Groups[1].Value);
|
||||
var minor = int.Parse(match.Groups[2].Value);
|
||||
var patch = int.Parse(match.Groups[3].Value);
|
||||
var suffix = match.Groups[4].Value;
|
||||
return new SoftwareVersion(major, minor, patch, 0, string.IsNullOrEmpty(suffix) ? null : suffix,
|
||||
versionString);
|
||||
}
|
||||
|
||||
// Try matching format: X.X or X.X-suffix
|
||||
match = Regex.Match(versionString, @"(\d+)\.(\d+)(?:-(.*))?");
|
||||
if (match.Success)
|
||||
{
|
||||
var major = int.Parse(match.Groups[1].Value);
|
||||
var minor = int.Parse(match.Groups[2].Value);
|
||||
var suffix = match.Groups[3].Value;
|
||||
return new SoftwareVersion(major, minor, 0, 0, string.IsNullOrEmpty(suffix) ? null : suffix, versionString);
|
||||
}
|
||||
|
||||
// Try matching format: X or X-suffix
|
||||
match = Regex.Match(versionString, @"(\d+)(?:-(.*))?");
|
||||
if (match.Success)
|
||||
{
|
||||
var major = int.Parse(match.Groups[1].Value);
|
||||
var suffix = match.Groups[2].Value;
|
||||
return new SoftwareVersion(major, 0, 0, 0, string.IsNullOrEmpty(suffix) ? null : suffix, versionString);
|
||||
}
|
||||
|
||||
// Special case for "latest"
|
||||
if (versionString == "latest")
|
||||
{
|
||||
return new SoftwareVersion(1, 0, 0, 0, null, versionString, isLatest: true);
|
||||
}
|
||||
|
||||
throw new ArgumentException($"Invalid version string: {versionString}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user