Exemple #1
0
        private static async Task <ExitCode> ExecuteNuGetPackAsync(
            string nuGetExePath,
            string packagesDirectoryPath,
            ILogger logger,
            string nuSpecFileCopyPath,
            string properties,
            NuSpec nuSpecCopy,
            List <string> removedTags,
            bool keepBinaryAndSourcePackagesTogetherEnabled = false,
            bool nugetSymbolPackageEnabled      = false,
            bool ignoreWarnings                 = false,
            CancellationToken cancellationToken = default)
        {
            bool hasRemovedNoSourceTag =
                removedTags.Any(
                    tag => tag.Equals(WellKnownNuGetTags.NoSource, StringComparison.InvariantCultureIgnoreCase));

            ExitCode result;

            try
            {
                var arguments = new List <string>
                {
                    "pack",
                    nuSpecFileCopyPath,
                    "-Properties",
                    properties,
                    "-OutputDirectory",
                    packagesDirectoryPath,
                    "-Version",
                    nuSpecCopy.Version
                };

                if (!hasRemovedNoSourceTag && nugetSymbolPackageEnabled)
                {
                    arguments.Add("-Symbols");
                }

                if (LogLevel.Verbose.Level <= logger.LogLevel.Level)
                {
                    arguments.Add("-Verbosity");
                    arguments.Add("Detailed");
                }

                if (ignoreWarnings)
                {
                    arguments.Add("-NoPackageAnalysis");
                }

                ExitCode processResult =
                    await
                    ProcessRunner.ExecuteAsync(
                        nuGetExePath,
                        arguments : arguments,
                        standardOutLog : logger.Write,
                        standardErrorAction : logger.WriteError,
                        toolAction : logger.Write,
                        cancellationToken : cancellationToken,
                        verboseAction : logger.WriteVerbose,
                        debugAction : logger.WriteDebug,
                        addProcessNameAsLogCategory : true,
                        addProcessRunnerCategory : true);

                var packagesDirectory = new DirectoryInfo(packagesDirectoryPath);

                if (!keepBinaryAndSourcePackagesTogetherEnabled)
                {
                    logger.Write(
                        $"The flag {WellKnownVariables.NuGetKeepBinaryAndSymbolPackagesTogetherEnabled} is set to false, separating binary packages from symbol packages");
                    List <string> nugetPackages = packagesDirectory.GetFiles("*.nupkg", SearchOption.TopDirectoryOnly)
                                                  .Select(file => file.FullName)
                                                  .ToList();
                    List <string> nugetSymbolPackages = packagesDirectory
                                                        .GetFiles("*.symbols.nupkg", SearchOption.TopDirectoryOnly)
                                                        .Select(file => file.FullName)
                                                        .ToList();

                    List <string> binaryPackages = nugetPackages.Except(nugetSymbolPackages).ToList();

                    DirectoryInfo binaryPackagesDirectory =
                        new DirectoryInfo(Path.Combine(packagesDirectory.FullName, "binary")).EnsureExists();

                    DirectoryInfo symbolPackagesDirectory =
                        new DirectoryInfo(Path.Combine(packagesDirectory.FullName, "symbol")).EnsureExists();

                    foreach (string binaryPackage in binaryPackages)
                    {
                        var sourceFile       = new FileInfo(binaryPackage);
                        var targetBinaryFile =
                            new FileInfo(Path.Combine(binaryPackagesDirectory.FullName, sourceFile.Name));

                        if (targetBinaryFile.Exists)
                        {
                            targetBinaryFile.Delete();
                        }

                        logger.WriteDebug($"Copying NuGet binary package '{binaryPackage}' to '{targetBinaryFile}'");
                        sourceFile.MoveTo(targetBinaryFile.FullName);
                    }

                    foreach (string sourcePackage in nugetSymbolPackages)
                    {
                        var sourceFile       = new FileInfo(sourcePackage);
                        var targetSymbolFile =
                            new FileInfo(Path.Combine(symbolPackagesDirectory.FullName, sourceFile.Name));

                        if (targetSymbolFile.Exists)
                        {
                            targetSymbolFile.Delete();
                        }

                        logger.WriteDebug($"Copying NuGet symbol package '{sourcePackage}' to '{targetSymbolFile}'");
                        sourceFile.MoveTo(targetSymbolFile.FullName);
                    }
                }

                result = processResult;
            }
            finally
            {
                if (File.Exists(nuSpecFileCopyPath))
                {
                    File.Delete(nuSpecFileCopyPath);
                }
            }

            return(result);
        }
Exemple #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);
        }