@@ -17,13 +17,12 @@ namespace Microsoft.PowerShell.PSResourceGet.Cmdlets
17
17
"PSResource" ,
18
18
SupportsShouldProcess = true ) ]
19
19
[ Alias ( "pbres" ) ]
20
- public sealed class PublishPSResource : PSCmdlet , IDynamicParameters
20
+ public sealed class PublishPSResource : PSCmdlet
21
21
{
22
22
#region Parameters
23
23
24
24
private const string PathParameterSet = "PathParameterSet" ;
25
25
private const string NupkgPathParameterSet = "NupkgPathParameterSet" ;
26
- private ContainerRegistryDynamicParameters _pkgPrefix ;
27
26
28
27
/// <summary>
29
28
/// Specifies the API key that you want to use to publish a module to the online gallery.
@@ -117,20 +116,12 @@ public PSCredential ProxyCredential {
117
116
[ ValidateNotNullOrEmpty ]
118
117
public string NupkgPath { get ; set ; }
119
118
120
- #endregion
121
-
122
- #region DynamicParameters
123
- public object GetDynamicParameters ( )
124
- {
125
- PSRepositoryInfo repository = RepositorySettings . Read ( new [ ] { Repository } , out string [ ] _ ) . FirstOrDefault ( ) ;
126
- if ( repository is not null && repository . ApiVersion == PSRepositoryInfo . APIVersion . ContainerRegistry )
127
- {
128
- _pkgPrefix = new ContainerRegistryDynamicParameters ( ) ;
129
- return _pkgPrefix ;
130
- }
131
-
132
- return null ;
133
- }
119
+ /// <summary>
120
+ /// Prefix for module name which only applies to repositories of type 'ContainerRegistry'
121
+ /// </summary>
122
+ [ Parameter ]
123
+ [ ValidateNotNullOrEmpty ]
124
+ public string ModulePrefix { get ; set ; }
134
125
135
126
#endregion
136
127
@@ -151,16 +142,42 @@ protected override void BeginProcessing()
151
142
152
143
_networkCredential = Credential != null ? new NetworkCredential ( Credential . UserName , Credential . Password ) : null ;
153
144
145
+ // Create a respository story (the PSResourceRepository.xml file) if it does not already exist
146
+ // This is to create a better experience for those who have just installed v3 and want to get up and running quickly
147
+ RepositorySettings . CheckRepositoryStore ( ) ;
148
+
149
+ if ( MyInvocation . BoundParameters . ContainsKey ( nameof ( ModulePrefix ) ) )
150
+ {
151
+ if ( MyInvocation . BoundParameters . ContainsKey ( nameof ( Repository ) ) ) // can remove if Repository is 'Mandatory' parameter
152
+ {
153
+ // at this point it is ensured PSResourceRepository.xml file is created
154
+ PSRepositoryInfo repository = RepositorySettings . Read ( new [ ] { Repository } , out string [ ] _ ) . FirstOrDefault ( ) ;
155
+ if ( repository is null || repository . ApiVersion != PSRepositoryInfo . APIVersion . ContainerRegistry )
156
+ {
157
+ ThrowTerminatingError ( new ErrorRecord (
158
+ new PSInvalidOperationException ( "ModulePrefix parameter can only be provided with the Repository parameter for a registered repository of type 'ContainerRegistry'" ) ,
159
+ "ModulePrefixParameterIncorrectlyProvided" ,
160
+ ErrorCategory . InvalidOperation ,
161
+ this ) ) ;
162
+ }
163
+ }
164
+ else
165
+ {
166
+ // can remove if Repository is 'Mandatory' parameter
167
+ ThrowTerminatingError ( new ErrorRecord (
168
+ new PSInvalidOperationException ( "ModulePrefix parameter can only be provided with the Repository parameter." ) ,
169
+ "ModulePrefixParameterProvidedWithoutRepositoryParameter" ,
170
+ ErrorCategory . InvalidOperation ,
171
+ this ) ) ;
172
+ }
173
+ }
174
+
154
175
if ( ! string . IsNullOrEmpty ( NupkgPath ) )
155
176
{
156
177
_isNupkgPathSpecified = true ;
157
178
Path = NupkgPath ;
158
179
}
159
180
160
- // Create a respository story (the PSResourceRepository.xml file) if it does not already exist
161
- // This is to create a better experience for those who have just installed v3 and want to get up and running quickly
162
- RepositorySettings . CheckRepositoryStore ( ) ;
163
-
164
181
_publishHelper = new PublishHelper (
165
182
this ,
166
183
Credential ,
@@ -186,18 +203,10 @@ protected override void EndProcessing()
186
203
return ;
187
204
}
188
205
189
- string modulePrefix = _pkgPrefix ? . ModulePrefix ;
190
-
191
- _publishHelper . PushResource ( Repository , modulePrefix , SkipDependenciesCheck , _networkCredential ) ;
206
+ _publishHelper . PushResource ( Repository , ModulePrefix , SkipDependenciesCheck , _networkCredential ) ;
192
207
}
193
208
194
209
#endregion
195
210
196
211
}
197
-
198
- public class ContainerRegistryDynamicParameters
199
- {
200
- [ Parameter ]
201
- public string ModulePrefix { get ; set ; }
202
- }
203
212
}
0 commit comments