Skip to content

Commit aa116d7

Browse files
authored
Add InputObject parameter sets for appropriate *-PSResource cmdlets, to provide pipeline support (#510)
Add InputObjects for {Install, Save, Uninstall}-PSResource cmdlets.
1 parent 0c9d93b commit aa116d7

File tree

6 files changed

+277
-181
lines changed

6 files changed

+277
-181
lines changed

src/code/InstallPSResource.cs

Lines changed: 102 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Collections.Specialized;
21
// Copyright (c) Microsoft Corporation. All rights reserved.
32
// Licensed under the MIT License.
43
using System;
@@ -26,14 +25,14 @@ class InstallPSResource : PSCmdlet
2625
/// Specifies the exact names of resources to install from a repository.
2726
/// A comma-separated list of module names is accepted. The resource name must match the resource name in the repository.
2827
/// </summary>
29-
[Parameter(Mandatory = true, Position = 0, ValueFromPipelineByPropertyName = true, ParameterSetName = NameParameterSet)]
28+
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true, ParameterSetName = NameParameterSet)]
3029
[ValidateNotNullOrEmpty]
3130
public string[] Name { get; set; }
3231

3332
/// <summary>
3433
/// Specifies the version or version range of the package to be installed
3534
/// </summary>
36-
[Parameter(ValueFromPipelineByPropertyName = true, ParameterSetName = NameParameterSet)]
35+
[Parameter(ParameterSetName = NameParameterSet)]
3736
[ValidateNotNullOrEmpty]
3837
public string Version { get; set; }
3938

@@ -54,44 +53,58 @@ class InstallPSResource : PSCmdlet
5453
/// <summary>
5554
/// Specifies a user account that has rights to find a resource from a specific repository.
5655
/// </summary>
57-
[Parameter(ValueFromPipelineByPropertyName = true, ParameterSetName = NameParameterSet)]
56+
[Parameter(ParameterSetName = NameParameterSet)]
57+
[Parameter(ParameterSetName = InputObjectParameterSet)]
5858
public PSCredential Credential { get; set; }
5959

6060
/// <summary>
6161
/// Specifies the scope of installation.
6262
/// </summary>
6363
[Parameter(ParameterSetName = NameParameterSet)]
64+
[Parameter(ParameterSetName = InputObjectParameterSet)]
6465
public ScopeType Scope { get; set; }
6566

6667
/// <summary>
6768
/// Suppresses being prompted for untrusted sources.
6869
/// </summary>
6970
[Parameter(ParameterSetName = NameParameterSet)]
71+
[Parameter(ParameterSetName = InputObjectParameterSet)]
7072
public SwitchParameter TrustRepository { get; set; }
7173

7274
/// <summary>
7375
/// Overwrites a previously installed resource with the same name and version.
7476
/// </summary>
7577
[Parameter(ParameterSetName = NameParameterSet)]
78+
[Parameter(ParameterSetName = InputObjectParameterSet)]
7679
public SwitchParameter Reinstall { get; set; }
7780

7881
/// <summary>
7982
/// Suppresses progress information.
8083
/// </summary>
8184
[Parameter(ParameterSetName = NameParameterSet)]
85+
[Parameter(ParameterSetName = InputObjectParameterSet)]
8286
public SwitchParameter Quiet { get; set; }
8387

8488
/// <summary>
8589
/// For modules that require a license, AcceptLicense automatically accepts the license agreement during installation.
8690
/// </summary>
8791
[Parameter(ParameterSetName = NameParameterSet)]
92+
[Parameter(ParameterSetName = InputObjectParameterSet)]
8893
public SwitchParameter AcceptLicense { get; set; }
8994

95+
/// <summary>
96+
/// Used for pipeline input.
97+
/// </summary>
98+
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true, ParameterSetName = InputObjectParameterSet)]
99+
[ValidateNotNullOrEmpty]
100+
public PSResourceInfo InputObject { get; set; }
101+
90102
#endregion
91103

92104
#region Members
93105

