-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathContainerDetails.xaml.cs
More file actions
517 lines (426 loc) · 13.8 KB
/
ContainerDetails.xaml.cs
File metadata and controls
517 lines (426 loc) · 13.8 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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
using JetBrains.Annotations;
using SIM.Extensions;
using SIM.IO.Real;
using SIM.Products;
using SIM.Tool.Base;
using SIM.Tool.Base.Pipelines;
using SIM.Tool.Base.Profiles;
using SIM.Tool.Base.Wizards;
using SIM.Tool.Windows.UserControls.Helpers;
using Sitecore.Diagnostics.Base;
using Sitecore.Diagnostics.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
namespace SIM.Tool.Windows.UserControls.Install.Containers
{
[UsedImplicitly]
public partial class ContainerDetails : IWizardStep, IFlowControl
{
#region Fields
private Window owner;
private InstallWizardArgs _InstallParameters = null;
private IEnumerable<Product> _Products;
// According to the following document the maximum length for a path in Windows systems is defined as 260 characters:
// https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file
// To prevent possibility of additional characters in the full path while combining, the value is set to 250 characters.
private const int MaxFileSystemPathLength = 250;
#endregion
#region Constructors
public ContainerDetails()
{
InitializeComponent();
}
#endregion
#region Public properties
public static bool InstallEverywhere
{
get
{
return WindowsSettings.AppInstallEverywhere.Value;
}
}
#endregion
#region Public Methods
public bool OnMovingBack(WizardArgs wizardArg)
{
return true;
}
public bool OnMovingNext(WizardArgs wizardArgs)
{
var productRevision = ProductRevision;
Assert.IsNotNull(productRevision, nameof(productRevision));
Product product = productRevision.SelectedValue as Product;
Assert.IsNotNull(product, nameof(product));
var name = GetValidWebsiteName();
if (name == null)
return false;
var licensePath = ProfileManager.Profile.License;
Assert.IsNotNull(licensePath, @"The license file isn't set in the Settings window");
FileSystem.FileSystem.Local.File.AssertExists(licensePath, "The {0} file is missing".FormatWith(licensePath));
var args = (InstallContainerWizardArgs)wizardArgs;
args.InstanceName = name;
args.InstanceProduct = product;
args.LicenseFileInfo = new FileInfo(licensePath);
args.Product = product;
args.FilesRoot = System.IO.Path.Combine(Directory.GetParent(args.Product.PackagePath).FullName, System.IO.Path.GetFileNameWithoutExtension(args.Product.PackagePath));
if (!this.IsFilePathLengthValidInPackage(args.Product.PackagePath, args.FilesRoot))
{
return false;
}
if (!Directory.Exists(args.FilesRoot))
{
Directory.CreateDirectory(args.FilesRoot);
WindowHelper.LongRunningTask(() => this.UnpackInstallationFiles(args), "Unpacking installation files.", wizardArgs.WizardWindow);
}
else
{
if (MessageBox.Show(string.Format("Path '{0}' already exists. Do you want to overwrite it?", args.FilesRoot), "Overwrite?", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
Directory.Delete(args.FilesRoot, true);
Directory.CreateDirectory(args.FilesRoot);
WindowHelper.LongRunningTask(() => this.UnpackInstallationFiles(args), "Unpacking installation files.", wizardArgs.WizardWindow);
}
}
string rootPath = this.LocationText.Text;
if (string.IsNullOrWhiteSpace(rootPath))
{
WindowHelper.ShowMessage("Please specify location.");
return false;
}
if (SIM.FileSystem.FileSystem.Local.Directory.Exists(rootPath))
{
if (Directory.EnumerateFileSystemEntries(rootPath).Any())
{
if (WindowHelper.ShowMessage("The folder with the same name already exists and is not empty. Would you like to delete it?", MessageBoxButton.OKCancel, MessageBoxImage.Question, MessageBoxResult.OK) == MessageBoxResult.OK)
{
SIM.FileSystem.FileSystem.Local.Directory.DeleteIfExists(rootPath, null);
SIM.FileSystem.FileSystem.Local.Directory.CreateDirectory(rootPath);
}
}
}
else
{
SIM.FileSystem.FileSystem.Local.Directory.CreateDirectory(rootPath);
}
args.DestinationFolder = rootPath;
return true;
}
public void UnpackInstallationFiles(InstallContainerWizardArgs args)
{
RealZipFile zip = new RealZipFile(new RealFile(new RealFileSystem(), args.Product.PackagePath));
zip.ExtractTo(new RealFolder(new RealFileSystem(), args.FilesRoot));
}
[CanBeNull]
private string GetValidWebsiteName()
{
var instanceName = InstanceName;
Assert.IsNotNull(instanceName, nameof(instanceName));
var name = instanceName.Text.EmptyToNull();
Assert.IsNotNull(name, @"Instance name isn't set");
return name;
}
#endregion
#region Methods
#region Private methods
private void Init()
{
using (new ProfileSection("Initializing InstanceDetails", this))
{
DataContext = new Model();
_Products = ProductManager.ContainerProducts;
}
}
private void ProductNameChanged([CanBeNull] object sender, [CanBeNull] SelectionChangedEventArgs e)
{
var productName = ProductName;
Assert.IsNotNull(productName, nameof(productName));
var grouping = productName.SelectedValue as IGrouping<string, Product>;
if (grouping == null)
{
return;
}
var productVersion = ProductVersion;
Assert.IsNotNull(productVersion, nameof(productVersion));
productVersion.DataContext = grouping.Where(x => x != null).GroupBy(p => p.ShortVersion).Where(x => x != null).OrderBy(p => Int32.Parse(p.Key));
SelectFirst(productVersion);
}
private void ProductRevisionChanged([CanBeNull] object sender, [CanBeNull] SelectionChangedEventArgs e)
{
using (new ProfileSection("Product revision changed", this))
{
var product = ProductRevision.SelectedValue as Product;
if (product == null)
{
return;
}
var name = product.ShortName.Replace(" ", "");
InstanceName.Text = name;
}
}
private void ProductVersionChanged([CanBeNull] object sender, [CanBeNull] SelectionChangedEventArgs e)
{
var productVersion = ProductVersion;
Assert.IsNotNull(productVersion, nameof(productVersion));
var grouping = productVersion.SelectedValue as IGrouping<string, Product>;
if (grouping == null)
{
return;
}
ProductRevision.DataContext = grouping.OrderBy(p => p.Revision);
SelectLast(ProductRevision);
var solrName = ProfileManager.Profile.VersionToSolrMap.FirstOrDefault(s => s.Vesrion == grouping.Key)?.Solr;
}
private void Select([NotNull] Selector element, [NotNull] string value)
{
Assert.ArgumentNotNull(element, nameof(element));
Assert.ArgumentNotNull(value, nameof(value));
if (element.Items.Count <= 0)
{
return;
}
for (int i = 0; i < element.Items.Count; ++i)
{
object item0 = element.Items[i];
IGrouping<string, Product> item1 = item0 as IGrouping<string, Product>;
if (item1 != null)
{
var key = item1.Key;
if (key.EqualsIgnoreCase(value))
{
element.SelectedIndex = i;
break;
}
}
else
{
Product item2 = item0 as Product;
if (item2 != null)
{
var key = item2.Revision;
if (key.EqualsIgnoreCase(value))
{
element.SelectedIndex = i;
break;
}
}
}
}
}
private void SelectByValue([NotNull] Selector element, string value)
{
Assert.ArgumentNotNull(element, nameof(element));
if (string.IsNullOrEmpty(value))
{
SelectLast(element);
return;
}
if (element.Items.Count > 0)
{
if (element.Items[0].GetType() == typeof(Product))
{
foreach (Product item in element.Items)
{
if (item.Name.EqualsIgnoreCase(value, true))
{
element.SelectedItem = item;
break;
}
if (item.Revision.EqualsIgnoreCase(value, true))
{
element.SelectedItem = item;
break;
}
}
}
else
{
foreach (var item in element.Items)
{
if (item is ContentControl)
{
if ((item as ContentControl).Content.ToString().EqualsIgnoreCase(value, true))
{
element.SelectedItem = item;
break;
}
}
if (item is string)
{
if ((item as string).EqualsIgnoreCase(value, true))
{
element.SelectedItem = item;
break;
}
}
}
}
}
}
private void SelectFirst([NotNull] Selector element)
{
Assert.ArgumentNotNull(element, nameof(element));
if (element.Items.Count > 0)
{
element.SelectedIndex = 0;
}
}
private void SelectLast([NotNull] Selector element)
{
Assert.ArgumentNotNull(element, nameof(element));
if (element.Items.Count > 0)
{
element.SelectedIndex = element.Items.Count - 1;
}
}
private void SelectProductByValue([CanBeNull] Selector element, [NotNull] string value)
{
Assert.ArgumentNotNull(value, nameof(value));
if (element == null)
{
return;
}
if (string.IsNullOrEmpty(value))
{
SelectLast(element);
return;
}
var items = element.Items;
Assert.IsNotNull(items, nameof(items));
if (items.Count > 0)
{
foreach (IGrouping<string, Product> item in items)
{
if (item.First().Name.EqualsIgnoreCase(value, true))
{
element.SelectedItem = item;
break;
}
if (item.First().TwoVersion.EqualsIgnoreCase(value, true))
{
element.SelectedItem = item;
break;
}
}
}
}
private void WindowLoaded(object sender, RoutedEventArgs e)
{
using (new ProfileSection("Window loaded", this))
{
var args = _InstallParameters;
Assert.IsNotNull(args, nameof(args));
var product = args.Product;
if (product != null)
{
return;
}
SelectProductByValue(ProductName, WindowsSettings.AppInstallationDefaultProduct.Value);
SelectProductByValue(ProductVersion, WindowsSettings.AppInstallationDefaultProductVersion.Value);
SelectByValue(ProductRevision, WindowsSettings.AppInstallationDefaultProductRevision.Value);
}
}
private bool IsFilePathLengthValidInPackage(string packagePath, string scriptRoot)
{
if (File.Exists(packagePath))
{
int maxAllowedFilePathLength = MaxFileSystemPathLength - scriptRoot.Length;
using (ZipArchive zipArchive = ZipFile.OpenRead(packagePath))
{
foreach (ZipArchiveEntry entry in zipArchive.Entries)
{
if (!(maxAllowedFilePathLength > entry.FullName.Length))
{
WindowHelper.ShowMessage("The full path length of some files in the package after unzipping is too long! Please change the path of the Local Repository folder in Settings, so it has less path length.");
return false;
}
}
}
}
else
{
WindowHelper.ShowMessage(string.Format("Please make sure that the \"{0}\" package exists.", packagePath));
return false;
}
return true;
}
#endregion
#endregion
#region Nested type: Model
public class Model
{
#region Fields
[CanBeNull]
[UsedImplicitly]
public readonly Product[] _Products = ProductManager.ContainerProducts.ToArray();
[NotNull]
private string _Name;
#endregion
#region Properties
[NotNull]
[UsedImplicitly]
public string Name
{
get
{
return _Name;
}
set
{
Assert.IsNotNull(value.EmptyToNull(), "Name must not be empty");
_Name = value;
}
}
[UsedImplicitly]
public IGrouping<string, Product> SelectedProductGroup1 { get; set; }
#endregion
}
#endregion
#region IWizardStep Members
void IWizardStep.InitializeStep(WizardArgs wizardArgs)
{
Init();
this.owner = wizardArgs.WizardWindow;
ProductName.DataContext = _Products.GroupBy(p => p.Name);
var args = (InstallWizardArgs)wizardArgs;
_InstallParameters = args;
Product product = args.Product;
if (product != null)
{
Select(ProductName, product.Name);
Select(ProductVersion, product.ShortVersion);
Select(ProductRevision, product.Revision);
}
else
{
SelectFirst(ProductName);
}
var name = args.InstanceName;
if (!string.IsNullOrEmpty(name))
{
InstanceName.Text = name;
}
}
bool IWizardStep.SaveChanges(WizardArgs wizardArgs)
{
return true;
}
#endregion
private void LocationBtn_Click(object sender, RoutedEventArgs e)
{
WindowHelper.PickFolder("Choose location folder", this.LocationText, null);
}
private void InstanceName_PreviewTextInput(object sender, System.Windows.Input.TextCompositionEventArgs e)
{
if (!NameCharsHelper.IsValidNameChar(e.Text, "project name"))
{
e.Handled = true;
}
}
}
}