async Task RestoreAnyMissingPackagesAsync(SourceCacheContext sourceCacheContext, CancellationToken cancellationToken)
        {
            var packages = await restoreManager.GetPackagesInSolutionAsync(
                solutionManager.SolutionDirectory,
                cancellationToken);

            if (!packages.Any(package => IsMissingForProject(package)))
            {
                return;
            }

            using (var monitor = new PackageRestoreMonitor(restoreManager, packageManagementEvents)) {
                var downloadContext = new PackageDownloadContext(sourceCacheContext);
                await restoreManager.RestoreMissingPackagesAsync(
                    solutionManager.SolutionDirectory,
                    packages,
                    context,
                    downloadContext,
                    cancellationToken);
            }

            await Runtime.RunInMainThread(() => {
                foreach (IDotNetProject dotNetProject in dotNetProjects)
                {
                    dotNetProject.RefreshReferenceStatus();
                }
            });

            packageManagementEvents.OnPackagesRestored();
        }
        /// <summary>
        /// Restores missing packages for the entire solution
        /// </summary>
        /// <returns></returns>
        public virtual async Task <PackageRestoreResult> RestoreMissingPackagesInSolutionAsync(
            string solutionDirectory,
            INuGetProjectContext nuGetProjectContext,
            CancellationToken token)
        {
            var packageReferencesDictionary = await GetPackagesReferencesDictionaryAsync(token);

            // When this method is called, the step to compute if a package is missing is implicit. Assume it is true
            var packages = packageReferencesDictionary.Select(p =>
            {
                Debug.Assert(p.Value != null);
                return(new PackageRestoreData(p.Key, p.Value, isMissing: true));
            });

            using (var cacheContext = new SourceCacheContext())
            {
                var downloadContext = new PackageDownloadContext(cacheContext);

                return(await RestoreMissingPackagesAsync(
                           solutionDirectory,
                           packages,
                           nuGetProjectContext,
                           downloadContext,
                           token));
            }
        }
        private async Task DownloadPackages(PackageIdentity identity, CancellationToken cancellationToken)
        {
            var resolutionContext = new ResolutionContext(DependencyBehavior.Lowest,
                                                          IncludePrerelease,
                                                          false,
                                                          VersionConstraints.None,
                                                          new GatherCache(),
                                                          new SourceCacheContext
            {
                NoCache        = true,
                DirectDownload = true
            });

            INuGetProjectContext projectContext = new ProjectContext(Logger);

            projectContext.PackageExtractionContext = new PackageExtractionContext(PackageSaveMode.Defaultv2,
                                                                                   XmlDocFileSaveMode.None,
                                                                                   ClientPolicyContext.GetClientPolicy(Settings, Logger),
                                                                                   Logger);

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

            await _packageManager.InstallPackageAsync(_project,
                                                      identity,
                                                      resolutionContext,
                                                      projectContext,
                                                      downloadContext,
                                                      SourceRepositories,
                                                      Array.Empty <SourceRepository>(),
                                                      cancellationToken);
        }
Пример #4
0
        /// <summary>
        /// Download the specified packageId from the NuGet feed.
        /// </summary>
        /// <param name="packageId">PackageIdentity to download.</param>
        /// <returns>Path to the downloaded package.</returns>
        public virtual async Task <string> DownloadPackageAsync(PackageIdentity packageId)
        {
            string downloadPath = Path.Combine(this.downloadFolder, $"{this.feedName}.zip");
            PackageDownloadContext packageDownloadContext = new PackageDownloadContext(
                this.sourceCacheContext,
                this.downloadFolder,
                true);

            DownloadResource downloadResource = await this.sourceRepository.GetResourceAsync <DownloadResource>();

            using (DownloadResourceResult downloadResourceResult = await downloadResource.GetDownloadResourceResultAsync(
                       packageId,
                       packageDownloadContext,
                       globalPackagesFolder: string.Empty,
                       logger: this.nuGetLogger,
                       token: CancellationToken.None))
            {
                if (downloadResourceResult.Status != DownloadResourceResultStatus.Available)
                {
                    throw new Exception("Download of NuGet package failed. DownloadResult Status: {downloadResourceResult.Status}");
                }

                using (FileStream fileStream = File.Create(downloadPath))
                {
                    downloadResourceResult.PackageStream.CopyTo(fileStream);
                }
            }

            return(downloadPath);
        }
Пример #5
0
        private async Task RestoreMissingPackagesInSolutionAsync(
            string solutionDirectory,
            IEnumerable <PackageRestoreData> packages,
            ILogger logger,
            CancellationToken token)
        {
            await TaskScheduler.Default;

            using (var cacheContext = new SourceCacheContext())
            {
                var downloadContext = new PackageDownloadContext(cacheContext)
                {
                    ParentId            = _nuGetProjectContext.OperationId,
                    ClientPolicyContext = ClientPolicyContext.GetClientPolicy(_settings, logger)
                };

                await _packageRestoreManager.RestoreMissingPackagesAsync(
                    solutionDirectory,
                    packages,
                    _nuGetProjectContext,
                    downloadContext,
                    logger,
                    token);
            }
        }
Пример #6
0
        public virtual Task <PackageRestoreResult> RestoreMissingPackagesAsync(string solutionDirectory,
                                                                               IEnumerable <PackageRestoreData> packages,
                                                                               INuGetProjectContext nuGetProjectContext,
                                                                               PackageDownloadContext downloadContext,
                                                                               CancellationToken token)
        {
            if (packages == null)
            {
                throw new ArgumentNullException(nameof(packages));
            }

            var nuGetPackageManager = GetNuGetPackageManager(solutionDirectory);

            var packageRestoreContext = new PackageRestoreContext(
                nuGetPackageManager,
                packages,
                token,
                PackageRestoredEvent,
                PackageRestoreFailedEvent,
                sourceRepositories: null,
                maxNumberOfParallelTasks: PackageManagementConstants.DefaultMaxDegreeOfParallelism,
                logger: NullLogger.Instance);

            return(RestoreMissingPackagesAsync(packageRestoreContext, nuGetProjectContext, downloadContext));
        }
