PatchVersionPrefix.ps1 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. }
  51. return "Bumping version from $version to $($node.InnerText)"
  52. }
  53. foreach ($repo in $Repos) {
  54. $repoPath = "$PSScriptRoot/../modules/$repo"
  55. Push-Location $repoPath
  56. try
  57. {
  58. $path = "$repoPath/version.props"
  59. Write-Host -ForegroundColor Magenta "Updating $repo"
  60. if (-not (Test-Path $path)) {
  61. Write-Warning "$path does not exist"
  62. continue
  63. }
  64. $path = Resolve-Path $path
  65. Write-Verbose "$path"
  66. [xml] $xml = LoadXml $path
  67. $suffix = $xml.SelectSingleNode('/Project/PropertyGroup/VersionSuffix')
  68. if (-not $suffix) {
  69. write-error "$path does not have VersionSuffix"
  70. }
  71. if ($VersionSuffix) {
  72. SetVersionSuffix $xml.SelectSingleNode('/Project/PropertyGroup/VersionSuffix') | write-host
  73. SetVersionSuffix $xml.SelectSingleNode('/Project/PropertyGroup/ExperimentalProjectVersionSuffix') | write-host
  74. SetVersionSuffix $xml.SelectSingleNode('/Project/PropertyGroup/ExperimentalVersionSuffix') | write-host
  75. }
  76. $versionPrefix = $xml.SelectSingleNode('/Project/PropertyGroup/VersionPrefix')
  77. $epxVersionPrefix = $xml.SelectSingleNode('/Project/PropertyGroup/ExperimentalProjectVersionPrefix')
  78. $exVersionPrefix = $xml.SelectSingleNode('/Project/PropertyGroup/ExperimentalVersionPrefix')
  79. BumpVersion $epxVersionPrefix | write-host
  80. BumpVersion $exVersionPrefix | write-host
  81. $message = BumpVersion $versionPrefix
  82. Write-Host $message
  83. if ($PSCmdlet.ShouldProcess("Update $path")) {
  84. SaveXml $xml $path
  85. if (-not $NoCommit) {
  86. Invoke-Block { & git add $path }
  87. Invoke-Block { & git commit -m $message }
  88. }
  89. }
  90. }
  91. finally
  92. {
  93. Pop-Location
  94. }
  95. }