Skip to content

Fix 1794 by handling if prerelease is supplied as boolean too #1843

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions src/code/InstallPSResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,14 @@ protected override void ProcessRecord()
break;

case InputObjectParameterSet:
foreach (var inputObj in InputObject) {
foreach (var inputObj in InputObject)
{
string normalizedVersionString = Utils.GetNormalizedVersionString(inputObj.Version.ToString(), inputObj.Prerelease);
ProcessInstallHelper(
pkgNames: new string[] { inputObj.Name },
pkgVersion: normalizedVersionString,
pkgPrerelease: inputObj.IsPrerelease,
pkgRepository: new string[]{ inputObj.Repository },
pkgRepository: new string[] { inputObj.Repository },
pkgCredential: Credential,
reqResourceParams: null);
}
Expand Down Expand Up @@ -430,8 +431,10 @@ private void RequiredResourceHelper(Hashtable reqResourceHash)
WriteDebug("In InstallPSResource::RequiredResourceHelper()");
foreach (DictionaryEntry entry in reqResourceHash)
{
InstallPkgParams pkgParams = new InstallPkgParams();
InstallPkgParams pkgParams = new();
PSCredential pkgCredential = Credential;
string pkgVersion = String.Empty;
bool isPrerelease = false;

// The package name will be the key for the inner hashtable and is present for all scenarios,
// including the scenario where only package name is specified
Expand All @@ -450,8 +453,7 @@ private void RequiredResourceHelper(Hashtable reqResourceHash)
return;
}

string pkgVersion = String.Empty;
if (!(entry.Value is Hashtable pkgInstallInfo))
if (entry.Value is not Hashtable pkgInstallInfo)
{
var requiredResourceHashtableInputFormatError = new ErrorRecord(
new ArgumentException($"The RequiredResource input with name '{pkgName}' does not have a valid value, the value must be a hashtable."),
Expand All @@ -467,7 +469,7 @@ private void RequiredResourceHelper(Hashtable reqResourceHash)
// Install-PSResource -RequiredResource @ { MyPackage = @{ version = '1.2.3', repository = 'PSGallery' } }
if (pkgInstallInfo.Count != 0)
{
var pkgParamNames = pkgInstallInfo.Keys;
ICollection pkgParamNames = pkgInstallInfo.Keys;

foreach (string paramName in pkgParamNames)
{
Expand All @@ -491,13 +493,27 @@ private void RequiredResourceHelper(Hashtable reqResourceHash)
}

pkgVersion = pkgInstallInfo["version"] == null ? String.Empty : pkgInstallInfo["version"].ToString();

// Prerelease - Handle both string and boolean
object prereleaseObj = pkgInstallInfo.ContainsKey("prerelease") ? pkgInstallInfo["prerelease"] : null;
if (prereleaseObj != null)
{
if (prereleaseObj is bool b)
{
isPrerelease = b;
}
else if (prereleaseObj is string s)
{
isPrerelease = s.Equals("true", StringComparison.OrdinalIgnoreCase);
}
}
}

ProcessInstallHelper(
pkgNames: new string[] { pkgName },
pkgVersion: pkgVersion,
pkgPrerelease: pkgParams.Prerelease,
pkgRepository: pkgParams.Repository != null ? new string[] { pkgParams.Repository } : new string[]{},
pkgPrerelease: isPrerelease,
pkgRepository: pkgParams.Repository != null ? new string[] { pkgParams.Repository } : new string[] { },
pkgCredential: pkgCredential,
reqResourceParams: pkgParams);
}
Expand All @@ -506,7 +522,7 @@ private void RequiredResourceHelper(Hashtable reqResourceHash)
private void ProcessInstallHelper(string[] pkgNames, string pkgVersion, bool pkgPrerelease, string[] pkgRepository, PSCredential pkgCredential, InstallPkgParams reqResourceParams)
{
WriteDebug("In InstallPSResource::ProcessInstallHelper()");
var inputNameToInstall = Utils.ProcessNameWildcards(pkgNames, removeWildcardEntries:false, out string[] errorMsgs, out bool nameContainsWildcard);
var inputNameToInstall = Utils.ProcessNameWildcards(pkgNames, removeWildcardEntries: false, out string[] errorMsgs, out bool nameContainsWildcard);
if (nameContainsWildcard)
{
WriteError(new ErrorRecord(
Expand Down Expand Up @@ -549,7 +565,7 @@ private void ProcessInstallHelper(string[] pkgNames, string pkgVersion, bool pkg
this));
}

var installedPkgs = _installHelper.BeginInstallPackages(
IEnumerable<PSResourceInfo> installedPkgs = _installHelper.BeginInstallPackages(
names: pkgNames,
versionRange: versionRange,
nugetVersion: nugetVersion,
Expand Down
Loading