forked from dotnet/ResXResourceManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainViewModel.cs
More file actions
369 lines (303 loc) · 13.2 KB
/
Copy pathMainViewModel.cs
File metadata and controls
369 lines (303 loc) · 13.2 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
namespace ResXManager
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using JetBrains.Annotations;
using Microsoft.WindowsAPICodePack.Dialogs;
using ResXManager.Infrastructure;
using ResXManager.Model;
using ResXManager.Properties;
using ResXManager.View.Tools;
using ResXManager.View.Visuals;
using TomsToolbox.Wpf;
using TomsToolbox.Wpf.Composition.Mef;
[VisualCompositionExport(RegionId.Main)]
internal class MainViewModel : ObservableObject
{
private readonly ITracer _tracer;
private readonly Configuration _configuration;
private readonly ResourceViewModel _resourceViewModel;
private readonly PowerShellProcessor _powerShell;
[ImportingConstructor]
public MainViewModel([NotNull] ResourceManager resourceManager, [NotNull] Configuration configuration, [NotNull] ResourceViewModel resourceViewModel, [NotNull] ITracer tracer, [NotNull] SourceFilesProvider sourceFilesProvider)
{
ResourceManager = resourceManager;
_configuration = configuration;
_resourceViewModel = resourceViewModel;
_tracer = tracer;
_powerShell = new PowerShellProcessor(tracer);
SourceFilesProvider = sourceFilesProvider;
resourceManager.BeginEditing += ResourceManager_BeginEditing;
resourceManager.Reloading += ResourceManager_Reloading;
resourceManager.ProjectFileSaved += ResourceManager_ProjectFileSaved;
try
{
var folder = Settings.Default.StartupFolder;
if (string.IsNullOrEmpty(folder))
return;
SourceFilesProvider.SolutionFolder = folder;
if (Directory.Exists(folder))
{
Load();
}
}
catch (Exception ex)
{
_tracer.TraceError(ex.ToString());
MessageBox.Show(ex.Message);
}
}
private async void ResourceManager_ProjectFileSaved(object sender, ProjectFileEventArgs e)
{
var solutionFolder = SourceFilesProvider.SolutionFolder;
if (string.IsNullOrEmpty(solutionFolder))
return;
var scriptFile = Path.Combine(solutionFolder, "Resources.ps1");
if (!File.Exists(scriptFile))
return;
await _powerShell.Run(solutionFolder, scriptFile);
}
[NotNull]
public ICommand BrowseCommand => new DelegateCommand(Browse);
[NotNull]
public ResourceManager ResourceManager { get; }
[NotNull]
public SourceFilesProvider SourceFilesProvider { get; }
private void Browse()
{
var settings = Settings.Default;
try
{
using (var dlg = new CommonOpenFileDialog { IsFolderPicker = true, InitialDirectory = settings.StartupFolder, EnsurePathExists = true })
{
if (dlg.ShowDialog() != CommonFileDialogResult.Ok)
return;
SourceFilesProvider.SolutionFolder = settings.StartupFolder = dlg.FileName;
Load();
return;
}
}
catch (NotSupportedException)
{
// CommonOpenFileDialog not supported on current platform.
}
using (var dlg = new System.Windows.Forms.FolderBrowserDialog { SelectedPath = Settings.Default.StartupFolder })
{
if (dlg.ShowDialog() != System.Windows.Forms.DialogResult.OK)
return;
SourceFilesProvider.SolutionFolder = Settings.Default.StartupFolder = dlg.SelectedPath;
Load();
}
}
private void Load()
{
try
{
_resourceViewModel.Reload();
}
catch (Exception ex)
{
_tracer.TraceError(ex.ToString());
MessageBox.Show(ex.Message);
}
}
private void ResourceManager_Reloading([NotNull] object sender, [NotNull] CancelEventArgs e)
{
if (!ResourceManager.HasChanges)
return;
if (MessageBoxResult.Yes == MessageBox.Show(Resources.WarningUnsavedChanges, View.Properties.Resources.Title, MessageBoxButton.YesNo, MessageBoxImage.Hand, MessageBoxResult.No))
return;
e.Cancel = true;
}
private void ResourceManager_BeginEditing([NotNull] object sender, [NotNull] ResourceBeginEditingEventArgs e)
{
if (!CanEdit(e.Entity, e.CultureKey))
{
e.Cancel = true;
}
}
private bool CanEdit([NotNull] ResourceEntity entity, [CanBeNull] CultureKey cultureKey)
{
string message;
var languages = entity.Languages.Where(lang => (cultureKey == null) || cultureKey.Equals(lang.CultureKey)).ToArray();
var rootFolder = SourceFilesProvider.SolutionFolder;
if (string.IsNullOrEmpty(rootFolder))
return false;
if (!languages.Any())
{
try
{
var culture = cultureKey?.Culture;
if (culture == null)
return false; // no neutral culture => this should never happen.
if (_configuration.ConfirmAddLanguageFile)
{
message = string.Format(CultureInfo.CurrentCulture, Resources.ProjectHasNoResourceFile, culture.DisplayName);
if (MessageBox.Show(message, View.Properties.Resources.Title, MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
return false;
}
var neutralLanguage = entity.Languages.FirstOrDefault();
if (neutralLanguage == null)
return false;
var languageFileName = neutralLanguage.ProjectFile.GetLanguageFileName(culture);
if (!File.Exists(languageFileName))
{
var directoryName = Path.GetDirectoryName(languageFileName);
if (!string.IsNullOrEmpty(directoryName))
Directory.CreateDirectory(directoryName);
File.WriteAllText(languageFileName, Model.Properties.Resources.EmptyResxTemplate);
}
entity.AddLanguage(new ProjectFile(languageFileName, rootFolder, entity.ProjectName, null));
return true;
}
catch (Exception ex)
{
MessageBox.Show(string.Format(CultureInfo.CurrentCulture, View.Properties.Resources.ErrorAddingNewResourceFile, ex), "ResX Resource Manager");
}
}
else
{
var lockedFiles = languages.Where(l => !l.ProjectFile.IsWritable).Select(l => l.FileName).ToArray();
if (!lockedFiles.Any())
return true;
message = string.Format(CultureInfo.CurrentCulture, Resources.ProjectHasReadOnlyFiles, FormatFileNames(lockedFiles));
MessageBox.Show(message);
}
return false;
}
[NotNull]
[Localizable(false)]
private static string FormatFileNames([NotNull][ItemNotNull] IEnumerable<string> lockedFiles)
{
return string.Join("\n", lockedFiles.Select(x => "\xA0-\xA0" + x));
}
private class PowerShellProcessor
{
private readonly ITracer _tracer;
[CanBeNull]
private readonly string _powerShellPath = FindInPath("PowerShell.exe");
private bool _isConverting;
private bool _isConversionRequestPending;
// ReSharper disable once AssignNullToNotNullAttribute
private readonly string _workingDirectory = Path.GetDirectoryName(typeof(Scripting.Host).Assembly.Location);
public PowerShellProcessor(ITracer tracer)
{
_tracer = tracer;
}
public async Task Run(string solutionFolder, string scriptFile)
{
if (_powerShellPath == null)
return;
if (_isConverting)
{
_isConversionRequestPending = true;
return;
}
_isConverting = true;
while (true)
{
try
{
await Task.Run(() =>
{
var startInfo = new ProcessStartInfo()
{
FileName = _powerShellPath,
Arguments = $@"-NoProfile -ExecutionPolicy Bypass -file ""{scriptFile}"" -solutionDir ""{solutionFolder}""",
CreateNoWindow = true,
WorkingDirectory = _workingDirectory,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true
};
var process = Process.Start(startInfo);
if (process == null)
return;
process.StandardError.ReadToEndAsync().ContinueWith(task =>
{
var errors = task.Result;
if (!string.IsNullOrEmpty(errors))
{
_tracer.TraceError("PowerShell: " + errors);
}
});
process.StandardOutput.ReadToEndAsync().ContinueWith(task =>
{
var value = task.Result;
if (!string.IsNullOrEmpty(value))
{
_tracer.WriteLine("PowerShell: " + value);
}
});
if (!process.WaitForExit(10000))
{
process.Kill();
_tracer.TraceError("Script execution timed out: " + scriptFile);
}
});
}
catch (Exception ex)
{
_tracer.TraceError(ex.ToString());
}
if (!_isConversionRequestPending)
{
_isConverting = false;
return;
}
_isConversionRequestPending = false;
}
}
[CanBeNull]
private static string FindInPath(string fileName)
{
return Environment.GetEnvironmentVariable("PATH")?
.Split(';')
.Select(item => Path.Combine(item.Trim(), fileName))
.FirstOrDefault(File.Exists);
}
}
}
[Export]
[Export(typeof(ISourceFilesProvider))]
internal class SourceFilesProvider : ObservableObject, ISourceFilesProvider
{
[NotNull]
private readonly Configuration _configuration;
[NotNull]
private readonly PerformanceTracer _performanceTracer;
[ImportingConstructor]
public SourceFilesProvider([NotNull] Configuration configuration, [NotNull] PerformanceTracer performanceTracer)
{
_configuration = configuration;
_performanceTracer = performanceTracer;
}
[CanBeNull]
public string SolutionFolder { get; set; }
public IList<ProjectFile> SourceFiles
{
get
{
var folder = SolutionFolder;
if (string.IsNullOrEmpty(folder))
return Array.Empty<ProjectFile>();
using (_performanceTracer.Start("Enumerate source files"))
{
return new DirectoryInfo(folder).GetAllSourceFiles(new FileFilter(_configuration));
}
}
}
public void Invalidate()
{
}
}
}