Пример #7
0
        public static async Task <bool> EnsurePackageInstalled(string packageName, NuGetVersion nugetVersion)
        {
            var packageDir = Path.Combine(GlobalPackagesFolder, packageName.ToLowerInvariant(),
                                          nugetVersion.ToString());

            if (Directory.Exists(packageDir))
            {
                return(true);
            }

            var packagerIdentity = new PackageIdentity(packageName, nugetVersion);

            var pkgDownloadContext = new PackageDownloadContext(Cache);
            var downloadRes        = await Repository.GetResourceAsync <DownloadResource>();

            await RetryHelper.TryInvokeAsync(async() =>
                                             await downloadRes.GetDownloadResourceResultAsync(
                                                 packagerIdentity,
                                                 pkgDownloadContext,
                                                 GlobalPackagesFolder,
                                                 Logger,
                                                 CancellationToken.None), r => true);

            return(Directory.Exists(packageDir));
        }
        async Task RestorePackagesAsync(CancellationToken cancellationToken)
        {
            if (restoreManager != null)
            {
                using (var monitor = new PackageRestoreMonitor(restoreManager)) {
                    using (var cacheContext = new SourceCacheContext()) {
                        var downloadContext = new PackageDownloadContext(cacheContext);
                        await restoreManager.RestoreMissingPackagesAsync(
                            solutionManager.SolutionDirectory,
                            packagesToRestore,
                            new NuGetProjectContext(),
                            downloadContext,
                            cancellationToken);
                    }
                }
            }

            if (buildIntegratedRestorer != null)
            {
                await buildIntegratedRestorer.RestorePackages(buildIntegratedProjectsToBeRestored, cancellationToken);
            }

            if (nugetAwareRestorer != null)
            {
                await nugetAwareRestorer.RestoreMissingPackagesAsync(
                    nugetAwareProjectsToBeRestored,
                    new NuGetProjectContext(),
                    cancellationToken);
            }

            await Runtime.RunInMainThread(() => RefreshProjectReferences());

            packageManagementEvents.OnPackagesRestored();
        }
        async Task RestoreAnyMissingPackages(CancellationToken cancellationToken)
        {
            var packages = await restoreManager.GetPackagesInSolutionAsync(
                solutionManager.SolutionDirectory,
                cancellationToken);

            var missingPackages = packages.Select(IsMissingForCurrentProject).ToList();

            if (missingPackages.Any())
            {
                using (var monitor = new PackageRestoreMonitor(restoreManager, packageManagementEvents)) {
                    using (var cacheContext = new SourceCacheContext()) {
                        var downloadContext = new PackageDownloadContext(cacheContext);
                        await restoreManager.RestoreMissingPackagesAsync(
                            solutionManager.SolutionDirectory,
                            project,
                            context,
                            downloadContext,
                            cancellationToken);
                    }
                }

                await RunInMainThread(() => dotNetProject.RefreshReferenceStatus());

                packageManagementEvents.OnPackagesRestored();
            }
        }
Пример #10
0
        /// <summary>
        /// Downloads a specified NuGet package.
        /// </summary>
        /// <param name="packageMetadata">The package to download.</param>
        /// <param name="token">A cancellation token to allow cancellation of the task.</param>
        public async Task <DownloadResourceResult> DownloadPackageAsync(IPackageSearchMetadata packageMetadata, CancellationToken token = default)
        {
            var packageIdentity = new PackageIdentity(packageMetadata.Identity.Id, packageMetadata.Identity.Version);
            var downloadContext = new PackageDownloadContext(new SourceCacheContext(), Environment.CurrentDirectory, true);

            return(await _downloadResource.GetDownloadResourceResultAsync(packageIdentity, downloadContext, Path.GetTempPath(), _nullLogger, token));
        }
Пример #11
0
        public virtual Task <PackageRestoreResult> RestoreMissingPackagesAsync(string solutionDirectory,
                                                                               IEnumerable <PackageRestoreData> packages,
                                                                               INuGetProjectContext nuGetProjectContext,
                                                                               PackageDownloadContext downloadContext,
                                                                               ILogger logger,
                                                                               CancellationToken token)
        {
            if (packages == null)
            {
                throw new ArgumentNullException(nameof(packages));
            }

            var nuGetPackageManager = GetNuGetPackageManager(solutionDirectory);

            var packageRestoreContext = new PackageRestoreContext(
                nuGetPackageManager,
                packages,
                token,
                PackageRestoredEvent,
                PackageRestoreFailedEvent,
                sourceRepositories: null,
                maxNumberOfParallelTasks: PackageManagementConstants.DefaultMaxDegreeOfParallelism,
                logger: logger);

            if (nuGetProjectContext.PackageExtractionContext == null)
            {
                nuGetProjectContext.PackageExtractionContext = new PackageExtractionContext(
                    PackageSaveMode.Defaultv2,
                    PackageExtractionBehavior.XmlDocFileSaveMode,
                    ClientPolicyContext.GetClientPolicy(Settings, packageRestoreContext.Logger),
                    packageRestoreContext.Logger);
            }

            return(RestoreMissingPackagesAsync(packageRestoreContext, nuGetProjectContext, downloadContext));
        }
