/// <summary> /// package the nuspec file /// </summary> /// <returns>true, if successful</returns> private bool Pack(string additionalOptions, NuspecItemInfo item, NuspecItemConfig itemConfig, ref string outputFile) { WriteOutput("Packing nuspec file: " + item.FileName); var startInfo = new ProcessStartInfo(itemConfig.NuGetExe); startInfo.Arguments = $"pack \"{item.FileName}\" -NoDefaultExcludes -OutputDirectory \"{itemConfig.OutputPath}\" {additionalOptions}"; startInfo.RedirectStandardOutput = true; startInfo.RedirectStandardError = true; startInfo.CreateNoWindow = true; startInfo.UseShellExecute = false; startInfo.WindowStyle = ProcessWindowStyle.Hidden; var process = System.Diagnostics.Process.Start(startInfo); process.WaitForExit(); var output = process.StandardOutput.ReadToEnd(); WriteOutput(output); if (process.ExitCode == 0) { var regx = new Regex(@"(')([^']+)\1"); outputFile = regx.Matches(output).Cast <Match>().Select(m => m.Groups[2].Value).Last(); WriteOutput("Successfully packed nuspec file: " + item.FileName); return(true); } else { outputFile = null; var error = process.StandardError.ReadToEnd(); WriteOutput("Error packing nuspec file: " + item.FileName + Environment.NewLine + "ERROR: " + error); return(false); } }
private bool ValidateOptions(NuspecItemInfo item, NuspecItemConfig itemConfig) { if (!File.Exists(itemConfig.NuGetExe)) { WriteOutput("NuspecPackager configuration error: The path to NuGet.exe is not valid: " + itemConfig.NuGetExe); return(false); } //clean up path a little itemConfig.OutputPath = itemConfig.OutputPath.TrimEnd(new[] { '/', '\\' }); if (!Directory.Exists(itemConfig.OutputPath)) { //try to create it WriteOutput("NuspecPackager configuration error: The output directory does not exist, and could not be created."); if (!String.IsNullOrWhiteSpace(itemConfig.OutputPath)) { try { WriteOutput("NuspecPackager configuration warning: Trying to create the output directory: " + itemConfig.OutputPath); Directory.CreateDirectory(itemConfig.OutputPath); } catch { } } if (!Directory.Exists(itemConfig.OutputPath)) { WriteOutput("NuspecPackager configuration error: The output directory does not exist, and could not be created."); return(false); } } return(true); }
/// <summary> /// populate this object's empty properties with values from the source object /// </summary> internal void MergeFrom(NuspecItemConfig source) { if (String.IsNullOrWhiteSpace(this.NuGetExe)) { this.NuGetExe = source.NuGetExe; } if (String.IsNullOrWhiteSpace(this.OutputPath)) { this.OutputPath = source.OutputPath; } }
/// <summary> /// populate this object's empty properties with values from the source object /// </summary> internal void MergeFrom(NuspecItemConfig source) { //if (String.IsNullOrWhiteSpace(this.NuGetExe)) //{ // this.NuGetExe = source.NuGetExe; //} //if (String.IsNullOrWhiteSpace(this.OutputPath)) //{ // this.OutputPath = source.OutputPath; //} var typeInfo = typeof(NuspecItemConfig).GetTypeInfo(); var pis = typeInfo.GetRuntimeProperties(); foreach (var pi in pis) { var currentValue = pi.GetValue(this); var sourceValue = pi.GetValue(source); if (source != null) { pi.SetValue(this, sourceValue); } } }
private bool ValidateOptions(NuspecItemInfo item, NuspecItemConfig itemConfig) { if (!File.Exists(itemConfig.NuGetExe)) { WriteOutput("NuspecPackager configuration error: The path to NuGet.exe is not valid: " + itemConfig.NuGetExe); return false; } //clean up path a little itemConfig.OutputPath = itemConfig.OutputPath.TrimEnd(new[] { '/', '\\' }); if (!Directory.Exists(itemConfig.OutputPath)) { //try to create it WriteOutput("NuspecPackager configuration error: The output directory does not exist, and could not be created."); if (!String.IsNullOrWhiteSpace(itemConfig.OutputPath)) { try { WriteOutput("NuspecPackager configuration warning: Trying to create the output directory: " + itemConfig.OutputPath); Directory.CreateDirectory(itemConfig.OutputPath); } catch { } } if (!Directory.Exists(itemConfig.OutputPath)) { WriteOutput("NuspecPackager configuration error: The output directory does not exist, and could not be created."); return false; } } return true; }
/// <summary> /// package the nuspec file /// </summary> /// <returns>true, if successful</returns> private bool PublishPack(string additionalOptions, NuspecItemInfo item, string pkgFullPath, NuspecItemConfig itemConfig) { WriteOutput($"Uploading nuspec file: {pkgFullPath}"); var startInfo = new ProcessStartInfo(itemConfig.NuGetExe); var publishUrlAppend = (itemConfig.AppendV2ApiTrait ?? false) ? "api/v2/package" : ""; startInfo.Arguments = $"push {pkgFullPath} {itemConfig.RemoteFeedApiKey} -Source {itemConfig.PublishUrl}{publishUrlAppend} {additionalOptions}"; startInfo.RedirectStandardOutput = true; startInfo.RedirectStandardError = true; startInfo.CreateNoWindow = true; startInfo.UseShellExecute = false; startInfo.WindowStyle = ProcessWindowStyle.Hidden; var process = System.Diagnostics.Process.Start(startInfo); process.WaitForExit(); var output = process.StandardOutput.ReadToEnd(); WriteOutput(output); if (process.ExitCode == 0) { WriteOutput("Successfully published nupkg file: " + pkgFullPath); return true; } else { var error = process.StandardError.ReadToEnd(); WriteOutput("Error publish nupkg file: " + pkgFullPath + Environment.NewLine + "ERROR: " + error); return false; } }
/// <summary> /// package the nuspec file /// </summary> /// <returns>true, if successful</returns> private bool Pack(string additionalOptions, NuspecItemInfo item, NuspecItemConfig itemConfig, ref string outputFile) { WriteOutput("Packing nuspec file: " + item.FileName); var startInfo = new ProcessStartInfo(itemConfig.NuGetExe); startInfo.Arguments = $"pack \"{item.FileName}\" -NoDefaultExcludes -OutputDirectory \"{itemConfig.OutputPath}\" {additionalOptions}"; startInfo.RedirectStandardOutput = true; startInfo.RedirectStandardError = true; startInfo.CreateNoWindow = true; startInfo.UseShellExecute = false; startInfo.WindowStyle = ProcessWindowStyle.Hidden; var process = System.Diagnostics.Process.Start(startInfo); process.WaitForExit(); var output = process.StandardOutput.ReadToEnd(); WriteOutput(output); if (process.ExitCode == 0) { var regx = new Regex(@"(')([^']+)\1"); outputFile = regx.Matches(output).Cast<Match>().Select(m => m.Groups[2].Value).Last(); WriteOutput("Successfully packed nuspec file: " + item.FileName); return true; } else { outputFile = null; var error = process.StandardError.ReadToEnd(); WriteOutput("Error packing nuspec file: " + item.FileName + Environment.NewLine + "ERROR: " + error); return false; } }
private NuspecItemConfig GetItemConfig(NuspecItemInfo item) { var dte = (DTE2)GetService(typeof(SDTE)); var optionPage = GetOptionsPage(); //get the config options from VS Options Dialog var defaultConfig = new NuspecItemConfig { NuGetExe = optionPage.NuGetExeDir, OutputPath = optionPage.DefaultOutputPath, PackFromProject = optionPage.PackFromProject, AppendV2ApiTrait = optionPage.AppendV2ApiTrait, RemoteFeedApiKey = optionPage.RemoteFeedApiKey, PublishUrl = optionPage.PublishUrl, UploadToFeed = optionPage.UploadToFeed }; if (!String.IsNullOrEmpty(optionPage.NuGetExeDir)) { //use global config as default nuget exe dir defaultConfig.NuGetExe = Path.Combine(optionPage.NuGetExeDir, "NuGet.exe"); } else { //default path is at same level as item defaultConfig.NuGetExe = Path.Combine(item.Directory, "NuGet.exe"); //if exe not there, then let default path be at .nuget folder at solution level if (!File.Exists(defaultConfig.NuGetExe)) { WriteOutput("Could not find nuget.exe at: " + defaultConfig.NuGetExe); defaultConfig.NuGetExe = Path.Combine(Path.GetDirectoryName(dte.Solution.FullName), ".nuget\\NuGet.exe"); if (!File.Exists(defaultConfig.NuGetExe)) { WriteOutput("Could not find nuget.exe at: " + defaultConfig.NuGetExe); WriteOutput("Create a NuspecPackager.config file or set the NugetExeDir property in the Visual Studio options page to specify a directory where nuget.exe is located."); } } } //get config otions from folder's default config file var folderConfig = Util.GetDirectoryConfig(item); //get config options for this item's config file var itemConfig = Util.GetNuspecItemConfig(item); //merge properties from folder and default into item config's empty properties itemConfig.MergeFrom(folderConfig); itemConfig.MergeFrom(defaultConfig); // TODO: Resolve envrionment variable here. var regx = new Regex(@"\$\(([^)]*)\)"); var envVars = regx.Matches(itemConfig.OutputPath); if (envVars.Count > 0) { WriteOutput($"Environment variable found in the output path. Resolving varables..."); var prj = item.Project; if (prj == null) { WriteOutput($"Failed to find the project {item.ProjectPath}..."); } else { foreach (Match match in envVars) { var varName = match.Groups[1].Value; WriteOutput($"Resolving varable {varName}..."); var varValue = prj.ConfigurationManager.ActiveConfiguration.Properties.Item(varName)?.Value?.ToString(); WriteOutput($"Resolved varable {varName}: {varValue}."); if (!string.IsNullOrEmpty(varValue)) { itemConfig.OutputPath = match.Result(varValue); WriteOutput($"Replaced string: {itemConfig.OutputPath}"); } } } } itemConfig.EnsureAbsolutePaths(item); return itemConfig; }
public static void Serialize(NuspecItemConfig o, Stream stream) { System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(o.GetType()); x.Serialize(stream, o); }
/// <summary> /// package the nuspec file /// </summary> /// <returns>true, if successful</returns> private void Pack(string additionalOptions, NuspecItemInfo item, NuspecItemConfig itemConfig) { WriteOutput("Packing nuspec file: " + item.FileName); var startInfo = new ProcessStartInfo(itemConfig.NuGetExe); startInfo.Arguments = string.Format( "pack \"{0}\" -NoDefaultExcludes -OutputDirectory \"{1}\" {2}", item.FileName, itemConfig.OutputPath, additionalOptions); startInfo.RedirectStandardOutput = true; startInfo.RedirectStandardError = true; startInfo.CreateNoWindow = true; startInfo.UseShellExecute = false; startInfo.WindowStyle = ProcessWindowStyle.Hidden; var process = System.Diagnostics.Process.Start(startInfo); process.WaitForExit(); var output = process.StandardOutput.ReadToEnd(); WriteOutput(output); if (process.ExitCode == 0) { WriteOutput("Successfully packed nuspec file: " + item.FileName); } else { var error = process.StandardError.ReadToEnd(); WriteOutput("Error packing nuspec file: " + item.FileName + Environment.NewLine + "ERROR: " + error); } }
private NuspecItemConfig GetItemConfig(NuspecItemInfo item) { var dte = (DTE2)GetService(typeof(SDTE)); var optionPage = GetOptionsPage(); //get the config options from VS Options Dialog var defaultConfig = new NuspecItemConfig { NuGetExe = optionPage.CustomNuGetExePath, OutputPath = optionPage.DefaultOutputPath }; if (optionPage.UseDefaultNuGetExePath) { defaultConfig.NuGetExe = Path.Combine(Path.GetDirectoryName(dte.Solution.FullName), ".nuget\\NuGet.exe"); } //get config otions from folder's default config file var folderConfig = Util.GetDirectoryConfig(item); //get config options for this item's config file var itemConfig = Util.GetNuspecItemConfig(item); //merge properties from folder and default into item config's empty properties itemConfig.MergeFrom(folderConfig); itemConfig.MergeFrom(defaultConfig); itemConfig.EnsureAbsolutePaths(item); return itemConfig; }
/// <summary> /// package the nuspec file /// </summary> /// <returns>true, if successful</returns> private bool PublishPack(string additionalOptions, NuspecItemInfo item, string pkgFullPath, NuspecItemConfig itemConfig) { WriteOutput($"Uploading nuspec file: {pkgFullPath}"); var startInfo = new ProcessStartInfo(itemConfig.NuGetExe); var publishUrlAppend = (itemConfig.AppendV2ApiTrait ?? false) ? "api/v2/package" : ""; startInfo.Arguments = $"push {pkgFullPath} {itemConfig.RemoteFeedApiKey} -Source {itemConfig.PublishUrl}{publishUrlAppend} {additionalOptions}"; startInfo.RedirectStandardOutput = true; startInfo.RedirectStandardError = true; startInfo.CreateNoWindow = true; startInfo.UseShellExecute = false; startInfo.WindowStyle = ProcessWindowStyle.Hidden; var process = System.Diagnostics.Process.Start(startInfo); process.WaitForExit(); var output = process.StandardOutput.ReadToEnd(); WriteOutput(output); if (process.ExitCode == 0) { WriteOutput("Successfully published nupkg file: " + pkgFullPath); return(true); } else { var error = process.StandardError.ReadToEnd(); WriteOutput("Error publish nupkg file: " + pkgFullPath + Environment.NewLine + "ERROR: " + error); return(false); } }
private NuspecItemConfig GetItemConfig(NuspecItemInfo item) { var dte = (DTE2)GetService(typeof(SDTE)); var optionPage = GetOptionsPage(); //get the config options from VS Options Dialog var defaultConfig = new NuspecItemConfig { NuGetExe = optionPage.NuGetExeDir, OutputPath = optionPage.DefaultOutputPath, PackFromProject = optionPage.PackFromProject, AppendV2ApiTrait = optionPage.AppendV2ApiTrait, RemoteFeedApiKey = optionPage.RemoteFeedApiKey, PublishUrl = optionPage.PublishUrl, UploadToFeed = optionPage.UploadToFeed }; if (!String.IsNullOrEmpty(optionPage.NuGetExeDir)) { //use global config as default nuget exe dir defaultConfig.NuGetExe = Path.Combine(optionPage.NuGetExeDir, "NuGet.exe"); } else { //default path is at same level as item defaultConfig.NuGetExe = Path.Combine(item.Directory, "NuGet.exe"); //if exe not there, then let default path be at .nuget folder at solution level if (!File.Exists(defaultConfig.NuGetExe)) { WriteOutput("Could not find nuget.exe at: " + defaultConfig.NuGetExe); defaultConfig.NuGetExe = Path.Combine(Path.GetDirectoryName(dte.Solution.FullName), ".nuget\\NuGet.exe"); if (!File.Exists(defaultConfig.NuGetExe)) { WriteOutput("Could not find nuget.exe at: " + defaultConfig.NuGetExe); WriteOutput("Create a NuspecPackager.config file or set the NugetExeDir property in the Visual Studio options page to specify a directory where nuget.exe is located."); } } } //get config otions from folder's default config file var folderConfig = Util.GetDirectoryConfig(item); //get config options for this item's config file var itemConfig = Util.GetNuspecItemConfig(item); //merge properties from folder and default into item config's empty properties itemConfig.MergeFrom(folderConfig); itemConfig.MergeFrom(defaultConfig); // TODO: Resolve envrionment variable here. var regx = new Regex(@"\$\(([^)]*)\)"); var envVars = regx.Matches(itemConfig.OutputPath); if (envVars.Count > 0) { WriteOutput($"Environment variable found in the output path. Resolving varables..."); var prj = item.Project; if (prj == null) { WriteOutput($"Failed to find the project {item.ProjectPath}..."); } else { foreach (Match match in envVars) { var varName = match.Groups[1].Value; WriteOutput($"Resolving varable {varName}..."); var varValue = prj.ConfigurationManager.ActiveConfiguration.Properties.Item(varName)?.Value?.ToString(); WriteOutput($"Resolved varable {varName}: {varValue}."); if (!string.IsNullOrEmpty(varValue)) { itemConfig.OutputPath = match.Result(varValue); WriteOutput($"Replaced string: {itemConfig.OutputPath}"); } } } } itemConfig.EnsureAbsolutePaths(item); return(itemConfig); }
private void PackageNuspecFiles(List <NuspecItemInfo> nuspecItems, string additionalOptions = "", bool buildFromProject = false) { Logger.Clear(); WriteOutput("Nuspec Packager starting...", true); var hasErrors = false; try { foreach (var item in nuspecItems) { WriteOutput("Processing nuspec file: " + item.FileName); //get configuration for this nuspec file and make sure it is valid NuspecItemConfig itemConfig = GetItemConfig(item); if (!ValidateOptions(item, itemConfig)) { hasErrors = true; WriteOutput("Skipping nuspec file: " + item.Name); continue; } //build the project var buildSuccess = BuildProject(item); if (!buildSuccess) { hasErrors = true; WriteOutput("Skipping nuspec file: " + item.Name); continue; } var outputPkgPath = ""; if (buildFromProject || (itemConfig.PackFromProject ?? false)) { var actualFileToProcess = item.ProjectPath; WriteOutput("Handling file: " + actualFileToProcess); hasErrors = !Pack(additionalOptions, new NuspecItemInfo() { Project = item.Project, FileName = actualFileToProcess, // item.ProjectItem.Properties.Item("FullPath").Value, ProjectPath = item.ProjectPath, ProjectUniqueName = item.ProjectUniqueName, ProjectName = item.ProjectName }, itemConfig, ref outputPkgPath) || hasErrors; } else { //process the nuspec file and keep track if any errors occur var actualFileToProcess = item.FileName; WriteOutput("Handling file: " + actualFileToProcess); hasErrors = !Pack(additionalOptions, item, itemConfig, ref outputPkgPath) || hasErrors; } WriteOutput($"Trying to upload {outputPkgPath}...{itemConfig.UploadToFeed}"); if ((itemConfig.UploadToFeed ?? false) && !hasErrors && !string.IsNullOrEmpty(outputPkgPath)) { hasErrors = !this.PublishPack(additionalOptions, item, outputPkgPath, itemConfig) || hasErrors; } } } catch (Exception ex) { var message = string.Format(CultureInfo.CurrentCulture, "Exception during NuspecPackagerFiles() of {0}: {1}", this.ToString(), ex.Message); WriteOutput(message); MessageBoxHelper.ShowMessageBox(message, OLEMSGICON.OLEMSGICON_CRITICAL); hasErrors = true; } //display final result var msg = "Nuspec Packager finished " + (hasErrors ? "with errors." : "successfully."); WriteOutput(msg, showInStatus: true); }