Пример #1
0
        public static PackageList ReadFromCacheDirectory(string rootDirectory)
        {
            var packageList = new PackageList();

            foreach (var packageDirectory in Directory.GetDirectories(rootDirectory))
            {
                foreach (var versionDirectory in Directory.GetDirectories(packageDirectory))
                {
                    if (Directory.GetFiles(versionDirectory, "*.nupkg").Length == 1)
                    {
                        var packageId = Path.GetFileName(packageDirectory);
                        var version   = Path.GetFileName(versionDirectory);
                        packageList.Add(packageId, version);
                    }
                }
            }

            return(packageList);
        }
Пример #2
0
        public static PackageList FromText(string text)
        {
            var result = new PackageList();

            if (string.IsNullOrEmpty(text))
            {
                return(result);
            }

            var lines = text.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

            foreach (var line in lines)
            {
                int space = line.IndexOf(' ');
                if (space > 0)
                {
                    string packageId = line.Substring(0, space);
                    string version   = line.Substring(space + 1, line.Length - space - 1);
                    result.Add(packageId, version);
                }
            }

            return(result);
        }
Пример #3
0
        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}");
                }
            }
        }