forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSavePackage.cs
More file actions
279 lines (238 loc) · 11.1 KB
/
Copy pathSavePackage.cs
File metadata and controls
279 lines (238 loc) · 11.1 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
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
namespace Microsoft.PowerShell.PackageManagement.Cmdlets {
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Management.Automation;
using Microsoft.PackageManagement.Implementation;
using Microsoft.PackageManagement.Internal.Implementation;
using Microsoft.PackageManagement.Internal.Packaging;
using Microsoft.PackageManagement.Internal.Utility.Async;
using Microsoft.PackageManagement.Internal.Utility.Collections;
using Microsoft.PackageManagement.Internal.Utility.Extensions;
using Microsoft.PackageManagement.Packaging;
using Utility;
using Directory = System.IO.Directory;
using File = System.IO.File;
using Microsoft.PackageManagement.Internal.Utility.Versions;
[Cmdlet(VerbsData.Save, Constants.Nouns.PackageNoun, SupportsShouldProcess = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=517140")]
public sealed class SavePackage : CmdletWithSearchAndSource {
public SavePackage()
: base(new[] {
OptionCategory.Provider, OptionCategory.Source, OptionCategory.Package
}) {
}
protected override IEnumerable<string> ParameterSets {
get {
return new[] {Constants.ParameterSets.PackageByInputObjectSet, ""};
}
}
[Parameter(Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = Constants.ParameterSets.PackageBySearchSet)]
public override string[] Name { get; set; }
[Parameter(ParameterSetName = Constants.ParameterSets.PackageBySearchSet)]
public override string RequiredVersion { get; set; }
[Parameter(ParameterSetName = Constants.ParameterSets.PackageBySearchSet)]
public override string MinimumVersion { get; set; }
[Parameter(ParameterSetName = Constants.ParameterSets.PackageBySearchSet)]
public override string MaximumVersion { get; set; }
[Parameter(ValueFromPipelineByPropertyName = true, ParameterSetName = Constants.ParameterSets.PackageBySearchSet)]
// Use the base Source property so relative path will be resolved
public override string[] Source
{
get
{
return base.Source;
}
set
{
base.Source = value;
}
}
protected override void GenerateCmdletSpecificParameters(Dictionary<string, object> unboundArguments) {
if (!IsInvocation) {
var providerNames = PackageManagementService.AllProviderNames;
var whatsOnCmdline = GetDynamicParameterValue<string[]>("ProviderName");
if (whatsOnCmdline != null) {
providerNames = providerNames.Concat(whatsOnCmdline).Distinct();
}
DynamicParameterDictionary.AddOrSet("ProviderName", new RuntimeDefinedParameter("ProviderName", typeof(string[]), new Collection<Attribute> {
new ParameterAttribute {
ValueFromPipelineByPropertyName = true,
ParameterSetName = Constants.ParameterSets.PackageBySearchSet
},
new AliasAttribute("Provider"),
new ValidateSetAttribute(providerNames.ToArray())
}));
}
else {
DynamicParameterDictionary.AddOrSet("ProviderName", new RuntimeDefinedParameter("ProviderName", typeof(string[]), new Collection<Attribute> {
new ParameterAttribute {
ValueFromPipelineByPropertyName = true,
ParameterSetName = Constants.ParameterSets.PackageBySearchSet
},
new AliasAttribute("Provider")
}));
}
}
[Parameter]
public string Path {get; set;}
[Parameter]
public string LiteralPath {get; set;}
[Parameter(Mandatory = true, ValueFromPipeline = true, ParameterSetName = Constants.ParameterSets.PackageByInputObjectSet)]
public SoftwareIdentity InputObject {get; set;}
private string SaveFileName(string packageName) {
string resolvedPath = null;
try
{
if (!string.IsNullOrWhiteSpace(Path))
{
resolvedPath = ResolveExistingFolderPath(Path, !Force);
}
if (!string.IsNullOrWhiteSpace(LiteralPath))
{
// Validate that the path exists
try
{
SessionState.InvokeProvider.Item.Get(new string[] { LiteralPath }, false, true);
}
catch (ItemNotFoundException)
{
if(!Force)
{
throw;
}
}
resolvedPath = LiteralPath;
}
if (string.IsNullOrWhiteSpace(resolvedPath)) {
Error(Constants.Errors.DestinationPathInvalid, resolvedPath, packageName);
return null;
}
// If the destination directory doesn't exist, create it
if (!Directory.Exists(resolvedPath)) {
Directory.CreateDirectory(resolvedPath);
}
// don't append path and package name here
return resolvedPath;
}
catch (Exception e)
{
Error(Constants.Errors.SavePackageError, e.Message);
return null;
}
}
public override bool ProcessRecordAsync() {
if (string.IsNullOrWhiteSpace(Path) && string.IsNullOrWhiteSpace(LiteralPath)) {
Error(Constants.Errors.DestinationOrLiteralPathNotSpecified);
return false;
}
if (IsPackageByObject)
{
return DownloadPackage(InputObject);
}
if (Name.Any(each => each.ContainsWildcards()))
{
Error(Constants.Errors.WildCardCharsAreNotSupported, Name.JoinWithComma());
return false;
}
return base.ProcessRecordAsync();
}
public override bool EndProcessingAsync()
{
if (IsCanceled)
{
return false;
}
if (IsPackageByObject)
{
// we should have handled these already in ProcessRecordAsync()
return true;
}
if (!CheckUnmatchedPackages())
{
// there are unmatched packages
// not going to install.
return false;
}
var swids = CheckMatchedDuplicates().ReEnumerable();
if (swids == null || !swids.Any())
{
// there are duplicate packages
// not going to install.
return false;
}
// good list. Let's roll...
return DownloadPackage(swids.ToArray());
}
private bool DownloadPackage(params SoftwareIdentity[] packagesToSave)
{
foreach (var package in packagesToSave)
{
if (IsCanceled)
{
return false;
}
var provider = package.Provider;
if (!provider.IsMethodImplemented("DownloadPackage"))
{
Error(Constants.Errors.MethodNotImplemented, provider.ProviderName, "Save-Package");
return false;
}
// if we do save-package jquery -path C:\test then savepath would be C:\test
var savePath = SaveFileName(package.PackageFilename);
bool mainPackageDownloaded = false;
if (!string.IsNullOrWhiteSpace(savePath))
{
// let the provider handles everything
// message would be something like What if: Performing the operation "Save Package" on target "'jQuery' to location 'C:\test\test'".
if (ShouldProcess(FormatMessageString(Resources.Messages.SavePackageWhatIfDescription, package.Name, savePath), FormatMessageString(Resources.Messages.SavePackage)).Result)
{
var host = this.ProviderSpecific(provider);
foreach (var downloadedPkg in provider.DownloadPackage(package, savePath, host).CancelWhen(CancellationEvent.Token))
//foreach (var downloadedPkg in provider.DownloadPackage(package, savePath, ErrorAndWarningContinue ? host.SuppressErrorsAndWarnings(IsProcessing) : host).CancelWhen(CancellationEvent.Token))
{
if (IsCanceled)
{
Error(Constants.Errors.ProviderFailToDownloadFile, downloadedPkg.PackageFilename, provider.ProviderName);
return false;
}
// check whether main package is downloaded;
if (downloadedPkg.Name.EqualsIgnoreCase(package.Name) && (FourPartVersion)downloadedPkg.Version >= (FourPartVersion)package.Version)
{
mainPackageDownloaded = true;
}
WriteObject(AddPropertyToSoftwareIdentity(downloadedPkg));
LogEvent(EventTask.Download, EventId.Save, Resources.Messages.PackageSaved, downloadedPkg.Name, downloadedPkg.Version, downloadedPkg.ProviderName, downloadedPkg.Source ?? string.Empty, downloadedPkg.Status ?? string.Empty, downloadedPkg.InstallationPath ?? string.Empty);
TraceMessage(Constants.SavePackageTrace, downloadedPkg);
}
}
else
{
// What if scenario, don't error out
return true;
}
}
if (!mainPackageDownloaded)
{
Error(Constants.Errors.ProviderFailToDownloadFile, package.PackageFilename, provider.ProviderName);
return false;
}
}
return true;
}
}
}