private static void Spec(string projectPath) { var process = new Process { StartInfo = new ProcessStartInfo { UseShellExecute = false, FileName = Nuget, CreateNoWindow = true, RedirectStandardOutput = true, WorkingDirectory = projectPath, Arguments = "spec -Force -NonInteractive" } }; CommandOutput.WriteLine("开始调用nuget spec -Force -NonInteractive"); process.Start(); var output = process.StandardOutput.ReadToEnd(); process.WaitForExit(); if (process.ExitCode != 0) { throw new Exception($"nuget spec失败,项目:{projectPath},结果:{output}"); } CommandOutput.WriteLine("调用nuget spec完成"); }
private async Task RunAsync() { await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); var dte = (DTE)await this.ServiceProvider.GetServiceAsync(typeof(DTE)); if (dte.Solution == null || dte.Solution.Projects.Count == 0) { return; } var solutionRoot = Path.GetDirectoryName(dte.Solution.FullName); NuspecConfiguration.SolutionRoot = solutionRoot; if (!NuspecConfiguration.Load()) { CommandOutput.WriteLine("首次运行,已在解决方案根目录创建 nuspec.config,请更新全局配置后再次运行。"); return; } var projects = Directory.GetFiles(solutionRoot, "*.csproj", SearchOption.AllDirectories); var count = projects.Length; var i = 1; foreach (var fullName in projects) { var name = Path.GetFileNameWithoutExtension(fullName); CommandOutput.WriteLine($"{i}/{count}: 开始更新项目{fullName}"); var projectPath = Path.GetDirectoryName(fullName); NuspecConfiguration.Make(name, projectPath); CommandOutput.WriteLine(); i++; } }
/// <summary> /// This function is the callback used to execute the command when the menu item is clicked. /// See the constructor to see how the menu item is associated with this function using /// OleMenuCommandService service and MenuCommand class. /// </summary> /// <param name="sender">Event sender.</param> /// <param name="e">Event args.</param> private void Execute(object sender, EventArgs e) { ThreadHelper.ThrowIfNotOnUIThread(); CommandOutput.Output.Clear(); CommandOutput.WriteLine("开始更新.nuspec文件"); CommandOutput.WriteLine(); try { ThreadHelper.JoinableTaskFactory.Run(RunAsync); CommandOutput.WriteLine("创建/更新 .nuspec 完成"); } catch (Exception ex) { CommandOutput.WriteLine($"操作失败:{ex.Message}"); } }
public static void Make(string projectName, string projectPath) { if (_nuspecOption.MatchIgnore(projectName, projectPath)) { CommandOutput.WriteLine($"{projectName} 满足排除条件,跳过"); return; } var nuspecFile = Path.Combine(projectPath, projectName + ".nuspec"); var newNuspec = false; if (!File.Exists(nuspecFile)) { CommandOutput.WriteLine($"{nuspecFile}不存在,开始生成"); Spec(projectPath); newNuspec = true; } else { CommandOutput.WriteLine($"{nuspecFile}已经存在,开始更新依赖包"); } var document = new XmlDocument(); CommandOutput.WriteLine($"开始使用全局配置更新: {nuspecFile}"); document.Load(nuspecFile); var metadata = document["package"]["metadata"]; if (newNuspec) { CommandOutput.WriteLine("为新创建的nuspec文件,更新全局配置"); var metadataNodes = document["package"]["metadata"].ChildNodes; foreach (XmlElement el in metadataNodes) { if (_nuspecOption.Global.ContainsKey(el.LocalName)) { el.InnerText = _nuspecOption.Global[el.LocalName]; } } } var dependList = metadata.GetElementsByTagName("dependencies"); var pkgList = GetDependencies(projectPath); if (pkgList.Count == 0) { CommandOutput.WriteLine("没有第三方依赖包"); if (dependList.Count != 0) { metadata.RemoveChild(dependList[0]); } } else { XmlElement dependencies; if (dependList.Count == 0) { dependencies = document.CreateElement("dependencies"); } else { dependencies = (XmlElement)dependList[0]; dependencies.RemoveAll(); } foreach (var net in pkgList) { var group = document.CreateElement("group"); group.SetAttribute("targetFramework", net.Key); // .NETStandard2.0 CommandOutput.WriteLine($"添加targetFramework:{net.Key}"); foreach (var nugetPkg in net.Value) { CommandOutput.WriteLine($"添加{nugetPkg.Key}到{net.Key}"); var dependency = document.CreateElement("dependency"); dependency.SetAttribute("id", nugetPkg.Key); dependency.SetAttribute("version", nugetPkg.Value); dependency.SetAttribute("exclude", "Build,Analyzers"); group.AppendChild(dependency); } dependencies.AppendChild(group); } metadata.AppendChild(dependencies); } document.Save(nuspecFile); CommandOutput.WriteLine("更新完成"); }