public bool RunNuGetProcess(string nuGetFilePath, string arguments, IBuildMessageWriter buildMessageWriter) { var stopwatch = new Stopwatch(); stopwatch.Start(); var proc = new Process { StartInfo = { FileName = nuGetFilePath, Arguments = arguments, UseShellExecute = false, CreateNoWindow = true, RedirectStandardError = true } }; proc.Start(); var errorOutput = proc.StandardError.ReadToEnd(); // Block for now but force release of the process after 5 minutes - Just in case NuGet fails to return // otherwise it will return as soon as NuGet is done. var result = proc.WaitForExit(300000); stopwatch.Stop(); if (buildMessageWriter != null) { if (!string.IsNullOrWhiteSpace(errorOutput)) { result = false; buildMessageWriter.WriteBuildMessage(string.Format("Error reported in the NuGet Process: {0}", errorOutput), BuildMessageImportance.High); } buildMessageWriter.WriteBuildMessage(string.Format("NuGet Process execution time: {0}", stopwatch.Elapsed.Seconds), BuildMessageImportance.High); } return result; }
public WorkspaceContext(Workspace workspace, IBuildMessageWriter buildMessageWriter) { _workspace = workspace; _buildMessageWriter = buildMessageWriter; }
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 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 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); } }