From 15a555a508bac264d4fbda09357cb645a2443cb8 Mon Sep 17 00:00:00 2001 From: Rob Date: Thu, 11 Jul 2024 01:50:30 +0200 Subject: [PATCH] version can be added to download line --- ChocolateStoreCore/ChocolateStoreCore.csproj | 6 +-- ChocolateStoreCore/Helpers/HttpHelper.cs | 16 +++++--- ChocolateStoreCore/Helpers/StringHelper.cs | 18 +++++++++ ChocolateStoreCore/PackageCacher.cs | 2 +- ChocolateStoreCore/appsettings.json | 2 +- ChocolateStoreCoreTests/AppTests.cs | 2 +- ChocolateStoreCoreTests/StringHelperTests.cs | 39 ++++++++++++++++++++ version.txt | 2 +- 8 files changed, 75 insertions(+), 12 deletions(-) diff --git a/ChocolateStoreCore/ChocolateStoreCore.csproj b/ChocolateStoreCore/ChocolateStoreCore.csproj index 3cc5dbd..b18667d 100644 --- a/ChocolateStoreCore/ChocolateStoreCore.csproj +++ b/ChocolateStoreCore/ChocolateStoreCore.csproj @@ -17,9 +17,9 @@ ChocolateStoreCore Download, modify and cache chocolatey packages locally - 1.0.7 - 1.0.7.21 - 1.0.7.21 + 1.1.0 + 1.1.0.2 + 1.1.0.2 Robert Zaufall Robert Zaufall IT Consulting (c)2023 by Robert Zaufall diff --git a/ChocolateStoreCore/Helpers/HttpHelper.cs b/ChocolateStoreCore/Helpers/HttpHelper.cs index 2a4424c..452da69 100644 --- a/ChocolateStoreCore/Helpers/HttpHelper.cs +++ b/ChocolateStoreCore/Helpers/HttpHelper.cs @@ -40,9 +40,15 @@ public string GetPackageIdInfoTemplate(string packageId, string version) return String.Format(_settings.ApiPackageRequestWithVersion, packageId, version); } - public string GetMetadataForPackageId(string packageId) + public string GetMetadataForPackageId(string downloadItem) { - var url = new Uri(new Uri(_settings.ApiUrl), _settings.ApiPath + GetPackageIdInfoTemplate(packageId.ToLower(CultureInfo.InvariantCulture))); + var id = StringHelper.GetPackageIdFromString(downloadItem); + var version = StringHelper.GetVersionFromString(downloadItem); + + var url = new Uri( + new Uri(_settings.ApiUrl), + _settings.ApiPath + (version == "" ? GetPackageIdInfoTemplate(id) : GetPackageIdInfoTemplate(id, version)) + ); try { @@ -52,7 +58,7 @@ public string GetMetadataForPackageId(string packageId) if (response.StatusCode == HttpStatusCode.OK) { var content = new StreamReader(response.Content.ReadAsStreamAsync().Result).ReadToEnd(); - _logger.LogInformation("Download metadata for '{packageId}' done.", packageId); + _logger.LogInformation("Download metadata for '{downloadItem}' done.", id); return content; } } @@ -60,7 +66,7 @@ public string GetMetadataForPackageId(string packageId) } catch (Exception ex) { - _logger.LogError(ex, "Failed to get metadata for '{packageId}'", packageId); + _logger.LogError(ex, "Failed to get metadata for '{downloadItem}'", id); return null; } } @@ -72,7 +78,7 @@ public string CheckUrl(string url) using (var request = _httpClientFactory.CreateClient("chocolatey")) { HttpRequestMessage reqMessage = new(HttpMethod.Head, url); - + var response = request.SendAsync(reqMessage).Result; if (response.StatusCode == HttpStatusCode.OK) { diff --git a/ChocolateStoreCore/Helpers/StringHelper.cs b/ChocolateStoreCore/Helpers/StringHelper.cs index 0c60ac1..e7c218b 100644 --- a/ChocolateStoreCore/Helpers/StringHelper.cs +++ b/ChocolateStoreCore/Helpers/StringHelper.cs @@ -89,5 +89,23 @@ public static string GetPathWithLocal(string root, string path) } return path; } + + public static string GetPackageIdFromString(string packageId) + { + if (packageId.Contains(' ')) + { + return packageId.ToLowerInvariant().Substring(0, packageId.IndexOf(' ')); + } + return packageId; + } + + public static string GetVersionFromString(string packageId) + { + if (packageId.Contains(' ')) + { + return packageId.ToLowerInvariant().Substring(packageId.IndexOf(' ') + 1, packageId.Length - packageId.IndexOf(' ') - 1); + } + return String.Empty; + } } } diff --git a/ChocolateStoreCore/PackageCacher.cs b/ChocolateStoreCore/PackageCacher.cs index ba9100b..fdb5ada 100644 --- a/ChocolateStoreCore/PackageCacher.cs +++ b/ChocolateStoreCore/PackageCacher.cs @@ -133,7 +133,7 @@ public List GetLastVersions(List downloads, bool flat { var chocolateyPackages = downloads.Select(x => { - return _chocolateyHelper.GetLastVersion(x.ToLower(CultureInfo.InvariantCulture)); + return _chocolateyHelper.GetLastVersion(x.ToLowerInvariant()); }).Where(x => x != null).ToList(); return !flattenDependencies ? chocolateyPackages : _chocolateyHelper.FlattenDependencies(chocolateyPackages); } diff --git a/ChocolateStoreCore/appsettings.json b/ChocolateStoreCore/appsettings.json index 3625b1f..a26f536 100644 --- a/ChocolateStoreCore/appsettings.json +++ b/ChocolateStoreCore/appsettings.json @@ -5,7 +5,7 @@ "ApiUserAgent": "User-Agent: ChocolateStoreCore", "ApiPath": "/api/v2", "ApiPackageRequest": "/Packages()?$filter=(tolower(Id)%20eq%20'{0}')%20and%20IsLatestVersion", - "ApiPackageRequestWithVersion": "/Packages()?$filter=(tolower(Id)%20eq%20'{0}')%20and%20version%20eq%20'{1}'", + "ApiPackageRequestWithVersion": "/Packages()?$filter=(tolower(Id)%20eq%20'{0}')%20and%20(Version%20eq%20'{1}')", "ApiFindAllRequest": "/FindPackagesById()?id='{0}.app'", "ApiFindAllNextRequest": "/FindPackagesById?id='{0}'&$skiptoken='{0}','{1}'", "ApiGetRequest": "/package/{0}/{1}", diff --git a/ChocolateStoreCoreTests/AppTests.cs b/ChocolateStoreCoreTests/AppTests.cs index de908f0..dfd3f18 100644 --- a/ChocolateStoreCoreTests/AppTests.cs +++ b/ChocolateStoreCoreTests/AppTests.cs @@ -22,7 +22,7 @@ public async void RunTest(bool whatif) var fixture = TestFixture.GetFixture(); var chocolateyHelper = fixture.Freeze>(); - chocolateyHelper.Setup(_ => _.GetDownloadList(It.IsAny())).Returns(new List { "a", "b" }); + chocolateyHelper.Setup(_ => _.GetDownloadList(It.IsAny())).Returns(new List { "a", "b", "c 1.0.0" }); chocolateyHelper.Setup(_ => _.FlattenDependencies(It.IsAny>())).Returns(new List { new ChocolateyPackage() }).Verifiable(); var packageCacher = fixture.Freeze>(); diff --git a/ChocolateStoreCoreTests/StringHelperTests.cs b/ChocolateStoreCoreTests/StringHelperTests.cs index 26cb065..1d9282b 100644 --- a/ChocolateStoreCoreTests/StringHelperTests.cs +++ b/ChocolateStoreCoreTests/StringHelperTests.cs @@ -81,5 +81,44 @@ public void ReplaceTokensByVariables_Firefox() result.Should().NotBeNullOrWhiteSpace(); Regex.Replace(result, @"\s+", "").Should().Be(Regex.Replace(input, @"\s+", "")); } + + [Theory] + [InlineData("a", "a")] + [InlineData("a ", "a")] + [InlineData("a b", "a")] + [InlineData("a b b", "a")] + [InlineData("", "")] + [InlineData("abcdefghijk", "abcdefghijk")] + [InlineData("test 1.0.0", "test")] + public void GetPackageId_From_PackageId (string packageId, string id) + { + // Arrange + + // Act + var resultId = StringHelper.GetPackageIdFromString(packageId); + + // Assert + resultId.Should().NotBeNull(); + resultId.Should().Be(id); + } + + [Theory] + [InlineData("a", "")] + [InlineData("a ", "")] + [InlineData("a b", "b")] + [InlineData("a b b", "b b")] + [InlineData("", "")] + [InlineData("test 1.0.0", "1.0.0")] + public void GetVersion_From_PackageId(string packageId, string id) + { + // Arrange + + // Act + var resultId = StringHelper.GetVersionFromString(packageId); + + // Assert + resultId.Should().NotBeNull(); + resultId.Should().Be(id); + } } } diff --git a/version.txt b/version.txt index f0491b2..a3fdef3 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -1.0.7.21 +1.1.0.2