Пример #12
0
        public async Task InstallPackageAsync(PackageIdentity packageIdentity,
                                              ResolutionContext resolutionContext, PackageDownloadContext downloadContext, CancellationToken token)
        {
            var(primaryPackages, downgradeAllowed) = GetPackageInstallContext(packageIdentity);

            //Server
            var context = await GetRequiredPackages(primaryPackages, new HashSet <PackageIdentity> {
                packageIdentity
            },
                                                    downgradeAllowed, resolutionContext, _project.Framework, _logger, token);

            var actions = GetRequiredActions(context);

            await ExecuteActionsAsync(actions, downloadContext, token);
            await WriteModuleState(context, primaryPackages);

            //Other frameworks
            foreach (var framework in _project.FrameworkLibraries.Keys.Where(x => x != _project.Framework))
            {
                context = await GetRequiredPackages(primaryPackages, new HashSet <PackageIdentity> {
                    packageIdentity
                },
                                                    downgradeAllowed, resolutionContext, framework, _logger, token);

                actions = GetRequiredActions(context);
                await ExecuteActionsAsync(actions, downloadContext, token);

                await _project.AddModulesLock(framework, ExtractLock(context));
            }
        }
Пример #13
0
        private IObservable <DownloadResourceResult> GetDownloadResults()
        {
            var sourceSearchMetadatas = PackageSourceSearchMetadatas();

            if (!sourceSearchMetadatas.Any())
            {
                return(Observable.Empty <DownloadResourceResult>());
            }
            var packageSourceSearchMetadatas = sourceSearchMetadatas.ToObservable().Replay().RefCount();
            var downloadContext = new PackageDownloadContext(new SourceCacheContext());
            var providers       = new List <Lazy <INuGetResourceProvider> >();

            providers.AddRange(Repository.Provider.GetCoreV3());

            var sourceRepository = new SourceRepository(new PackageSource(Source), providers);

            var downloads = sourceRepository.GetResourceAsync <DownloadResource>().ToObservable()
                            .Select(resource => (resource, sourceRepository))
                            .Replay().RefCount();

            return(packageSourceSearchMetadatas
                   .SelectMany(metadata => downloads.FirstAsync(tuple => tuple.sourceRepository.PackageSource.Source == Source)
                               .SelectMany(tuple => tuple.resource.GetDownloadResourceResultAsync(metadata.Identity,
                                                                                                  downloadContext, OutputFolder, NullLogger.Instance, CancellationToken.None)))
                   .Where(result => result.Status == DownloadResourceResultStatus.Available)
                   .HandleErrors(this, Name));
        }
Пример #14
0
        private async Task <DownloadResourceResult> DownloadPackageResourceAsync(
            SourcePackageDependencyInfo package, SourceCacheContext cacheContext, CancellationToken cancellationToken, string globalFolder = "")
        {
            if (string.Equals(globalFolder, ""))
            {
                globalFolder = DefaultNuGetFolders.GetGlobalPackagesFolder();
            }

            using (var progressToken = await _downloadingProgressTrackerService?.TrackDownloadOperationAsync(this, package))
            {
                var downloadResource = await package.Source.GetResourceAsync <DownloadResource>(cancellationToken);

                var packageDownloadContext = new PackageDownloadContext(cacheContext);

                var downloadResult = await downloadResource.GetDownloadResourceResultAsync
                                     (
                    package,
                    new PackageDownloadContext(cacheContext),
                    globalFolder,
                    _nugetLogger,
                    cancellationToken
                                     );

                return(downloadResult);
            }
        }
Пример #15
0
        public async Task <IPackageContent> DownloadAsync(SourceRepository repository, IPackageSearchMetadata package, CancellationToken cancellationToken)
        {
            Ensure.NotNull(repository, "repository");
            Ensure.NotNull(package, "package");

            DownloadResource download = await repository.GetResourceAsync <DownloadResource>();

            if (download == null)
            {
                throw Ensure.Exception.InvalidOperation($"Unnable to resolve '{nameof(DownloadResource)}'.");
            }

            using (var sourceCacheContext = new SourceCacheContext())
            {
                var context = new PackageDownloadContext(sourceCacheContext, Path.GetTempPath(), true);
                var result  = await download.GetDownloadResourceResultAsync(package.Identity, context, String.Empty, nuGetLog, cancellationToken);

                if (result.Status == DownloadResourceResultStatus.Cancelled)
                {
                    throw new OperationCanceledException();
                }
                else if (result.Status == DownloadResourceResultStatus.NotFound)
                {
                    throw Ensure.Exception.InvalidOperation($"Package '{package.Identity.Id}-v{package.Identity.Version}' not found");
                }

                return(new NuGetPackageContent(new PackageArchiveReader(result.PackageStream), log, frameworkFilter));
            }
        }
