Esempio n. 1
0
 private static JProperty GetProperty(NuGetFeed feed)
 {
     return new JProperty(feed.Name, feed.Url);
 }
        public async Task <IEnumerable <(SourceRepository Repository, IPackageSearchMetadata Package)> > SearchPackagesAsync(NuGetFeed packageFeed,
                                                                                                                             string searchTerm, int maxResults = 128,
                                                                                                                             bool includePrerelease            = false)
        {
            var providers             = GetNugetResourceProviders();
            var sourceRepo            = GetSourceRepo(packageFeed, providers);
            var packageSearchResource = await sourceRepo.GetResourceAsync <PackageSearchResource>();

            SearchFilter searchFilter;

            if (includePrerelease)
            {
                searchFilter = new SearchFilter(includePrerelease, SearchFilterType.IsAbsoluteLatestVersion);
            }
            else
            {
                searchFilter = new SearchFilter(includePrerelease);
            }

            var packages = await packageSearchResource.SearchAsync(searchTerm, searchFilter, 0, maxResults, _logger, CancellationToken.None);

            return(packages.Select(x => (sourceRepo, x)));
        }
        public async Task <string[]> DownloadAsync(string packageFolder, string packageName, string packageVersion = null, bool includePrerelease = false,
                                                   NuGetFeed packageFeed = null, bool onlyDownload = false)
        {
            if (!Directory.Exists(packageFolder))
            {
                Directory.CreateDirectory(packageFolder);
            }

            var providers                = GetNugetResourceProviders();
            var settings                 = Settings.LoadDefaultSettings(packageFolder, null, new MachineWideSettings());
            var packageSourceProvider    = new PackageSourceProvider(settings);
            var sourceRepositoryProvider = new SourceRepositoryProvider(packageSourceProvider, providers);

            var dotNetFramework = Assembly
                                  .GetEntryAssembly()
                                  .GetCustomAttribute <TargetFrameworkAttribute>()?
                                  .FrameworkName;

            var frameworkNameProvider = new FrameworkNameProvider(
                new[] { DefaultFrameworkMappings.Instance },
                new[] { DefaultPortableFrameworkMappings.Instance });

            var nuGetFramework = NuGetFramework.ParseFrameworkName(dotNetFramework, frameworkNameProvider);

            IPackageSearchMetadata package    = null;
            SourceRepository       sourceRepo = null;

            if (!string.IsNullOrWhiteSpace(packageFeed?.Feed))
            {
                sourceRepo = GetSourceRepo(packageFeed, providers);

                package = await SearchPackageAsync(packageName, packageVersion, includePrerelease, sourceRepo);
            }
            else
            {
                foreach (var repo in sourceRepositoryProvider.GetRepositories())
                {
                    if (packageFeed != null && repo.PackageSource.Name != packageFeed.Name)
                    {
                        continue;
                    }

                    package = await SearchPackageAsync(packageName, packageVersion, includePrerelease, repo);

                    if (package != null)
                    {
                        sourceRepo = repo;

                        break;
                    }
                }
            }

            if (package == null)
            {
                throw new PackageNotFoundException($"Couldn't find package '{packageVersion}'.{packageVersion}.");
            }

            var project        = new PluginFolderNugetProject(packageFolder, package, nuGetFramework, onlyDownload);
            var packageManager = new NuGetPackageManager(sourceRepositoryProvider, settings, packageFolder)
            {
                PackagesFolderNuGetProject = project
            };

            var clientPolicyContext = ClientPolicyContext.GetClientPolicy(settings, _logger);

            var projectContext = new FolderProjectContext(_logger)
            {
                PackageExtractionContext = new PackageExtractionContext(
                    PackageSaveMode.Defaultv2,
                    PackageExtractionBehavior.XmlDocFileSaveMode,
                    clientPolicyContext,
                    _logger)
            };

            var resolutionContext = new ResolutionContext(
                DependencyBehavior.Lowest,
                includePrerelease,
                includeUnlisted: false,
                VersionConstraints.None);

            var downloadContext = new PackageDownloadContext(
                resolutionContext.SourceCacheContext,
                packageFolder,
                resolutionContext.SourceCacheContext.DirectDownload);

            // We are waiting here instead of await as await actually doesn't seem to work correctly.
            packageManager.InstallPackageAsync(
                project,
                package.Identity,
                resolutionContext,
                projectContext,
                downloadContext,
                sourceRepo,
                new List <SourceRepository>(),
                CancellationToken.None).Wait();

            if (onlyDownload)
            {
                var versionFolder = Path.Combine(packageFolder, package.Identity.ToString());

                return(Directory.GetFiles(versionFolder, "*.*", SearchOption.AllDirectories));
            }

            return(await project.GetPluginAssemblyFilesAsync());
        }
