Skip to content

Commit f108b73

Browse files
committed
#110 Support SemVer 2.0 by converting the prerelease
1 parent bd91dfd commit f108b73

3 files changed

Lines changed: 117 additions & 0 deletions

File tree

Source/Private/InitializeBuild.ps1

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ function InitializeBuild {
5555
$BuildInfo = $BuildInfo | Update-Object @{ SemVer = $SemVer }
5656
}
5757

58+
if ($BuildInfo.ZeroPadLegacy -ne 0 -and $BuildInfo.Prerelease) {
59+
Write-Verbose "ZeroPadLegacy is enabled"
60+
$BuildInfo.Prerelease = @($BuildInfo.Prerelease -split "\.").ForEach({
61+
if ($_ -match '^\d+$') {
62+
$_.PadLeft($BuildInfo.ZeroPadLegacy, '0')
63+
} else {
64+
$_
65+
}
66+
}) -join '_'
67+
}
68+
5869
# Override VersionedOutputDirectory with UnversionedOutputDirectory
5970
if ($BuildInfo.UnversionedOutputDirectory -and $BuildInfo.VersionedOutputDirectory) {
6071
$BuildInfo.VersionedOutputDirectory = $false

Source/Public/Build-Module.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ function Build-Module {
121121
# The default is nothing. See examples for more details.
122122
[string]$Prefix,
123123

124+
# ZeroPadLegacy controls the conversion of versions to SemVer 1.0 syntax
125+
# If this is set to a non-zero integer, the numeric identifiers in the prerelease will be zero-padded to that many digits, and dots will be replaced with underscores, to allow compaitibility with the PowerShell gallery.
126+
[int]$ZeroPadLegacy = 0,
127+
124128
# The Suffix is either the path to a file (relative to the module folder) or text to put at the bottom of the file.
125129
# If the value of Suffix resolves to a file, that file will be read in, otherwise, the value will be used.
126130
# The default is nothing. See examples for more details.

Tests/Private/InitializeBuild.Tests.ps1

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,106 @@ Describe "InitializeBuild" {
7575
$Result.Result.VersionedOutputDirectory | Should -Be $false
7676
}
7777
}
78+
79+
Context "Semver 2.0 to Semver 1.0" {
80+
BeforeAll {
81+
# Note that "Path" is an alias for "SourcePath"
82+
New-Item "TestDrive:\build.psd1" -Type File -Force -Value '@{
83+
Path = ".\Source\MyModule.psd1"
84+
SourceDirectories = @("Classes", "Private", "Public")
85+
}'
86+
87+
New-Item "TestDrive:\Source\" -Type Directory
88+
89+
New-ModuleManifest "TestDrive:\Source\MyModule.psd1" -RootModule "MyModule.psm1" -Author "Test Manager" -ModuleVersion "1.0.0"
90+
91+
$Result = @{}
92+
}
93+
94+
It "Handles Semver 2.0 prerelease values" {
95+
Push-Location TestDrive:\
96+
$Result.Result = InModuleScope -ModuleName ModuleBuilder {
97+
function Test-Build {
98+
[CmdletBinding()]
99+
param(
100+
[Alias("ModuleManifest", "Path")]
101+
$SourcePath = "./Source",
102+
$SourceDirectories = @("Enum", "Classes", "Private", "Public"),
103+
$OutputDirectory = "./Output",
104+
$VersionedOutputDirectory = $true,
105+
$UnversionedOutputDirectory = $false,
106+
# ^ those are required for InitializeBuild to not throw
107+
# v these are what we're testing
108+
[string]$SemVer = "1.0.0-alpha.1+build.12.sha.abcdef",
109+
[version]$Version = "1.0.0",
110+
[string]$Prerelease = "alpha.1",
111+
[string]$BuildMetadata = "build.12.sha.abcdef",
112+
[int]$ZeroPadLegacy = 4
113+
)
114+
try {
115+
Write-Warning $($MyInvocation.MyCommand | Out-String)
116+
InitializeBuild -SourcePath $SourcePath
117+
} catch {
118+
$_
119+
}
120+
}
121+
122+
Test-Build 'TestDrive:\'
123+
}
124+
Pop-Location
125+
126+
$Result.Result | Should -Not -BeOfType [System.Management.Automation.ErrorRecord]
127+
}
128+
129+
It "Converts the dots to _ in the Prerelease " {
130+
$Result.Result.Prerelease | Should -Match "alpha_"
131+
}
132+
133+
It "ZeroPads the prerelease numbers to make them sort correctly" {
134+
$Result.Result.Prerelease | Should -Be "alpha_0001"
135+
}
136+
137+
It "Handles multiple numeric sections" {
138+
Push-Location TestDrive:\
139+
$Result.Result = InModuleScope -ModuleName ModuleBuilder {
140+
function Test-Build {
141+
[CmdletBinding()]
142+
param(
143+
[Alias("ModuleManifest", "Path")]
144+
$SourcePath = "./Source",
145+
$SourceDirectories = @("Enum", "Classes", "Private", "Public"),
146+
$OutputDirectory = "./Output",
147+
$VersionedOutputDirectory = $true,
148+
$UnversionedOutputDirectory = $false,
149+
# ^ those are required for InitializeBuild to not throw
150+
# v these are what we're testing
151+
[string]$SemVer = "1.0.0-alpha.1+build.12.sha.abcdef",
152+
[version]$Version = "1.0.0",
153+
[string]$Prerelease = "alpha.1.2",
154+
[string]$BuildMetadata = "build.12.sha.abcdef",
155+
[int]$ZeroPadLegacy = 4
156+
)
157+
try {
158+
Write-Warning $($MyInvocation.MyCommand | Out-String)
159+
InitializeBuild -SourcePath $SourcePath
160+
} catch {
161+
$_
162+
}
163+
}
164+
165+
Test-Build 'TestDrive:\'
166+
}
167+
Pop-Location
168+
169+
$Result.Result | Should -Not -BeOfType [System.Management.Automation.ErrorRecord]
170+
}
171+
172+
It "Converts the dots to _ in the Prerelease " {
173+
$Result.Result.Prerelease | Should -Match "alpha_"
174+
}
175+
176+
It "ZeroPads the prerelease numbers to make them sort correctly" {
177+
$Result.Result.Prerelease | Should -Be "alpha_0001_0002"
178+
}
179+
}
78180
}

0 commit comments

Comments
 (0)