Пример #16
0
        private async Task RestoreMissingPackagesInSolutionAsync(
            string solutionDirectory,
            IEnumerable <PackageRestoreData> packages,
            ILogger logger,
            CancellationToken token)
        {
            await TaskScheduler.Default;

            using (var cacheContext = new SourceCacheContext())
            {
                var signedPackageVerifier = new PackageSignatureVerifier(SignatureVerificationProviderFactory.GetSignatureVerificationProviders());

                var downloadContext = new PackageDownloadContext(cacheContext)
                {
                    ParentId          = _nuGetProjectContext.OperationId,
                    ExtractionContext = new PackageExtractionContext(
                        PackageSaveMode.Defaultv3,
                        PackageExtractionBehavior.XmlDocFileSaveMode,
                        logger,
                        signedPackageVerifier,
                        SignedPackageVerifierSettings.GetDefault())
                };

                await _packageRestoreManager.RestoreMissingPackagesAsync(
                    solutionDirectory,
                    packages,
                    _nuGetProjectContext,
                    downloadContext,
                    logger,
                    token);
            }
        }
        public async Task GetDownloadResultUtility_WithTwoOfTheSameDownloads_DoesNotCollide()
        {
            // Arrange
            var uri             = new Uri("http://fake/content.nupkg");
            var expectedContent = "TestContent";
            var identity        = new PackageIdentity("PackageA", NuGetVersion.Parse("1.0.0-Beta"));
            var testHttpSource  = new TestHttpSource(
                new PackageSource("http://fake"),
                new Dictionary <string, string>
            {
                { uri.ToString(), expectedContent }
            });
            var logger = new TestLogger();
            var token  = CancellationToken.None;

            using (var cacheContext = new SourceCacheContext())
                using (var downloadDirectory = TestDirectory.Create())
                    using (var globalPackagesFolder = TestDirectory.Create())
                    {
                        var downloadContext = new PackageDownloadContext(
                            cacheContext,
                            downloadDirectory,
                            directDownload: true);

                        // Act
                        using (var resultA = await GetDownloadResultUtility.GetDownloadResultAsync(
                                   testHttpSource,
                                   identity,
                                   uri,
                                   downloadContext,
                                   globalPackagesFolder,
                                   logger,
                                   token))
                            using (var resultB = await GetDownloadResultUtility.GetDownloadResultAsync(
                                       testHttpSource,
                                       identity,
                                       uri,
                                       downloadContext,
                                       globalPackagesFolder,
                                       logger,
                                       token))
                            {
                                // Assert
                                var files = Directory.EnumerateFileSystemEntries(downloadDirectory).ToArray();
                                Assert.Equal(2, files.Length);
                                Assert.EndsWith(".nugetdirectdownload", Path.GetFileName(files[0]));
                                Assert.EndsWith(".nugetdirectdownload", Path.GetFileName(files[1]));

                                var actualContentA = new StreamReader(resultA.PackageStream).ReadToEnd();
                                Assert.Equal(expectedContent, actualContentA);

                                var actualContentB = new StreamReader(resultB.PackageStream).ReadToEnd();
                                Assert.Equal(expectedContent, actualContentB);
                            }

                        Assert.Equal(0, Directory.EnumerateFileSystemEntries(downloadDirectory).Count());
                        Assert.Equal(0, Directory.EnumerateFileSystemEntries(globalPackagesFolder).Count());
                    }
        }
Пример #18
0
        /// <summary>
        /// The static method which takes in all the possible parameters
        /// </summary>
        /// <returns>Returns true if at least one of the packages needed to be restored and got restored</returns>
        /// <remarks>
        /// Best use case is 'nuget.exe restore .sln' where there is no project loaded and there is no SolutionManager.
        /// The references are obtained by parsing of solution file and by using PackagesConfigReader. In this case,
        /// you don't construct an object of PackageRestoreManager,
        /// but just the NuGetPackageManager using constructor that does not need the SolutionManager, and, optionally
        /// register to events and/or specify the source repositories
        /// </remarks>
        public static async Task <PackageRestoreResult> RestoreMissingPackagesAsync(
            PackageRestoreContext packageRestoreContext,
            INuGetProjectContext nuGetProjectContext,
            PackageDownloadContext downloadContext)
        {
            if (packageRestoreContext == null)
            {
                throw new ArgumentNullException(nameof(packageRestoreContext));
            }

            if (nuGetProjectContext == null)
            {
                throw new ArgumentNullException(nameof(nuGetProjectContext));
            }

            ActivityCorrelationId.StartNew();

            var missingPackages = packageRestoreContext.Packages.Where(p => p.IsMissing).ToList();

            if (!missingPackages.Any())
            {
                return(new PackageRestoreResult(true, Enumerable.Empty <PackageIdentity>()));
            }

            // It is possible that the dictionary passed in may not have used the PackageReferenceComparer.
            // So, just to be sure, create a hashset with the keys from the dictionary using the PackageReferenceComparer
            // Now, we are guaranteed to not restore the same package more than once
            var hashSetOfMissingPackageReferences = new HashSet <PackageReference>(missingPackages.Select(p => p.PackageReference), new PackageReferenceComparer());

            nuGetProjectContext.PackageExtractionContext.CopySatelliteFiles = false;

            packageRestoreContext.Token.ThrowIfCancellationRequested();

            foreach (SourceRepository enabledSource in packageRestoreContext.SourceRepositories)
            {
                PackageSource source = enabledSource.PackageSource;
                if (source.IsHttp && !source.IsHttps)
                {
                    packageRestoreContext.Logger.Log(LogLevel.Warning, string.Format(CultureInfo.CurrentCulture, Strings.Warning_HttpServerUsage, "restore", source.Source));
                }
            }

            var attemptedPackages = await ThrottledPackageRestoreAsync(
                hashSetOfMissingPackageReferences,
                packageRestoreContext,
                nuGetProjectContext,
                downloadContext);

            packageRestoreContext.Token.ThrowIfCancellationRequested();

            await ThrottledCopySatelliteFilesAsync(
                hashSetOfMissingPackageReferences,
                packageRestoreContext,
                nuGetProjectContext);

            return(new PackageRestoreResult(
                       attemptedPackages.All(p => p.Restored),
                       attemptedPackages.Select(p => p.Package.PackageIdentity).ToList()));
        }
