public async Task RunCommand(CommanderCommand command) { if (AddinModel.AppModel == null) { InitializeAddinModel(); } string code = command.CommandText; ConsoleTextWriter consoleWriter = null; bool showConsole = commanderWindow != null && commanderWindow.Visibility == Visibility.Visible; if (showConsole) { var tbox = commanderWindow.TextConsole; tbox.Clear(); WindowUtilities.DoEvents(); consoleWriter = new ConsoleTextWriter() { tbox = tbox }; Console.SetOut(consoleWriter); } AddinModel.AppModel.Window.ShowStatusProgress("Executing Command '" + command.Name + "' started..."); var parser = new ScriptParser(); bool result = await parser.EvaluateScriptAsync(code, AddinModel); if (!result) { var msg = parser.ErrorMessage; if (parser.ScriptInstance.ErrorType == Westwind.Scripting.ExecutionErrorTypes.Compilation) { msg = "Script compilation error."; } AddinModel.AppModel.Window.ShowStatusError("Command '" + command.Name + "' execution failed: " + msg); if (showConsole) { if (parser.ScriptInstance.ErrorType == Westwind.Scripting.ExecutionErrorTypes.Compilation) { var adjusted = ScriptParser.FixupLineNumbersAndErrors(parser.ScriptInstance); parser.ErrorMessage = adjusted.UpdatedErrorMessage; Console.WriteLine($"*** Script Compilation Errors:\n{parser.ErrorMessage}\n"); Console.WriteLine("\n*** Generated Code:"); Console.WriteLine(parser.ScriptInstance.GeneratedClassCode); } else { Console.WriteLine($"*** Runtime Execution Error:\n{parser.ErrorMessage}\n"); } } } else { if (mmApp.Model.Window.StatusText.Text.StartsWith("Executing Command ")) { AddinModel.AppModel.Window.ShowStatusSuccess("Command '" + command.Name + "' executed successfully"); } } if (showConsole) { consoleWriter.Close(); commanderWindow.TextConsole.ScrollToHome(); StreamWriter standardOutput = new StreamWriter(Console.OpenStandardOutput()); standardOutput.AutoFlush = true; Console.SetOut(standardOutput); } }
public void RunCommand(CommanderCommand command) { if (AddinModel.AppModel == null) { InitializeAddinModel(); } string code = command.CommandText; bool showConsole = commanderWindow != null && commanderWindow.Visibility == Visibility.Visible; if (showConsole) { var tbox = commanderWindow.TextConsole; tbox.Clear(); WindowUtilities.DoEvents(); Console.SetOut(new ConsoleTextWriter() { tbox = tbox }); } var parser = new ScriptParser(); if (!parser.EvaluateScript(code, AddinModel)) { if (!showConsole) { AddinModel.Window.ShowStatus("*** Addin execution failed: " + parser.ErrorMessage, 6000); AddinModel.Window.SetStatusIcon(FontAwesomeIcon.Warning, Colors.Red); } else { Console.WriteLine($"*** Error running Script code:\r\n{parser.ErrorMessage}"); } if (CommanderAddinConfiguration.Current.OpenSourceInEditorOnErrors) { string fname = Path.Combine(Path.GetTempPath(), "Commander_Compiled_Code.cs"); File.WriteAllText(fname, parser.ScriptInstance.SourceCode); var tab = OpenTab(fname); File.Delete(fname); if (tab != null) { var editor = tab.Tag as MarkdownDocumentEditor; editor.SetEditorSyntax("csharp"); editor.SetMarkdown(parser.ScriptInstance.SourceCode); Dispatcher.CurrentDispatcher.InvokeAsync(() => { if (editor.AceEditor == null) { Task.Delay(400); } editor.AceEditor.setshowlinenumbers(true); if (commanderWindow == null) { commanderWindow = new CommanderWindow(this); commanderWindow.Show(); } else { commanderWindow.Activate(); } }, DispatcherPriority.ApplicationIdle); } } } else { AddinModel.Window.ShowStatus("Command execution for " + command.Name + " completed successfully", 6000); } if (showConsole) { commanderWindow.TextConsole.ScrollToHome(); StreamWriter standardOutput = new StreamWriter(Console.OpenStandardOutput()); standardOutput.AutoFlush = true; Console.SetOut(standardOutput); } }