forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstallPackage.cs
More file actions
260 lines (219 loc) · 11 KB
/
Copy pathInstallPackage.cs
File metadata and controls
260 lines (219 loc) · 11 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
//
// 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.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Management.Automation;
using Microsoft.PackageManagement.Implementation;
using Microsoft.PackageManagement.Internal.Packaging;
using Microsoft.PackageManagement.Internal.Utility.Collections;
using Microsoft.PackageManagement.Internal.Utility.Extensions;
using Microsoft.PackageManagement.Packaging;
[Cmdlet(VerbsLifecycle.Install, Constants.Nouns.PackageNoun, SupportsShouldProcess = true, DefaultParameterSetName = Constants.ParameterSets.PackageBySearchSet, HelpUri = "http://go.microsoft.com/fwlink/?LinkID=517138")]
public sealed class InstallPackage : CmdletWithSearchAndSource {
public InstallPackage()
: base(new[] {
OptionCategory.Provider, OptionCategory.Source, OptionCategory.Package, OptionCategory.Install
}) {
}
protected override IEnumerable<string> ParameterSets {
get {
return new[] {Constants.ParameterSets.PackageBySearchSet, Constants.ParameterSets.PackageByInputObjectSet};
}
}
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
[Parameter(Mandatory = true, ValueFromPipeline = true, Position = 0, ParameterSetName = Constants.ParameterSets.PackageByInputObjectSet),]
public SoftwareIdentity[] InputObject {get; set;}
[Parameter(Position = 0, Mandatory = 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 DEEP_DEBUG
Console.WriteLine("»» Entering GCSP ");
#endif
if (!IsInvocation) {
#if DEEP_DEBUG
Console.WriteLine("»»» Does not appear to be Invocation (locked:{0})", IsReentrantLocked);
#endif
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 {
#if DEEP_DEBUG
Console.WriteLine("»»» Does appear to be Invocation (locked:{0})", IsReentrantLocked);
#endif
DynamicParameterDictionary.AddOrSet("ProviderName", new RuntimeDefinedParameter("ProviderName", typeof(string[]), new Collection<Attribute> {
new ParameterAttribute {
ValueFromPipelineByPropertyName = true,
ParameterSetName = Constants.ParameterSets.PackageBySearchSet
},
new AliasAttribute("Provider")
}));
}
}
public override bool BeginProcessingAsync() {
return true;
}
public override bool ProcessRecordAsync() {
if (IsPackageByObject) {
return InstallPackages(InputObject);
}
if (MyInvocation.BoundParameters.Count == 0 || (MyInvocation.BoundParameters.Count == 1 && MyInvocation.BoundParameters.ContainsKey("ProviderName")) ) {
// didn't pass in anything, (except maybe Providername)
// that's no ok -- we need some criteria
Error(Constants.Errors.MustSpecifyCriteria);
return false;
}
if (Name.Any(each => each.ContainsWildcards())) {
Error(Constants.Errors.WildCardCharsAreNotSupported, Name.JoinWithComma());
return false;
}
// otherwise, just do the search right now.
return base.ProcessRecordAsync();
}
public override bool EndProcessingAsync() {
if (IsPackageByObject) {
// we should have handled these already.
// buh-bye
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 base.InstallPackages(swids.ToArray());
}
protected override void ProcessPackage(PackageProvider provider, IEnumerable<string> searchKey, SoftwareIdentity package) {
if (WhatIf) {
// grab the dependencies and return them *first*
bool hasDependencyLoop = false;
var dependencies = GetDependenciesToInstall(package, ref hasDependencyLoop);
if (!hasDependencyLoop)
{
foreach (var dependency in dependencies)
{
base.ProcessPackage(provider, searchKey.Select(each => each+dependency.Name).ToArray(), dependency);
}
}
}
base.ProcessPackage(provider, searchKey, package);
}
private IEnumerable<SoftwareIdentity> GetDependenciesToInstall(SoftwareIdentity package, ref bool hasDependencyLoop)
{
// No dependency
if (package.Dependencies == null || package.Dependencies.Count() == 0)
{
return Enumerable.Empty<SoftwareIdentity>();
}
// Returns list of dependency to be installed in the correct order that we should install them
List<SoftwareIdentity> dependencyToBeInstalled = new List<SoftwareIdentity>();
HashSet<SoftwareIdentity> permanentlyMarked = new HashSet<SoftwareIdentity>(new SoftwareIdentityNameVersionComparer());
HashSet<SoftwareIdentity> temporarilyMarked = new HashSet<SoftwareIdentity>(new SoftwareIdentityNameVersionComparer());
// checks that there are no dependency loop
hasDependencyLoop = !DepthFirstVisit(package, temporarilyMarked, permanentlyMarked, dependencyToBeInstalled);
if (!hasDependencyLoop)
{
// remove the last item of the list because that is the package itself
dependencyToBeInstalled.RemoveAt(dependencyToBeInstalled.Count - 1);
return dependencyToBeInstalled;
}
// there are dependency loop.
return Enumerable.Empty<SoftwareIdentity>();
}
/// <summary>
/// Do a dfs visit. returns false if a cycle is encountered. Add the packageItem to the list at the end of each visit
/// </summary>
/// <param name="packageItem"></param>
/// <param name="dependencyToBeInstalled"></param>
/// <param name="permanentlyMarked"></param>
/// <param name="temporarilyMarked"></param>
/// <returns></returns>
internal bool DepthFirstVisit(SoftwareIdentity packageItem, HashSet<SoftwareIdentity> temporarilyMarked,
HashSet<SoftwareIdentity> permanentlyMarked, List<SoftwareIdentity> dependencyToBeInstalled)
{
// dependency loop detected because the element is temporarily marked
if (temporarilyMarked.Contains(packageItem))
{
return false;
}
// this is permanently marked. So we don't have to visit it.
// This is to resolve a case where we have: A->B->C and A->C. Then we need this when we visit C again from either B or A.
if (permanentlyMarked.Contains(packageItem))
{
return true;
}
// Mark this node temporarily so we can detect cycle.
temporarilyMarked.Add(packageItem);
// Visit the dependency
foreach (var dependency in packageItem.Dependencies)
{
var dependencies = PackageManagementService.FindPackageByCanonicalId(dependency, this);
var depPkg = dependencies.OrderByDescending(pp => pp, SoftwareIdentityVersionComparer.Instance).FirstOrDefault();
if (!DepthFirstVisit(depPkg, temporarilyMarked, permanentlyMarked, dependencyToBeInstalled))
{
// if dfs returns false then we have encountered a loop
return false;
}
// otherwise visit the next dependency
}
// Add the package to the list so we can install later
dependencyToBeInstalled.Add(packageItem);
// Done with this node so mark it permanently
permanentlyMarked.Add(packageItem);
// Unmark it temporarily
temporarilyMarked.Remove(packageItem);
return true;
}
}
}