Пример #19
0
        private static async Task <AttemptedPackage> RestorePackageAsync(
            PackageReference packageReference,
            PackageRestoreContext packageRestoreContext,
            INuGetProjectContext nuGetProjectContext,
            PackageDownloadContext downloadContext)
        {
            Exception exception = null;
            var       restored  = false;

            try
            {
                restored = await packageRestoreContext.PackageManager.RestorePackageAsync(
                    packageReference.PackageIdentity,
                    nuGetProjectContext,
                    downloadContext,
                    packageRestoreContext.SourceRepositories,
                    packageRestoreContext.Token);
            }
            catch (Exception ex)
            {
                exception = ex;
            }

            packageRestoreContext.PackageRestoredEvent?.Invoke(null, new PackageRestoredEventArgs(packageReference.PackageIdentity, restored));

            // PackageReferences cannot be null here
            if (exception != null)
            {
                if (!string.IsNullOrEmpty(exception.Message))
                {
                    nuGetProjectContext.Log(MessageLevel.Warning, exception.Message);
                }

                if (packageRestoreContext.PackageRestoreFailedEvent != null)
                {
                    var packageReferenceComparer = new PackageReferenceComparer();

                    var packageRestoreData = packageRestoreContext.Packages
                                             .Where(p => packageReferenceComparer.Equals(p.PackageReference, packageReference))
                                             .SingleOrDefault();

                    if (packageRestoreData != null)
                    {
                        Debug.Assert(packageRestoreData.ProjectNames != null);
                        packageRestoreContext.PackageRestoreFailedEvent(
                            null,
                            new PackageRestoreFailedEventArgs(packageReference,
                                                              exception,
                                                              packageRestoreData.ProjectNames));
                    }
                }
            }

            return(new AttemptedPackage
            {
                Restored = restored,
                Package = packageReference
            });
        }
Пример #20
0
        private async Task PerformV2Restore(string packagesConfigFilePath, string installPath)
        {
            var sourceRepositoryProvider = GetSourceRepositoryProvider();
            var nuGetPackageManager      = new NuGetPackageManager(sourceRepositoryProvider, Settings, installPath, ExcludeVersion);

            var installedPackageReferences = GetInstalledPackageReferences(
                packagesConfigFilePath,
                allowDuplicatePackageIds: true);

            var packageRestoreData = installedPackageReferences.Select(reference =>
                                                                       new PackageRestoreData(
                                                                           reference,
                                                                           new[] { packagesConfigFilePath },
                                                                           isMissing: true));

            var packageSources = GetPackageSources(Settings);

            Console.PrintPackageSources(packageSources);

            var packageRestoreContext = new PackageRestoreContext(
                nuGetPackageManager,
                packageRestoreData,
                CancellationToken.None,
                packageRestoredEvent: null,
                packageRestoreFailedEvent: null,
                sourceRepositories: packageSources.Select(sourceRepositoryProvider.CreateRepository),
                maxNumberOfParallelTasks: DisableParallelProcessing ? 1 : PackageManagementConstants.DefaultMaxDegreeOfParallelism);

            var missingPackageReferences = installedPackageReferences.Where(reference =>
                                                                            !nuGetPackageManager.PackageExistsInPackagesFolder(reference.PackageIdentity)).Any();

            if (!missingPackageReferences)
            {
                var message = string.Format(
                    CultureInfo.CurrentCulture,
                    LocalizedResourceManager.GetString("InstallCommandNothingToInstall"),
                    packagesConfigFilePath);

                Console.LogMinimal(message);
            }
            using (var cacheContext = new SourceCacheContext())
            {
                cacheContext.NoCache        = NoCache;
                cacheContext.DirectDownload = DirectDownload;

                var downloadContext = new PackageDownloadContext(cacheContext, installPath, DirectDownload);

                await PackageRestoreManager.RestoreMissingPackagesAsync(
                    packageRestoreContext,
                    new ConsoleProjectContext(Console),
                    downloadContext);

                if (downloadContext.DirectDownload)
                {
                    GetDownloadResultUtility.CleanUpDirectDownloads(downloadContext);
                }
            }
        }
Пример #21
0
        /// <summary>
        /// The static method which takes in all the possible parameters
        /// </summary>
        /// <returns>Returns true if at least one of the packages needed to be restored and got restored</returns>
        /// <remarks>
        /// Best use case is 'nuget.exe restore .sln' where there is no project loaded and there is no SolutionManager.
        /// The references are obtained by parsing of solution file and by using PackagesConfigReader. In this case,
        /// you don't construct an object of PackageRestoreManager,
        /// but just the NuGetPackageManager using constructor that does not need the SolutionManager, and, optionally
        /// register to events and/or specify the source repositories
        /// </remarks>
        public static async Task <PackageRestoreResult> RestoreMissingPackagesAsync(
            PackageRestoreContext packageRestoreContext,
            INuGetProjectContext nuGetProjectContext,
            PackageDownloadContext downloadContext)
        {
            if (packageRestoreContext == null)
            {
                throw new ArgumentNullException(nameof(packageRestoreContext));
            }

            if (nuGetProjectContext == null)
            {
                throw new ArgumentNullException(nameof(nuGetProjectContext));
            }

            ActivityCorrelationId.StartNew();

            var missingPackages = packageRestoreContext.Packages.Where(p => p.IsMissing).ToList();

            if (!missingPackages.Any())
            {
                return(new PackageRestoreResult(true, Enumerable.Empty <PackageIdentity>()));
            }

            // It is possible that the dictionary passed in may not have used the PackageReferenceComparer.
            // So, just to be sure, create a hashset with the keys from the dictionary using the PackageReferenceComparer
            // Now, we are guaranteed to not restore the same package more than once
            var hashSetOfMissingPackageReferences = new HashSet <PackageReference>(missingPackages.Select(p => p.PackageReference), new PackageReferenceComparer());

            // Before starting to restore package, set the nuGetProjectContext such that satellite files are not copied yet
            // Satellite files will be copied as a post operation. This helps restore packages in parallel
            // and not have to determine if the package is a satellite package beforehand
            if (nuGetProjectContext.PackageExtractionContext == null)
            {
                nuGetProjectContext.PackageExtractionContext = new PackageExtractionContext(new LoggerAdapter(nuGetProjectContext));
            }

            nuGetProjectContext.PackageExtractionContext.CopySatelliteFiles = false;

            packageRestoreContext.Token.ThrowIfCancellationRequested();

            var attemptedPackages = await ThrottledPackageRestoreAsync(
                hashSetOfMissingPackageReferences,
                packageRestoreContext,
                nuGetProjectContext,
                downloadContext);

            packageRestoreContext.Token.ThrowIfCancellationRequested();

            await ThrottledCopySatelliteFilesAsync(
                hashSetOfMissingPackageReferences,
                packageRestoreContext,
                nuGetProjectContext);

            return(new PackageRestoreResult(
                       attemptedPackages.All(p => p.Restored),
                       attemptedPackages.Select(p => p.Package.PackageIdentity).ToList()));
        }
