Skip to content

Commit 184e230

Browse files
authored
Merge pull request #13 from JustinGrote/feature/perspecPrerelease
🎉 Add per-spec Prerelease support
2 parents 4f2ac7f + 2dbbe10 commit 184e230

File tree

2 files changed

+85
-17
lines changed

2 files changed

+85
-17
lines changed

ModuleFast.psm1

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -266,14 +266,6 @@ function Get-ModuleFastPlan {
266266
}
267267
}
268268
END {
269-
foreach ($version in $modulesToResolve.VersionRange.MinVersion, $modulesToResolve.VersionRange.MaxVersion) {
270-
if ($version.IsPreRelease -or $version.HasMetadata) {
271-
Write-Warning 'A specification with a prerelease version was detected, forcing -Prerelease switch. Specify -Prerelease in the future to avoid this warning.'
272-
$Prerelease = $true
273-
break
274-
}
275-
}
276-
277269
# A deduplicated list of modules to install
278270
[HashSet[ModuleFastInfo]]$modulesToInstall = @{}
279271

@@ -363,7 +355,10 @@ function Get-ModuleFastPlan {
363355

364356
foreach ($candidate in $inlinedVersions.Reverse()) {
365357
#Skip Prereleases unless explicitly requested
366-
if (($candidate.IsPrerelease -or $candidate.HasMetadata) -and -not $Prerelease) { continue }
358+
if (($candidate.IsPrerelease -or $candidate.HasMetadata) -and -not ($currentModuleSpec.PreRelease -or $Prerelease)) {
359+
Write-Debug "Skipping candidate $candidate because it is a prerelease and prerelease was not specified either with the -Prerelease parameter or with a ! on the module name."
360+
continue
361+
}
367362

368363
if ($currentModuleSpec.SatisfiedBy($candidate)) {
369364
Write-Debug "$currentModuleSpec`: Found satisfying version $candidate in the inlined index."
@@ -707,16 +702,22 @@ class ModuleFastInfo: IComparable {
707702
}
708703

709704
[int] CompareTo($other) {
710-
return $(switch ($true) {
705+
return $(
706+
switch ($true) {
711707
($other -isnot 'ModuleFastInfo') {
712-
$this.ToUniqueString().CompareTo([string]$other); break
713-
}
708+
$this.ToUniqueString().CompareTo([string]$other); break
709+
}
714710
($this -eq $other) { 0; break }
715711
($this.Name -ne $other.Name) { $this.Name.CompareTo($other.Name); break }
716-
default {
717-
$this.ModuleVersion.CompareTo($other.ModuleVersion)
712+
default {
713+
$this.ModuleVersion.CompareTo($other.ModuleVersion)
714+
}
718715
}
719-
})
716+
)
717+
}
718+
719+
static hidden [bool]Get_Prerelease([PSObject]$i) {
720+
return $i.ModuleVersion.IsPrerelease -or $i.ModuleVersion.HasMetadata
720721
}
721722

722723
#endregion ImplicitBehaviors
@@ -748,6 +749,16 @@ class ModuleFastSpec {
748749
hidden [VersionRange]$_VersionRange
749750
static hidden [VersionRange]Get_VersionRange([PSObject]$i) { return $i._VersionRange }
750751

752+
#A flag to indicate if prerelease should be included if the name had ! specified (this is done in the constructor)
753+
hidden [bool]$_PreReleaseName
754+
static hidden [bool]Get_PreRelease([PSObject]$i) {
755+
return $i._VersionRange.MinVersion.IsPrerelease -or
756+
$i._VersionRange.MaxVersion.IsPrerelease -or
757+
$i._VersionRange.MinVersion.HasMetadata -or
758+
$i._VersionRange.MaxVersion.HasMetadata -or
759+
$i._PreReleaseName
760+
}
761+
751762
static hidden [NugetVersion]Get_Min([PSObject]$i) { return $i._VersionRange.MinVersion }
752763
static hidden [NugetVersion]Get_Max([PSObject]$i) { return $i._VersionRange.MaxVersion }
753764
static hidden [NugetVersion]Get_Required([PSObject]$i) {
@@ -849,7 +860,14 @@ class ModuleFastSpec {
849860
hidden Initialize([string]$Name, [VersionRange]$Range, [guid]$Guid) {
850861
#HACK: The nulls here are just to satisfy the ternary operator, they go off into the ether and arent returned or used
851862
if (-not $Name) { throw 'Name is required' }
852-
$this._Name = $Name
863+
# Strip ! from the beginning or end of the name
864+
$TrimmedName = $Name.Trim('!')
865+
if ($TrimmedName -ne $Name) {
866+
Write-Debug "ModuleSpec $TrimmedName had prerelease identifier ! specified. Will include Prerelease modules"
867+
$this._PreReleaseName = $true
868+
}
869+
870+
$this._Name = $TrimmedName
853871
$this._VersionRange = $Range ?? [VersionRange]::new()
854872
$this._Guid = $Guid ?? [Guid]::Empty
855873
# TODO: Fix this check logic
@@ -1122,7 +1140,6 @@ function Find-LocalModule {
11221140
[Switch]$Update
11231141
)
11241142
$ErrorActionPreference = 'Stop'
1125-
# BUG: Prerelease Module paths are still not recognized by internal PS commands and can break things
11261143

11271144
# Search all psmodulepaths for the module
11281145
$modulePaths = $env:PSModulePath.Split([Path]::PathSeparator, [StringSplitOptions]::RemoveEmptyEntries)

ModuleFast.tests.ps1

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,42 @@ Describe 'Get-ModuleFastPlan' -Tag 'E2E' {
216216
Check = {
217217
$actual.ModuleVersion | Should -Be '2.7.3'
218218
}
219+
},
220+
@{
221+
Spec = 'PrereleaseTest'
222+
Check = {
223+
$actual.Name | Should -Be 'PrereleaseTest'
224+
$actual.ModuleVersion | Should -Be '0.0.1'
225+
}
226+
ModuleName = 'PrereleaseTest'
227+
},
228+
@{
229+
Spec = 'PrereleaseTest!'
230+
Check = {
231+
$actual.Name | Should -Be 'PrereleaseTest'
232+
$actual.ModuleVersion | Should -Be '0.0.2-newerversion'
233+
}
234+
ModuleName = 'PrereleaseTest'
235+
},
236+
@{
237+
Spec = '!PrereleaseTest'
238+
Check = {
239+
$actual.Name | Should -Be 'PrereleaseTest'
240+
$actual.ModuleVersion | Should -Be '0.0.2-newerversion'
241+
}
242+
ModuleName = 'PrereleaseTest'
243+
},
244+
@{
245+
Spec = 'PrereleaseTest!<0.0.1'
246+
Check = {
247+
$actual.Name | Should -Be 'PrereleaseTest'
248+
$actual.ModuleVersion | Should -Be '0.0.1-newerversion'
249+
}
250+
ModuleName = 'PrereleaseTest'
219251
}
252+
220253
)
254+
221255
It 'Gets Module with String Parameter: <Spec>' {
222256
$actual = Get-ModuleFastPlan $Spec
223257
$actual | Should -HaveCount 1
@@ -268,6 +302,23 @@ Describe 'Get-ModuleFastPlan' -Tag 'E2E' {
268302
$PSItem.ModuleVersion -as 'NuGet.Versioning.NuGetVersion' | Should -BeGreaterThan '1.0'
269303
}
270304
}
305+
306+
It 'Prerelease does not affect non-prerelease' {
307+
#The prerelease flag on az.accounts should not trigger prerelease on PrereleaseTest
308+
$actual = 'Az.Accounts!', 'PrereleaseTest' | Get-ModuleFastPlan
309+
$actual | Should -HaveCount 2
310+
$actual | Where-Object Name -EQ 'PrereleaseTest' | ForEach-Object {
311+
$PSItem.ModuleVersion | Should -Be '0.0.1'
312+
}
313+
}
314+
It '-Prerelease overrides even if prerelease is not specified' {
315+
#The prerelease flag on az.accounts should not trigger prerelease on PrereleaseTest
316+
$actual = 'Az.Accounts!', 'PrereleaseTest' | Get-ModuleFastPlan -PreRelease
317+
$actual | Should -HaveCount 2
318+
$actual | Where-Object Name -EQ 'PrereleaseTest' | ForEach-Object {
319+
$PSItem.ModuleVersion | Should -Be '0.0.2-newerversion'
320+
}
321+
}
271322
}
272323
}
273324

0 commit comments

Comments
 (0)