示例#1
0
        private void CopyNupkgFilesToTarget(PackageArchiveReader nupkg, string destPath, IEnumerable <string> files)
        {
            var packageFileExtractor = new PackageFileExtractor(files,
                                                                PackageExtractionBehavior.XmlDocFileSaveMode);

            nupkg.CopyFiles(destPath, files, packageFileExtractor.ExtractPackageFile, new TestCommandOutputLogger(),
                            CancellationToken.None);
        }
示例#2
0
        void ProcessPackageFramework(PackageArchiveReader package, NuGetFramework framework)
        {
            if (!consolidations.TryGetValue(framework, out var consolidation))
            {
                consolidation = new Consolidation(framework);
                consolidations.Add(framework, consolidation);
            }

            foreach (var assemblyName in GetItems(package.GetFrameworkItems(), framework))
            {
                consolidation.FrameworkAssemblyReferences.Add(new FrameworkAssemblyReference(
                                                                  assemblyName,
                                                                  new [] { framework }));
            }

            package.CopyFiles(
                workPath,
                GetItems(package.GetLibItems(), framework),
                (sourceFile, targetPath, fileStream) => {
                var targetDirectory = Path.GetDirectoryName(targetPath);
                if (!consolidation.AssemblySearchPaths.Contains(targetDirectory))
                {
                    consolidation.AssemblySearchPaths.Add(targetDirectory);
                }

                Directory.CreateDirectory(targetDirectory);

                using (var targetStream = File.Open(
                           targetPath,
                           FileMode.Create,
                           FileAccess.Write))
                    fileStream.CopyTo(targetStream);

                consolidation.Assemblies.Add(targetPath);

                return(targetPath);
            },
                null,
                default);

            consolidation.AssemblySearchPaths.AddRange(package
                                                       .GetPackageDependencies()
                                                       .Where(d => d.TargetFramework == framework)
                                                       .SelectMany(d => d.Packages)
                                                       .Select(d => NuGetLocalRepoHelper.GetPackageAssemblySearchPath(
                                                                   d.Id,
                                                                   d.VersionRange,
                                                                   framework))
                                                       .Where(d => d != null));
        }
示例#3
0
        /// <summary>
        /// Extracts the content files of the NuGet package to a specified directory.
        /// </summary>
        /// <param name="stream">Stream containing the NuGet package.</param>
        /// <param name="targetDirectory">The directory to extract the package content to.</param>
        /// <param name="token">A cancellation token to allow cancellation of the task.</param>
        public static void ExtractPackage(Stream stream, string targetDirectory, CancellationToken token = default)
        {
            PackageReaderBase packageReader = new PackageArchiveReader(stream);
            var items         = packageReader.GetFiles();
            var tempDirectory = $"{Path.GetTempPath()}\\{packageReader.NuspecReader.GetId()}";

            // Remove all items ending with a front or backslash (directories)
            items = items.Where(x => !(x.EndsWith("\\") || x.EndsWith("/")));

            if (Directory.Exists(tempDirectory))
            {
                Directory.Delete(tempDirectory, true);
            }

            packageReader.CopyFiles(tempDirectory, items, ExtractFile, new NullLogger(), token);

            var fullTargetDirectory = Path.GetFullPath(targetDirectory);

            IOEx.MoveDirectory(tempDirectory, fullTargetDirectory);
        }
        public void Extract(Stream nupkgStream, string targetPath)
        {
            var tempHashPath = Path.Combine(targetPath, Path.GetRandomFileName());
            var targetNuspec = Path.Combine(targetPath, Path.GetRandomFileName());
            var targetNupkg  = Path.Combine(targetPath, Path.GetRandomFileName());

            targetNupkg = Path.ChangeExtension(targetNupkg, ".nupkg");
            var hashPath = Path.Combine(targetPath, Path.GetRandomFileName());

            using (var packageReader = new PackageArchiveReader(nupkgStream))
            {
                var nuspecFile = packageReader.GetNuspecFile();
                if ((packageSaveMode & PackageSaveMode.Nuspec) == PackageSaveMode.Nuspec)
                {
                    packageReader.ExtractFile(nuspecFile, targetNuspec);
                }

                if ((packageSaveMode & PackageSaveMode.Files) == PackageSaveMode.Files)
                {
                    var nupkgFileName = Path.GetFileName(targetNupkg);
                    var hashFileName  = Path.GetFileName(hashPath);
                    var packageFiles  = packageReader.GetFiles()
                                        .Where(file => ShouldInclude(file, nupkgFileName, nuspecFile, hashFileName));
                    var packageFileExtractor = new PackageFileExtractor(
                        packageFiles,
                        XmlDocFileSaveMode.None);
                    packageReader.CopyFiles(
                        targetPath,
                        packageFiles,
                        packageFileExtractor.ExtractPackageFile,
                        CancellationToken.None);
                }

                string packageHash;
                nupkgStream.Position = 0;
                packageHash          = Convert.ToBase64String(new CryptoHashProvider("SHA512").CalculateHash(nupkgStream));

                File.WriteAllText(tempHashPath, packageHash);
            }
        }