private void OnBeforeQueryStatusDynamicItem(object sender, EventArgs args) { CustomScriptItemMenuCommand matchedCommand = (CustomScriptItemMenuCommand)sender; matchedCommand.Enabled = true; matchedCommand.Visible = true; var items = JsonConvert.DeserializeObject <CustomScriptItem[]>(File.ReadAllText(ConfigPath())); var index = matchedCommand.MatchedCommandId - 257; if (index < 0) { index = 0; } matchedCommand.Text = items[index].Name; matchedCommand.Path = items[index].Path; if (!Path.IsPathRooted(matchedCommand.Path)) { matchedCommand.Path = Path.Combine(GetProjectPath(), matchedCommand.Path); } matchedCommand.MatchedCommandId = 0; }
private void OnInvokedDynamicItem(object sender, EventArgs args) { CustomScriptItemMenuCommand invokedCommand = (CustomScriptItemMenuCommand)sender; if (!File.Exists(invokedCommand.Path)) { VsShellUtilities.ShowMessageBox( this.package, $"找不到 {Path.GetFileName(invokedCommand.Path)} 檔案", "找不到腳本路徑", OLEMSGICON.OLEMSGICON_CRITICAL, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST); return; } var proc = System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo() { FileName = invokedCommand.Path, WorkingDirectory = Path.GetDirectoryName(invokedCommand.Path), }); proc.WaitForExit(); if (proc.ExitCode == 0) { VsShellUtilities.ShowMessageBox( this.package, "自訂腳本執行完成", "執行成功", OLEMSGICON.OLEMSGICON_INFO, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST); return; } else { VsShellUtilities.ShowMessageBox( this.package, "自訂腳本執行失敗", "執行失敗", OLEMSGICON.OLEMSGICON_CRITICAL, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST); return; } }
/// <summary> /// Initializes a new instance of the <see cref="CustomScriptCommand"/> class. /// Adds our command handlers for menu (commands must exist in the command table file) /// </summary> /// <param name="package">Owner package, not null.</param> /// <param name="commandService">Command service to add command to, not null.</param> private CustomScriptCommand(AsyncPackage package, OleMenuCommandService commandService) { this.package = package ?? throw new ArgumentNullException(nameof(package)); commandService = commandService ?? throw new ArgumentNullException(nameof(commandService)); var menuCommandID = new CommandID(CommandSet, CommandId); var menuItem = new OleMenuCommand(this.Execute, menuCommandID); commandService.AddCommand(menuItem); dynamicMenuCommand = new CustomScriptItemMenuCommand(new CommandID(CommandSet, CommandId + 1), IsValidDynamicItem, OnInvokedDynamicItem, OnBeforeQueryStatusDynamicItem); commandService.AddCommand(dynamicMenuCommand); dte2 = (DTE2)this.ServiceProvider.GetServiceAsync(typeof(DTE)).GetAwaiter().GetResult(); }