forked from DynamoDS/Dynamo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPackageManagerClientViewModel.cs
More file actions
1226 lines (1071 loc) · 55.6 KB
/
Copy pathPackageManagerClientViewModel.cs
File metadata and controls
1226 lines (1071 loc) · 55.6 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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Dynamo.Core;
using Dynamo.Graph.Nodes.CustomNodes;
using Dynamo.Graph.Workspaces;
using Dynamo.Logging;
using Dynamo.Models;
using Dynamo.PackageManager;
using Dynamo.PackageManager.UI;
using Dynamo.Selection;
using Dynamo.Utilities;
using Dynamo.Wpf.Interfaces;
using Dynamo.Wpf.Properties;
using Dynamo.Wpf.UI.GuidedTour;
using Dynamo.Wpf.Utilities;
using DynamoServices;
using Greg.AuthProviders;
using Greg.Responses;
using Prism.Commands;
namespace Dynamo.ViewModels
{
public class TermsOfUseHelperParams
{
internal IBrandingResourceProvider ResourceProvider { get; set; }
internal PackageManagerClient PackageManagerClient { get; set; }
internal AuthenticationManager AuthenticationManager { get; set; }
internal Action AcceptanceCallback { get; set; }
internal Window Parent { get; set; }
}
/// <summary>
/// A helper class to check asynchronously whether the Terms of Use has
/// been accepted, and if so, continue to execute the provided Action.
/// </summary>
public class TermsOfUseHelper
{
private static int lockCount = 0;
private readonly IBrandingResourceProvider resourceProvider;
private readonly Action callbackAction;
private readonly PackageManagerClient packageManagerClient;
private readonly AuthenticationManager authenticationManager;
private readonly Window parent;
private static bool isTermsOfUseCreated;
public TermsOfUseHelper(TermsOfUseHelperParams touParams)
{
if (touParams == null)
throw new ArgumentNullException("touParams");
if (touParams.PackageManagerClient == null)
throw new ArgumentNullException("PackageManagerClient");
if (touParams.AuthenticationManager == null)
throw new ArgumentNullException("AuthenticationManager");
if (touParams.AcceptanceCallback == null)
throw new ArgumentNullException("AcceptanceCallback");
if (touParams.ResourceProvider == null)
throw new ArgumentNullException("ResourceProvider");
resourceProvider = touParams.ResourceProvider;
packageManagerClient = touParams.PackageManagerClient;
callbackAction = touParams.AcceptanceCallback;
authenticationManager = touParams.AuthenticationManager;
parent = touParams.Parent;
}
internal void Execute(bool preventReentrant)
{
if (preventReentrant && Interlocked.Increment(ref lockCount) > 1)
{
// Determine if there is already a previous request to display
// terms of use dialog, if so, do nothing for the second time.
Interlocked.Decrement(ref lockCount);
return;
}
Task<bool>.Factory.StartNew(() => packageManagerClient.GetTermsOfUseAcceptanceStatus()).
ContinueWith(t =>
{
// The above GetTermsOfUseAcceptanceStatus call will get the
// user to sign-in. If the user cancels that sign-in dialog
// without signing in, we won't show the terms of use dialog,
// simply return from here.
//
if (authenticationManager.LoginState != LoginState.LoggedIn)
return;
var termsOfUseAccepted = t.Result;
if (termsOfUseAccepted)
{
// Terms of use accepted, proceed to publish.
callbackAction.Invoke();
}
else
{
// Prompt user to accept the terms of use.
ShowTermsOfUseForPublishing();
}
}, TaskScheduler.FromCurrentSynchronizationContext()).
ContinueWith(t =>
{
// Done with terms of use dialog, decrement counter.
Interlocked.Decrement(ref lockCount);
}, TaskScheduler.FromCurrentSynchronizationContext());
}
internal static bool ShowTermsOfUseDialog(bool forPublishing, string additionalTerms, Window parent = null)
{
if (isTermsOfUseCreated) return false;
var executingAssemblyPathName = Assembly.GetExecutingAssembly().Location;
var rootModuleDirectory = Path.GetDirectoryName(executingAssemblyPathName);
var touFilePath = Path.Combine(rootModuleDirectory, "TermsOfUse.rtf");
var termsOfUseView = new TermsOfUseView(touFilePath);
termsOfUseView.Closed += TermsOfUseView_Closed;
isTermsOfUseCreated = true;
termsOfUseView.Owner = parent;
//If any Guide is being executed then the ShowTermsOfUse Window WON'T be modal otherwise will be modal (as in the normal behavior)
if (!GuideFlowEvents.IsAnyGuideActive)
{
termsOfUseView.ShowDialog();
}
else
{
//When a Guide is being executed then the TermsOfUseView cannot be modal and has the DynamoView as owner
termsOfUseView.Show();
}
if (!termsOfUseView.AcceptedTermsOfUse)
return false; // User rejected the terms, go no further.
if (string.IsNullOrEmpty(additionalTerms)) // No additional terms.
return termsOfUseView.AcceptedTermsOfUse;
// If user has accepted the terms, and if there is an additional
// terms specified, then that should be shown, too. Note that if
// the file path is provided, it has to represent a valid file path.
//
if (!File.Exists(additionalTerms))
throw new FileNotFoundException(additionalTerms);
var additionalTermsView = new TermsOfUseView(additionalTerms);
if (parent == null)
additionalTermsView.ShowDialog();
else
{
//Means that a Guide is being executed then the TermsOfUseView cannot be modal and has the DynamoView as owner
additionalTermsView.Owner = parent;
additionalTermsView.Show();
}
return additionalTermsView.AcceptedTermsOfUse;
}
private static void TermsOfUseView_Closed(object sender, EventArgs e)
{
isTermsOfUseCreated = false;
}
private void ShowTermsOfUseForPublishing()
{
var additionalTerms = string.Empty;
if (resourceProvider != null)
additionalTerms = resourceProvider.AdditionalPackagePublisherTermsOfUse;
if (!ShowTermsOfUseDialog(true, additionalTerms))
return; // Terms of use not accepted.
// If user accepts the terms of use, then update the record on
// the server, before proceeding to show the publishing dialog.
// This method is invoked on the UI thread, so when the server call
// returns, invoke the publish dialog on the UI thread's context.
//
Task<bool>.Factory.StartNew(() => packageManagerClient.SetTermsOfUseAcceptanceStatus()).
ContinueWith(t =>
{
if (t.Result)
callbackAction.Invoke();
},
TaskScheduler.FromCurrentSynchronizationContext());
}
}
/// <summary>
/// A thin wrapper on the Greg rest client for performing IO with
/// the Package Manager
/// </summary>
public class PackageManagerClientViewModel : NotificationObject, IPackageInstaller
{
#region Properties/Fields
private readonly string QUARANTINED = "quarantined";
/// <summary>
/// The System.Windows.Window owner of the view model.
/// Used to align messagebox dialogs created by this model
/// </summary>
public Window ViewModelOwner { get; set; }
ObservableCollection<PackageUploadHandle> _uploads = new ObservableCollection<PackageUploadHandle>();
public ObservableCollection<PackageUploadHandle> Uploads
{
get { return _uploads; }
set { _uploads = value; }
}
ObservableCollection<PackageDownloadHandle> _downloads = new ObservableCollection<PackageDownloadHandle>();
public ObservableCollection<PackageDownloadHandle> Downloads
{
get { return _downloads; }
set { _downloads = value; }
}
private PackageManagerExtension pmExtension;
internal virtual PackageManagerExtension PackageManagerExtension
{
get { return pmExtension ?? (pmExtension = DynamoViewModel.Model.GetPackageManagerExtension()); }
}
public virtual List<PackageManagerSearchElement> CachedPackageList { get; private set; }
public List<PackageManagerSearchElement> InfectedPackageList { get; private set; }
public readonly DynamoViewModel DynamoViewModel;
public AuthenticationManager AuthenticationManager { get; set; }
internal PackageManagerClient Model { get; private set; }
public LoginState LoginState
{
get
{
return AuthenticationManager.LoginState;
}
}
public string Username
{
get
{
return AuthenticationManager.Username;
}
}
#endregion
public ICommand ToggleLoginStateCommand { get; private set; }
/// <summary>
/// Contains all votes the user has submitted.
/// Will allow the user to vote for a package they have not upvoted before
/// </summary>
private List<string> Uservotes { get; set; }
internal PackageManagerClientViewModel(DynamoViewModel dynamoViewModel, PackageManagerClient model)
{
this.DynamoViewModel = dynamoViewModel;
this.AuthenticationManager = dynamoViewModel.Model.AuthenticationManager;
Model = model;
CachedPackageList = new List<PackageManagerSearchElement>();
this.ToggleLoginStateCommand = new DelegateCommand(ToggleLoginState, CanToggleLoginState);
if (AuthenticationManager != null)
{
AuthenticationManager.LoginStateChanged += (loginState) =>
{
RaisePropertyChanged("LoginState");
RaisePropertyChanged("Username");
};
}
}
private void ToggleLoginState()
{
if(!this.DynamoViewModel.IsIDSDKInitialized()) return;
if (AuthenticationManager.LoginState == LoginState.LoggedIn)
{
AuthenticationManager.Logout();
}
else
{
AuthenticationManager.Login();
}
}
private bool CanToggleLoginState()
{
return AuthenticationManager.LoginState == LoginState.LoggedOut || AuthenticationManager.LoginState == LoginState.LoggedIn;
}
public void PublishCurrentWorkspace(object m)
{
if (!this.DynamoViewModel.IsIDSDKInitialized(true, ViewModelOwner)) return;
var ws = (CustomNodeWorkspaceModel)DynamoViewModel.CurrentSpace;
CustomNodeDefinition currentFunDef;
if (DynamoViewModel.Model.CustomNodeManager.TryGetFunctionDefinition(
ws.CustomNodeId,
DynamoModel.IsTestMode,
out currentFunDef))
{
CustomNodeInfo currentFunInfo;
if (DynamoViewModel.Model.CustomNodeManager.TryGetNodeInfo(
ws.CustomNodeId,
out currentFunInfo))
{
// Use the workspace's FileName directly if info.Path is not set
if (string.IsNullOrEmpty(currentFunInfo.Path) && !string.IsNullOrEmpty(ws.FileName))
{
currentFunInfo = new CustomNodeInfo(
currentFunInfo.FunctionId,
currentFunInfo.Name,
currentFunInfo.Category,
currentFunInfo.Description,
ws.FileName, // Use workspace FileName
currentFunInfo.IsVisibleInDynamoLibrary);
}
var touParams = new TermsOfUseHelperParams
{
PackageManagerClient = Model,
AuthenticationManager = DynamoViewModel.Model.AuthenticationManager,
ResourceProvider = DynamoViewModel.BrandingResourceProvider,
AcceptanceCallback = () => ShowNodePublishInfo(new[]
{
Tuple.Create(currentFunInfo, currentFunDef)
}),
Parent = ViewModelOwner ?? DynamoViewModel.Owner,
};
var termsOfUseCheck = new TermsOfUseHelper(touParams);
termsOfUseCheck.Execute(true);
return;
}
}
MessageBoxService.Show(
ViewModelOwner,
Resources.MessageSelectSymbolNotFound,
Resources.SelectionErrorMessageBoxTitle,
MessageBoxButton.OK, MessageBoxImage.Question);
}
public bool CanPublishCurrentWorkspace(object m)
{
return DynamoViewModel.Model.CurrentWorkspace is CustomNodeWorkspaceModel && AuthenticationManager.HasAuthProvider && !Model.NoNetworkMode;
}
public void PublishNewPackage(object m)
{
if (!this.DynamoViewModel.IsIDSDKInitialized(true, ViewModelOwner)) return;
var termsOfUseCheck = new TermsOfUseHelper(new TermsOfUseHelperParams
{
PackageManagerClient = Model,
AuthenticationManager = AuthenticationManager,
ResourceProvider = DynamoViewModel.BrandingResourceProvider,
AcceptanceCallback = ShowNodePublishInfo,
Parent = ViewModelOwner ?? DynamoViewModel.Owner,
});
termsOfUseCheck.Execute(true);
}
public bool CanPublishNewPackage(object m)
{
return AuthenticationManager.HasAuthProvider && !Model.NoNetworkMode;
}
public void PublishCustomNode(Function m)
{
if (!this.DynamoViewModel.IsIDSDKInitialized(true, ViewModelOwner)) return;
CustomNodeInfo currentFunInfo;
if (DynamoViewModel.Model.CustomNodeManager.TryGetNodeInfo(
m.Definition.FunctionId,
out currentFunInfo))
{
var termsOfUseCheck = new TermsOfUseHelper(new TermsOfUseHelperParams
{
PackageManagerClient = Model,
AuthenticationManager = AuthenticationManager,
ResourceProvider = DynamoViewModel.BrandingResourceProvider,
AcceptanceCallback = () => ShowNodePublishInfo(new[]
{
Tuple.Create(currentFunInfo, m.Definition)
}),
Parent = ViewModelOwner ?? DynamoViewModel.Owner,
});
termsOfUseCheck.Execute(true);
}
}
public bool CanPublishCustomNode(Function m)
{
return AuthenticationManager.HasAuthProvider && m != null && !Model.NoNetworkMode;
}
public void PublishSelectedNodes(object m)
{
if (!this.DynamoViewModel.IsIDSDKInitialized(true, ViewModelOwner)) return;
var nodeList = DynamoSelection.Instance.Selection
.Where(x => x is Function)
.Cast<Function>()
.Select(x => x.Definition.FunctionId)
.ToList();
if (!nodeList.Any())
{
MessageBoxService.Show(
ViewModelOwner,
Resources.MessageSelectAtLeastOneNode,
Resources.SelectionErrorMessageBoxTitle,
MessageBoxButton.OK,
MessageBoxImage.Question);
return;
}
var manager = DynamoViewModel.Model.CustomNodeManager;
var defs = new List<Tuple<CustomNodeInfo, CustomNodeDefinition>>();
foreach (var node in nodeList)
{
CustomNodeInfo info;
if (manager.TryGetNodeInfo(node, out info))
{
CustomNodeDefinition def;
if (manager.TryGetFunctionDefinition(node, DynamoModel.IsTestMode, out def))
{
defs.Add(Tuple.Create(info, def));
continue;
}
}
MessageBoxService.Show(
ViewModelOwner,
Resources.MessageGettingNodeError,
Resources.SelectionErrorMessageBoxTitle,
MessageBoxButton.OK, MessageBoxImage.Question);
}
var termsOfUseCheck = new TermsOfUseHelper(new TermsOfUseHelperParams
{
PackageManagerClient = Model,
AuthenticationManager = AuthenticationManager,
ResourceProvider = DynamoViewModel.BrandingResourceProvider,
AcceptanceCallback = () => ShowNodePublishInfo(defs),
Parent = ViewModelOwner ?? DynamoViewModel.Owner,
});
termsOfUseCheck.Execute(true);
}
public bool CanPublishSelectedNodes(object m)
{
return DynamoSelection.Instance.Selection.Count > 0 &&
DynamoSelection.Instance.Selection.All(x => x is Function) && AuthenticationManager.HasAuthProvider && !Model.NoNetworkMode;
}
private void ShowNodePublishInfo()
{
if (!this.DynamoViewModel.IsIDSDKInitialized(true, ViewModelOwner)) return;
var newPkgVm = new PublishPackageViewModel(DynamoViewModel);
DynamoViewModel.OnRequestPackagePublishDialog(newPkgVm);
}
private void ShowNodePublishInfo(ICollection<Tuple<CustomNodeInfo, CustomNodeDefinition>> funcDefs)
{
if (!this.DynamoViewModel.IsIDSDKInitialized(true, ViewModelOwner)) return;
foreach (var f in funcDefs)
{
var pkg = PackageManagerExtension.PackageLoader.GetOwnerPackage(f.Item1);
if (pkg != null)
{
var m = Dynamo.Wpf.Utilities.MessageBoxService.Show(ViewModelOwner,
String.Format(Resources.MessageSubmitSameNamePackage,
DynamoViewModel.BrandingResourceProvider.ProductName, pkg.Name),
Resources.PackageWarningMessageBoxTitle,
MessageBoxButton.YesNo, MessageBoxImage.Question);
if (m == MessageBoxResult.Yes)
{
var pkgVm = new PackageViewModel(DynamoViewModel, pkg);
pkgVm.PublishNewPackageVersionCommand.Execute();
return;
}
}
}
var newPkgVm = new PublishPackageViewModel(DynamoViewModel);
// Populate CustomDyfFilepaths FIRST (before CustomNodeDefinitions triggers RefreshPackageContents)
foreach (var funcDef in funcDefs)
{
var info = funcDef.Item1; // CustomNodeInfo
var def = funcDef.Item2; // CustomNodeDefinition
if (!string.IsNullOrEmpty(info.Path))
{
var fileName = System.IO.Path.GetFileName(info.Path);
newPkgVm.CustomDyfFilepaths.TryAdd(fileName, info.Path);
}
}
// Now set CustomNodeDefinitions - this will trigger RefreshPackageContents() which includes CustomDyfFilepaths
newPkgVm.CustomNodeDefinitions = funcDefs.Select(pair => pair.Item2).ToList();
DynamoViewModel.OnRequestPackagePublishDialog(newPkgVm);
}
public List<PackageManagerSearchElement> ListAll()
{
CachedPackageList = new List<PackageManagerSearchElement>();
// Calls to Model.UserVotes and Model.ListAll might take a long time to run (so do not use them syncronously in the UI thread)
Uservotes = this.Model.UserVotes();
foreach (var header in Model.ListAll())
{
var ele = new PackageManagerSearchElement(header);
ele.UpvoteRequested += this.Model.Upvote;
if (Uservotes != null)
{
ele.HasUpvote = Uservotes.Contains(header._id);
}
CachedPackageList.Add(ele);
}
return CachedPackageList;
}
/// <summary>
/// Returns a dictionary of infected package(s) with name and version, if the last published version of package uploaded by the current user was flagged as infected.
/// </summary>
/// <returns></returns>
public List<PackageManagerSearchElement> GetInfectedPackages()
{
if (!this.DynamoViewModel.IsIDSDKInitialized(true, ViewModelOwner)) return null;
InfectedPackageList = new List<PackageManagerSearchElement>();
var latestPkgs = Model.GetUsersLatestPackages();
if (latestPkgs != null && latestPkgs.maintains?.Count > 0)
{
foreach (var infectedVer in latestPkgs.maintains)
{
if (infectedVer.scan_status == QUARANTINED)
{
var ele = new PackageManagerSearchElement(infectedVer);
InfectedPackageList.Add(ele);
}
}
}
return InfectedPackageList;
}
/// <summary>
/// Download and install a specific package from the package manager
/// </summary>
/// <param name="packageInfo"></param>
/// <param name="downloadPath"></param>
public void DownloadAndInstallPackage(IPackageInfo packageInfo, string downloadPath = null)
{
// User needs to accept terms of use before any packages can be downloaded from package manager
if (!this.DynamoViewModel.IsIDSDKInitialized(true, ViewModelOwner)) return;
var prefSettings = DynamoViewModel.Model.PreferenceSettings;
var touAccepted = prefSettings.PackageDownloadTouAccepted;
if (!touAccepted)
{
touAccepted = TermsOfUseHelper.ShowTermsOfUseDialog(false, null, ViewModelOwner);
prefSettings.PackageDownloadTouAccepted = touAccepted;
if (!touAccepted)
{
return;
}
}
// Try to get the package version for this package
PackageVersion version;
try
{
version = Model.GetPackageVersionHeader(packageInfo);
}
catch
{
MessageBoxService.Show(
ViewModelOwner,
string.Format(Resources.MessagePackageVersionNotFound, packageInfo.Version.ToString(), packageInfo.Name),
Resources.PackageDownloadErrorMessageBoxTitle,
MessageBoxButton.OK,
MessageBoxImage.Error);
return;
}
ExecutePackageDownload(packageInfo.Name, version, downloadPath);
}
private string JoinPackageNames(IEnumerable<Package> pkgs, string seperator = ", ")
{
return String.Join(seperator, pkgs.Select(x => x.Name + " " + x.VersionName));
}
/// <summary>
/// Warns about conflicts with both the main package and its dependencies.
/// </summary>
/// <param name="package">package version being downloaded</param>
/// <param name="duplicatePackage">local package found to be duplicate of one being downloaded</param>
/// <param name="dependencyConflicts">List of packages that are in conflict with the dependencies of the package version to be downloaded (does not include the main package)</param>
/// <returns>True if the User opted to continue with the download operation. False otherwise</returns>
private bool WarnAboutDuplicatePackageConflicts(PackageVersion package,
Package duplicatePackage,
List<Package> dependencyConflicts)
{
var packageToDownload = $"{package.name} {package.version}";
if (duplicatePackage != null)
{
var dupPkg = JoinPackageNames(new[] { duplicatePackage });
if (duplicatePackage.BuiltInPackage)
{
if (package.version == duplicatePackage.VersionName)
{
var message = string.Format(Resources.MessageSamePackageSameVersInBuiltinPackages, packageToDownload);
MessageBoxService.Show(ViewModelOwner, message, Resources.CannotDownloadPackageMessageBoxTitle,
MessageBoxButton.OK, MessageBoxImage.Information);
}
else
{
var message = string.Format(Resources.MessageSamePackageDiffVersInBuiltinPackages, packageToDownload, dupPkg,
DynamoViewModel.BrandingResourceProvider.ProductName);
MessageBoxService.Show(ViewModelOwner, message, Resources.CannotDownloadPackageMessageBoxTitle,
MessageBoxButton.OK, MessageBoxImage.Exclamation);
}
Analytics.TrackEvent(Actions.BuiltInPackageConflict, Categories.PackageManagerOperations, packageToDownload);
return false;// All conflicts with built-in packages must be first resolved manually before continuing to download.
}
if (package.version == duplicatePackage.VersionName)
{
var message = string.Format(Resources.MessageSamePackageSameVersInLocalPackages, packageToDownload);
MessageBoxService.Show(ViewModelOwner, message, Resources.CannotDownloadPackageMessageBoxTitle,
MessageBoxButton.OK, MessageBoxImage.Information);
return false;
}
else
{
MessageBoxResult dialogResult;
if (duplicatePackage.InUse(DynamoViewModel.Model))
{// Loaded assemblies or package in use in workspace
dialogResult = MessageBoxService.Show(
string.Format(Resources.MessageSamePackageDiffVersInLocalPackages, packageToDownload, dupPkg, DynamoViewModel.BrandingResourceProvider.ProductName),
Resources.LoadedPackagesConflictMessageBoxTitle,
MessageBoxButton.OKCancel, new string[] { Resources.UninstallLoadedPackage, Resources.GenericTaskDialogOptionCancel }, MessageBoxImage.Exclamation);
if (dialogResult == MessageBoxResult.OK)
{
// mark for uninstallation
duplicatePackage.MarkForUninstall(DynamoViewModel.Model.PreferenceSettings);
}
return false;// Any package that is in use must first be uninstalled before continuing to download.
}
else
{
dialogResult = MessageBoxService.Show(
String.Format(Resources.MessageAlreadyInstallDynamo,
DynamoViewModel.BrandingResourceProvider.ProductName, dupPkg, packageToDownload),
Resources.PackagesInUseConflictMessageBoxTitle,
MessageBoxButton.OKCancel, new string[] { Resources.UninstallLoadedPackage, Resources.GenericTaskDialogOptionCancel },
MessageBoxImage.Exclamation);
if (dialogResult != MessageBoxResult.OK)
{
return false;
}
// The conflicting package will be uninstalled just before the new package is installed
}
}
}
var uninstallsRequiringRestart = new List<Package>();
var uninstallRequiringUserModifications = new List<Package>();
var immediateUninstalls = new List<Package>();
var builtinPackages = new List<Package>();
foreach (var pkg in dependencyConflicts)
{
if (pkg == null) continue;
if (pkg.BuiltInPackage)
{
builtinPackages.Add(pkg);
continue;
}
if (pkg.LoadedAssemblies.Any())
{
uninstallsRequiringRestart.Add(pkg);
continue;
}
if (pkg.InUse(DynamoViewModel.Model))
{
uninstallRequiringUserModifications.Add(pkg);
continue;
}
immediateUninstalls.Add(pkg);
}
if (builtinPackages.Any())
{
// Conflicts with builtin packages
var message = string.Format(Resources.MessagePackageDepsInBuiltinPackages, packageToDownload,
JoinPackageNames(builtinPackages));
Analytics.TrackEvent(Actions.BuiltInPackageConflict, Categories.PackageManagerOperations, packageToDownload);
var dialogResult = MessageBoxService.Show(ViewModelOwner, message,
Resources.BuiltInPackageConflictMessageBoxTitle,
MessageBoxButton.OKCancel, MessageBoxImage.Exclamation);
if (dialogResult == MessageBoxResult.Cancel || dialogResult == MessageBoxResult.None) return false;
}
if (uninstallRequiringUserModifications.Any())
{
var conflictingPkgs = JoinPackageNames(uninstallRequiringUserModifications);
var message = string.Format(Resources.MessageForceInstallOrUninstallToContinue, packageToDownload, conflictingPkgs,
DynamoViewModel.BrandingResourceProvider.ProductName);
var dialogResult = MessageBoxService.Show(ViewModelOwner, message,
Resources.PackagesInUseConflictMessageBoxTitle,
MessageBoxButton.YesNo, MessageBoxImage.Exclamation);
if (dialogResult == MessageBoxResult.No || dialogResult == MessageBoxResult.None) return false;
}
var settings = DynamoViewModel.Model.PreferenceSettings;
if (uninstallsRequiringRestart.Any())
{
var conflictingPkgs = JoinPackageNames(uninstallsRequiringRestart, Environment.NewLine);
var message = string.Format(Resources.MessageForceInstallOrUninstallUponRestart, packageToDownload,
conflictingPkgs);
var dialogResult = MessageBoxService.Show(message,
Resources.LoadedPackagesConflictMessageBoxTitle,
MessageBoxButton.YesNoCancel, new string[] { Resources.ContinueInstall, Resources.UninstallLoadedPackages, Resources.GenericTaskDialogOptionCancel }, MessageBoxImage.Exclamation);
if (dialogResult == MessageBoxResult.No)
{
// mark for uninstallation
uninstallsRequiringRestart.ForEach(x => x.MarkForUninstall(settings));
return false;
}
else if (dialogResult == MessageBoxResult.Cancel || dialogResult == MessageBoxResult.None) return false;
}
if (immediateUninstalls.Any())
{
// if the package is not in use, tell the user we will uninstall it and give them the opportunity to cancel
var message = String.Format(Resources.MessageAlreadyInstallDynamo,
DynamoViewModel.BrandingResourceProvider.ProductName,
JoinPackageNames(immediateUninstalls), packageToDownload);
var dialogResult = MessageBoxService.Show(ViewModelOwner, message,
Resources.DownloadWarningMessageBoxTitle, MessageBoxButton.OKCancel, MessageBoxImage.Warning);
if (dialogResult == MessageBoxResult.Cancel || dialogResult == MessageBoxResult.None)
{
return false;
}
}
return true;
}
internal async void ExecutePackageDownload(string name, PackageVersion package, string installPath)
{
string msg;
MessageBoxResult result;
// initialize default download consent message
msg = String.IsNullOrEmpty(installPath) ?
String.Format(Resources.MessageConfirmToInstallPackage, name, package.version) :
String.Format(Resources.MessageConfirmToInstallPackageToFolder, name, package.version, installPath);
// Calculate compatibility and display a single download consent across cases
var compatible = PackageManagerSearchElement.CalculateCompatibility(package.compatibility_matrix);
// Unknown package compatibility with current Dynamo env, this is expected to be the most popular case for now
if (compatible == null && !DynamoModel.IsTestMode)
{
msg = msg + "\n\n" + Resources.PackageUnknownCompatibilityVersionDownloadMsg;
result = MessageBoxService.Show(ViewModelOwner, msg,
Resources.PackageDownloadConfirmMessageBoxTitle,
MessageBoxButton.OKCancel, MessageBoxImage.Question);
if (result != MessageBoxResult.OK)
{
return;
}
}
// Package incompatible with current Dynamo env
else if (compatible == false && !DynamoModel.IsTestMode)
{
msg = msg + "\n\n" + Resources.PackageManagerIncompatibleVersionDownloadMsg;
result = MessageBoxService.Show(ViewModelOwner, msg,
Resources.PackageManagerIncompatibleVersionDownloadTitle,
MessageBoxButton.OKCancel, MessageBoxImage.Warning);
if (result != MessageBoxResult.OK)
{
return;
}
}
// Package compatible with current Dynamo env
else
{
result = MessageBoxService.Show(ViewModelOwner, msg,
Resources.PackageDownloadConfirmMessageBoxTitle,
MessageBoxButton.OKCancel, MessageBoxImage.Question);
}
var pmExt = DynamoViewModel.Model.GetPackageManagerExtension();
if (result == MessageBoxResult.OK)
{
if (string.IsNullOrEmpty(package.name))
{// package.name is not set sometimes
package.name = name;
}
Debug.Assert(package.full_dependency_ids.Count == package.full_dependency_versions.Count);
// get all of the dependency version headers
// we reverse these arrays because the package manager returns dependencies in topological order starting at
// the current package - and we want to install the dependencies first!.
var reversedVersions = package.full_dependency_versions.Select(x => x).Reverse().ToList();
var dependencyVersionHeaders = package.full_dependency_ids.Select(x => x).Reverse().Select((dep, i) =>
{
var depVersion = reversedVersions[i];
try
{
return Model.GetPackageVersionHeader(dep._id, depVersion);
}
catch
{
MessageBoxService.Show(
ViewModelOwner,
String.Format(Resources.MessageFailedToDownloadPackageVersion, depVersion, dep._id),
Resources.PackageDownloadErrorMessageBoxTitle,
MessageBoxButton.OK, MessageBoxImage.Error);
return null;
}
}).ToList();
// if any header download fails, abort
if (dependencyVersionHeaders.Any(x => x == null))
{
return;
}
if (!dependencyVersionHeaders.Any(x => x.name == name))
{// Add the main package if it does not exist
dependencyVersionHeaders.Add(package);
}
var localPkgs = pmExt.PackageLoader.LocalPackages;
// if the new package has one or more dependencies that are already installed
// we need to either first uninstall them, or allow the user to forcibly install the package,
// or cancel the installation if they do not want either of the first two options.
// local package that conflicts with package itself.
Package duplicatePackage = null;
// list of local packages that conflict (have different versions) with package dependencies.
// Does not contain the main package since it is handled separately by duplicatePackage
var localPkgsConflictingWithPkgDeps = new List<Package>();
var newPackageHeaders = new List<PackageVersion>();
foreach (var dependencyHeader in dependencyVersionHeaders)
{
var localPkgWithSameName = localPkgs.FirstOrDefault(x =>
(x.LoadState.State == PackageLoadState.StateTypes.Loaded ||
x.LoadState.State == PackageLoadState.StateTypes.Error) &&
x.Name.Equals(dependencyHeader.name));
bool exactMatch = false;
if (localPkgWithSameName != null)
{
// Packages with same name and same version
exactMatch = localPkgWithSameName.VersionName.Equals(dependencyHeader.version);
if (name.Equals(localPkgWithSameName.Name))
{// Handle the main package duplicate
// Main package has a duplicate in local packages
duplicatePackage = localPkgWithSameName;
}
else
{// Handle the dependency duplicates here
// exclude dependencies that exactly match existing local packages
if (!exactMatch)
{
// Local packages that have the same name but different versions
localPkgsConflictingWithPkgDeps.Add(localPkgWithSameName);
}
}
}
if (!exactMatch)
{
// Package headers that do not match by name or version with existing local packages
newPackageHeaders.Add(dependencyHeader);
}
}
if (!WarnAboutDuplicatePackageConflicts(package, duplicatePackage, localPkgsConflictingWithPkgDeps))
{
// User chose to cancel because of conflicts.
return;
}
// determine if any of the packages contain binaries or python scripts.
var containsBinariesOrPythonScripts = newPackageHeaders.Any(x =>
{
// The contents (string) property of the PackageVersion object can be null for an empty package
// like LunchBox.
var are_contents_empty = string.IsNullOrEmpty(x.contents);
var contains_binaries = x.contains_binaries || !are_contents_empty;
var contains_python = !are_contents_empty;
return contains_binaries || contains_python;
});
// if any do, notify user and allow cancellation
if (containsBinariesOrPythonScripts)
{
var res = MessageBoxService.Show(ViewModelOwner,
Resources.MessagePackageContainPythonScript,
Resources.PackageDownloadMessageBoxTitle,
MessageBoxButton.OKCancel, MessageBoxImage.Exclamation);
if (res == MessageBoxResult.Cancel || res == MessageBoxResult.None) return;
}
var containsPackagesThatTargetOtherHosts = PackageManagerExtension.CheckIfPackagesTargetOtherHosts(newPackageHeaders);
// if unknown compatibility, and package target other hosts, notify user and allow cancellation
if (compatible == null && containsPackagesThatTargetOtherHosts)
{
var res = MessageBoxService.Show(ViewModelOwner,
Resources.MessagePackageTargetOtherHosts,
Resources.PackageDownloadMessageBoxTitle,
MessageBoxButton.OKCancel, MessageBoxImage.Exclamation);
if (res == MessageBoxResult.Cancel || res == MessageBoxResult.None) return;
}
// Determine if there are any dependencies that have a newer dynamo version, (this includes the root package).
// We assume this means this package is compatible with that dynamo version, but we should warn the user it
// may not work with the current version of Dynamo.
// If the compatibility matrix already indicates compatibility, skip the engine_version heuristic.
// Only perform engine_version based warnings when compatibility is not explicitly true.
if (compatible != true)
{
//wrap in try catch as it's possible version info could be missing. If so, we install the package and log an error.
try
{
var dynamoVersion = Version.Parse(DynamoModel.Version);
var futureDeps = newPackageHeaders.Where(dep => Version.Parse(dep.engine_version) > dynamoVersion);
// also identify packages that have a Dynamo engine version from a different major version as a special case,
// as different major Dynamo versions may use different runtime frameworks - these packages may not be compatible.
// This check will return empty if the current major version does not require this validation.
var preDYNDeps = newPackageHeaders.Where(dep => Version.Parse(dep.engine_version).Major < dynamoVersion.Major);
// If any of the required packages use a newer version of Dynamo, show a dialog to the user
// allowing them to cancel the package download
if (futureDeps.Any())
{
var res = MessageBoxService.Show(ViewModelOwner,
$"{string.Format(Resources.MessagePackageNewerDynamo, DynamoViewModel.BrandingResourceProvider.ProductName)} {Resources.MessagePackOlderDynamoLink}",
string.Format(Resources.PackageUseNewerDynamoMessageBoxTitle, DynamoViewModel.BrandingResourceProvider.ProductName),
//this message has a url link so we use the rich text box version of the message box.
showRichTextBox: true,
MessageBoxButton.OKCancel,
MessageBoxImage.Warning);
if (res == MessageBoxResult.Cancel || res == MessageBoxResult.None)
{
return;
}
}