forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImportPackageProvider.cs
More file actions
123 lines (102 loc) · 5.09 KB
/
Copy pathImportPackageProvider.cs
File metadata and controls
123 lines (102 loc) · 5.09 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
//
// 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.Management.Automation;
using System.Linq;
using Microsoft.PackageManagement.Internal.Utility.Extensions;
using Microsoft.PackageManagement.Internal.Packaging;
[Cmdlet("Import", Constants.Nouns.PackageProviderNoun, HelpUri = "http://go.microsoft.com/fwlink/?LinkId=626942")]
public sealed class ImportPackageProvider : CmdletBase {
protected override IEnumerable<string> ParameterSets {
get {
return new[] {""};
}
}
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
[Parameter(ValueFromPipelineByPropertyName = true, Position = 0, Mandatory = true)]
public string[] Name { get; set; }
[Parameter]
public string RequiredVersion {get; set;}
[Parameter]
public string MinimumVersion {get; set;}
[Parameter]
public string MaximumVersion {get; set;}
public override bool BeginProcessingAsync() {
ValidateVersion(RequiredVersion);
ValidateVersion(MinimumVersion);
ValidateVersion(MaximumVersion);
//Error out for the case where requiredVersion with with maximumVersion or minimumVersion
if (!string.IsNullOrWhiteSpace(RequiredVersion)) {
if (!string.IsNullOrWhiteSpace(MaximumVersion) || !string.IsNullOrWhiteSpace(MinimumVersion)) {
Error(Constants.Errors.VersionRangeAndRequiredVersionCannotBeSpecifiedTogether);
return false;
}
}
if (!string.IsNullOrWhiteSpace(MaximumVersion) && !string.IsNullOrWhiteSpace(MinimumVersion)
&& new Version(MaximumVersion) < new Version(MinimumVersion)) {
Error(Constants.Errors.MinimumVersionMustBeLessThanMaximumVersion, MinimumVersion, MaximumVersion);
return false;
}
return true;
}
public override bool ProcessRecordAsync() {
//Error out for the case where multiple provider names with any version specififed
if (((!Name.IsNullOrEmpty() && Name.Length > 1) || Name[0].ContainsWildcards())
&& ((RequiredVersion != null) || (MinimumVersion != null) || (MaximumVersion != null)))
{
Error(Constants.Errors.MultipleNamesWithVersionNotAllowed);
return false;
}
foreach (var path in Name) {
var isRooted = false;
var resolvedPath = path;
if (!string.IsNullOrWhiteSpace(path)) {
if (IsRooted(path)) {
if ((RequiredVersion != null) || (MaximumVersion !=null) || (MinimumVersion !=null)) {
Error(Constants.Errors.FullProviderFilePathVersionNotAllowed);
}
try {
ProviderInfo provider = null;
Collection<string> resolvedPaths = GetResolvedProviderPathFromPSPath(path, out provider);
// Ensure the path is a single path from the file system provider
if ((resolvedPaths.Count > 1) ||
(!String.Equals(provider.Name, "FileSystem", StringComparison.OrdinalIgnoreCase))) {
Error(Constants.Errors.FilePathMustBeFileSystemPath, path);
return false;
}
resolvedPath = resolvedPaths[0];
isRooted = true;
} catch (Exception ex) {
Error(Constants.Errors.FileNotFound, ex.Message);
return true;
}
}
foreach (var p in PackageManagementService.ImportPackageProvider(this, resolvedPath, RequiredVersion.ToVersion(),
MinimumVersion.ToVersion(), MaximumVersion.ToVersion(), isRooted, Force.IsPresent)) {
WriteObject(p);
}
}
} //foreach name
return true;
}
public override bool EndProcessingAsync() {
return base.EndProcessingAsync();
}
}
}