This repository has been archived by the owner on Oct 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 770
/
PackageViewModel.cs
439 lines (369 loc) · 11.6 KB
/
PackageViewModel.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
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
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
using ICSharpCode.PackageManagement.Scripting;
using NuGet;
namespace ICSharpCode.PackageManagement
{
public class PackageViewModel : ViewModelBase<PackageViewModel>
{
DelegateCommand addPackageCommand;
DelegateCommand removePackageCommand;
DelegateCommand managePackageCommand;
PackageManagementSelectedProjects selectedProjects;
IPackageManagementEvents packageManagementEvents;
IPackageFromRepository package;
IEnumerable<PackageOperation> packageOperations = new PackageOperation[0];
PackageViewModelOperationLogger logger;
IPackageActionRunner actionRunner;
public PackageViewModel(
IPackageFromRepository package,
PackageManagementSelectedProjects selectedProjects,
IPackageManagementEvents packageManagementEvents,
IPackageActionRunner actionRunner,
ILogger logger)
{
this.package = package;
this.selectedProjects = selectedProjects;
this.packageManagementEvents = packageManagementEvents;
this.actionRunner = actionRunner;
this.logger = CreateLogger(logger);
CreateCommands();
}
protected virtual PackageViewModelOperationLogger CreateLogger(ILogger logger)
{
return new PackageViewModelOperationLogger(logger, package);
}
void CreateCommands()
{
addPackageCommand = new DelegateCommand(param => AddPackage());
removePackageCommand = new DelegateCommand(param => RemovePackage());
managePackageCommand = new DelegateCommand(param => ManagePackage());
}
public ICommand AddPackageCommand {
get { return addPackageCommand; }
}
public ICommand RemovePackageCommand {
get { return removePackageCommand; }
}
public ICommand ManagePackageCommand {
get { return managePackageCommand; }
}
public IPackage GetPackage()
{
return package;
}
public bool HasLicenseUrl {
get { return LicenseUrl != null; }
}
public Uri LicenseUrl {
get { return package.LicenseUrl; }
}
public bool HasProjectUrl {
get { return ProjectUrl != null; }
}
public Uri ProjectUrl {
get { return package.ProjectUrl; }
}
public bool HasReportAbuseUrl {
get { return ReportAbuseUrl != null; }
}
public Uri ReportAbuseUrl {
get { return package.ReportAbuseUrl; }
}
public bool IsAdded {
get { return IsPackageInstalled(); }
}
bool IsPackageInstalled()
{
return selectedProjects.IsPackageInstalled(package);
}
public IEnumerable<PackageDependencySet> Dependencies {
get { return package.DependencySets; }
}
public bool HasDependencies {
get { return package.HasDependencies; }
}
public bool HasNoDependencies {
get { return !HasDependencies; }
}
public IEnumerable<string> Authors {
get { return package.Authors; }
}
public bool HasDownloadCount {
get { return package.DownloadCount >= 0; }
}
public string Id {
get { return package.Id; }
}
public Uri IconUrl {
get { return package.IconUrl; }
}
public string Summary {
get {
if (!String.IsNullOrEmpty(package.Summary)) {
return package.Summary;
}
return package.Description;
}
}
public SemanticVersion Version {
get { return package.Version; }
}
public int DownloadCount {
get { return package.DownloadCount; }
}
public string Description {
get { return package.Description; }
}
public DateTimeOffset? LastPublished {
get { return package.Published; }
}
public bool HasLastPublished {
get { return package.Published.HasValue; }
}
public void AddPackage()
{
ClearReportedMessages();
logger.LogAddingPackage();
TryInstallingPackage();
logger.LogAfterPackageOperationCompletes();
}
void ClearReportedMessages()
{
packageManagementEvents.OnPackageOperationsStarting();
}
void GetPackageOperations()
{
IPackageManagementProject project = GetSingleProjectSelected();
project.Logger = logger;
var installAction = project.CreateInstallPackageAction();
packageOperations = project.GetInstallPackageOperations(package, installAction);
}
IPackageManagementProject GetSingleProjectSelected()
{
return selectedProjects.GetSingleProjectSelected(package.Repository);
}
bool CanInstallPackage()
{
IEnumerable<IPackage> packages = GetPackagesRequiringLicenseAcceptance();
if (packages.Any()) {
return packageManagementEvents.OnAcceptLicenses(packages);
}
return true;
}
IEnumerable<IPackage> GetPackagesRequiringLicenseAcceptance()
{
IList<IPackage> packagesToBeInstalled = GetPackagesToBeInstalled();
return GetPackagesRequiringLicenseAcceptance(packagesToBeInstalled);
}
IEnumerable<IPackage> GetPackagesRequiringLicenseAcceptance(IList<IPackage> packagesToBeInstalled)
{
return packagesToBeInstalled.Where(package => PackageRequiresLicenseAcceptance(package));
}
IList<IPackage> GetPackagesToBeInstalled()
{
List<IPackage> packages = new List<IPackage>();
foreach (PackageOperation operation in packageOperations) {
if (operation.Action == PackageAction.Install) {
packages.Add(operation.Package);
}
}
return packages;
}
bool PackageRequiresLicenseAcceptance(IPackage package)
{
return package.RequireLicenseAcceptance && !IsPackageInstalledInSolution(package);
}
bool IsPackageInstalledInSolution(IPackage package)
{
return selectedProjects.IsPackageInstalledInSolution(package);
}
void TryInstallingPackage()
{
try {
GetPackageOperations();
if (CanInstallPackage()) {
InstallPackage();
}
} catch (Exception ex) {
ReportError(ex);
logger.LogError(ex);
}
}
void InstallPackage()
{
InstallPackage(packageOperations);
OnPropertyChanged(model => model.IsAdded);
}
void InstallPackage(IEnumerable<PackageOperation> packageOperations)
{
IPackageManagementProject project = GetSingleProjectSelected();
ProcessPackageOperationsAction action = CreateInstallPackageAction(project);
action.Package = package;
action.Operations = packageOperations;
actionRunner.Run(action);
}
protected virtual ProcessPackageOperationsAction CreateInstallPackageAction(
IPackageManagementProject project)
{
return project.CreateInstallPackageAction();
}
void ReportError(Exception ex)
{
packageManagementEvents.OnPackageOperationError(ex);
}
public void RemovePackage()
{
ClearReportedMessages();
logger.LogRemovingPackage();
TryUninstallingPackage();
logger.LogAfterPackageOperationCompletes();
OnPropertyChanged(model => model.IsAdded);
}
void LogRemovingPackage()
{
logger.LogRemovingPackage();
}
void TryUninstallingPackage()
{
try {
IPackageManagementProject project = GetSingleProjectSelected();
UninstallPackageAction action = project.CreateUninstallPackageAction();
action.Package = package;
actionRunner.Run(action);
} catch (Exception ex) {
ReportError(ex);
logger.LogError(ex);
}
}
public bool IsManaged {
get {
if (selectedProjects.HasMultipleProjects()) {
return true;
}
return !selectedProjects.HasSingleProjectSelected();
}
}
public void ManagePackage()
{
List<IPackageManagementSelectedProject> projects = GetSelectedProjectsForPackage();
if (packageManagementEvents.OnSelectProjects(projects)) {
ManagePackagesForSelectedProjects(projects);
}
}
List<IPackageManagementSelectedProject> GetSelectedProjectsForPackage()
{
return selectedProjects.GetProjects(package).ToList();
}
public void ManagePackagesForSelectedProjects(IEnumerable<IPackageManagementSelectedProject> projects)
{
ManagePackagesForSelectedProjects(projects.ToList());
}
void ManagePackagesForSelectedProjects(IList<IPackageManagementSelectedProject> projects)
{
ClearReportedMessages();
logger.LogManagingPackage();
TryInstallingPackagesForSelectedProjects(projects);
logger.LogAfterPackageOperationCompletes();
OnPropertyChanged(model => model.IsAdded);
}
void TryInstallingPackagesForSelectedProjects(IList<IPackageManagementSelectedProject> projects)
{
try {
if (AnyProjectsSelected(projects)) {
InstallPackagesForSelectedProjects(projects);
}
} catch (Exception ex) {
ReportError(ex);
logger.LogError(ex);
}
}
protected virtual bool AnyProjectsSelected(IList<IPackageManagementSelectedProject> projects)
{
return projects.Any(project => project.IsSelected);
}
void InstallPackagesForSelectedProjects(IList<IPackageManagementSelectedProject> projects)
{
if (CanInstallPackage(projects)) {
IList<ProcessPackageAction> actions = GetProcessPackageActionsForSelectedProjects(projects);
RunActionsIfAnyExist(actions);
}
}
public virtual IList<ProcessPackageAction> GetProcessPackageActionsForSelectedProjects(
IList<IPackageManagementSelectedProject> selectedProjects)
{
var actions = new List<ProcessPackageAction>();
foreach (IPackageManagementSelectedProject selectedProject in selectedProjects) {
if (selectedProject.IsSelected) {
ProcessPackageAction action = CreateInstallPackageAction(selectedProject);
actions.Add(action);
}
}
return actions;
}
bool CanInstallPackage(IList<IPackageManagementSelectedProject> projects)
{
IPackageManagementSelectedProject project = projects.FirstOrDefault();
if (project != null) {
return CanInstallPackage(project);
}
return false;
}
bool CanInstallPackage(IPackageManagementSelectedProject selectedProject)
{
IEnumerable<IPackage> licensedPackages = GetPackagesRequiringLicenseAcceptance(selectedProject);
if (licensedPackages.Any()) {
return packageManagementEvents.OnAcceptLicenses(licensedPackages);
}
return true;
}
protected ProcessPackageAction CreateInstallPackageAction(IPackageManagementSelectedProject selectedProject)
{
IPackageManagementProject project = selectedProject.Project;
project.Logger = logger;
ProcessPackageOperationsAction action = CreateInstallPackageAction(project);
action.Package = package;
return action;
}
protected ProcessPackageAction CreateUninstallPackageAction(IPackageManagementSelectedProject selectedProject)
{
IPackageManagementProject project = selectedProject.Project;
project.Logger = logger;
ProcessPackageAction action = project.CreateUninstallPackageAction();
action.Package = package;
return action;
}
void RunActionsIfAnyExist(IList<ProcessPackageAction> actions)
{
if (actions.Any()) {
actionRunner.Run(actions);
}
}
IEnumerable<IPackage> GetPackagesRequiringLicenseAcceptance(IPackageManagementSelectedProject selectedProject)
{
IPackageManagementProject project = selectedProject.Project;
project.Logger = logger;
IEnumerable<PackageOperation> operations = project.GetInstallPackageOperations(package, project.CreateInstallPackageAction());
return GetPackagesRequiringLicenseAcceptance(operations);
}
IEnumerable<IPackage> GetPackagesRequiringLicenseAcceptance(IEnumerable<PackageOperation> operations)
{
foreach (PackageOperation operation in operations) {
if (PackageOperationRequiresLicenseAcceptance(operation)) {
yield return operation.Package;
}
}
}
bool PackageOperationRequiresLicenseAcceptance(PackageOperation operation)
{
return
(operation.Action == PackageAction.Install) &&
operation.Package.RequireLicenseAcceptance &&
!IsPackageInstalledInSolution(operation.Package);
}
}
}