Created
April 17, 2020 08:06
-
-
Save alistairjevans/05be81ee57bd715a218439112bc755fd to your computer and use it in GitHub Desktop.
Package Identity Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private async Task<PackageIdentity> GetPackageIdentity( | |
ExtensionConfiguration extConfig, SourceCacheContext cache, ILogger nugetLogger, | |
IEnumerable<SourceRepository> repositories, CancellationToken cancelToken) | |
{ | |
// Go through each repository. | |
// If a repository contains only pre-release packages (e.g. AutoStep CI), and | |
// the configuration doesn't permit pre-release versions, | |
// the search will look at other ones (e.g. NuGet). | |
foreach (var sourceRepository in repositories) | |
{ | |
// Get a 'resource' from the repository. | |
var findPackageResource = await sourceRepository.GetResourceAsync<FindPackageByIdResource>(); | |
// Get the list of all available versions of the package in the repository. | |
var allVersions = await findPackageResource.GetAllVersionsAsync(extConfig.Package, cache, nugetLogger, cancelToken); | |
NuGetVersion selected; | |
// Have we specified a version range? | |
if (extConfig.Version != null) | |
{ | |
if (!VersionRange.TryParse(extConfig.Version, out var range)) | |
{ | |
throw new Exception("Invalid version range provided."); | |
} | |
// Find the best package version match for the range. | |
// Consider pre-release versions, but only if the extension is configured to use them. | |
var bestVersion = range.FindBestMatch(allVersions.Where(v => extConfig.PreRelease || !v.IsPrerelease)); | |
selected = bestVersion; | |
} | |
else | |
{ | |
// No version; choose the latest, allow pre-release if configured. | |
selected = allVersions.LastOrDefault(v => v.IsPrerelease == extConfig.PreRelease); | |
} | |
if (selected is object) | |
{ | |
return new PackageIdentity(extConfig.Package, selected); | |
} | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment