Esempio n. 1
0
        public WorkflowActivityContext(ICodeActivityContext activityContext)
        {
            this.EnsureNotNull(activityContext);

            ExecutionContext    = activityContext;
            WorkflowContext     = activityContext.GetExtension <IWorkflowContext>();
            ServiceFactory      = activityContext.GetExtension <IOrganizationServiceFactory>();
            OrganizationService = ServiceFactory.CreateOrganizationService(WorkflowContext.UserId);
            TracingService      = activityContext.GetExtension <ITracingService>();


            if (WorkflowContext == null)
            {
                throw new InvalidPluginExecutionException("Failed to retrieve workflow context.");
            }
        }
        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);
            }
        }