Beispiel #1
0
        public ManifestReWriteResult Rewrite(
            string nuspecFullPath,
            string tagPrefix = "x-arbor-x",
            ILogger logger   = null)
        {
            if (string.IsNullOrWhiteSpace(nuspecFullPath))
            {
                throw new ArgumentNullException(nameof(nuspecFullPath));
            }

            if (string.IsNullOrWhiteSpace(tagPrefix))
            {
                throw new ArgumentNullException(nameof(tagPrefix));
            }

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

            var removeTags = new List <string>();

            bool isReWritten = false;

            string tempFile = $"{nuspecFullPath}.{Guid.NewGuid()}.tmp";

            DirectoryInfo baseDir = new FileInfo(nuspecFullPath).Directory;

            using (var stream = new FileStream(nuspecFullPath, FileMode.Open))
            {
                var packageBuilder = new PackageBuilder(stream, baseDir.FullName);

                logger?.WriteVerbose("Using starts with-pattern '" + tagPrefix + "' to exclude tags from NuSpec");

                string[] tagsToRemove = packageBuilder.Tags.Where(tag => tag.StartsWith(tagPrefix, StringComparison.Ordinal)).ToArray();

                if (!tagsToRemove.Any())
                {
                    logger?.WriteVerbose($"No tags to remove from NuSpec '{nuspecFullPath}'");
                }

                foreach (string tagToRemove in tagsToRemove)
                {
                    logger?.WriteVerbose($"Removing tag '{tagToRemove}' from NuSpec '{nuspecFullPath}'");
                    packageBuilder.Tags.Remove(tagToRemove);
                }

                if (tagsToRemove.Any())
                {
                    using (var outStream = new FileStream(tempFile, FileMode.CreateNew))
                    {
                        packageBuilder.Save(outStream);
                        isReWritten = true;
                    }
                }
            }

            if (isReWritten)
            {
                logger?.WriteVerbose($"Deleting NuSpec file '{nuspecFullPath}'");
                File.Delete(nuspecFullPath);

                logger?.WriteVerbose($"Moving NuSpec temp copy '{tempFile}' to file '{nuspecFullPath}'");
                File.Move(tempFile, nuspecFullPath);
            }

            var result = new ManifestReWriteResult(removeTags, tagPrefix);

            return(result);
        }
Beispiel #2
0
        public async Task <ExitCode> CreatePackageAsync(
            string packageSpecificationPath,
            NuGetPackageConfiguration packageConfiguration,
            bool ignoreWarnings = false,
            CancellationToken cancellationToken = default)
        {
            _logger.WriteDebug($"Using NuGet package configuration {packageConfiguration}");

            NuSpec nuSpec = NuSpec.Parse(packageSpecificationPath);

            string properties = GetProperties(packageConfiguration.Configuration);

            if (!string.IsNullOrWhiteSpace(packageConfiguration.PackageIdOverride))
            {
                _logger.Write($"Using NuGet package id override '{packageConfiguration.PackageIdOverride}'");
            }

            string packageId = !string.IsNullOrWhiteSpace(packageConfiguration.PackageIdOverride)
                ? packageConfiguration.PackageIdOverride
                : NuGetPackageIdHelper.CreateNugetPackageId(
                nuSpec.PackageId,
                packageConfiguration.IsReleaseBuild,
                packageConfiguration.BranchName,
                packageConfiguration.BranchNameEnabled);

            if (string.IsNullOrWhiteSpace(packageConfiguration.PackageIdOverride))
            {
                _logger.Write($"Using NuGet package ID {packageId}");
            }
            else
            {
                _logger.Write($"Using NuGet package version override '{packageConfiguration.PackageIdOverride}'");
            }

            var nuGetVersioningSettings = new NuGetVersioningSettings {
                MaxZeroPaddingLength = 5, SemVerVersion = 1
            };
            string nuGetPackageVersion = !string.IsNullOrWhiteSpace(packageConfiguration.NuGetPackageVersionOverride)
                ? packageConfiguration.NuGetPackageVersionOverride
                : NuGetVersionHelper.GetVersion(
                packageConfiguration.Version,
                packageConfiguration.IsReleaseBuild,
                packageConfiguration.Suffix,
                packageConfiguration.BuildNumberEnabled,
                packageConfiguration.PackageBuildMetadata,
                _logger,
                nuGetVersioningSettings);

            _logger.Write(
                string.IsNullOrWhiteSpace(packageConfiguration.NuGetPackageVersionOverride)
                    ? $"Using NuGet package version {nuGetPackageVersion}"
                    : $"Using NuGet package version override '{packageConfiguration.NuGetPackageVersionOverride}'");

            var nuSpecInfo = new FileInfo(packageSpecificationPath);

            // ReSharper disable AssignNullToNotNullAttribute
            string nuSpecFileCopyPath = Path.Combine(nuSpecInfo.DirectoryName, $"{Guid.NewGuid()}-{nuSpecInfo.Name}");

            // ReSharper restore AssignNullToNotNullAttribute

            var nuSpecCopy = new NuSpec(packageId, nuGetPackageVersion, nuSpecInfo.FullName);

            string nuSpecTempDirectory = Path.Combine(packageConfiguration.TempPath, "nuspecs");

            if (!Directory.Exists(nuSpecTempDirectory))
            {
                Directory.CreateDirectory(nuSpecTempDirectory);
            }

            _logger.WriteVerbose($"Saving new nuspec '{nuSpecFileCopyPath}'");
            nuSpecCopy.Save(nuSpecFileCopyPath);

            var removedTags = new List <string>();

            if (packageConfiguration.AllowManifestReWrite)
            {
                _logger.WriteVerbose($"Rewriting manifest in NuSpec '{nuSpecFileCopyPath}'");

                var manitestReWriter = new ManitestReWriter();
                ManifestReWriteResult manifestReWriteResult = manitestReWriter.Rewrite(nuSpecFileCopyPath);

                removedTags.AddRange(manifestReWriteResult.RemoveTags);
            }
            else
            {
                _logger.WriteVerbose("Rewriting manifest disabled");
            }

            _logger.WriteVerbose(
                $"Created nuspec content: {Environment.NewLine}{File.ReadAllText(nuSpecFileCopyPath)}");

            ExitCode result = await ExecuteNuGetPackAsync(
                packageConfiguration.NuGetExePath,
                packageConfiguration.PackagesDirectory,
                _logger,
                nuSpecFileCopyPath,
                properties,
                nuSpecCopy,
                removedTags,
                cancellationToken : cancellationToken,
                ignoreWarnings : ignoreWarnings);

            return(result);
        }