Esempio n. 4
0
 public NuGetStore(NuGetFeed feed, string packagesCachePath)
 {
     _feed = feed;
     _packagesCachePath = packagesCachePath;
 }
Esempio n. 5
0
 private void UnsubscribeFromFeedPropertyChanged(NuGetFeed feed)
 {
     feed.PropertyChanged -= OnFeedPropertyPropertyChanged;
 }
Esempio n. 6
0
 private void SubscribeToFeedPropertyChanged(NuGetFeed feed)
 {
     feed.PropertyChanged += OnFeedPropertyPropertyChanged;
 }
    public NuGetPackageListCrawler(NuGetFeed feed)
    {
        ArgumentNullException.ThrowIfNull(feed);

        _feed = feed;
    }
    public static async Task CreateAsync(string packageListPath)
    {
        var feed = new NuGetFeed(NuGetFeeds.NuGetOrg);

        Console.WriteLine($"Fetching owner information...");
        var ownerInformation = await feed.GetOwnerMappingAsync();

        var packageDocument = new XDocument();
        var root            = new XElement("packages");

        packageDocument.Add(root);

        var packageIds = ownerInformation.Keys
                         .ToHashSet(StringComparer.OrdinalIgnoreCase)
                         .Where(id => IsOwnedByDotNet(ownerInformation, id) &&
                                PackageFilter.Default.IsMatch(id))
                         .ToArray();

        Console.WriteLine($"Found {packageIds.Length:N0} relevant platform package IDs.");

        Console.WriteLine($"Filtering to latest versions...");

        var filteredPackages = new ConcurrentBag <PackageIdentity>();

        await Parallel.ForEachAsync(packageIds, async (packageId, _) =>
        {
            var versions   = await feed.GetAllVersionsAsync(packageId);
            var identities = versions.Select(v => new PackageIdentity(packageId, v))
                             .OrderByDescending(v => v.Version, VersionComparer.VersionReleaseMetadata)
                             .ToArray();

            var latestStable     = identities.FirstOrDefault(i => !i.Version.IsPrerelease);
            var latestPrerelease = identities.FirstOrDefault(i => i.Version.IsPrerelease);

            if (latestStable is not null && latestPrerelease is not null)
            {
                var stableIsNewer = VersionComparer.VersionReleaseMetadata.Compare(latestPrerelease.Version, latestStable.Version) <= 0;
                if (stableIsNewer)
                {
                    latestPrerelease = null;
                }
            }

            if (latestStable is not null)
            {
                filteredPackages.Add(latestStable);
            }

            if (latestPrerelease is not null)
            {
                filteredPackages.Add(latestPrerelease);
            }
        });

        Console.WriteLine($"Found {filteredPackages.Count:N0} platform package versions.");

        foreach (var item in filteredPackages.OrderBy(pi => pi.Id)
                 .ThenBy(pi => pi.Version, VersionComparer.VersionReleaseMetadata))
        {
            var e = new XElement("package",
                                 new XAttribute("id", item.Id),
                                 new XAttribute("version", item.Version)
                                 );

            root.Add(e);
        }

        Directory.CreateDirectory(Path.GetDirectoryName(packageListPath));
        packageDocument.Save(packageListPath);
    }