forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindPackageProvider.cs
More file actions
189 lines (165 loc) · 7.94 KB
/
Copy pathFindPackageProvider.cs
File metadata and controls
189 lines (165 loc) · 7.94 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
//
// 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.Linq;
using System.Management.Automation;
using Microsoft.PackageManagement.Implementation;
using Microsoft.PackageManagement.Internal.Api;
using Microsoft.PackageManagement.Internal.Packaging;
using Microsoft.PackageManagement.Internal.Utility.Extensions;
using Microsoft.PackageManagement.Internal.Utility.Plugin;
using Microsoft.PackageManagement.Packaging;
using Utility;
[Cmdlet("Find", Constants.Nouns.PackageProviderNoun, HelpUri = "https://go.microsoft.com/fwlink/?LinkId=626939"), OutputType(new Type[1] {typeof(SoftwareIdentity)})]
public sealed class FindPackageProvider : CmdletWithSearchAndSource {
private const string FilterOnTag = "FilterOnTag";
public FindPackageProvider() : base(new[] {OptionCategory.Package}) {
//this will include bootstrap provider
ShouldSelectAllProviders = true;
}
[Parameter(ValueFromPipelineByPropertyName = true, Position = 0)]
public override string[] Name { get; set; }
[Parameter]
public override SwitchParameter AllVersions { get; set; }
[Parameter(ValueFromPipelineByPropertyName = true)]
public override string[] Source
{
get
{
return base.Source;
}
set
{
base.Source = value;
}
}
[Parameter]
public SwitchParameter IncludeDependencies { get; set; }
protected override void GenerateCmdletSpecificParameters(Dictionary<string, object> unboundArguments) {
//this will suppress the dynamic parameters from the parent classes
//Noop
}
protected override DynamicOption[] CachedDynamicOptions
{
//this will suppress the dynamic parameters from parent classes
get
{
return new[] {new DynamicOption()};
}
}
public override bool ProcessRecordAsync() {
ValidateVersion(RequiredVersion);
ValidateVersion(MinimumVersion);
ValidateVersion(MaximumVersion);
if (RequiredVersion != null) {
if ((!string.IsNullOrWhiteSpace(MaximumVersion)) || (!string.IsNullOrWhiteSpace(MinimumVersion))) {
Error(Constants.Errors.VersionRangeAndRequiredVersionCannotBeSpecifiedTogether);
return false;
}
}
//Error out for the case where multiple provider names with any version specified
if (Name.IsNullOrEmpty()) {
if ((!string.IsNullOrWhiteSpace(RequiredVersion)) || (!string.IsNullOrWhiteSpace(MinimumVersion)) || (!string.IsNullOrWhiteSpace(MaximumVersion))) {
Error(Constants.Errors.MultipleNamesWithVersionNotAllowed);
return false;
}
} else {
if (((Name.Length > 1) || Name.Any(each => each.ContainsWildcards())) &&
((!string.IsNullOrWhiteSpace(RequiredVersion)) || (!string.IsNullOrWhiteSpace(MinimumVersion)) || (!string.IsNullOrWhiteSpace(MaximumVersion)))) {
Error(Constants.Errors.MultipleNamesWithVersionNotAllowed);
return false;
}
}
if (AllVersions.IsPresent) {
if ((!string.IsNullOrWhiteSpace(RequiredVersion)) || (!string.IsNullOrWhiteSpace(MinimumVersion)) || (!string.IsNullOrWhiteSpace(MaximumVersion))) {
Error(Constants.Errors.AllVersionsCannotBeUsedWithOtherVersionParameters);
return false;
}
}
base.ProcessRecordAsync();
return true;
}
/// <summary>
/// Select existing providers on the system that will be used for searching any package providers in the repositories.
/// </summary>
protected override IEnumerable<PackageProvider> SelectedProviders
{
get
{
var availableProviders = base.SelectedProviders.ToArray();
if (availableProviders.Any(each => RequiredProviders.ContainsAnyOfIgnoreCase(each.ProviderName)))
{
return availableProviders.Where(each => RequiredProviders.ContainsIgnoreCase(each.ProviderName));
}
//if the existing available providers don't have what we required, error out
Error(Constants.Errors.RegisterPackageSourceRequired, Source.JoinWithComma());
return Enumerable.Empty<PackageProvider>();
}
}
protected override IHostApi GetProviderSpecificOption(PackageProvider pv)
{
var host = this.ProviderSpecific(pv);
var host1 = host;
//add filterontag for finding providers. Provider keys are: PackageManagement and Provider
host = host.Extend<IRequest>(
new
{
GetOptionValues = new Func<string, IEnumerable<string>>(key =>
{
if (key.EqualsIgnoreCase(FilterOnTag)) {
return ProviderFilters;
}
return host1.GetOptionValues(key);
}),
});
return host;
}
protected override bool EnsurePackageIsProvider(SoftwareIdentity package) {
//Make sure the package is a provider package
return ValidatePackageProvider(package);
}
protected override void ProcessPackage(PackageProvider provider, IEnumerable<string> searchKey, SoftwareIdentity package) {
Debug("Calling ProcessPackage SearchKey = '{0}' and provider name ='{1}'", searchKey, package.ProviderName);
try {
base.ProcessPackage(provider, searchKey, package);
// output to console
WriteObject(AddPropertyToSoftwareIdentity(package));
if (IncludeDependencies) {
var missingDependencies = new HashSet<string>();
foreach (var dep in package.Dependencies) {
var dependencies = PackageManagementService.FindPackageByCanonicalId(dep, this);
var depPkg = dependencies.OrderByDescending(pp => pp, SoftwareIdentityVersionComparer.Instance).FirstOrDefault();
if (depPkg == null) {
missingDependencies.Add(dep);
Warning(Constants.Messages.UnableToFindDependencyPackage, dep);
} else {
ProcessPackage(depPkg.Provider, searchKey.Select(each => each + depPkg.Name).ToArray(), depPkg);
}
}
if (missingDependencies.Any()) {
Error(Constants.Errors.UnableToFindDependencyPackage, missingDependencies.JoinWithComma());
}
}
} catch (Exception ex) {
Debug("Calling ProcessPackage {0}", ex.Message);
}
}
public override bool EndProcessingAsync() {
return CheckUnmatchedPackages();
}
}
}