|
| 1 | +function Update-ModuleVersion { |
| 2 | + [CmdletBinding(DefaultParameterSetName = 'Minor')] |
| 3 | + [OutputType([String])] |
| 4 | + param ( |
| 5 | + |
| 6 | + # Specify the version to update from (or read from a module manifest). |
| 7 | + [version] $InputVersion, |
| 8 | + |
| 9 | + # Basic version switches. |
| 10 | + [Parameter(ParameterSetName = 'Major')] |
| 11 | + [Switch] $Major, |
| 12 | + |
| 13 | + [Parameter(ParameterSetName = 'Minor')] |
| 14 | + [Switch] $Minor, |
| 15 | + |
| 16 | + [Parameter(ParameterSetName = 'Patch')] |
| 17 | + [Switch] $Patch, |
| 18 | + |
| 19 | + [Parameter()] |
| 20 | + [ValidateScript({ $_ -match '^(?:[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)$' })] |
| 21 | + [string] $PrereleaseTag |
| 22 | + |
| 23 | + ) |
| 24 | + |
| 25 | + process { |
| 26 | + # $Version = Get-ModuleVersion |
| 27 | + # $IsPrerelease = Get-ModuleVersion -Prerelease |
| 28 | + if ($InputVersion) { |
| 29 | + $Version = $InputVersion |
| 30 | + Write-Verbose -Message "InputVersion: $InputVersion" |
| 31 | + } |
| 32 | + $VersionParts = [ordered]@{ |
| 33 | + Major = $Version.Major |
| 34 | + Minor = $Version.Minor |
| 35 | + Patch = $Version.Build |
| 36 | + } |
| 37 | + Write-Verbose -Message "VersionParts: `n$($VersionParts | Out-String)" |
| 38 | + |
| 39 | + |
| 40 | + #region IncrementVersion |
| 41 | + if ($Major.IsPresent) { |
| 42 | + $VersionParts.Major++ |
| 43 | + $VersionParts.Minor = 0 |
| 44 | + $VersionParts.Patch = 0 |
| 45 | + } |
| 46 | + |
| 47 | + if ($Minor.IsPresent) { |
| 48 | + $VersionParts.Minor++ |
| 49 | + $VersionParts.Patch = 0 |
| 50 | + } |
| 51 | + |
| 52 | + if ($Patch.IsPresent ) { |
| 53 | + $VersionParts.Patch++ |
| 54 | + } |
| 55 | + #endregion IncrementVersion |
| 56 | + |
| 57 | + |
| 58 | + #region Prerelease |
| 59 | + if ($PrereleaseTag ) { |
| 60 | + # Prepend a hyphen to the prerelease tag if it is specified. |
| 61 | + Write-Verbose -Message "`nPrereleaseTag: $PrereleaseTag" |
| 62 | + $PrereleaseTag = "-$PrereleaseTag" |
| 63 | + |
| 64 | + # Warn if the prerelease tag is added but the major, minor, and patch versions are not incremented. |
| 65 | + if (-not ($Major.IsPresent -or $Minor.IsPresent -or $Patch.IsPresent)) { |
| 66 | + Write-Warning -Message "The prerelease tag '$PrereleaseTag' was added but the major, minor, or patch version was not incremented." -WarningAction Continue |
| 67 | + } |
| 68 | + } |
| 69 | + #endregion Prerelease |
| 70 | + |
| 71 | + |
| 72 | + #region ValidateVersion |
| 73 | + $NewVersion = $VersionParts.Major, $VersionParts.Minor, $VersionParts.Patch -Join '.' |
| 74 | + if ($NewVersion -eq $Version) { |
| 75 | + Write-Warning -Message 'The version did not change.' -WarningAction Continue |
| 76 | + } |
| 77 | + |
| 78 | + # https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string |
| 79 | + $PatternValidation = '^(?<Major>0|[1-9]\d*)\.(?<Minor>0|[1-9]\d*)\.(?<patch>0|[1-9]\d*)(?:-(?<Prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?<BuildMetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$' |
| 80 | + if ($NewVersion -notmatch $PatternValidation) { |
| 81 | + Write-Error -Message "The new version '$NewVersion' is not a valid semantic version." -ErrorAction Continue |
| 82 | + } else { |
| 83 | + foreach ($match in $matches.GetEnumerator()) { |
| 84 | + "$($match.Key)" | Write-Debug -Debug |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if ("$NewVersion$PrereleaseTag" -notmatch $PatternValidation) { |
| 89 | + Write-Error -Message "The prerelease version '$PrereleaseVersion' is not a valid semantic version." -ErrorAction Continue |
| 90 | + } else { |
| 91 | + $matches | Write-Debug -Debug |
| 92 | + foreach ($match in $matches.GetEnumerator()) { |
| 93 | + "$($match.Key)" | Write-Debug -Debug |
| 94 | + } |
| 95 | + } |
| 96 | + #endregion ValidateVersion |
| 97 | + |
| 98 | + |
| 99 | + # Update-ModuleManifest -Path (Get-ModuleManifestFile).FullName -ModuleVersion $NewVersion |
| 100 | + } |
| 101 | + |
| 102 | + begin {} |
| 103 | + |
| 104 | + end { |
| 105 | + Write-Output "$NewVersion$PrereleaseTag" |
| 106 | + } |
| 107 | +} |
0 commit comments