Skip to content
This repository was archived by the owner on Jun 13, 2024. It is now read-only.

Commit acee8cc

Browse files
wooseung-simbmanikm
authored andcommitted
Added version for REQUIREDSCRIPTS (#162)
* Added versioning for REQUIREDSCRIPTS Enabled following scenarios for REQUIREDSCRIPTS [1.0] - RequiredVersion [1.0,2.0] - Min and Max Version (,1.0] - Max Version 1.0 - Min Version
1 parent 57f85bf commit acee8cc

File tree

4 files changed

+2050
-1778
lines changed

4 files changed

+2050
-1778
lines changed

PowerShellGet/PSGet.Resource.psd1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@ ConvertFrom-StringData @'
246246
InvalidValueBoolean=The specified value '{0}' for the parameter '{1}' is invalid. It should be $true or $false.
247247
UserDeclinedLicenseAcceptance=User declined license acceptance.
248248
AcceptLicense=License Acceptance
249+
RequiredScriptVersion=REQUIREDSCRIPTS: Required version of script '{0}' is '{1}'.
250+
RequiredScriptVersoinFormat=<ScriptName>, <ScriptName>:<MinimumVersion>, <ScriptName>:[<RequiredVersion>], <ScriptName>:[<MinimumVersion>,<MaximumVersion>], <ScriptName>:[,<MaximumVersion>]
251+
FailedToParseRequiredScripts=Cannot parse REQUIREDSCRIPTS '{0}'. Acceptable formats are: '{1}'.
252+
FailedToParseRequiredScriptsVersion=Version format error: {0}, '{1}'. Acceptable formats are: '{2}'.
249253
###PSLOC
250254
'@
251255

PowerShellGet/PSModule.psm1

Lines changed: 179 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7821,29 +7821,60 @@ function ValidateAndGet-ScriptDependencies
78217821
WarningAction = 'SilentlyContinue'
78227822
Debug = $DebugPreference
78237823
}
7824+
$ReqScriptInfo = @{}
7825+
78247826
if ($PSBoundParameters.ContainsKey('Credential'))
78257827
{
78267828
$FindScriptArguments.Add('Credential',$Credential)
78277829
}
7830+
7831+
if (-not ($requiredScript -match '^(?<ScriptName>[^:]+)(:(?<Version>[^:\s]+))?$'))
7832+
{
7833+
$message = $LocalizedData.FailedToParseRequiredScripts -f ($requiredScript)
7834+
7835+
ThrowError `
7836+
-ExceptionName "System.ArgumentException" `
7837+
-ExceptionMessage $message `
7838+
-ErrorId "FailedToParseRequiredScripts" `
7839+
-CallerPSCmdlet $CallerPSCmdlet `
7840+
-ErrorCategory InvalidOperation
7841+
}
78287842

7829-
if($DependentScriptInfo.ExternalScriptDependencies -contains $requiredScript)
7843+
$scriptName = $Matches['ScriptName']
7844+
if ($DependentScriptInfo.ExternalScriptDependencies -contains $scriptName)
78307845
{
7831-
Write-Verbose -Message ($LocalizedData.SkippedScriptDependency -f $requiredScript)
7846+
Write-Verbose -Message ($LocalizedData.SkippedScriptDependency -f $scriptName)
78327847

78337848
continue
78347849
}
78357850

7836-
$FindScriptArguments['Name'] = $requiredScript
7837-
$ReqScriptInfo = @{}
7838-
$ReqScriptInfo['Name'] = $requiredScript
7851+
if ($Matches.Keys -Contains 'Version')
7852+
{
7853+
$ReqScriptInfo = ValidateAndGet-NuspecVersionString -Version $Matches['Version']
7854+
7855+
if($ReqScriptInfo.Keys -Contains 'RequiredVersion')
7856+
{
7857+
$FindScriptArguments['RequiredVersion'] = $ReqScriptInfo['RequiredVersion']
7858+
}
7859+
elseif($ReqScriptInfo.Keys -Contains 'MinimumVersion')
7860+
{
7861+
$FindScriptArguments['MinimumVersion'] = $ReqScriptInfo['MinimumVersion']
7862+
}
7863+
if($ReqScriptInfo.Keys -Contains 'MaximumVersion')
7864+
{
7865+
$FindScriptArguments['MaximumVersion'] = $ReqScriptInfo['MaximumVersion']
7866+
}
7867+
}
78397868

7869+
$ReqScriptInfo['Name'] = $scriptName
7870+
$FindScriptArguments['Name'] = $scriptName
78407871
$psgetItemInfo = Find-Script @FindScriptArguments |
7841-
Microsoft.PowerShell.Core\Where-Object {$_.Name -eq $requiredScript} |
7872+
Microsoft.PowerShell.Core\Where-Object {$_.Name -eq $scriptName} |
78427873
Microsoft.PowerShell.Utility\Select-Object -Last 1 -ErrorAction Ignore
78437874

78447875
if(-not $psgetItemInfo)
78457876
{
7846-
$message = $LocalizedData.UnableToResolveScriptDependency -f ('script', $requiredScript, $DependentScriptInfo.Name, $Repository, 'ExternalScriptDependencies')
7877+
$message = $LocalizedData.UnableToResolveScriptDependency -f ('script', $scriptName, $DependentScriptInfo.Name, $Repository, 'ExternalScriptDependencies')
78477878
ThrowError -ExceptionName "System.InvalidOperationException" `
78487879
-ExceptionMessage $message `
78497880
-ErrorId "UnableToResolveScriptDependency" `
@@ -7858,6 +7889,146 @@ function ValidateAndGet-ScriptDependencies
78587889
return $DependenciesDetails
78597890
}
78607891

