-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
MEService.cs
220 lines (196 loc) · 11.1 KB
/
MEService.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
using System;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Management;
using System.Net.Http;
using System.Reactive;
using System.Reactive.Linq;
using System.Reactive.Threading.Tasks;
using System.Threading;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Model;
using DevExpress.ExpressApp.SystemModule;
using DevExpress.Persistent.Base;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Xpand.Extensions.AppDomainExtensions;
using Xpand.Extensions.BytesExtensions;
using Xpand.Extensions.LinqExtensions;
using Xpand.Extensions.Reactive.Combine;
using Xpand.Extensions.Reactive.Filter;
using Xpand.Extensions.Reactive.Transform;
using Xpand.Extensions.Reactive.Transform.System.Diagnostics;
using Xpand.Extensions.StringExtensions;
using Xpand.XAF.ModelEditor.Module.Win.BusinessObjects;
using Xpand.XAF.Modules.Reactive.Services;
using Xpand.XAF.Modules.Reactive.Services.Controllers;
namespace Xpand.XAF.ModelEditor.Module.Win {
public static class MEService {
static readonly HttpClient HttpClient = new();
static MEService() => HttpClient.DefaultRequestHeaders.Add("User-Agent",nameof(MEService));
internal static IObservable<Unit> Connect(this ApplicationModulesManager manager)
=> manager.WhenApplication(application => Observable.Defer(() => application.DeleteMESettings(true).ShowModelListView().RunME().ToUnit()
.Merge(application.CloseViewWhenNotSettings(MeInstallationPath))))
.Merge(manager.WhenExtendingModel().Do(extenders => extenders.Add<IModelApplication,IModelApplicationME>()).ToUnit());
private static IObservable<XafApplication> RunME(this IObservable<XafApplication> source)
=> source.MergeIgnored(application => application.WhenViewOnFrame().WhenFrame(ViewType.ListView)
.Select(frame => frame.GetController<ListViewProcessCurrentObjectController>())
.SelectMany(controller => controller.WhenCustomProcessSelectedItem(true)
.Do(_ => {
controller.View.ObjectSpace.CommitChanges();
((Window) controller.Frame).Close();
})
.Select(e => e.SelectedObjects.Cast<XafModel>().First())
.Do(model => {
if (model.Project.CannotRunMEMessage != null) {
throw new CannotRunMEException(model.Project.CannotRunMEMessage);
}
})
.SelectMany(xafModel => xafModel.DownloadME(((IModelApplicationME)application.Model).ModelEditor,application)
.StartMEProcess(xafModel))));
internal static XafApplication DeleteMESettings(this XafApplication application,bool setup=false) {
var path = GetMESettingsPath();
if (File.Exists(path)) {
File.Delete(path);
}
if (setup) {
if (File.Exists(GetReadyPath())) {
File.Delete(GetReadyPath());
}
}
return application;
}
internal static IObservable<string> WhenMESettings()
=> Observable.Using(() => new FileSystemWatcher(MeInstallationPath), watcher => {
watcher.EnableRaisingEvents = true;
var meSettingsPath = GetMESettingsPath();
return watcher.WhenEvent<FileSystemWatcher,FileSystemEventArgs>(nameof(watcher.Created)).Where(t => t.args.FullPath == meSettingsPath)
.SelectMany(_ => Observable.Defer(() => $"{JsonConvert.DeserializeObject<dynamic>(File.ReadAllText(meSettingsPath))?.Solution}".ReturnObservable()).Retry())
.Merge("".ReturnObservable().Do(_ => File.CreateText(GetReadyPath())).IgnoreElements());
})
.ObserveOn(SynchronizationContext.Current!)
.TraceModelEditorWindowsFormsModule();
internal static string GetReadyPath() => $"{AppDomain.CurrentDomain.ApplicationPath()}\\Ready.txt";
internal static string GetMESettingsPath() => $"{MeInstallationPath}\\MESettings.json";
private static IObservable<string> DownloadME(this XafModel xafModel, IModelME modelME, XafApplication application) {
var assemblyPath = xafModel.Project.AssemblyPath;
if (!File.Exists(assemblyPath)) {
throw new UserFriendlyException($"File {assemblyPath} not found, please build the project");
}
var versionsGroup = Directory.GetFiles(Path.GetDirectoryName(assemblyPath)!,"DevExpress.ExpressApp*.dll")
.Where(s => {
var fileName = Path.GetFileName(s);
return fileName.StartsWith("DevExpress.ExpressApp")&&!fileName.Contains("CodeAnalysis");
})
.GroupBy(s => Version.Parse(FileVersionInfo.GetVersionInfo(s).FileVersion!)).ToArray();
if (versionsGroup.Length > 1){
var conflicts = versionsGroup.SelectMany(grouping => grouping.Take(1).Select(path => (name: Path.GetFileName(path),
version: Version.Parse(FileVersionInfo.GetVersionInfo(path).FileVersion!)))).JoinCommaSpace();
throw new UserFriendlyException($"Multiple DevExpress versions found in {assemblyPath} (${conflicts})");
}
if (!versionsGroup.Any()) {
throw new UserFriendlyException($"Cannot find any DevExpress assembly in {Path.GetDirectoryName(assemblyPath)}");
}
var dxVersion = versionsGroup.First().Key;
if (new Uri(modelME.DownloadUrl).IsFile) {
return dxVersion.ReturnObservable().DownloadME(xafModel, modelME);
}
return dxVersion.RXXafReleaseVersion(modelME.DownloadPreRelease, modelME.NoFoundVersion())
.Do( version => application.ShowViewStrategy.ShowMessage($"Latest release is {version} downloading..."))
.DownloadME(xafModel,modelME);
}
private static IObservable<string> DownloadME(this IObservable<Version> source, XafModel xafModel, IModelME modelME)
=> source.SelectMany(version => {
var meType = "WinDesktop";
if (xafModel.Project.TargetFramework.StartsWith("net4")) {
meType = "Win";
}
var directory = $"{MeInstallationPath}\\{meType}\\{version}\\";
string filename = $"Xpand.XAF.ModelEditor.{meType}.exe";
var path = $"{directory}{filename}";
if (!File.Exists(path)) {
if (!Directory.Exists(directory)) {
Directory.CreateDirectory(directory);
}
return modelME.DownloadUrl.StringFormat($"{version}", meType).ReturnObservable()
.TraceModelEditorWindowsFormsModule(s => $"Download {s}")
.SelectMany(url => MEBytes(url)
.ObserveOn(SynchronizationContext.Current!)
.Do(bytes => {
var pathToZip = $"{directory}Xpand.XAF.ModelEditor.{meType}.zip";
bytes.Save(pathToZip);
using var archive = ZipFile.OpenRead(pathToZip);
archive.ExtractToDirectory(directory,true);
}).To(path));
}
return path.ReturnObservable();
});
private static IObservable<byte[]> MEBytes(string url) {
var uri = new Uri(url);
if (uri.IsFile) {
url = uri.LocalPath;
}
if (File.Exists(url)) {
return File.ReadAllBytes(url).ReturnObservable();
}
return HttpClient.GetByteArrayAsync(url).ToObservable();
}
private static Func<Version, IObservable<Version>> NoFoundVersion(this IModelME modelME)
=> dxVersion =>modelME.DevVersion==null? Observable.Throw<Version>(new VersionNotFoundException(
$"Version {dxVersion} not on GitHub. Consider the ${nameof(IModelME.DownloadPreRelease)}, or the ({nameof(IModelME.DevVersion)}, {nameof(IModelME.DownloadUrl)}) model attributes")):modelME.DevVersion.ReturnObservable();
private static IObservable<Version> RXXafReleaseVersion(this Version dxVersion, bool preRelease, Func<Version,IObservable<Version>> noFound)
=> HttpClient.GetStringAsync("https://api.github.com/repos/expandframework/reactive.xaf/tags").ToObservable()
.SelectMany(s => JsonConvert.DeserializeObject<JArray>(s)!.Select(token => Version.Parse($"{token["name"]}")))
.FirstAsync(version => (version.Revision == 0 && !preRelease)||preRelease)
.Where(version => $"{version.Minor}" == $"{dxVersion.Major}{dxVersion.Minor}")
.SwitchIfEmpty(noFound(dxVersion))
.FirstAsync()
.ObserveOn(SynchronizationContext.Current!)
.TraceModelEditorWindowsFormsModule(version => $"Locating {dxVersion} release = {version}");
public static string MeInstallationPath =>
$"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\\Xpand.XAF.ModelEditor.Win";
static IObservable<Unit> StartMEProcess(this IObservable<string> source, XafModel xafModel)
=> source.SelectMany(mePath => {
KillProcess(mePath);
return xafModel.StartME(xafModel.Project.Path, mePath);
}).ToUnit();
private static IObservable<Unit> StartME(this XafModel xafModel, string fullPath, string destFileName) {
string debugMe = xafModel.Debug ? "d" : null;
string arguments = String.Format("{0} {4} \"{1}\" \"{3}\" \"{2}\"", debugMe, xafModel.Project.AssemblyPath,
fullPath, xafModel.Path, xafModel.Project.IsApplicationProject);
var process = new Process() {
StartInfo = new ProcessStartInfo(destFileName,arguments) {
WorkingDirectory = Path.GetDirectoryName(destFileName)!
}
};
Tracing.Tracer.LogSeparator("Xpand Model Editor");
Tracing.Tracer.LogValue("MEPath",destFileName);
Tracing.Tracer.LogValue("MEArgs",arguments);
process.StartWithEvents();
return process.WhenErrorDataReceived().WhenNotDefault()
.Do(s => throw new Exception(s)).ToUnit();
}
static void KillProcess(string path){
const string wmiQueryString = "SELECT ProcessId, ExecutablePath, CommandLine FROM Win32_Process";
using var searcher = new ManagementObjectSearcher(wmiQueryString);
using var results = searcher.Get();
var query = Process.GetProcesses()
.Join(results.Cast<ManagementObject>(), p => p.Id, mo => (int) (uint) mo["ProcessId"],
(p, mo) => new{
Process = p,
Path = (string) mo["ExecutablePath"],
CommandLine = (string) mo["CommandLine"],
});
foreach (var item in query.Where(arg => arg.Path==path)) {
item.Process.Kill();
}
}
}
internal class CannotRunMEException : UserFriendlyException {
public CannotRunMEException(string message):base(message) { }
}
}