PatchVersionPrefix.ps1 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/usr/bin/env pwsh -c
  2. <#
  3. .SYNOPSIS
  4. Updates the version.props file in repos to a newer patch version
  5. .PARAMETER Repos
  6. A list of the repositories that should be patched
  7. .PARAMETER Mode
  8. Version bump options: Major, Minor, Patch
  9. .PARAMETER VersionSuffix
  10. The version suffix to use
  11. #>
  12. [cmdletbinding(SupportsShouldProcess = $true)]
  13. param(
  14. [Parameter(Mandatory = $true)]
  15. [string[]]$Repos,
  16. [Parameter(Mandatory = $true)]
  17. [ValidateSet('Major', 'Minor', 'Patch')]
  18. [string]$Mode,
  19. [string]$VersionSuffix = $null,
  20. [switch]$NoCommit
  21. )
  22. $ErrorActionPreference = 'Stop'
  23. Import-Module -Scope Local -Force "$PSScriptRoot/common.psm1"
  24. function SetVersionSuffix([System.Xml.XmlNode]$node) {
  25. if (-not $node) {
  26. return
  27. }
  28. $node.InnerText = $VersionSuffix
  29. return "Setting $($node.Name) to $VersionSuffix"
  30. }
  31. function BumpVersion([System.Xml.XmlNode]$node) {
  32. if (-not $node) {
  33. return
  34. }
  35. [version] $version = $node.InnerText
  36. $experimental = $version.Major -eq 0
  37. switch ($mode) {
  38. { ($_ -ne 'Patch') -and $experimental} {
  39. $node.InnerText = "{0}.{1}.{2}" -f $version.Major, ($version.Minor + 1), 0
  40. }
  41. { ($_ -eq 'Major') -and -not $experimental } {
  42. $node.InnerText = "{0}.{1}.{2}" -f ($version.Major + 1), 0, 0
  43. }
  44. { ($_ -eq 'Minor') -and -not $experimental } {
  45. $node.InnerText = "{0}.{1}.{2}" -f $version.Major, ($version.Minor + 1), 0
  46. }
  47. 'Patch' {
  48. $node.InnerText = "{0}.{1}.{2}" -f $version.Major, $version.Minor, ($version.Build + 1)
  49. }
  50. default {
  51. throw "Could not figure out how to apply patch policy $mode"
  52. }
  53. }
  54. return "Bumping version from $version to $($node.InnerText)"
  55. }
  56. foreach ($repo in $Repos) {
  57. $repoPath = "$PSScriptRoot/../modules/$repo"
  58. Push-Location $repoPath
  59. try
  60. {
  61. $path = "$repoPath/version.props"
  62. Write-Host -ForegroundColor Magenta "Updating $repo"
  63. if (-not (Test-Path $path)) {
  64. Write-Warning "$path does not exist"
  65. continue
  66. }
  67. $path = Resolve-Path $path
  68. Write-Verbose "$path"
  69. [xml] $xml = LoadXml $path
  70. $suffix = $xml.SelectSingleNode('/Project/PropertyGroup/VersionSuffix')
  71. if (-not $suffix) {
  72. write-error "$path does not have VersionSuffix"
  73. }
  74. if ($VersionSuffix) {
  75. SetVersionSuffix $xml.SelectSingleNode('/Project/PropertyGroup/VersionSuffix') | write-host
  76. SetVersionSuffix $xml.SelectSingleNode('/Project/PropertyGroup/ExperimentalProjectVersionSuffix') | write-host
  77. SetVersionSuffix $xml.SelectSingleNode('/Project/PropertyGroup/ExperimentalVersionSuffix') | write-host
  78. }
  79. $versionPrefix = $xml.SelectSingleNode('/Project/PropertyGroup/VersionPrefix')
  80. $epxVersionPrefix = $xml.SelectSingleNode('/Project/PropertyGroup/ExperimentalProjectVersionPrefix')
  81. $exVersionPrefix = $xml.SelectSingleNode('/Project/PropertyGroup/ExperimentalVersionPrefix')
  82. BumpVersion $epxVersionPrefix | write-host
  83. BumpVersion $exVersionPrefix | write-host
  84. $message = BumpVersion $versionPrefix
  85. Write-Host $message
  86. if ($PSCmdlet.ShouldProcess("Update $path")) {
  87. SaveXml $xml $path
  88. if (-not $NoCommit) {
  89. Invoke-Block { & git add $path }
  90. Invoke-Block { & git commit -m $message }
  91. }
  92. }
  93. }
  94. finally
  95. {
  96. Pop-Location
  97. }
  98. }