Skip to content

Commit

Permalink
version can be added to download line
Browse files Browse the repository at this point in the history
  • Loading branch information
robertZaufall committed Jul 10, 2024
1 parent 70dfeb6 commit 15a555a
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 12 deletions.
6 changes: 3 additions & 3 deletions ChocolateStoreCore/ChocolateStoreCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
<PropertyGroup>
<Product>ChocolateStoreCore</Product>
<Description>Download, modify and cache chocolatey packages locally</Description>
<Version>1.0.7</Version>
<FileVersion>1.0.7.21</FileVersion>
<AssemblyVersion>1.0.7.21</AssemblyVersion>
<Version>1.1.0</Version>
<FileVersion>1.1.0.2</FileVersion>
<AssemblyVersion>1.1.0.2</AssemblyVersion>
<Authors>Robert Zaufall</Authors>
<Company>Robert Zaufall IT Consulting</Company>
<Copyright>(c)2023 by Robert Zaufall</Copyright>
Expand Down
16 changes: 11 additions & 5 deletions ChocolateStoreCore/Helpers/HttpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -52,15 +58,15 @@ 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;
}
}
return null;
}
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;
}
}
Expand All @@ -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)
{
Expand Down
18 changes: 18 additions & 0 deletions ChocolateStoreCore/Helpers/StringHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
2 changes: 1 addition & 1 deletion ChocolateStoreCore/PackageCacher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public List<ChocolateyPackage> GetLastVersions(List<string> 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);
}
Expand Down
2 changes: 1 addition & 1 deletion ChocolateStoreCore/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand Down
2 changes: 1 addition & 1 deletion ChocolateStoreCoreTests/AppTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public async void RunTest(bool whatif)
var fixture = TestFixture.GetFixture();

var chocolateyHelper = fixture.Freeze<Mock<IChocolateyHelper>>();
chocolateyHelper.Setup(_ => _.GetDownloadList(It.IsAny<string>())).Returns(new List<string> { "a", "b" });
chocolateyHelper.Setup(_ => _.GetDownloadList(It.IsAny<string>())).Returns(new List<string> { "a", "b", "c 1.0.0" });
chocolateyHelper.Setup(_ => _.FlattenDependencies(It.IsAny<List<ChocolateyPackage>>())).Returns(new List<ChocolateyPackage> { new ChocolateyPackage() }).Verifiable();

var packageCacher = fixture.Freeze<Mock<IPackageCacher>>();
Expand Down
39 changes: 39 additions & 0 deletions ChocolateStoreCoreTests/StringHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.7.21
1.1.0.2

0 comments on commit 15a555a

Please sign in to comment.