Пример #1
0
        private void CommandsWindow_Loaded(object sender, RoutedEventArgs e)
        {
            string initialValue = null;

            if (Model.AddinConfiguration.Commands.Count > 0)
            {
                CommanderCommand selectedItem = null;
                if (!string.IsNullOrEmpty(CommanderAddinConfiguration.Current.LastCommand))
                {
                    selectedItem =
                        Model.AddinConfiguration.Commands.FirstOrDefault(
                            cmd => cmd.Name == Model.AddinConfiguration.LastCommand);
                }
                else
                {
                    selectedItem = Model.AddinConfiguration.Commands[0];
                }

                ListCommands.SelectedItem = selectedItem;
                if (selectedItem != null)
                {
                    initialValue = selectedItem.CommandText;
                }
            }

            editor = new MarkdownEditorSimple(WebBrowserCommand, initialValue);
            editor.IsDirtyAction = () =>
            {
                string val = editor.GetMarkdown();
                if (val != null && Model.ActiveCommand != null)
                {
                    Model.ActiveCommand.CommandText = val;
                }

                return(true);
            };

            Dispatcher.InvokeAsync(() =>
            {
                ListCommands.Focus();
                editor.SetEditorSyntax("csharp");
            }, System.Windows.Threading.DispatcherPriority.ApplicationIdle);

            pageLoaded = true;
        }
        private async void CommandsWindow_Loaded(object sender, RoutedEventArgs e)
        {
            await Task.Delay(1);

            string initialValue = null;

            if (Model.AddinConfiguration.Commands.Count > 0)
            {
                CommanderCommand selectedItem = null;
                if (!string.IsNullOrEmpty(CommanderAddinConfiguration.Current.LastCommand))
                {
                    selectedItem =
                        Model.AddinConfiguration.Commands.FirstOrDefault(
                            cmd => cmd.Name == Model.AddinConfiguration.LastCommand);
                }
                else
                {
                    selectedItem = Model.AddinConfiguration.Commands[0];
                }

                ListCommands.SelectedItem = selectedItem;
                if (selectedItem != null)
                {
                    initialValue = selectedItem.CommandText;
                }
            }

            editor = new MarkdownEditorSimple(WebBrowserCommand, initialValue, "csharp");
            editor.ShowLineNumbers = true;
            editor.IsDirtyAction   = (isDirty, newText, oldText) =>
            {
                if (newText != null && Model.ActiveCommand != null)
                {
                    Model.ActiveCommand.CommandText = newText;
                }

                return(newText != oldText);
            };

            Dispatcher.Invoke(() => ListCommands.Focus(), DispatcherPriority.ApplicationIdle);

            pageLoaded = true;
        }
        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);
            }
        }