Explorar o código

Added Script Checking window to Support screen

Kenric Nugteren hai 4 días
pai
achega
4ff1b7f7aa
Modificáronse 1 ficheiros con 142 adicións e 1 borrados
  1. 142 1
      prs.desktop/Forms/Issues/IssuesGrid.cs

+ 142 - 1
prs.desktop/Forms/Issues/IssuesGrid.cs

@@ -13,6 +13,9 @@ using System.Windows.Controls;
 using System.Windows.Media;
 using InABox.Wpf;
 using System.Windows;
+using InABox.Clients;
+using InABox.Scripting;
+using Microsoft.Win32;
 
 namespace PRSDesktop.Forms.Issues;
 
@@ -118,8 +121,146 @@ public class IssuesGrid : DynamicGrid<Kanban>, ISpecificGrid
 
         if (File.Exists(Path.Combine(_baseDirectory, "PRSDigitalKey", "PRS.DigitalKey.Desktop.exe")))
             AddButton("PRS Digital Key App", PRSDesktop.Resources.key.AsBitmapImage(), LaunchPRSDigitalKey);
+
+        var scriptButton = AddButton("Check Script Status", PRSDesktop.Resources.edit.AsBitmapImage(), CheckScriptStatus);
+        scriptButton.Margin = new Thickness(20, scriptButton.Margin.Top, scriptButton.Margin.Right, scriptButton.Margin.Bottom);
     }
-    
+
+    private bool CheckScriptStatus(Button button, CoreRow[] rows)
+    {
+        var showHidden = MessageWindow.ShowYesNo("Include Hidden Scripts?", "Confirm");
+
+        var results = new List<Tuple<CustomModule, string>>();
+        Progress.ShowModal("Loading Data", progress =>
+        {
+            var filter = showHidden
+                ? Filter.All<CustomModule>()
+                : Filter<CustomModule>.Where(x => x.Visible).IsEqualTo(true);
+
+            var modules = Client.Query(
+                filter,
+                Columns.None<CustomModule>()
+                    .Add(x => x.ID)
+                    .Add(x => x.Section)
+                    .Add(x => x.Name)
+                    .Add(x => x.Script))
+                .ToArray<CustomModule>();
+            foreach(var module in modules)
+            {
+                try
+                {
+                    progress.Report($"{module.Section} -> {module.Name}");
+
+                    var scriptDocument = new ScriptDocument(module.Script);
+                    if (!scriptDocument.Compile())
+                    {
+                        var errors = scriptDocument.Result.Split(Environment.NewLine);
+                        results.Add(new(module, string.Join('\n', errors.Select(x => $"- {x}"))));
+                    }
+                }
+                catch(Exception e)
+                {
+                    results.Add(new(module, e.Message));
+                }
+            }
+        });
+
+        if (results.Any())
+        {
+            var grid = new Grid();
+            grid.AddRow(GridUnitType.Auto);
+            grid.AddRow(GridUnitType.Star);
+            grid.AddRow(GridUnitType.Auto);
+            grid.AddChild(new Label
+            {
+                Content = "The following errors were found in custom modules:"
+            }, 0, 0);
+
+            var list = new ListBox();
+            foreach(var (module, errors) in results)
+            {
+                var itemGrid = new Grid
+                {
+
+                };
+                itemGrid.AddColumn(GridUnitType.Auto);
+                itemGrid.AddColumn(GridUnitType.Star);
+                var itemBtn = new Button
+                {
+                    Content = new Image
+                    {
+                        Source = PRSDesktop.Resources.pencil.AsBitmapImage(),
+                    },
+                    Width = 30,
+                    Height = 30,
+                    Padding = new(5),
+                    Margin = new(0, 0, 5, 0),
+                    Tag = module
+                };
+                itemBtn.VerticalAlignment = VerticalAlignment.Top;
+                itemBtn.Click += ModuleOpen_Click;
+                itemGrid.AddChild(itemBtn, 0, 0);
+                itemGrid.AddChild(new Label
+                {
+                    Content = $"{module.Section}/{module.Name}:\n{errors}"
+                }, 0, 1);
+                list.Items.Add(itemGrid);
+            }
+            grid.AddChild(list, 1, 0);
+
+            var dockPanel = new DockPanel
+            {
+                LastChildFill = false
+            };
+
+            var exportButton = new Button
+            {
+                Content = "Export",
+                Padding = new(5),
+                Margin = new(0, 5, 0, 0)
+            };
+            exportButton.Click += (o, e) =>
+            {
+                var result = string.Join("\n\n", results.Select(x => $"{x.Item1.Section}/{x.Item1.Name}:\n{x.Item2}"));
+                var dlg = new SaveFileDialog()
+                {
+                    Filter = "Text Files (*.txt)|*.txt"
+                };
+
+                if(dlg.ShowDialog() == true)
+                {
+                    using var writer = new StreamWriter(dlg.FileName);
+                    writer.Write(result);
+                }
+            };
+            DockPanel.SetDock(exportButton, Dock.Left);
+            dockPanel.Children.Add(exportButton);
+
+            grid.AddChild(dockPanel, 2, 0);
+
+            var window = new DynamicContentDialog(grid, buttonsVisible: false)
+            {
+                Title = "Custom Module Errors"
+            };
+            window.ShowDialog();
+        }
+
+        return false;
+    }
+
+    private void ModuleOpen_Click(object sender, RoutedEventArgs e)
+    {
+        if (sender is not FrameworkElement element
+            || element.Tag is not CustomModule module) return;
+
+        var editor = new ScriptEditorWindow(module.Script, scriptTitle: $"{module.Section}/{module.Name}");
+        if (editor.ShowDialog() == true)
+        {
+            module.Script = editor.Script;
+            Client.Save(module, "Updated by User");
+        }
+    }
+
     private bool LaunchPRSMobile(Button button, CoreRow[] rows)
     {
         var _mobileApp = System.IO.Path.Combine(_baseDirectory, "PRSAvalonia", "PRS.Avalonia.Desktop.exe");