Пример #22
0
        /// <summary>
        /// Asynchronously downloads a package.
        /// </summary>
        /// <param name="identity">The package identity.</param>
        /// <param name="downloadContext">A package download context.</param>
        /// <param name="globalPackagesFolder">The path to the global packages folder.</param>
        /// <param name="logger">A logger.</param>
        /// <param name="cancellationToken">A cancellation token.</param>
        /// <returns>A task that represents the asynchronous operation.
        /// The task result (<see cref="Task{TResult}.Result" />) returns
        /// a <see cref="DownloadResourceResult" />.</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="identity" /> is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="downloadContext" />
        /// is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="logger" /> is <c>null</c>.</exception>
        /// <exception cref="OperationCanceledException">Thrown if <paramref name="cancellationToken" />
        /// is cancelled.</exception>
        public async override Task <DownloadResourceResult> GetDownloadResourceResultAsync(
            PackageIdentity identity,
            PackageDownloadContext downloadContext,
            string globalPackagesFolder,
            ILogger logger,
            CancellationToken cancellationToken)
        {
            if (identity == null)
            {
                throw new ArgumentNullException(nameof(identity));
            }

            if (downloadContext == null)
            {
                throw new ArgumentNullException(nameof(downloadContext));
            }

            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            cancellationToken.ThrowIfCancellationRequested();

            AddOrUpdateLogger(_plugin, logger);

            await _utilities.DoOncePerPluginLifetimeAsync(
                MessageMethod.SetLogLevel.ToString(),
                () => SetLogLevelAsync(logger, cancellationToken),
                cancellationToken);

            var response = await _plugin.Connection.SendRequestAndReceiveResponseAsync <PrefetchPackageRequest, PrefetchPackageResponse>(
                MessageMethod.PrefetchPackage,
                new PrefetchPackageRequest(_packageSource.Source, identity.Id, identity.Version.ToNormalizedString()),
                cancellationToken);

            if (response != null)
            {
                if (response.ResponseCode == MessageResponseCode.Success)
                {
                    var packageReader = new PluginPackageReader(_plugin, identity, _packageSource.Source);

                    return(new DownloadResourceResult(packageReader, _packageSource.Source));
                }

                if (response.ResponseCode == MessageResponseCode.NotFound)
                {
                    return(new DownloadResourceResult(DownloadResourceResultStatus.NotFound));
                }
            }

            throw new PluginException(
                      string.Format(CultureInfo.CurrentCulture,
                                    Strings.Plugin_PackageDownloadFailed,
                                    _plugin.Name,
                                    $"{identity.Id}.{identity.Version.ToNormalizedString()}"));
        }
 internal PackageDownloaderTest()
 {
     _sourceCacheContext = new SourceCacheContext();
     Context             = new PackageDownloadContext(_sourceCacheContext);
     PackageIdentity     = new PackageIdentity(id: "a", version: NuGetVersion.Parse("1.0.0"));
     SourceRepository    = new SourceRepository(
         new PackageSource("https://unit.test"),
         Enumerable.Empty <INuGetResourceProvider>());
 }
Пример #24
0
        public IReadOnlyCollection <IFile> Install(PackageReference package, PackageType type, DirectoryPath path)
        {
            if (package == null)
            {
                throw new ArgumentNullException(nameof(package));
            }
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            var packageRoot              = path.MakeAbsolute(_environment).FullPath;
            var targetFramework          = type == PackageType.Addin ? _currentFramework : NuGetFramework.AnyFramework;
            var sourceRepositoryProvider = new NuGetSourceRepositoryProvider(_nugetSettings, _config, package);
            var packageIdentity          = GetPackageId(package, sourceRepositoryProvider, targetFramework, _nugetLogger);

            if (packageIdentity == null)
            {
                return(Array.Empty <IFile>());
            }

            var pathResolver   = new PackagePathResolver(packageRoot);
            var project        = new NugetFolderProject(_fileSystem, _contentResolver, _config, _log, pathResolver, packageRoot, targetFramework);
            var packageManager = new NuGetPackageManager(sourceRepositoryProvider, _nugetSettings, project.Root)
            {
                PackagesFolderNuGetProject = project
            };
            var includePrerelease  = package.IsPrerelease();
            var dependencyBehavior = GetDependencyBehavior(type, package);
            var resolutionContext  = new ResolutionContext(dependencyBehavior, includePrerelease, false, VersionConstraints.None, _gatherCache);
            var projectContext     = new NuGetProjectContext(_log);

            using (var sourceCacheContext = new SourceCacheContext())
            {
                var downloadContext = new PackageDownloadContext(sourceCacheContext);

                // First get the install actions.
                // This will give us the list of packages to install, and which feed should be used.
                var actions = packageManager.GetInstallProjectActionsAsync(
                    project,
                    packageIdentity,
                    resolutionContext,
                    projectContext,
                    sourceRepositoryProvider.GetRepositories(),
                    CancellationToken.None).Result;

                // Then install the packages.
                packageManager.ExecuteNuGetProjectActionsAsync(
                    project,
                    actions,
                    projectContext,
                    downloadContext,
                    CancellationToken.None).Wait();
            }

            return(project.GetFiles(path, package, type));
        }
