Exemplo n.º 1
0
        /// <summary>
        /// Does the NuGet Processing work.  Formats data into an argument string and then calls NuGet.Exe to do the Push or Push/Publish
        /// </summary>
        /// <param name="nuGetExeFilePath">Full path to the nuget.exe application OR empty if nuget is in the machine's path</param>
        /// <param name="packageLocation">Full path to the NuPkg file that is to be pushed</param>
        /// <param name="pushDestination">Address of the gallery that nuget.exe will push the package to</param>
        /// <param name="apiKey">Optional: (if required) the key used to push the package to the nuget gallery</param>
        /// <param name="context">the workflow/build context - used to send build messages</param>
        /// <returns>true if the nuget process succeeds</returns>
        public bool NuGetPublishing(string nuGetExeFilePath, string packageLocation, string pushDestination, string apiKey, CodeActivityContext context)
        {
            #region Parameter Validation
            if (string.IsNullOrWhiteSpace(packageLocation))
            {
                throw new ArgumentNullException("packageLocation");
            }

            if (pushDestination == null)
            {
                // Assume that we will push to the NuGet Gallery
                pushDestination = string.Empty;
            }
            #endregion

            // Match a unc or drive based location - Do a copy if found
            var regex = new Regex(@"^((\\\\[a-zA-Z0-9-]+\\[a-zA-Z0-9`~!@#$%^&(){}'._-]+([ ]+[a-zA-Z0-9`~!@#$%^&(){}'._-]+)*)|([a-zA-Z]:))(\\[^ \\/:*?""<>|]+([ ]+[^ \\/:*?""<>|]+)*)*\\?$");

            if (!regex.Match(pushDestination).Success)
            {
                var pushDestinationArgument = string.IsNullOrWhiteSpace(pushDestination) ?
                                              string.Empty : string.Format("-s \"{0}\"", pushDestination);

                var arguments = string.Format("push \"{0}\" {1} {2}",
                                              packageLocation, apiKey, pushDestinationArgument);

                if (context != null)
                {
                    context.WriteBuildMessage(string.Format("Push Arguments: {0}", arguments), BuildMessageImportance.High);
                }

                return(NuGetProcess.RunNuGetProcess(NuGetHelper.ValidateNuGetExe(nuGetExeFilePath), arguments, context));
            }

            var fileName = Path.GetFileName(packageLocation);

            if (fileName != null)
            {
                var destinationFilePath = Path.Combine(pushDestination, fileName);

                File.Copy(packageLocation, destinationFilePath, true);

                return(true);
            }

            return(false);
        }
        /// <summary>
        /// Does the NuGet Processing work.  Formats data into an argument string and then calls NuGet.Exe
        /// </summary>
        /// <param name="nuGetExeFilePath">Full path to the nuget.exe application OR empty if nuget is in the machine's path</param>
        /// <param name="nuSpecFilePath">path to the NuSpec "manifest" file</param>
        /// <param name="outputDirectory">path to the folder where the generated NuPkg file should be placed</param>
        /// <param name="basePath">path to the folder containing all of the files that should be packaged</param>
        /// <param name="versionNumber">optional: is a version number is provided it will be used to override the version of the package</param>
        /// <param name="additionalOptions">additional NuGet command line arguments (optional)</param>
        /// <param name="context">the workflow/build context - used to send build messages</param>
        /// <returns>true if the nuget process succeeds</returns>
        public bool NuGetPackaging(string nuGetExeFilePath, string nuSpecFilePath, string outputDirectory, string basePath, string versionNumber, string additionalOptions, CodeActivityContext context)
        {
            var versionParameter = string.Empty;

            #region Parameter Validation
            if (string.IsNullOrWhiteSpace(nuSpecFilePath))
            {
                throw new ArgumentNullException("nuSpecFilePath");
            }

            if (string.IsNullOrWhiteSpace(basePath))
            {
                throw new ArgumentNullException("basePath");
            }

            if (string.IsNullOrWhiteSpace(outputDirectory))
            {
                throw new ArgumentNullException("outputDirectory");
            }
            #endregion

            // If the version number was provided, then use it
            if (!string.IsNullOrWhiteSpace(versionNumber))
            {
                versionParameter = string.Format("-version {0}", versionNumber);
            }

            var arguments = string.Format(
                "pack \"{0}\" -OutputDirectory \"{1}\" -BasePath \"{2}\" {3} {4}", nuSpecFilePath, outputDirectory, basePath, versionParameter, additionalOptions).Trim();

            if (context != null)
            {
                context.WriteBuildMessage(string.Format("CallNuGetPackageCommandLine arguments: {0}", arguments), BuildMessageImportance.High);
            }

            return(NuGetProcess.RunNuGetProcess(NuGetHelper.ValidateNuGetExe(nuGetExeFilePath), arguments, context));
        }