Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-1294: Leverage AnalyzedPackageVersion and AnalyzedPackageIsPrerelease from addin metadata #1399

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,7 @@ Task("GetExtensionPackages")
context.DownloadPackages(extensionDir,
extensionSpecs
.Where(x => !string.IsNullOrEmpty(x.NuGet))
.Select(x => x.NuGet)
.ToArray());
.ToDictionary(e => e.NuGet, e => e.AnalyzedPackageVersion));

context.CalcSupportedCakeVersions(extensionDir,
extensionSpecs
Expand Down
6 changes: 0 additions & 6 deletions config.wyam
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ Settings[DocsKeys.BlogRssPath] = "blog/feed/rss/index.xml";
Pipelines.InsertBefore(Docs.Code, "Extensions",
ReadFiles("../extensions/*.yml"),
Yaml(),
Meta(
"IsPrerelease",
FileSystem.GetInputFile($"../release/extensions/{@doc.String("NuGet")}.isprerelease").Exists
? bool.Parse(FileSystem.GetInputFile($"../release/extensions/{@doc.String("NuGet")}.isprerelease").ReadAllText())
: false
),
Meta(
"SupportedCakeVersions",
FileSystem.GetInputFile($"../release/extensions/{@doc.String("NuGet")}.supportedcakeversions").Exists
Expand Down
2 changes: 1 addition & 1 deletion input/_UsageAddin.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@{
string nuget = Model.String("NuGet");
string version = Model.String("AnalyzedPackageVersion");
bool isPrerelease = Model.Bool("isPrerelease");
bool isPrerelease = Model.Bool("AnalyzedPackageIsPrerelease");
string preReleaseQueryParam = isPrerelease ? "&prerelease" : string.Empty;
}

Expand Down
54 changes: 21 additions & 33 deletions nuget.cake
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#addin "nuget:https://api.nuget.org/v3/index.json?package=Polly&version=7.1.0"
#addin "nuget:https://api.nuget.org/v3/index.json?package=LitJson&version=0.13.0"
#addin "nuget:https://api.nuget.org/v3/index.json??package=Cake.FileHelpers&version=3.3.0"
#addin "nuget:https://api.nuget.org/v3/index.json?package=NuGet.Protocol&version=5.7.0"

Expand All @@ -12,7 +11,7 @@ using NuGetRepository = NuGet.Protocol.Core.Types.Repository;
using NuGet.Versioning;
using Polly;

public static void DownloadPackages(this ICakeContext context, DirectoryPath extensionDir, string[] packageIds)
public static void DownloadPackages(this ICakeContext context, DirectoryPath extensionDir, IDictionary<string, string> packageIdAndVersionLookup)
{
var retryPolicy = Policy
.Handle<Exception>()
Expand All @@ -23,45 +22,36 @@ public static void DownloadPackages(this ICakeContext context, DirectoryPath ext
using (var httpClient = new HttpClient())
{
Parallel.ForEach(
packageIds,
packageId => retryPolicy.Execute(
() => context.DownloadPackage(extensionDir, httpClient, packageId)
packageIdAndVersionLookup,
packageIdAndVersion => retryPolicy.Execute(
() => context.DownloadPackage(extensionDir, httpClient, packageIdAndVersion)
)
);
}
}

public static void DownloadPackage(this ICakeContext context, DirectoryPath extensionDir, HttpClient httpClient, string packageId)
public static void DownloadPackage(this ICakeContext context, DirectoryPath extensionDir, HttpClient httpClient, KeyValuePair<string, string> packageIdAndVersion)
{
context.Information("[{0}] fetching meta data...", packageId);
var packageJson = httpClient.GetStringAsync($"https://api.nuget.org/v3/registration5-semver1/{packageId.ToLowerInvariant()}/index.json")
.GetAwaiter()
.GetResult();

var package = LitJson.JsonMapper.ToObject<Package>(packageJson);

var packageInfo = (
from packageItem in package?.items ?? Enumerable.Empty<PackageItem>()
from item in packageItem.items ?? Enumerable.Empty<PackageRevisions>()
where item?.catalogEntry?.listed ?? false
let version = item?.catalogEntry?.version
let nugetVersion = version is null ? null : NuGetVersion.Parse(version)
orderby !(nugetVersion?.IsPrerelease ?? false), nugetVersion
select new
{
id = packageId,
version = nugetVersion?.ToNormalizedString(),
isPrerelease = nugetVersion?.IsPrerelease,
item?.packageContent
}
).LastOrDefault();
var packageId = packageIdAndVersion.Key;
var packageVersion = packageIdAndVersion.Value;

context.Verbose("[{0}] found {1}...", packageId, packageInfo);
if (string.IsNullOrWhiteSpace(packageVersion))
{
context.Warning($"[{{0}}] Skipping download of {packageId} - AnalyzedPackageVersion not specified in metadata", packageId);
pascalberger marked this conversation as resolved.
Show resolved Hide resolved
return;
}

context.Information($"[{{0}}] Downloading NuGet package {packageId} v{packageVersion}...", packageId);

var packageDir = extensionDir.Combine($"{packageId}.{packageInfo.version}".ToLower());
var packageIdLower = packageId.ToLowerInvariant();
var packageVersionLower = packageVersion.ToLowerInvariant();
var packageDownloadUrl = $"https://api.nuget.org/v3-flatcontainer/{packageIdLower}/{packageVersionLower}/{packageIdLower}.{packageVersionLower}.nupkg";

using (var stream = httpClient.GetStreamAsync(packageInfo.packageContent).GetAwaiter().GetResult())
context.Verbose($"[{{0}}] GET {packageDownloadUrl}", packageId);

var packageDir = extensionDir.Combine($"{packageId}.{packageVersion}".ToLowerInvariant());

using (var stream = httpClient.GetStreamAsync(packageDownloadUrl).GetAwaiter().GetResult())
{
using (var zipStream = new System.IO.Compression.ZipArchive(stream))
{
Expand Down Expand Up @@ -103,8 +93,6 @@ public static void DownloadPackage(this ICakeContext context, DirectoryPath exte
}
}

context.FileWriteText(extensionDir.CombineWithFilePath($"{packageId}.isprerelease"), packageInfo.isPrerelease.GetValueOrDefault().ToString());

context.Information("[{0}] done.", packageId);
}

Expand Down