public LocalPackageInfo AddPackage(LocalPackageInfo package) { var ct = CancellationToken.None; var destPackageDir = _pathResolver.GetPackageDirectory(package.Identity.Id, package.Identity.Version); var destPackagePath = _pathResolver.GetPackageFilePath(package.Identity.Id, package.Identity.Version); _fileSystem.MakeDirectoryForFile(destPackagePath); using (var downloader = new LocalPackageArchiveDownloader(package.Path, package.Identity, _logAdapter)) { downloader.CopyNupkgFileToAsync(destPackagePath, ct).Wait(); var hashFilePath = Path.ChangeExtension(destPackagePath, PackagingCoreConstants.HashFileExtension); var hash = downloader.GetPackageHashAsync("SHA512", ct).Result; var hashBytes = Encoding.UTF8.GetBytes(hash); _fileSystem.AddFile(hashFilePath, hashFileStream => { hashFileStream.Write(hashBytes, 0, hashBytes.Length); }); var nuspecPath = _pathResolver.GetManifestFilePath(package.Identity.Id, package.Identity.Version); using (var nuspecStream = File.OpenWrite(nuspecPath)) { using (var fs = downloader.CoreReader.GetNuspecAsync(ct).Result) { fs.CopyTo(nuspecStream); } } _log.DebugFormat("Saved manifest {0}", nuspecPath); Lazy <NuspecReader> nuspecReader = new Lazy <NuspecReader>(() => new NuspecReader(nuspecPath)); var packageReader = new Func <PackageReaderBase>(() => new PackageArchiveReader(File.OpenRead(destPackagePath))); return(new LocalPackageInfo(package.Identity, destPackagePath, package.LastWriteTimeUtc, nuspecReader, packageReader)); } }
public LocalPackageInfo AddPackage(Stream packageStream, bool allowOverwrite) { string tempFilePath = Path.Combine(this.Source, Path.GetRandomFileName() + PackagingCoreConstants.PackageDownloadMarkerFileExtension); using (var dest = File.OpenWrite(tempFilePath)) { packageStream.CopyTo(dest); } PackageIdentity identity; string destPackagePath; using (var archive = new PackageArchiveReader(File.OpenRead(tempFilePath))){ var id = archive.NuspecReader.GetId(); var version = archive.NuspecReader.GetVersion(); identity = new PackageIdentity(id, version); destPackagePath = _pathResolver.GetPackageFilePath(id, version); _fileSystem.MakeDirectoryForFile(destPackagePath); } var hashFilePath = Path.ChangeExtension(destPackagePath, PackagingCoreConstants.HashFileExtension); if (!allowOverwrite && File.Exists(hashFilePath)) { throw new PackageDuplicateException($"Package {identity} already exists"); } if (File.Exists(destPackagePath)) { File.Delete(destPackagePath); } File.Move(tempFilePath, destPackagePath); var ct = CancellationToken.None; using (var downloader = new LocalPackageArchiveDownloader(destPackagePath, identity, _logAdapter)) { var hash = downloader.GetPackageHashAsync("SHA512", ct).Result; var hashBytes = Encoding.UTF8.GetBytes(hash); File.WriteAllBytes(hashFilePath, hashBytes); var nuspecPath = _pathResolver.GetManifestFilePath(identity.Id, identity.Version); using (var nuspecStream = File.OpenWrite(nuspecPath)) { using (var stream = downloader.CoreReader.GetNuspecAsync(ct).Result){ stream.CopyTo(nuspecStream); } } _log.DebugFormat("Saved manifest {0}", nuspecPath); Lazy <NuspecReader> nuspecReader = new Lazy <NuspecReader>(() => new NuspecReader(nuspecPath)); var packageReader = new Func <PackageReaderBase>(() => new PackageArchiveReader(File.OpenRead(destPackagePath))); return(new LocalPackageInfo(identity, destPackagePath, DateTime.UtcNow, nuspecReader, packageReader)); } }
private static void AssertMyPackage(PhysicalFileSystem fileSystem, LocalPackageInfo package, LocalPackageInfo addedPackage) { string expectedPath = Path.Combine(fileSystem.Root, "mypackage", "1.0.0-beta2", "mypackage.1.0.0-beta2.nupkg"); Assert.Equal(expectedPath, addedPackage.Path); // Assert using (var fs = fileSystem.OpenFile(Path.Combine("mypackage", "1.0.0-beta2", "mypackage.nuspec"))){ var reader = Manifest.ReadFrom(fs, validateSchema: true); Assert.Equal("MyPackage", reader.Metadata.Id); Assert.Equal(NuGetVersion.Parse("1.0.0-beta2"), reader.Metadata.Version); using (var file = File.OpenRead(package.Path)) { Assert.True(file.ContentEquals(fileSystem.OpenFile(Path.Combine("mypackage", "1.0.0-beta2", "mypackage.1.0.0-beta2.nupkg")))); file.Seek(0, SeekOrigin.Begin); } using (var downloader = new LocalPackageArchiveDownloader(package.Path, package.Identity, NullLogger.Instance)) { var sha = downloader.GetPackageHashAsync("SHA512", CancellationToken.None).Result; Assert.Equal(sha, fileSystem.ReadAllText(Path.Combine("mypackage", "1.0.0-beta2", "mypackage.1.0.0-beta2.nupkg.sha512"))); } } }