Esempio n. 1
0
        public void Publish(string filePath)
        {
            NuGet.Packaging.Core.PackageIdentity packageIdentity;

            using (var stream = File.OpenRead(filePath))
                packageIdentity = new NuGet.Packaging.PackageArchiveReader(stream, false).NuspecReader.GetIdentity();

            var packageSearch = _sourceRepository.GetResource <FindPackageByIdResource>();
            var versions      = packageSearch.GetAllVersionsAsync(packageIdentity.Id, NullSourceCacheContext.Instance, NullLogger.Instance, CancellationToken.None).Result;

            var packageUpdate = _sourceRepository.GetResource <PackageUpdateResource>();

            var comparer = new VersionComparerExtended(VersionComparer.Pundit);

            foreach (var version in versions)
            {
                if (comparer.Equals(packageIdentity.Version, version))
                {
                    packageUpdate.Delete(packageIdentity.Id, version.ToString(),
                                         endpoint => GetApiKey(),
                                         confirm => true, true, NullLogger.Instance).Wait();
                }
            }

            packageUpdate.Push(filePath, null, (int)TimeOut.TotalSeconds, true,
                               endpoint => GetApiKey(),
                               symbolsEndpoint => null,
                               true, NullLogger.Instance).Wait();
        }
Esempio n. 2
0
        public async Task PublishPackage(Stream stream)
        {
            using (NuGet.Packaging.PackageArchiveReader packageArchiveReader = new NuGet.Packaging.PackageArchiveReader(stream))
            {
                var packageName = packageArchiveReader.NuspecReader.GetId().ToLower();
                var version     = packageArchiveReader.NuspecReader.GetVersion().ToNormalizedString();

                // Write the nupkg file
                await this.packagesPersistentStorage.WriteContent($"{packageName}/{version}/{packageName}.{version}.nupkg", stream).ConfigureAwait(false);

                // Write the nuspec file extracted from the nupkg
                var nuspecStream = packageArchiveReader.GetNuspec();
                await this.packagesPersistentStorage.WriteContent($"{packageName}/{version}/{packageName}.nuspec", nuspecStream).ConfigureAwait(false);

                // Index package
                var packageRegistrationResult = await this.packageIndex.IndexPackage(packageArchiveReader.NuspecReader).ConfigureAwait(false);

                // Write the registration index.json file
                await this.PublishRegistrationIndexFile(packageName, packageRegistrationResult).ConfigureAwait(false);

                // Write the version index.json file
                string packageId = packageRegistrationResult.Items.Last().Items.Last().CatalogEntry.Id;
                var    versions  = await this.packageIndex.GetAllVersions(packageId);

                await this.PublishVersionIndexFile(packageId, versions).ConfigureAwait(false);
            }
        }
Esempio n. 3
0
        public void NuGetToPundit()
        {
            Guard.NotEmpty(SourcePath, nameof(SourcePath));

            if (!File.Exists(SourcePath))
            {
                throw new FileNotFoundException("The given file does not exist", SourcePath);
            }

            if (Path.GetExtension(SourcePath) != ".nupkg")
            {
                throw new InvalidOperationException("The given file is not a NuGet package");
            }

            if (string.IsNullOrEmpty(DestinationFolder))
            {
                DestinationFolder = Path.GetDirectoryName(SourcePath);
            }
            else
            {
                DestinationFolder = Path.GetFullPath(DestinationFolder);
            }

            var punditSpec = new PackageSpec();

            using (var stream = File.Open(SourcePath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                var reader = new NuGet.Packaging.PackageArchiveReader(stream);

                punditSpec.PackageId    = reader.NuspecReader.GetIdentity().Id;
                punditSpec.Version      = reader.NuspecReader.GetVersion();
                punditSpec.Author       = reader.NuspecReader.GetAuthors();
                punditSpec.Description  = reader.NuspecReader.GetDescription();
                punditSpec.License      = reader.NuspecReader.GetLicenseUrl();
                punditSpec.ProjectUrl   = reader.NuspecReader.GetProjectUrl();
                punditSpec.ReleaseNotes = reader.NuspecReader.GetReleaseNotes();

                // TODO: Only bin supported for now
                foreach (var framework in reader.GetSupportedFrameworks())
                {
                    if (!NuGet.Frameworks.NuGetFrameworkExtensions.IsDesktop(framework))
                    {
                        _writer.Empty().Warning($"Framework '{framework}' is not supported, skipping...");
                        continue;
                    }

                    punditSpec.Dependencies = new List <PackageDependency>();
                    punditSpec.Files        = new List <SourceFiles>();

                    var dependencies = reader.GetPackageDependencies().FirstOrDefault(d => d.TargetFramework == framework);

                    //TODO: Finish this
                    throw new NotImplementedException();
                    //if (dependencies != null)
                    //foreach (var dependency in dependencies.Packages)
                    //   punditSpec.Dependencies.Add(new PackageDependency(dependency.Id, dependency.VersionRange));

                    var libs = reader.GetLibItems().FirstOrDefault(d => d.TargetFramework == framework);

                    if (libs == null)
                    {
                        _writer.Empty().Info($"No libraries found for framework '${framework}', skipping...");
                        continue;
                    }

                    var tempOut = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

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

                        Directory.CreateDirectory(tempOut);

                        foreach (var lib in libs.Items)
                        {
                            punditSpec.Files.Add(new SourceFiles(lib));

                            reader.ExtractFile(lib, Path.Combine(tempOut, lib), NuGet.Common.NullLogger.Instance);
                        }

                        var punditSpecFile = Path.Combine(tempOut, PackageManifest.DefaultManifestFileName);
                        //using (var outFileStream = File.Create(punditSpecFile))
                        //   new PunditPackageWriter(new XmlSerializer(), )
                        //   _packageSerializerFactory.GetPundit().SerializePackageSpec(punditSpec, outFileStream);

                        _packService.ManifestFileOrPath = punditSpecFile;
                        _packService.Pack();

                        var destinationFile = Path.Combine(DestinationFolder, Path.GetFileName(_packService.DestinationFile));
                        if (File.Exists(destinationFile))
                        {
                            File.Delete(destinationFile);
                        }

                        File.Move(_packService.DestinationFile, destinationFile);
                    }
                    finally
                    {
                        if (Directory.Exists(tempOut))
                        {
                            Directory.Delete(tempOut, true);
                        }
                    }
                }
            }
        }