1
- using System . Collections . Specialized ;
2
1
// Copyright (c) Microsoft Corporation. All rights reserved.
3
2
// Licensed under the MIT License.
4
3
using System ;
@@ -26,14 +25,14 @@ class InstallPSResource : PSCmdlet
26
25
/// Specifies the exact names of resources to install from a repository.
27
26
/// A comma-separated list of module names is accepted. The resource name must match the resource name in the repository.
28
27
/// </summary>
29
- [ Parameter ( Mandatory = true , Position = 0 , ValueFromPipelineByPropertyName = true , ParameterSetName = NameParameterSet ) ]
28
+ [ Parameter ( Mandatory = true , Position = 0 , ValueFromPipeline = true , ParameterSetName = NameParameterSet ) ]
30
29
[ ValidateNotNullOrEmpty ]
31
30
public string [ ] Name { get ; set ; }
32
31
33
32
/// <summary>
34
33
/// Specifies the version or version range of the package to be installed
35
34
/// </summary>
36
- [ Parameter ( ValueFromPipelineByPropertyName = true , ParameterSetName = NameParameterSet ) ]
35
+ [ Parameter ( ParameterSetName = NameParameterSet ) ]
37
36
[ ValidateNotNullOrEmpty ]
38
37
public string Version { get ; set ; }
39
38
@@ -54,44 +53,58 @@ class InstallPSResource : PSCmdlet
54
53
/// <summary>
55
54
/// Specifies a user account that has rights to find a resource from a specific repository.
56
55
/// </summary>
57
- [ Parameter ( ValueFromPipelineByPropertyName = true , ParameterSetName = NameParameterSet ) ]
56
+ [ Parameter ( ParameterSetName = NameParameterSet ) ]
57
+ [ Parameter ( ParameterSetName = InputObjectParameterSet ) ]
58
58
public PSCredential Credential { get ; set ; }
59
59
60
60
/// <summary>
61
61
/// Specifies the scope of installation.
62
62
/// </summary>
63
63
[ Parameter ( ParameterSetName = NameParameterSet ) ]
64
+ [ Parameter ( ParameterSetName = InputObjectParameterSet ) ]
64
65
public ScopeType Scope { get ; set ; }
65
66
66
67
/// <summary>
67
68
/// Suppresses being prompted for untrusted sources.
68
69
/// </summary>
69
70
[ Parameter ( ParameterSetName = NameParameterSet ) ]
71
+ [ Parameter ( ParameterSetName = InputObjectParameterSet ) ]
70
72
public SwitchParameter TrustRepository { get ; set ; }
71
73
72
74
/// <summary>
73
75
/// Overwrites a previously installed resource with the same name and version.
74
76
/// </summary>
75
77
[ Parameter ( ParameterSetName = NameParameterSet ) ]
78
+ [ Parameter ( ParameterSetName = InputObjectParameterSet ) ]
76
79
public SwitchParameter Reinstall { get ; set ; }
77
80
78
81
/// <summary>
79
82
/// Suppresses progress information.
80
83
/// </summary>
81
84
[ Parameter ( ParameterSetName = NameParameterSet ) ]
85
+ [ Parameter ( ParameterSetName = InputObjectParameterSet ) ]
82
86
public SwitchParameter Quiet { get ; set ; }
83
87
84
88
/// <summary>
85
89
/// For modules that require a license, AcceptLicense automatically accepts the license agreement during installation.
86
90
/// </summary>
87
91
[ Parameter ( ParameterSetName = NameParameterSet ) ]
92
+ [ Parameter ( ParameterSetName = InputObjectParameterSet ) ]
88
93
public SwitchParameter AcceptLicense { get ; set ; }
89
94
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
+
90
102
#endregion
91
103
92
104
#region Members
93
105
94
106
private const string NameParameterSet = "NameParameterSet" ;
107
+ private const string InputObjectParameterSet = "InputObjectParameterSet" ;
95
108
private const string RequiredResourceFileParameterSet = "RequiredResourceFileParameterSet" ;
96
109
private const string RequiredResourceParameterSet = "RequiredResourceParameterSet" ;
97
110
List < string > _pathsToInstallPkg ;
@@ -107,87 +120,51 @@ protected override void BeginProcessing()
107
120
// This is to create a better experience for those who have just installed v3 and want to get up and running quickly
108
121
RepositorySettings . CheckRepositoryStore ( ) ;
109
122
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
-
127
123
_pathsToInstallPkg = Utils . GetAllInstallationPaths ( this , Scope ) ;
128
124
}
129
125
130
126
protected override void ProcessRecord ( )
131
127
{
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
-
138
128
var installHelper = new InstallHelper ( updatePkg : false , savePkg : false , cmdletPassedIn : this ) ;
139
-
140
129
switch ( ParameterSetName )
141
130
{
142
131
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 )
145
135
{
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 ;
152
137
}
153
-
154
- foreach ( string error in errorMsgs )
138
+ else if ( ! Utils . TryParseVersionOrVersionRange ( Version , out _versionRange ) )
155
139
{
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 ) ;
161
144
}
162
145
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 ) )
166
155
{
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 ) ;
168
160
}
169
161
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 } ) ;
189
166
break ;
190
-
167
+
191
168
case RequiredResourceFileParameterSet :
192
169
ThrowTerminatingError ( new ErrorRecord (
193
170
new PSNotImplementedException ( "RequiredResourceFileParameterSet is not yet implemented. Please rerun cmdlet with other parameter set." ) ,
@@ -211,5 +188,63 @@ protected override void ProcessRecord()
211
188
}
212
189
213
190
#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
214
249
}
215
250
}
0 commit comments