Skip to content

Commit 3aa7901

Browse files
committed
Check for ModulePrefix parameter presence instead of it being a dynamic parameter
1 parent 3399c73 commit 3aa7901

File tree

1 file changed

+38
-29
lines changed

1 file changed

+38
-29
lines changed

src/code/PublishPSResource.cs

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@ namespace Microsoft.PowerShell.PSResourceGet.Cmdlets
1717
"PSResource",
1818
SupportsShouldProcess = true)]
1919
[Alias("pbres")]
20-
public sealed class PublishPSResource : PSCmdlet, IDynamicParameters
20+
public sealed class PublishPSResource : PSCmdlet
2121
{
2222
#region Parameters
2323

2424
private const string PathParameterSet = "PathParameterSet";
2525
private const string NupkgPathParameterSet = "NupkgPathParameterSet";
26-
private ContainerRegistryDynamicParameters _pkgPrefix;
2726

2827
/// <summary>
2928
/// Specifies the API key that you want to use to publish a module to the online gallery.
@@ -117,20 +116,12 @@ public PSCredential ProxyCredential {
117116
[ValidateNotNullOrEmpty]
118117
public string NupkgPath { get; set; }
119118

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; }
134125

135126
#endregion
136127

@@ -151,16 +142,42 @@ protected override void BeginProcessing()
151142

152143
_networkCredential = Credential != null ? new NetworkCredential(Credential.UserName, Credential.Password) : null;
153144

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+
154175
if (!string.IsNullOrEmpty(NupkgPath))
155176
{
156177
_isNupkgPathSpecified = true;
157178
Path = NupkgPath;
158179
}
159180

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-
164181
_publishHelper = new PublishHelper(
165182
this,
166183
Credential,
@@ -186,18 +203,10 @@ protected override void EndProcessing()
186203
return;
187204
}
188205

189-
string modulePrefix = _pkgPrefix?.ModulePrefix;
190-
191-
_publishHelper.PushResource(Repository, modulePrefix, SkipDependenciesCheck, _networkCredential);
206+
_publishHelper.PushResource(Repository, ModulePrefix, SkipDependenciesCheck, _networkCredential);
192207
}
193208

194209
#endregion
195210

196211
}
197-
198-
public class ContainerRegistryDynamicParameters
199-
{
200-
[Parameter]
201-
public string ModulePrefix { get; set; }
202-
}
203212
}

0 commit comments

Comments
 (0)