7892+
function ValidateAndGet-NuspecVersionString
7893+
{
7894+
param(
7895+
[Parameter(Mandatory=$true)]
7896+
[string]
7897+
$Version
7898+
)
7899+
7900+
$versionPattern = '^((?<MinRule>[\[\(])?((?<MinVersion>[^:\(\[\)\]\,]+))?((?<Comma>[\,])?(?<MaxVersion>[^:\(\[\)\]\,]+)?)?(?<MaxRule>[\]\)])?)$'
7901+
$VersionInfo = @{}
7902+
7903+
if ( -not ($Version -match $versionPattern))
7904+
{
7905+
$message = $LocalizedData.FailedToParseRequiredScriptsVersion -f ('Invalid Version format', $Version, $LocalizedData.RequiredScriptVersoinFormat)
7906+
Write-Verbose $message
7907+
ThrowError -ExceptionName "System.ArgumentException" `
7908+
-ExceptionMessage $message `
7909+
-ErrorId "UnableToResolveScriptDependency" `
7910+
-CallerPSCmdlet $CallerPSCmdlet `
7911+
-ErrorCategory InvalidOperation
7912+
}
7913+
7914+
if ($Matches.Keys -Contains 'MinRule' -xor $Matches.Keys -Contains 'MaxRule')
7915+
{
7916+
$message = $LocalizedData.FailedToParseRequiredScriptsVersion -f ('Minimum and Maximum inclusive/exclusive condition mismatch', $Version, $LocalizedData.RequiredScriptVersoinFormat)
7917+
Write-Verbose $message
7918+
ThrowError -ExceptionName "System.ArgumentException" `
7919+
-ExceptionMessage $message `
7920+
-ErrorId "UnableToResolveScriptDependency" `
7921+
-CallerPSCmdlet $CallerPSCmdlet `
7922+
-ErrorCategory InvalidOperation
7923+
}
7924+
7925+
if (-not ($Matches.Keys -Contains 'MinVersion' -or $Matches.Keys -Contains 'MaxVersion'))
7926+
{
7927+
$message = $LocalizedData.FailedToParseRequiredScriptsVersion -f ('No version.', $Version, $LocalizedData.RequiredScriptVersoinFormat)
7928+
Write-Verbose $message
7929+
ThrowError -ExceptionName "System.ArgumentException" `
7930+
-ExceptionMessage $message `
7931+
-ErrorId "UnableToResolveScriptDependency" `
7932+
-CallerPSCmdlet $CallerPSCmdlet `
7933+
-ErrorCategory InvalidOperation
7934+
}
7935+
7936+
if ((-not ($Matches.Keys -Contains 'MinRule' -and $Matches.Keys -Contains 'MaxRule')) -and $Matches.Keys -Contains 'Comma')
7937+
{
7938+
$message = $LocalizedData.FailedToParseRequiredScriptsVersion -f ('Invalid version format', $Version, $LocalizedData.RequiredScriptVersoinFormat)
7939+
Write-Verbose $message
7940+
ThrowError -ExceptionName "System.ArgumentException" `
7941+
-ExceptionMessage $message `
7942+
-ErrorId "UnableToResolveScriptDependency" `
7943+
-CallerPSCmdlet $CallerPSCmdlet `
7944+
-ErrorCategory InvalidOperation
7945+
}
7946+
7947+
if ($Matches.Keys -Contains 'MaxRule' -and -not ($Matches['MaxRule'] -eq ']') )
7948+
{
7949+
$message = $LocalizedData.FailedToParseRequiredScriptsVersion -f ('Maximum version condition should be inclusive', $Version, $LocalizedData.RequiredScriptVersoinFormat)
7950+
Write-Verbose $message
7951+
ThrowError -ExceptionName "System.ArgumentException" `
7952+
-ExceptionMessage $message `
7953+
-ErrorId "UnableToResolveScriptDependency" `
7954+
-CallerPSCmdlet $CallerPSCmdlet `
7955+
-ErrorCategory InvalidOperation
7956+
}
7957+
7958+
if ($Matches.Keys -Contains 'MinVersion' -and $Matches.Keys -Contains 'MaxVersion')
7959+
{
7960+
if ($Matches.Keys -Contains 'MinRule' -and $Matches.Keys -Contains 'MaxRule')
7961+
{
7962+
if ($Matches['MinRule'] -eq '[')
7963+
{
7964+
$VersionInfo['MinimumVersion'] = $Matches['MinVersion']
7965+
$VersionInfo['MaximumVersion'] = $Matches['MaxVersion']
7966+
}
7967+
else
7968+
{
7969+
$message = $LocalizedData.FailedToParseRequiredScriptsVersion -f ('Minimum version condition should be inclusive', $Version, $LocalizedData.RequiredScriptVersoinFormat)
7970+
Write-Verbose $message
7971+
ThrowError -ExceptionName "System.ArgumentException" `
7972+
-ExceptionMessage $message `
7973+
-ErrorId "UnableToResolveScriptDependency" `
7974+
-CallerPSCmdlet $CallerPSCmdlet `
7975+
-ErrorCategory InvalidOperation
7976+
}
7977+
}
7978+
else
7979+
{
7980+
$message = $LocalizedData.FailedToParseRequiredScriptsVersion -f ('Minimum and Maximum inclusive/exclusive condition mismatch', $Version, $LocalizedData.RequiredScriptVersoinFormat)
7981+
Write-Verbose $message
7982+
ThrowError -ExceptionName "System.ArgumentException" `
7983+
-ExceptionMessage $message `
7984+
-ErrorId "UnableToResolveScriptDependency" `
7985+
-CallerPSCmdlet $CallerPSCmdlet `
7986+
-ErrorCategory InvalidOperation
7987+
}
7988+
7989+
return $VersionInfo
7990+
}
7991+
7992+
if ($Matches.Keys -Contains 'MinVersion')
7993+
{
7994+
if ($Matches.Keys -Contains 'MinRule' -and $Matches.Keys -Contains 'MaxRule')
7995+
{
7996+
if (($Matches['MinRule'] -eq '[') -and ($Matches['MaxRule'] -eq ']'))
7997+
{
7998+
$VersionInfo['RequiredVersion'] = $Matches['MinVersion']
7999+
return $VersionInfo
8000+
}
8001+
}
8002+
else
8003+
{
8004+
$VersionInfo['MinimumVersion'] = $Matches['MinVersion']
8005+
return $VersionInfo
8006+
}
8007+
8008+
$message = $LocalizedData.FailedToParseRequiredScriptsVersion -f ("Minimum and Maximum version rules should be inclusive for 'RequiredVersion'", $Version, $LocalizedData.RequiredScriptVersoinFormat)
8009+
Write-Verbose $message
8010+
ThrowError -ExceptionName "System.ArgumentException" `
8011+
-ExceptionMessage $message `
8012+
-ErrorId "UnableToResolveScriptDependency" `
8013+
-CallerPSCmdlet $CallerPSCmdlet `
8014+
-ErrorCategory InvalidOperation
8015+
}
8016+
8017+
if ($Matches.Keys -Contains 'MaxVersion')
8018+
{
8019+
$VersionInfo['MaximumVersion'] = $Matches['MaxVersion']
8020+
return $VersionInfo
8021+
}
8022+
8023+
$message = $LocalizedData.FailedToParseRequiredScriptsVersion -f ("Failed to parse version string", $Version, $LocalizedData.RequiredScriptVersoinFormat)
8024+
Write-Verbose $message
8025+
ThrowError -ExceptionName "System.ArgumentException" `
8026+
-ExceptionMessage $message `
8027+
-ErrorId "UnableToResolveScriptDependency" `
8028+
-CallerPSCmdlet $CallerPSCmdlet `
8029+
-ErrorCategory InvalidOperation
8030+
}
8031+
78618032
function ValidateAndGet-RequiredModuleDetails
78628033
{
78638034
param(
@@ -8836,7 +9007,7 @@ function ValidateAndAdd-PSScriptInfoEntry
88369007

88379008
$script:RequiredScripts {
88389009
$KeyName = $script:RequiredScripts
8839-
$Value = $Value -split '[,\s+]' | Microsoft.PowerShell.Core\Where-Object {$_}
9010+
$Value = $Value -split ',(?=[^\[^\(]\w(?!\w+[\)\]]))|\s' | Microsoft.PowerShell.Core\Where-Object {$_}
88409011
}
88419012

88429013
$script:ExternalScriptDependencies {

Tests/Asserts.psm1

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,14 @@ function AssertNull
184184
Assert ($args[0] -eq $()) $args[1]
185185
}
186186

187+
# Usage: AssertNullOrEmpty <object> <message>
188+
#
189+
function AssertNullOrEmpty
190+
{
191+
Assert ($args.Length -eq 2) "AssertNullOrEmpty takes two parameters."
192+
Assert ($args[0] -eq $() -or $args[0] -eq '') $args[1]
193+
}
194+
187195
# This function waits for either a specified amount of time or for a script block to evaluate to true and throws an exception if the timeout period elapses. Example:
188196
#
189197
# WaitFor {get-process calc} 10000 250 "Calc.exe wasn't started within 10 seconds."

0 commit comments

Comments
 (0)