94106
private const string NameParameterSet = "NameParameterSet";
107+
private const string InputObjectParameterSet = "InputObjectParameterSet";
95108
private const string RequiredResourceFileParameterSet = "RequiredResourceFileParameterSet";
96109
private const string RequiredResourceParameterSet = "RequiredResourceParameterSet";
97110
List<string> _pathsToInstallPkg;
@@ -107,87 +120,51 @@ protected override void BeginProcessing()
107120
// This is to create a better experience for those who have just installed v3 and want to get up and running quickly
108121
RepositorySettings.CheckRepositoryStore();
109122

110-
// validate that if a -Version param is passed in that it can be parsed into a NuGet version range.
111-
// An exact version will be formatted into a version range.
112-
if (ParameterSetName.Equals(NameParameterSet) && Version != null && !Utils.TryParseVersionOrVersionRange(Version, out _versionRange))
113-
114-
{
115-
var exMessage = "Argument for -Version parameter is not in the proper format.";
116-
var ex = new ArgumentException(exMessage);
117-
var IncorrectVersionFormat = new ErrorRecord(ex, "IncorrectVersionFormat", ErrorCategory.InvalidArgument, null);
118-
ThrowTerminatingError(IncorrectVersionFormat);
119-
}
120-
121-
// if no Version specified, install latest version for the package
122-
if (Version == null)
123-
{
124-
_versionRange = VersionRange.All;
125-
}
126-
127123
_pathsToInstallPkg = Utils.GetAllInstallationPaths(this, Scope);
128124
}
129125

