Skip to content

Commit

Permalink
Add ConvertTo-DotNetVersionClass #717
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronparker committed Sep 22, 2024
1 parent a26c665 commit 62fc09c
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 6 deletions.
12 changes: 6 additions & 6 deletions Evergreen/Evergreen.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ PowerShellVersion = '3.0'
# NestedModules = @()

# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = @('Export-EvergreenApp', 'Export-EvergreenManifest',
'Find-EvergreenApp', 'Get-EvergreenApp', 'Get-EvergreenAppFromApi',
'Get-EvergreenAppFromLibrary', 'Get-EvergreenEndpointFromApi',
'Get-EvergreenLibrary', 'New-EvergreenLibrary', 'Save-EvergreenApp',
FunctionsToExport = @('Export-EvergreenApp', 'Export-EvergreenManifest',
'Find-EvergreenApp', 'Get-EvergreenApp', 'Get-EvergreenAppFromApi', 'ConvertTo-DotNetVersionClass',
'Get-EvergreenAppFromLibrary', 'Get-EvergreenEndpointFromApi',
'Get-EvergreenLibrary', 'New-EvergreenLibrary', 'Save-EvergreenApp',
'Start-EvergreenLibraryUpdate', 'Test-EvergreenApp')

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
Expand All @@ -82,8 +82,8 @@ CmdletsToExport = @()
# VariablesToExport = @()

# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
AliasesToExport = 'sea', 'gea', 'fea', 'tea', 'iea', 'Invoke-EvergreenLibraryUpdate',
'Get-EvergreenLibraryApp', 'Invoke-EvergreenApp',
AliasesToExport = 'sea', 'gea', 'fea', 'tea', 'iea', 'Invoke-EvergreenLibraryUpdate',
'Get-EvergreenLibraryApp', 'Invoke-EvergreenApp',
'Get-EvergreenEndpoint'

# DSC resources to export from this module
Expand Down
16 changes: 16 additions & 0 deletions Evergreen/Private/Convert-Segment.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function Convert-Segment {
param (
[System.String] $Segment
)

if ($Segment -match '^\d+$') {
return [System.Int32]$Segment
}
else {
$Normalized = 0
foreach ($Char in $Segment.ToCharArray()) {
$Normalized = $Normalized * 100 + [System.Int32][System.Char]$Char
}
return $Normalized
}
}
67 changes: 67 additions & 0 deletions Evergreen/Public/ConvertTo-DotNetVersionClass.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
function ConvertTo-DotNetVersionClass {
<#
.SYNOPSIS
Converts a version string to a standard .NET compliant Version class.
.DESCRIPTION
The ConvertTo-DotNetVersionClass function takes a version string as input and converts it into a .NET Version class.
It normalizes the segments of the version string, ensuring it has exactly four segments by either summing excess segments
or padding with zeros if there are fewer than four segments.
.PARAMETER Version
A version string to convert to a standard .NET compliant version class.
.EXAMPLE
PS> ConvertTo-DotNetVersionClass -Version "1.2.3.4"
1.2.3.4
.EXAMPLE
PS> ConvertTo-DotNetVersionClass -Version "1.2.3"
1.2.3.0
.EXAMPLE
PS> ConvertTo-DotNetVersionClass -Version "1.2.3.4.5"
1.2.3.9
.NOTES
If the conversion to a .NET Version class fails, the function will return the normalized version string as a string.
#>
param (
[Parameter(
Mandatory = $true,
Position = 0,
ValueFromPipeline,
HelpMessage = "A version string to convert to a standard .NET compliant version class.")]
[System.String] $Version
)

process {
# Split the version string into segments and initialise an array
$Segments = $Version -split '[.\-_+]'
$NormalizedSegments = @()

# Normalize each segment
foreach ($Segment in $Segments) {
$NormalizedSegments += @(Convert-Segment -Segment $Segment)
}

# If the number of segments is greater than 4, sum the last segments
if ($NormalizedSegments.Count -gt 4) {
$NormalizedSegments = $NormalizedSegments[0..2] + ($NormalizedSegments[3..($NormalizedSegments.Count - 1)] | Measure-Object -Sum).Sum
}

# If the number of segments is less than 4, pad with zeros
while ($NormalizedSegments.Count -lt 4) {
$NormalizedSegments += 0
}

# Return the version as a .NET Version class
try {
return [System.Version]($NormalizedSegments -join ".")
}
catch {
Write-Warning -Message "Failed to convert version string '$Version' to a .NET Version class."
return ($NormalizedSegments -join ".")
}
}
}

0 comments on commit 62fc09c

Please sign in to comment.