private static void FindPackageSourcesWithPackage(string[] packageSources, string packageId, string version) { foreach (var source in packageSources) { bool exists = NuGetAPI.DoesPackageExist(source, packageId, version).Result; if (!exists) { continue; } var versions = NuGetAPI.GetPackageVersions(source, packageId).Result.Where(v => v.ToString().Contains(version)).ToArray(); if (versions.Any()) { Output(source); foreach (var v in versions) { Output(v.ToString()); } } } }
public static async Task MapPackages(PackageList cachePackages, string[] packageSources) { List <(string packageId, string version, string source)> mapping = new List <(string packageId, string version, string source)>(); Dictionary <(string packageId, string version), List <string> > sourcesForPackage = new Dictionary <(string packageId, string version), List <string> >(); PackageList packagesNotFound = new PackageList(); Dictionary <string, PackageList> uniquePackagesInSource = new Dictionary <string, PackageList>(); void ClaimPackageVersion(string packageId, string version, string packageSource) { mapping.Add((packageId, version, packageId)); var key = (packageId, version); if (!sourcesForPackage.TryGetValue(key, out var bucket)) { bucket = new List <string>(); sourcesForPackage[key] = bucket; } bucket.Add(packageSource); } foreach (var source in packageSources) { var repository = NuGetAPI.GetSourceRepository(source); var listApi = await repository.GetResourceAsync <ListResource>(); var packageExistApi = await repository.GetResourceAsync <FindPackageByIdResource>(); HashSet <string> idsInSource = null; if (listApi != null && IncludePackageSource(source)) { var idsInSourceResult = await listApi.ListAsync( null, prerelease : true, allVersions : false, includeDelisted : true, NullLogger.Instance, CancellationToken.None); var enumerator = idsInSourceResult.GetEnumeratorAsync(); idsInSource = new HashSet <string>(StringComparer.OrdinalIgnoreCase); while (await enumerator.MoveNextAsync()) { var current = enumerator.Current; idsInSource.Add(current.Identity.Id); } } foreach (var packageIds in cachePackages.Packages) { if (idsInSource != null && !idsInSource.Contains(packageIds.Key)) { continue; } var packageId = packageIds.Key; foreach (var version in packageIds.Value.OrderBy(s => s)) { var exists = await packageExistApi.DoesPackageExistAsync(packageId, NuGetVersion.Parse(version), NuGetAPI.Cache, NullLogger.Instance, CancellationToken.None); if (exists) { ClaimPackageVersion(packageId, version, source); } } } } foreach (var kvp in sourcesForPackage) { if (kvp.Value.Count == 0) { packagesNotFound.Add(kvp.Key.packageId, kvp.Key.version); Output($"Package not found in any of the sources: {kvp.Key.packageId} {kvp.Key.version}"); } else if (kvp.Value.Count == 1) { var singleSource = kvp.Value[0]; if (!uniquePackagesInSource.TryGetValue(singleSource, out var bucket)) { bucket = new PackageList(); uniquePackagesInSource[singleSource] = bucket; } bucket.Add(kvp.Key.packageId, kvp.Key.version); } } foreach (var source in packageSources) { if (!uniquePackagesInSource.ContainsKey(source)) { Output($"Redundant package source: {source}"); } } }