-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMxLintPaneExtensionWebViewModel.cs
160 lines (133 loc) · 4.8 KB
/
MxLintPaneExtensionWebViewModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using Mendix.StudioPro.ExtensionsAPI.Model;
using Mendix.StudioPro.ExtensionsAPI.Model.Projects;
using Mendix.StudioPro.ExtensionsAPI.Services;
using Mendix.StudioPro.ExtensionsAPI.UI.DockablePane;
using Mendix.StudioPro.ExtensionsAPI.UI.Services;
using Mendix.StudioPro.ExtensionsAPI.UI.WebView;
using System.Text.Json.Nodes;
namespace com.cinaq.MxLintExtension;
public class MxLintPaneExtensionWebViewModel : WebViewDockablePaneViewModel
{
private readonly Uri _baseUri;
private readonly Func<IModel?> _getCurrentApp;
private readonly ILogService _logService;
private readonly IDockingWindowService _dockingWindowService;
private DateTime _lastUpdateTime;
private IWebView? _webView; // Change 1: Make _webView nullable
public MxLintPaneExtensionWebViewModel(Uri baseUri, Func<IModel?> getCurrentApp, ILogService logService, IDockingWindowService dockingWindowService)
{
_baseUri = baseUri;
_getCurrentApp = getCurrentApp;
_dockingWindowService = dockingWindowService;
_logService = logService;
_lastUpdateTime = DateTime.Now.AddYears(-100); // force refresh on first run
}
public override void InitWebView(IWebView webView)
{
_webView = webView;
webView.Address = new Uri(_baseUri, "index.html");
_logService.Info($"InitWebView: {_baseUri}");
webView.MessageReceived += HandleWebViewMessage;
//webView.ShowDevTools();
}
private async void HandleWebViewMessage(object? sender, MessageReceivedEventArgs args) // Change 2: Make sender nullable
{
var currentApp = _getCurrentApp();
if (currentApp == null) return;
if (args.Message == "refreshData")
{
await Refresh(currentApp);
}
if (args.Message == "toggleDebug")
{
_webView?.ShowDevTools();
}
if (args.Message == "openDocument")
{
_webView?.PostMessage("documentOpened");
await OpenDocument(currentApp, args.Data);
}
}
private async Task<bool> OpenDocument(IModel currentApp, JsonObject data)
{
var doc = GetUnit(currentApp, data);
if (doc == null)
{
_logService.Error($"Document not found: {data}");
return false;
}
_dockingWindowService.TryOpenEditor(doc, null);
return true;
}
private IAbstractUnit? GetUnit(IModel currentApp, JsonObject data)
{
_logService.Info($"Looking up document: {data}");
var documentName = data["document"].ToString();
if (documentName == "Security$ProjectSecurity")
{
return null;
}
var moduleName = data["module"].ToString();
var module = currentApp.Root.GetModules().Single(m => m.Name == moduleName);
if (module == null)
{
_logService.Error($"Module not found: {moduleName}");
return null;
}
if (documentName == "DomainModels$DomainModel")
{
return module.DomainModel;
}
IFolder folder = null;
while (documentName.Contains("/"))
{
var tokens = documentName.Split("/");
var folderName = tokens[0];
if (folder == null)
{
folder = module.GetFolders().FirstOrDefault(f => f.Name == folderName);
}
else
{
folder = folder.GetFolders().FirstOrDefault(f => f.Name == folderName);
}
documentName = documentName.Substring(folderName.Length + 1);
}
if (folder == null)
{
return module.GetDocuments().FirstOrDefault(d => d.Name == documentName);
}
else
{
return folder.GetDocuments().FirstOrDefault(d => d.Name == documentName);
}
}
private async Task<bool> Refresh(IModel currentApp)
{
var mprFile = GetMprFile(currentApp.Root.DirectoryPath);
if (mprFile == null) return false;
var lastWrite = File.GetLastWriteTime(mprFile);
if (lastWrite <= _lastUpdateTime)
{
_logService.Debug("No changes detected");
return false;
}
_webView?.PostMessage("start");
_lastUpdateTime = lastWrite;
_logService.Info($"Changes detected: {_lastUpdateTime}");
var cmd = new MxLint(currentApp, _logService);
await cmd.Lint();
_webView?.PostMessage("end");
_webView?.PostMessage("refreshData");
return true;
}
private string? GetMprFile(string directoryPath)
{
var mprFile = Directory.GetFiles(directoryPath, "*.mpr", SearchOption.TopDirectoryOnly).FirstOrDefault();
if (mprFile == null)
{
_logService.Error("No mpr file found");
}
return mprFile;
}
}