Skip to content

Commit 76c2f65

Browse files
🩹 [Patch]: Partial parse of version as string (#27)
## Description - Fixes #26 ## Type of change <!-- Use the check-boxes [x] on the options that are relevant. --> - [ ] 📖 [Docs] - [ ] 🪲 [Fix] - [x] 🩹 [Patch] - [ ] ⚠️ [Security fix] - [ ] 🚀 [Feature] - [ ] 🌟 [Breaking change] ## Checklist <!-- Use the check-boxes [x] on the options that are relevant. --> - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas
1 parent 17b8e94 commit 76c2f65

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

src/classes/public/PSSemVer.ps1

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#region Static properties
33
hidden static [string] $PSSemVerPattern = '^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)' +
44
'(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$'
5-
hidden static [string] $LoosePSSemVerPattern = '^(?:([a-zA-Z]*)-?)?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)' +
5+
hidden static [string] $LoosePSSemVerPattern = '^(?:([a-zA-Z]*)-?)?(0|[1-9]\d*)(?:\.(0|[1-9]\d*))?(?:\.(0|[1-9]\d*))?' +
66
'(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$'
77
#endregion Static properties
88

@@ -109,6 +109,18 @@
109109
$this.Minor = if ($version.Minor -lt 0) { 0 } else { $version.Minor }
110110
$this.Patch = if ($version.Build -lt 0) { 0 } else { $version.Build }
111111
}
112+
113+
PSSemVer([string]$version, [string]$PreReleaseLabel, [string]$BuildLabel) {
114+
if ($version -match [PSSemVer]::PSSemVerPattern) {
115+
$this.Major = [int]$Matches[1]
116+
$this.Minor = [int]$Matches[2]
117+
$this.Patch = [int]$Matches[3]
118+
$this.Prerelease = $PreReleaseLabel
119+
$this.BuildMetadata = $BuildLabel
120+
} else {
121+
throw [ArgumentException]::new('The version string is not a valid SemVer string')
122+
}
123+
}
112124
#endregion Constructors
113125

114126
#region Methods

src/functions/public/New-PSSemVer.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,13 @@
4848

4949
# The prerelease version.
5050
[Parameter(ParameterSetName = 'Values')]
51+
[Parameter(ParameterSetName = 'String')]
5152
[Alias('PreReleaseLabel')]
5253
[string] $Prerelease = '',
5354

5455
# The build metadata.
5556
[Parameter(ParameterSetName = 'Values')]
57+
[Parameter(ParameterSetName = 'String')]
5658
[Alias('Build', 'BuildLabel')]
5759
[string] $BuildMetadata = '',
5860

@@ -73,7 +75,7 @@
7375
if ([string]::IsNullOrEmpty($Version)) {
7476
$Version = '0.0.0'
7577
}
76-
return [PSSemVer]::New($Version)
78+
return [PSSemVer]::New($Version, $Prerelease, $BuildMetadata)
7779
}
7880
}
7981
}

tests/PSSemVer.Tests.ps1

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@
4343
$PSSemVer.Prerelease | Should -BeNullOrEmpty
4444
$PSSemVer.BuildMetadata | Should -BeNullOrEmpty
4545
}
46+
It "New-PSSemVer -Version '1.0.0' -PreRelease 'alpha' -Build '2020-01-01'" {
47+
$PSSemVer = New-PSSemVer -Version '1.0.0' -Prerelease 'alpha' -Build '2020-01-01'
48+
$PSSemVer.Major | Should -Be 1
49+
$PSSemVer.Minor | Should -Be 0
50+
$PSSemVer.Patch | Should -Be 0
51+
$PSSemVer.Prerelease | Should -Be 'alpha'
52+
$PSSemVer.BuildMetadata | Should -Be '2020-01-01'
53+
}
4654
}
4755

4856
Describe 'Function: ConvertTo-PSSemVer' {
@@ -138,6 +146,18 @@
138146
It "Throws on [PSSemVer]'1.0.0-beta!1'" {
139147
{ [PSSemVer]'1.0.0-beta!1' } | Should -Throw
140148
}
149+
It "[PSSemVer]'1.2' => '1.2.0'" {
150+
$PSSemVer = [PSSemVer]'1.2'
151+
$PSSemVer.Major | Should -Be 1
152+
$PSSemVer.Minor | Should -Be 2
153+
$PSSemVer.Patch | Should -Be 0
154+
}
155+
It "[PSSemVer]'2' => '2.0.0'" {
156+
$PSSemVer = [PSSemVer]'2'
157+
$PSSemVer.Major | Should -Be 2
158+
$PSSemVer.Minor | Should -Be 0
159+
$PSSemVer.Patch | Should -Be 0
160+
}
141161
}
142162

143163
Describe 'Class: ToString()' {

0 commit comments

Comments
 (0)