130126
protected override void ProcessRecord()
131127
{
132-
if (!ShouldProcess(string.Format("package to install: '{0}'", String.Join(", ", Name))))
133-
{
134-
WriteVerbose(string.Format("Install operation cancelled by user for packages: {0}", String.Join(", ", Name)));
135-
return;
136-
}
137-
138128
var installHelper = new InstallHelper(updatePkg: false, savePkg: false, cmdletPassedIn: this);
139-
140129
switch (ParameterSetName)
141130
{
142131
case NameParameterSet:
143-
var namesToInstall = Utils.ProcessNameWildcards(Name, out string[] errorMsgs, out bool nameContainsWildcard);
144-
if (nameContainsWildcard)
132+
// If no Version specified, install latest version for the package.
133+
// Otherwise validate Version can be parsed out successfully.
134+
if (Version == null)
145135
{
146-
WriteError(new ErrorRecord(
147-
new PSInvalidOperationException("Name with wildcards is not supported for Install-PSResource cmdlet"),
148-
"NameContainsWildcard",
149-
ErrorCategory.InvalidArgument,
150-
this));
151-
return;
136+
_versionRange = VersionRange.All;
152137
}
153-
154-
foreach (string error in errorMsgs)
138+
else if (!Utils.TryParseVersionOrVersionRange(Version, out _versionRange))
155139
{
156-
WriteError(new ErrorRecord(
157-
new PSInvalidOperationException(error),
158-
"ErrorFilteringNamesForUnsupportedWildcards",
159-
ErrorCategory.InvalidArgument,
160-
this));
140+
var exMessage = "Argument for -Version parameter is not in the proper format.";
141+
var ex = new ArgumentException(exMessage);
142+
var IncorrectVersionFormat = new ErrorRecord(ex, "IncorrectVersionFormat", ErrorCategory.InvalidArgument, null);
143+
ThrowTerminatingError(IncorrectVersionFormat);
161144
}
162145

163-
// this catches the case where Name wasn't passed in as null or empty,
164-
// but after filtering out unsupported wildcard names there are no elements left in namesToInstall
165-
if (namesToInstall.Length == 0)
146+
ProcessInstallHelper(installHelper: installHelper,
147+
pkgNames: Name,
148+
pkgPrerelease: Prerelease,
149+
pkgRepository: Repository);
150+
break;
151+
152+
case InputObjectParameterSet:
153+
string normalizedVersionString = Utils.GetNormalizedVersionString(InputObject.Version.ToString(), InputObject.PrereleaseLabel);
154+
if (!Utils.TryParseVersionOrVersionRange(normalizedVersionString, out _versionRange))
166155
{
167-
return;
156+
var exMessage = String.Format("Version '{0}' for resource '{1}' cannot be parsed.", normalizedVersionString, InputObject.Name);
157+
var ex = new ArgumentException(exMessage);
158+
var ErrorParsingVersion = new ErrorRecord(ex, "ErrorParsingVersion", ErrorCategory.ParserError, null);
159+
WriteError(ErrorParsingVersion);
168160
}
169161

170-
installHelper.InstallPackages(
171-
names: namesToInstall,
172-
versionRange: _versionRange,
173-
prerelease: Prerelease,
174-
repository: Repository,
175-
acceptLicense: AcceptLicense,
176-
quiet: Quiet,
177-
reinstall: Reinstall,
178-
force: false,
179-
trustRepository: TrustRepository,
180-
noClobber: false,
181-
credential: Credential,
182-
requiredResourceFile: null,
183-
requiredResourceJson: null,
184-
requiredResourceHash: null,
185-
specifiedPath: null,
186-
asNupkg: false,
187-
includeXML: true,
188-
pathsToInstallPkg: _pathsToInstallPkg);
162+
ProcessInstallHelper(installHelper: installHelper,
163+
pkgNames: new string[] { InputObject.Name },
164+
pkgPrerelease: InputObject.IsPrerelease,
165+
pkgRepository: new string[]{ InputObject.Repository });
189166
break;
190-
167+
191168
case RequiredResourceFileParameterSet:
192169
ThrowTerminatingError(new ErrorRecord(
193170
new PSNotImplementedException("RequiredResourceFileParameterSet is not yet implemented. Please rerun cmdlet with other parameter set."),
@@ -211,5 +188,63 @@ protected override void ProcessRecord()
211188
}
212189

213190
#endregion
191+
192+
#region Methods
193+
private void ProcessInstallHelper(InstallHelper installHelper, string[] pkgNames, bool pkgPrerelease, string[] pkgRepository)
194+
{
195+
var inputNameToInstall = Utils.ProcessNameWildcards(pkgNames, out string[] errorMsgs, out bool nameContainsWildcard);
196+
if (nameContainsWildcard)
197+
{
198+
WriteError(new ErrorRecord(
199+
new PSInvalidOperationException("Name with wildcards is not supported for Install-PSResource cmdlet"),
200+
"NameContainsWildcard",
201+
ErrorCategory.InvalidArgument,
202+
this));
203+
return;
204+
}
205+
206+
foreach (string error in errorMsgs)
207+
{
208+
WriteError(new ErrorRecord(
209+
new PSInvalidOperationException(error),
210+
"ErrorFilteringNamesForUnsupportedWildcards",
211+
ErrorCategory.InvalidArgument,
212+
this));
213+
}
214+
215+
// this catches the case where Name wasn't passed in as null or empty,
216+
// but after filtering out unsupported wildcard names there are no elements left in namesToInstall
217+
if (inputNameToInstall.Length == 0)
218+
{
219+
return;
220+
}
221+
222+
if (!ShouldProcess(string.Format("package to install: '{0}'", String.Join(", ", inputNameToInstall))))
223+
{
224+
WriteVerbose(string.Format("Install operation cancelled by user for packages: {0}", String.Join(", ", inputNameToInstall)));
225+
return;
226+
}
227+
228+
installHelper.InstallPackages(
229+
names: pkgNames,
230+
versionRange: _versionRange,
231+
prerelease: pkgPrerelease,
232+
repository: pkgRepository,
233+
acceptLicense: AcceptLicense,
234+
quiet: Quiet,
235+
reinstall: Reinstall,
236+
force: false,
237+
trustRepository: TrustRepository,
238+
noClobber: false,
239+
credential: Credential,
240+
requiredResourceFile: null,
241+
requiredResourceJson: null,
242+
requiredResourceHash: null,
243+
specifiedPath: null,
244+
asNupkg: false,
245+
includeXML: true,
246+
pathsToInstallPkg: _pathsToInstallPkg);
247+
}
248+
#endregion
214249
}
215250
}

0 commit comments

Comments
 (0)