Пример #25
0
        public override Task <DownloadResourceResult> GetDownloadResourceResultAsync(
            PackageIdentity identity,
            PackageDownloadContext downloadContext,
            string globalPackagesFolder,
            ILogger logger,
            CancellationToken token)
        {
            if (identity == null)
            {
                throw new ArgumentNullException(nameof(identity));
            }

            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            var stopwatch = Stopwatch.StartNew();

            try
            {
                // Find the package from the local folder
                LocalPackageInfo packageInfo = null;

                var sourcePackage = identity as SourcePackageDependencyInfo;

                if (sourcePackage?.DownloadUri != null)
                {
                    // Get the package directly if the full path is known
                    packageInfo = _localResource.GetPackage(sourcePackage.DownloadUri, logger, token);
                }
                else
                {
                    // Search for the local package
                    packageInfo = _localResource.GetPackage(identity, logger, token);
                }

                if (packageInfo != null)
                {
                    var stream = File.OpenRead(packageInfo.Path);
                    return(Task.FromResult(new DownloadResourceResult(stream, packageInfo.GetReader(), _localResource.Root)));
                }
                else
                {
                    return(Task.FromResult(new DownloadResourceResult(DownloadResourceResultStatus.NotFound)));
                }
            }
            finally
            {
                ProtocolDiagnostics.RaiseEvent(new ProtocolDiagnosticResourceEvent(
                                                   _source,
                                                   resourceType: nameof(DownloadResource),
                                                   type: nameof(LocalDownloadResource),
                                                   method: nameof(GetDownloadResourceResultAsync),
                                                   stopwatch.Elapsed));
            }
        }
        public async Task GetDownloadResultUtility_WithSingleDirectDownload_ReturnsTemporaryDownloadResult()
        {
            // Arrange
            var uri             = new Uri("http://fake/content.nupkg");
            var expectedContent = "TestContent";
            var identity        = new PackageIdentity("PackageA", NuGetVersion.Parse("1.0.0-Beta"));
            var testHttpSource  = new TestHttpSource(
                new PackageSource("http://fake"),
                new Dictionary <string, string>
            {
                { uri.ToString(), expectedContent }
            });
            var logger = new TestLogger();
            var token  = CancellationToken.None;

            using (var cacheContext = new SourceCacheContext())
                using (var downloadDirectory = TestDirectory.Create())
                    using (var globalPackagesFolder = TestDirectory.Create())
                    {
                        var downloadContext = new PackageDownloadContext(
                            cacheContext,
                            downloadDirectory,
                            directDownload: true);

                        // Act
                        using (var result = await GetDownloadResultUtility.GetDownloadResultAsync(
                                   testHttpSource,
                                   identity,
                                   uri,
                                   downloadContext,
                                   globalPackagesFolder,
                                   logger,
                                   token))
                        {
                            // Assert
                            Assert.Null(result.PackageReader);
                            Assert.Equal(DownloadResourceResultStatus.Available, result.Status);
                            Assert.NotNull(result.PackageStream);
                            Assert.True(result.PackageStream.CanSeek);
                            Assert.True(result.PackageStream.CanRead);
                            Assert.Equal(0, result.PackageStream.Position);

                            var files = Directory.EnumerateFileSystemEntries(downloadDirectory).ToArray();
                            Assert.Equal(1, files.Length);
                            Assert.EndsWith(".nugetdirectdownload", Path.GetFileName(files[0]));

                            Assert.Equal(0, Directory.EnumerateFileSystemEntries(globalPackagesFolder).Count());

                            var actualContent = new StreamReader(result.PackageStream).ReadToEnd();
                            Assert.Equal(expectedContent, actualContent);
                        }

                        Assert.Equal(0, Directory.EnumerateFileSystemEntries(downloadDirectory).Count());
                        Assert.Equal(0, Directory.EnumerateFileSystemEntries(globalPackagesFolder).Count());
                    }
        }
        protected async Task CheckMissingPackagesAsync()
        {
            var solutionDirectory = VsSolutionManager.SolutionDirectory;

            var packages = await PackageRestoreManager.GetPackagesInSolutionAsync(solutionDirectory, CancellationToken.None);

            if (packages.Any(p => p.IsMissing))
            {
                var packageRestoreConsent = new PackageRestoreConsent(ConfigSettings);
                if (packageRestoreConsent.IsGranted)
                {
                    await TaskScheduler.Default;

                    using (var cacheContext = new SourceCacheContext())
                    {
                        var logger = new LoggerAdapter(this);

                        var signedPackageVerifier = new PackageSignatureVerifier(SignatureVerificationProviderFactory.GetSignatureVerificationProviders());

                        var downloadContext = new PackageDownloadContext(cacheContext)
                        {
                            ParentId          = OperationId,
                            ExtractionContext = new PackageExtractionContext(
                                PackageSaveMode.Defaultv3,
                                PackageExtractionBehavior.XmlDocFileSaveMode,
                                logger,
                                signedPackageVerifier,
                                SignedPackageVerifierSettings.GetClientPolicy(ConfigSettings, logger))
                        };

                        var result = await PackageRestoreManager.RestoreMissingPackagesAsync(
                            solutionDirectory,
                            packages,
                            this,
                            downloadContext,
                            logger,
                            Token);

                        if (result.Restored)
                        {
                            await PackageRestoreManager.RaisePackagesMissingEventForSolutionAsync(solutionDirectory, CancellationToken.None);

                            return;
                        }
                    }
                }

                ErrorHandler.HandleException(
                    new InvalidOperationException(Resources.Cmdlet_MissingPackages),
                    terminating: true,
                    errorId: NuGetErrorId.MissingPackages,
                    category: ErrorCategory.InvalidOperation);
            }
        }
