コード例 #1
0
        private async Task Decompress(InternalPackageInformation info, ILocalBaseFolder localBaseFolder)
        {
            using (var stream =
                       localBaseFolder.OpenStreamForReading(PathForNugetPackage(info)))
                using (var compressed = new ZipArchive(stream, ZipArchiveMode.Read))
                {
                    var totalCount = compressed.Entries.Count;

                    foreach (var(zipArchiveEntry, i) in compressed.Entries.Select((e, i) => (e, i)))
                    {
                        var fullName = Uri.UnescapeDataString(zipArchiveEntry.FullName);
                        var name     = Uri.UnescapeDataString(zipArchiveEntry.Name);
                        listeners(new Decompressing(info.Package, info.Source, fullName, i, totalCount));

                        if (fullName.StartsWith("_rels") ||
                            name == "[Content_Types].xml")
                        {
                            continue;
                        }
                        // we skip over directory entries for now, assume we don't extract empty dirs
                        if ((zipArchiveEntry.ExternalAttributes & FolderBitmaskForZipArchiveEntry) == FolderBitmaskForZipArchiveEntry)
                        {
                            continue;
                        }

                        using (var fileStream = zipArchiveEntry.Open())
                            using (var outputStream =
                                       localBaseFolder.OpenStreamForWriting(Path.Combine(info.Package.OutputFolder, fullName)))
                            {
                                await fileStream.CopyToAsync(outputStream);
                            }
                    }
                }
        }
コード例 #2
0
        private async Task DownloadPackage(ILocalBaseFolder localBaseFolder,
                                           string sourceName,
                                           CancellationToken cancelationToken, InternalPackageInformation info)
        {
            var descriptionPath = $@"{info.Package.OutputFolder}\vendor.dependency.description.json";
            VendorDependencyDescription description;

            if (!_forceRefresh && localBaseFolder.ContainsFolder(info.Package.OutputFolder))
            {
                var content = await localBaseFolder.FileContentOrEmptyAsync(
                    descriptionPath,
                    cancelationToken);

                if (!string.IsNullOrWhiteSpace(content))
                {
                    try
                    {
                        description = JsonConvert.DeserializeObject <VendorDependencyDescription>(content);
                    }
                    catch (Exception)
                    {
                        // TODO : log this error
                        description = new VendorDependencyDescription {
                            Version = ""
                        };
                    }

                    if (info.Identity.Version.ToFullString() == description.Version)
                    {
                        listeners(new AlreadyUpToDate(info.Package));
                        return;
                    }
                }
            }

            await Download(sourceName, info, localBaseFolder, cancelationToken);

            using (var descriptionStream = localBaseFolder.OpenStreamForWriting(descriptionPath))
            {
                description = new VendorDependencyDescription
                {
                    Version = info.Identity.Version.ToFullString()
                };
                var serialized = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(description, Formatting.Indented));

                await descriptionStream.WriteAsync(serialized, 0, serialized.Length, cancelationToken);
            }
        }
コード例 #3
0
        private async Task Download(string sourceName, InternalPackageInformation info,
                                    ILocalBaseFolder localBaseFolder,
                                    CancellationToken cancelationToken)
        {
            listeners(new Resolving(info.Package, info.Source));
            var sourceRepository = _sourceRepositories[sourceName];

            // This makes it work with coreclr
            _sourceCacheContext.DirectDownload     = true;
            _sourceCacheContext.RefreshMemoryCache = true;

            var remoteV3FindPackageByIdResource =
                await sourceRepository.GetResourceAsync <FindPackageByIdResource>(cancelationToken);

            await remoteV3FindPackageByIdResource.GetAllVersionsAsync(info.Identity.Id, _sourceCacheContext,
                                                                      NullLogger.Instance, cancelationToken);

            using (var stream =
                       localBaseFolder.OpenStreamForWriting(PathForNugetPackage(info)))
            {
                listeners(new Downloading(info.Package, info.Source));
                var result = await remoteV3FindPackageByIdResource.CopyNupkgToStreamAsync(info.Identity.Id, info.Identity.Version,
                                                                                          stream,
                                                                                          _sourceCacheContext,
                                                                                          NullLogger.Instance, cancelationToken
                                                                                          );

                if (result != true)
                {
                    throw new InvalidOperationException("Whoa, result is null");
                }
                listeners(new Downloaded(info.Package, info.Source));
            }

            if (info.Package.CleanOnUpdate)
            {
                listeners(new Cleaning(info.Package, info.Package.OutputFolder));

                localBaseFolder.Clean(info.Package.OutputFolder);
            }
            await Decompress(info, localBaseFolder);

            listeners(new Done(info.Package));
        }
コード例 #4
0
 private static string PathForNugetPackage(InternalPackageInformation info)
 {
     return($"{Path.Combine(info.Package.OutputFolder, info.Identity.ToString())}.nupkg");
 }