Skip to content

Commit

Permalink
Ensure that we extract version number properly
Browse files Browse the repository at this point in the history
  • Loading branch information
bcssov committed Jul 3, 2024
1 parent 800a066 commit f69af05
Showing 1 changed file with 33 additions and 9 deletions.
42 changes: 33 additions & 9 deletions src/IronyModManager.Shared/Extensions.Version.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@
// Created : 02-16-2020
//
// Last Modified By : Mario
// Last Modified On : 07-21-2022
// Last Modified On : 07-03-2024
// ***********************************************************************
// <copyright file="Extensions.Version.cs" company="Mario">
// Mario
// </copyright>
// <summary></summary>
// ***********************************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace IronyModManager.Shared
{
Expand All @@ -25,15 +27,40 @@ public static partial class Extensions
{
#nullable enable


#region Fields

/// <summary>
/// The version extract
/// </summary>
private static readonly Regex versionExtract = new(@"(?:\*|\d+)(?:\.\d+|\.\*)+");

#endregion Fields

#region Methods

/// <summary>
/// Converts to version.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>System.Nullable&lt;Shared.Version&gt;.</returns>
public static Shared.Version? ToVersion(this string value)
public static Version? ToVersion(this string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return null;
}

// Detect PDX mess
if (value.Any(char.IsLetter))
{
var result = versionExtract.Match(value);
if (result.Success)
{
value = result.Value;
}
}

var sb = new StringBuilder();
var count = 0;
foreach (var item in value.Split("."))
Expand All @@ -43,6 +70,7 @@ public static partial class Extensions
{
parsed = "*";
}

if (int.TryParse(parsed, out var part))
{
sb.Append($"{part}.");
Expand All @@ -51,17 +79,13 @@ public static partial class Extensions
{
sb.Append($"{(count > 1 ? int.MaxValue : 0)}.");
}

count++;
}
if (Shared.Version.TryParse(sb.ToString().Trim().Trim('.'), out var parsedVersion))
{
return parsedVersion;
}
return null;

return Version.TryParse(sb.ToString().Trim().Trim('.'), out var parsedVersion) ? parsedVersion : null;
}

#endregion Methods

#nullable disable
}
}

0 comments on commit f69af05

Please sign in to comment.