Improve hwi version parsing

This commit is contained in:
nicolas.dorier 2021-09-08 12:00:35 +09:00
parent 661e0db88a
commit 80427e7d05
No known key found for this signature in database
GPG Key ID: 6618763EF09186FE
2 changed files with 30 additions and 30 deletions

View File

@ -7,6 +7,7 @@ using System.IO;
using System.Linq;
using System.Text;
using BTCPayServer.Helpers;
using System.Text.RegularExpressions;
namespace BTCPayServer.Hwi
{
@ -200,39 +201,18 @@ namespace BTCPayServer.Hwi
return rawPath.Replace(@"\\", @"\");
}
public static bool TryParseVersion(string hwiResponse, string substringFrom, out Version version)
{
int startIndex = hwiResponse.IndexOf(substringFrom) + substringFrom.Length;
var versionString = hwiResponse.Substring(startIndex).Trim();
version = null;
if (Version.TryParse(versionString, out Version v))
{
version = v;
return true;
}
return false;
}
static Regex VersionRegex = new Regex(@"(\d+)\.(\d+)(\.(\d+))?");
public static bool TryParseVersion(string hwiResponse, out Version version)
{
version = null;
// Order matters! https://github.com/zkSNACKs/WalletWasabi/pull/1905/commits/cecefcc50af140cc06cb93961cda86f9b21db11b
// Example output: hwi.exe 1.0.1
if (TryParseVersion(hwiResponse, "hwi.exe", out Version v2))
{
version = v2;
}
// Example output: hwi 1.0.1
if (TryParseVersion(hwiResponse, "hwi", out Version v1))
{
version = v1;
}
return version != null;
var m = VersionRegex.Match(hwiResponse);
if (!m.Success || m.Groups.Count < 5)
return false;
int.TryParse(m.Groups[1].Value, out var major);
int.TryParse(m.Groups[2].Value, out var minor);
int.TryParse(m.Groups[4].Value, out var build);
version = new Version(major, minor, build);
return true;
}
public static Version ParseVersion(string hwiResponse)

View File

@ -29,6 +29,26 @@ namespace BTCPayServer.Vault.Tests
ILogger Logger;
[Fact]
public void CanParseVersion()
{
var v = new[]
{
("hwi.exe 1.0.1", new Version(1,0,1)),
("hwi 1.0.1", new Version(1,0,1)),
("hwi 1.2", new Version(1,2,0)),
("pouet 2.1", new Version(2,1,0)),
("pouet 2.1rl", new Version(2,1,0)),
("pouet 2.1 rl", new Version(2,1,0)),
("long 2.1.3.4 rl", new Version(2,1,3)),
};
foreach (var o in v)
{
Assert.Equal(o.Item2, HwiParser.ParseVersion(o.Item1));
}
}
[Fact]
public async Task CanGetVersion()
{