-
Notifications
You must be signed in to change notification settings - Fork 743
Expand file tree
/
Copy pathUninstallPackageCommand.cs
More file actions
182 lines (154 loc) · 6.45 KB
/
UninstallPackageCommand.cs
File metadata and controls
182 lines (154 loc) · 6.45 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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#nullable disable
using System;
using System.Diagnostics;
using System.Management.Automation;
using System.Threading;
using NuGet.Common;
using NuGet.PackageManagement.Telemetry;
using NuGet.ProjectManagement;
using NuGet.Protocol.Core.Types;
using NuGet.VisualStudio;
using Task = System.Threading.Tasks.Task;
namespace NuGet.PackageManagement.PowerShellCmdlets
{
[Cmdlet(VerbsLifecycle.Uninstall, "Package")]
public class UninstallPackageCommand : NuGetPowerShellBaseCommand
{
private readonly IDeleteOnRestartManager _deleteOnRestartManager;
private readonly INuGetLockService _lockService;
private UninstallationContext _context;
public UninstallPackageCommand()
{
_deleteOnRestartManager = ServiceLocator.GetComponentModelService<IDeleteOnRestartManager>();
_lockService = ServiceLocator.GetComponentModelService<INuGetLockService>();
}
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, Position = 0)]
public virtual string Id { get; set; }
[Parameter(ValueFromPipelineByPropertyName = true, Position = 1)]
[ValidateNotNullOrEmpty]
public virtual string ProjectName { get; set; }
[Parameter(Position = 2)]
[ValidateNotNullOrEmpty]
public virtual string Version { get; set; }
[Parameter]
public SwitchParameter WhatIf { get; set; }
[Parameter]
public SwitchParameter Force { get; set; }
[Parameter]
public SwitchParameter RemoveDependencies { get; set; }
private void Preprocess()
{
CheckSolutionState();
NuGetUIThreadHelper.JoinableTaskFactory.Run(async () =>
{
await GetNuGetProjectAsync(ProjectName);
await CheckMissingPackagesAsync();
});
ActionType = NuGetActionType.Uninstall;
}
protected override void ProcessRecordCore()
{
var startTime = DateTimeOffset.Now;
_packageCount = 1;
var stopWatch = Stopwatch.StartNew();
// Run Preprocess outside of JTF
Preprocess();
NuGetUIThreadHelper.JoinableTaskFactory.Run(async () =>
{
await _lockService.ExecuteNuGetOperationAsync(() =>
{
SubscribeToProgressEvents();
Task.Run(UninstallPackageAsync);
WaitAndLogPackageActions();
UnsubscribeFromProgressEvents();
#pragma warning disable VSTHRD003 // Avoid awaiting foreign Tasks
return TaskResult.True;
#pragma warning restore VSTHRD003 // Avoid awaiting foreign Tasks
}, Token);
});
stopWatch.Stop();
var isPackageSourceMappingEnabled = PackageSourceMappingUtility.IsMappingEnabled(ConfigSettings);
var actionTelemetryEvent = VSTelemetryServiceUtility.GetActionTelemetryEvent(
OperationId.ToString(),
new[] { Project },
NuGetProjectActionType.Uninstall,
OperationSource.PMC,
startTime,
_status,
_packageCount,
stopWatch.Elapsed.TotalSeconds,
isPackageSourceMappingEnabled: isPackageSourceMappingEnabled);
// emit telemetry event along with granular level events
TelemetryActivity.EmitTelemetryEvent(actionTelemetryEvent);
}
protected override void EndProcessing()
{
base.EndProcessing();
var packageDirectoriesMarkedForDeletion = _deleteOnRestartManager.GetPackageDirectoriesMarkedForDeletion();
if (packageDirectoriesMarkedForDeletion != null && packageDirectoriesMarkedForDeletion.Count != 0)
{
_deleteOnRestartManager.CheckAndRaisePackageDirectoriesMarkedForDeletion();
var message = string.Format(
System.Globalization.CultureInfo.CurrentCulture,
Resources.Cmdlet_RequestRestartToCompleteUninstall,
string.Join(", ", packageDirectoriesMarkedForDeletion));
WriteWarning(message);
}
}
/// <summary>
/// Async call for uninstall a package from the current project
/// </summary>
private async Task UninstallPackageAsync()
{
try
{
await UninstallPackageByIdAsync(Project, Id, UninstallContext, this, WhatIf.IsPresent);
}
catch (Exception ex)
{
_status = NuGetOperationStatus.Failed;
Log(MessageLevel.Error, ExceptionUtilities.DisplayMessage(ex));
}
finally
{
BlockingCollection.Add(new ExecutionCompleteMessage());
}
}
/// <summary>
/// Uninstall package by Id
/// </summary>
/// <param name="project"></param>
/// <param name="packageId"></param>
/// <param name="uninstallContext"></param>
/// <param name="projectContext"></param>
/// <param name="isPreview"></param>
/// <returns></returns>
protected async Task UninstallPackageByIdAsync(NuGetProject project, string packageId, UninstallationContext uninstallContext, INuGetProjectContext projectContext, bool isPreview)
{
var actions = await PackageManager.PreviewUninstallPackageAsync(project, packageId, uninstallContext, projectContext, CancellationToken.None);
if (isPreview)
{
PreviewNuGetPackageActions(actions);
}
else
{
await PackageManager.ExecuteNuGetProjectActionsAsync(project, actions, projectContext, NullSourceCacheContext.Instance, CancellationToken.None);
// Refresh Manager UI if needed
RefreshUI(actions);
}
}
/// <summary>
/// Uninstallation Context for Uninstall-Package command
/// </summary>
public UninstallationContext UninstallContext
{
get
{
_context = new UninstallationContext(RemoveDependencies.IsPresent, Force.IsPresent);
return _context;
}
}
}
}