예제 #1
0
        public async Task Load(PackagesLock packagesLock, IServerInfo serverInfo, CancellationToken token)
        {
            var serverRepo = new ServerRepository(new Uri(serverInfo.ServerUri, "nuget"));

            var context = new PackageDownloadContext(new SourceCacheContext {
                DirectDownload = true, NoCache = true
            },
                                                     Environment.ExpandEnvironmentVariables(_options.TempPath), true);

            var packageDownloadManager = new PackageDownloadManager(_modulesDirectory, serverRepo);
            var result =
                await packageDownloadManager.DownloadPackages(packagesLock, context, NullLogger.Instance, token);

            if (result.Any())
            {
                try
                {
                    foreach (var preFetchResult in result.Values)
                    {
                        using (var downloadPackageResult = await preFetchResult.GetResultAsync())
                        {
                            // use the version exactly as specified in the nuspec file
                            var packageIdentity = await downloadPackageResult.PackageReader.GetIdentityAsync(token);

                            var packageExtractionContext = new PackageExtractionContext(
                                PackageSaveMode.Defaultv3,
                                PackageExtractionBehavior.XmlDocFileSaveMode,
                                ClientPolicyContext.GetClientPolicy(new NullSettings(), new NullLogger()),
                                new NullLogger());

                            downloadPackageResult.PackageStream.Position = 0;

                            await PackageExtractor.InstallFromSourceAsync(serverRepo.PackageSource.Source,
                                                                          packageIdentity, stream => downloadPackageResult.PackageStream.CopyToAsync(stream),
                                                                          _modulesDirectory.VersionFolderPathResolver, packageExtractionContext, token);
                        }
                    }
                }
                finally
                {
                    foreach (var fetcherResult in result.Values)
                    {
                        await fetcherResult.EnsureResultAsync();

                        fetcherResult.Dispose();
                    }
                }
            }
        }
예제 #2
0
        public async Task <Dictionary <PackageIdentity, FileInfoBase> > LoadPackages(PackagesLock packagesLock, DirectoryInfoBase tempDirectory,
                                                                                     CancellationToken cancellationToken)
        {
            var result             = new Dictionary <PackageIdentity, FileInfoBase>();
            var packagesToDownload = new List <PackageIdentity>();

            foreach (var packageId in packagesLock.Keys)
            {
                var filePath = _versionFolderPathResolver.GetPackageFilePath(packageId.Id, packageId.Version);
                var file     = _fileSystem.FileInfo.FromFileName(filePath);
                if (file.Exists)
                {
                    _logger.LogDebug("Package {packageId} could be resolved locally at {path}", packageId, file.FullName);
                    result.Add(packageId, file);
                }
                else
                {
                    _logger.LogDebug("Package {packageId} could not be resolved locally", packageId);
                    packagesToDownload.Add(packageId);
                }

                cancellationToken.ThrowIfCancellationRequested();
            }

            if (packagesToDownload.Any())
            {
                _logger.LogInformation("{0} package(s) were not found locally and will be downloaded.", packagesToDownload.Count);

                var nugetFolder            = tempDirectory.CreateSubdirectory("nuget");
                var tempFolderPathResolver = new VersionFolderPathResolverFlat(nugetFolder.FullName);

                var serverRepo = new ServerRepository(new Uri(_restClient.Server.ServerUri, "nuget"));
                var downloader = new PackageDownloadManager(new ModulesDirectory(tempFolderPathResolver), serverRepo);

                var results = await downloader.DownloadPackages(
                    new PackagesLock(packagesToDownload.ToDictionary(x => x,
                                                                     _ => (IImmutableList <PackageIdentity>)ImmutableList <PackageIdentity> .Empty)),
                    new PackageDownloadContext(new SourceCacheContext {
                    DirectDownload = true, NoCache = true
                }, nugetFolder.FullName, true),
                    new NuGetLoggerWrapper(_logger), cancellationToken);

                try
                {
                    foreach (var preFetchResult in results.Values)
                    {
                        using (var downloadPackageResult = await preFetchResult.GetResultAsync())
                        {
                            var packageIdentity = await downloadPackageResult.PackageReader.GetIdentityAsync(cancellationToken);

                            _logger.LogInformation("Package {0} was successfully downloaded.", packageIdentity);

                            var fileStream = (FileStream)downloadPackageResult.PackageStream;
                            var file       = _fileSystem.FileInfo.FromFileName(fileStream.Name);
                            if (!file.Exists)
                            {
                                _logger.LogError("Package {0} was not found at {1}. The download may have failed.", packageIdentity, file.FullName);
                                throw new FileNotFoundException($"The file {file.FullName} does not exist.");
                            }

                            var packagePath = _fileSystem.Path.Combine(_fileSystem.Path.GetDirectoryName(file.FullName), packageIdentity + ".nupkg");
                            using (var stream = _fileSystem.FileStream.Create(packagePath, FileMode.CreateNew))
                                await fileStream.CopyToAsync(stream);

                            result.Add(packageIdentity, _fileSystem.FileInfo.FromFileName(packagePath));
                        }
                    }
                }
                finally
                {
                    foreach (var fetcherResult in results.Values)
                    {
                        await fetcherResult.EnsureResultAsync();

                        fetcherResult.Dispose();
                    }
                }
            }

            return(result);
        }