Пример #1
0
 public PackageProcessor(IActionExecutor actionExecutor, IConsoleWriter writer, INuGetProcess nugetProcess, RunContext runContext)
 {
     _actionExecutor = actionExecutor;
     _writer = writer;
     _nugetProcess = nugetProcess;
     _runContext = runContext;
 }
Пример #2
0
        protected override void Execute(CodeActivityContext context)
        {
            #region Extract Workflow Argument Values

            // The file path of the nuget.exe - if null or empty then
            //  assume nuget is "installed" on the build server and in the path
            var nuGetExeFilePath = NuGetExeFilePath.Get(context);

            // The path of the nuspec file
            var packageLocation = PackageLocation.Get(context);

            // The API Key for pushing
            var apiKey = ApiKey.Get(context);

            // The destination location if deployment is to be done
            var pushDestination = PushDestination.Get(context);

            #endregion

            // Don't assume that DI will have happened.  If the value is null then create the default object.)
            if (NuGetProcess == null)
            {
                NuGetProcess = new NuGetProcess();
            }

            // Call the method that will do the work
            var results = NuGetPublishing(nuGetExeFilePath, packageLocation, pushDestination, apiKey, context);

            // Send the result back to the workflow
            NuGetPushResult.Set(context, results);
        }
        protected override void Execute(CodeActivityContext context)
        {
            #region Extract Workflow Argument Values

            // The file path of the nuget.exe - if null or empty then
            //  assume nuget is "installed" on the build server and in the path
            var nuGetExeFilePath = NuGetExeFilePath.Get(context);

            // The path of the nuspec file
            var nuSpecFilePath = NuSpecFilePath.Get(context);

            // The folder location of the files to be packed
            var basePath = BasePath.Get(context);

            // The folder location of the files to be packed
            var outputDirectory = OutputDirectory.Get(context);

            // The destination location if deployment is to be done
            var versionNumber = VersionNumber.Get(context);

            // command line options to append (as is) to the nuget command line
            var additionalOptions = AdditionalOptions.Get(context);

            #endregion

            context.WriteBuildMessage(string.Format("In CallNuGetPackageCommandLine:"), BuildMessageImportance.High);
            context.WriteBuildMessage(string.Format("nuGetExeFilePath: {0}", nuGetExeFilePath), BuildMessageImportance.High);
            context.WriteBuildMessage(string.Format("basePath: {0}", basePath), BuildMessageImportance.High);
            context.WriteBuildMessage(string.Format("outputDirectory: {0}", outputDirectory), BuildMessageImportance.High);
            context.WriteBuildMessage(string.Format("versionNumber: {0}", versionNumber), BuildMessageImportance.High);
            context.WriteBuildMessage(string.Format("additionalOptions: {0}", additionalOptions), BuildMessageImportance.High);

            // Don't assume that DI will have happened.  If the value is null then create the default object.);)
            if (NuGetProcess == null)
            {
                NuGetProcess = new NuGetProcess();
            }

            // Call the method that will do the work
            var results = NuGetPackaging(nuGetExeFilePath, nuSpecFilePath, outputDirectory, basePath, versionNumber, additionalOptions, context);

            // Send the result back to the workflow
            NuGetPackagingResult.Set(context, results);
        }
        public void ProcessDeployment(string packageLocation, Models.PackagesPackageDeployment deployment, IBuildMessageWriter buildMessageWriter, INuGetProcess nuGetProcess, string nuGetExeFilePath)
        {
            // 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(deployment.path).Success)
            {
                var pushDestinationArgument = string.IsNullOrWhiteSpace(deployment.path) ?
                   string.Empty : string.Format("-s \"{0}\"", deployment.path);

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


                buildMessageWriter.WriteBuildMessage(string.Format("Push Arguments: {0}", arguments), BuildMessageImportance.High);

                nuGetProcess.RunNuGetProcess(nuGetExeFilePath, arguments, buildMessageWriter);

                return;
            }

            var fileName = Path.GetFileName(packageLocation);

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

                File.Copy(packageLocation, destinationFilePath, true);
            }
        }
        public void ProcessPackage(Models.PackagesPackage package, 
            string nuGetExeFilePath, 
            INuGetProcess nuGetProcess, 
            string sourcesDirectory, 
            string buildDirectory,
            string dropFolder, 
            IWorkspaceContext workspaceContext, 
            IBuildMessageWriter buildMessageWriter, 
            ICodeActivityContext context, 
            string buildNumber,
            int buildNumberPrefex, 
            string inputVersion)
        {
            var nuspecFile = GetFile(workspaceContext, sourcesDirectory, buildDirectory, package.NuSpecPath, package.name + "-nuspec");

            if(!File.Exists(nuspecFile))
            {
                throw new Exception(string.Format("The nuspec file \"{0}\" doesn't exist. It was defined in package \"{1}\"", nuspecFile, package.name));
            }

            if(string.IsNullOrEmpty(package.OutputDirectory))
            {
                package.OutputDirectory = "Packages";
            }
            if(string.IsNullOrEmpty(package.AdditionalOptions))
            {
                package.AdditionalOptions = string.Empty;
            }
            
            var outputDirectory = Path.Combine(dropFolder, package.OutputDirectory);
            if(!Directory.Exists(outputDirectory))
            {
                Directory.CreateDirectory(outputDirectory);
            }
            var basePath = dropFolder;

            if(!string.IsNullOrEmpty(package.BasePath))
            {
                basePath = Path.Combine(basePath, package.BasePath);
            }

            string version = !string.IsNullOrEmpty(package.Version)
                    ? new VersionPatternConverter().Convert(buildNumber, buildNumberPrefex, package.Version)
                    : new VersionPatternConverter().Convert(buildNumber, buildNumberPrefex, inputVersion);
            string versionParameter = string.Empty;
            // if the nuspec has a version token, it is expecting us to set it from out global version number created
            if (GetNuSpecPackageVersion(nuspecFile).Equals("$version$", StringComparison.InvariantCultureIgnoreCase))
            {
                // the package expects us to set a version
                // if the defintion defines a version, use it, otherwise, use the global version number
                versionParameter = string.Format("-version {0}", version);
            }

            // if the nuspec file has a $inputVersion$ token, we need to replace it.
            var nuspec = File.ReadAllText(nuspecFile);
            if(nuspec.Contains("$inputVersion$"))
            {
                FileAttributes attributes = File.GetAttributes(nuspecFile);

                if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                    File.SetAttributes(nuspecFile, attributes ^ FileAttributes.ReadOnly);

                File.Delete(nuspecFile);
                File.WriteAllText(nuspecFile, nuspec.Replace("$inputVersion$", version));
            }

            // TODO
            //package.PrePackagePowerShell

            var arguments = string.Format("pack \"{0}\" -OutputDirectory \"{1}\" -BasePath \"{2}\" {3} {4}", nuspecFile, outputDirectory, basePath, versionParameter, package.AdditionalOptions).Trim();
            buildMessageWriter.WriteBuildMessage(string.Format("Calling nuget with arguments: {0}", arguments), BuildMessageImportance.High);
            nuGetProcess.RunNuGetProcess(nuGetExeFilePath, arguments, buildMessageWriter);

            // TODO
            //package.PreDeployPowerShell

            if(package.Deployments != null && package.Deployments.Length > 0)
            {
                var packageLocation = string.Empty;
                var pattern = GetNuSpecPackageName(nuspecFile) + "*.nupkg";
                var files = new DirectoryInfo(outputDirectory).GetFiles(pattern).ToList();

                if(files.Count > 1)
                {
                    context.AddBuildError(string.Format("An attempt was made to deploy but there was more than 1 package found in the directory \"{0}\" matching the pattern \"{1}\".", outputDirectory, pattern));
                }else if(files.Count == 0)
                {
                    context.AddBuildError(string.Format("An attempt was made to deploy but there was no package found in the directory \"{0}\" matching the pattern \"{1}\".", outputDirectory, pattern));
                }else
                {
                    packageLocation = files[0].FullName;
                }

                if(!string.IsNullOrEmpty(packageLocation) && File.Exists(packageLocation))
                {
                    foreach (var deployment in package.Deployments)
                    {
                        ProcessDeployment(packageLocation, deployment, buildMessageWriter, nuGetProcess, nuGetExeFilePath);
                    }
                }
            }
        }
        public void DoProcessNuGetDefinitionsActivitiy(
            ICodeActivityContext context,
            IWorkspaceContext workspaceContext,
            IBuildMessageWriter buildMessageWriter,
            INuGetProcess nuGetProcess,
            string sourcesDirectory,
            string buildDirectory,
            string nuGetExeFilePath, 
            string definitionsFilePath,
            string dropFolder,
            string buildNumber,
            int buildNumberPrefex,
            string version)
        {
            if (string.IsNullOrEmpty(definitionsFilePath))
            {
                buildMessageWriter.WriteBuildMessage("No definition path was given.", BuildMessageImportance.High);
                return;
            }

            #region Gaurd

            if (string.IsNullOrEmpty(sourcesDirectory))
            {
                throw new Exception("sourcesDirectory is null/empty");
            }
            if (string.IsNullOrEmpty(dropFolder))
            {
                throw new Exception("dropFolder is null/empty");
            }
            if (workspaceContext == null)
            {
                throw new Exception("workspaceContext is null");
            }
            if (string.IsNullOrEmpty(buildDirectory))
            {
                throw new Exception("buildDirectory is null/empty");
            }
            if (string.IsNullOrEmpty(nuGetExeFilePath))
            {
                throw new Exception("nuGetExeFilePath is null/empty");
            }

            #endregion

            nuGetExeFilePath = GetFile(workspaceContext, sourcesDirectory, buildDirectory, nuGetExeFilePath, "NuGetExe");
            definitionsFilePath = GetFile(workspaceContext, sourcesDirectory, buildDirectory, definitionsFilePath, "DefinitionsFile");

            if (!File.Exists(nuGetExeFilePath))
            {
                context.AddBuildError(string.Format("The nuget exe was not found at the path {0}.", nuGetExeFilePath));
                return;
            }

            if (!File.Exists(definitionsFilePath))
            {
                context.AddBuildError(string.Format("The definitions xml was not found at the path {0}.", definitionsFilePath));
                return;
            }

            Models.Packages definitions = null;

            try
            {
                definitions = GetDefinitions(definitionsFilePath);
            }
            catch (Exception ex)
            {
                context.AddBuildError(string.Format("There was a problem with the format of the definitions xml. {0}", ex.Message));
                return;
            }

            if (definitions == null || definitions.Package == null || definitions.Package.Length == 0)
            {
                buildMessageWriter.WriteBuildMessage("There were not packages in the definition file.", BuildMessageImportance.High);
                return;
            }

            if(definitions.Package.Any(x => string.IsNullOrEmpty(x.name)))
            {
                context.AddBuildError("All package definitions must have a name (Package[name='name']).");
                return;
            }

            if (definitions.Package.Select(x => x.name.ToLower()).Distinct().Count() != definitions.Package.Count())
            {
                context.AddBuildError("The names for each package must be unique.");
                return;
            }

            foreach (var package in definitions.Package)
            {
                ProcessPackage(package, nuGetExeFilePath, nuGetProcess, sourcesDirectory, buildDirectory, dropFolder, workspaceContext, buildMessageWriter, context, buildNumber, buildNumberPrefex, version);
            }
        }