Пример #28
0
        internal Downloader(string downloadDirectory, ImmutableList <SourceRepository> sources)
        {
            this.Installed     = new ConcurrentDictionary <PackageIdentity, bool>();
            this.DownloadQueue = new ConcurrentQueue <PackageIdentity>();

            this.DownloadCache = new PackageDownloadContext(NullSourceCacheContext.Instance);

            this.DownloadDirectory = downloadDirectory;

            this.Sources = sources;
        }
Пример #29
0
        public async Task NuGetReader_DownloadPackageAsync()
        {
            // Arrange
            using (var packagesFolder = new TestFolder())
                using (var globalFolder = new TestFolder())
                    using (var target = new TestFolder())
                        using (var cache = new LocalCache())
                        {
                            var outputRoot = Path.Combine(target.Root, "output");
                            var baseUri    = UriUtility.CreateUri("https://localhost:8080/testFeed/");

                            var log         = new TestLogger();
                            var testPackage = new TestNupkg("packageA", "1.0.0");

                            var sleetConfig = TestUtility.CreateConfigWithLocal("local", outputRoot, baseUri.AbsoluteUri);

                            var sleetConfigPath = Path.Combine(target.Root, "sleet.config");
                            await JsonUtility.SaveJsonAsync(new FileInfo(sleetConfigPath), sleetConfig);

                            var zipFile = testPackage.Save(packagesFolder.Root);

                            var settings   = LocalSettings.Load(sleetConfigPath);
                            var fileSystem = await FileSystemFactory.CreateFileSystemAsync(settings, cache, "local");

                            var success = await InitCommand.RunAsync(settings, fileSystem, log);

                            // Act
                            // Run sleet
                            success &= await PushCommand.RunAsync(settings, fileSystem, new List <string>() { packagesFolder.Root }, false, false, log);

                            // Create a repository abstraction for nuget
                            var nugetFileSystem = new PhysicalFileSystem(cache, UriUtility.CreateUri(outputRoot), baseUri);
                            var localSource     = GetSource(outputRoot, baseUri, nugetFileSystem);

                            var resource = await localSource.GetResourceAsync <DownloadResource>();

                            var cacheContext = new SourceCacheContext()
                            {
                                NoCache = true
                            };

                            var downloadContext = new PackageDownloadContext(cacheContext, globalFolder, directDownload: false);

                            var result = await resource.GetDownloadResourceResultAsync(new PackageIdentity("packageA", NuGetVersion.Parse("1.0.0")), downloadContext, globalFolder, log, CancellationToken.None);

                            // Assert
                            Assert.True(success, log.ToString());

                            Assert.Equal(DownloadResourceResultStatus.Available, result.Status);
                            Assert.True(result.PackageStream.Length > 0);
                            Assert.Equal(new PackageIdentity("packageA", NuGetVersion.Parse("1.0.0")), result.PackageReader.GetIdentity());
                        }
        }
Пример #30
0
        public async Task <Dictionary <PackageIdentity, PackagePreFetcherResult> > DownloadPackages(
            PackagesLock packagesLock, PackageDownloadContext downloadContext, ILogger logger, CancellationToken token)
        {
            var packages   = packagesLock.Keys.Concat(packagesLock.SelectMany(x => x.Value)).Distinct();
            var toDownload = new Queue <PackageIdentity>();
            var result     = new Dictionary <PackageIdentity, PackagePreFetcherResult>();

            foreach (var package in packages)
            {
                if (_modulesDirectory.ModuleExists(package))
                {
                    continue;
                }

                toDownload.Enqueue(package);
            }

            if (toDownload.Any())
            {
                var maxParallelTasks = PackageManagementConstants.DefaultMaxDegreeOfParallelism;

                var downloadResults = new List <PackagePreFetcherResult>(maxParallelTasks);

                while (toDownload.Count > 0)
                {
                    // Throttle tasks
                    if (downloadResults.Count == maxParallelTasks)
                    {
                        // Wait for a task to complete
                        // This will not throw, exceptions are stored in the result
                        await Task.WhenAny(downloadResults.Select(e => e.EnsureResultAsync()));

                        // Remove all completed tasks
                        downloadResults.RemoveAll(e => e.IsComplete);
                    }

                    var package = toDownload.Dequeue();

                    // Download the package if it does not exist in the packages folder already
                    // Start the download task
                    var task = Task.Run(
                        async() => await PackageDownloader.GetDownloadResourceResultAsync(_sourceRepository, package,
                                                                                          downloadContext, "NO_GLOBAL_CACHE_DIRECTORY", logger, token), token);

                    var downloadResult = new PackagePreFetcherResult(task, package, _sourceRepository.PackageSource);

                    downloadResults.Add(downloadResult);
                    result.Add(package, downloadResult);
                }
            }

            return(result);
        }