UpdateBuildTools.ps1 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/env pwsh
  2. <#
  3. .SYNOPSIS
  4. Updates the build tools version and generates a commit message with the list of changes
  5. .PARAMETER RepoRoot
  6. The directory containing the repo
  7. .PARAMETER GitAuthorName
  8. The author name to use in the commit message. (Optional)
  9. .PARAMETER GitAuthorEmail
  10. The author email to use in the commit message. (Optional)
  11. .PARAMETER GitCommitArgs
  12. Additional arguments to pass into git-commit
  13. .PARAMETER NoCommit
  14. Make changes without executing git-commit
  15. .PARAMETER Force
  16. Specified this to make a commit with any changes
  17. #>
  18. [cmdletbinding(SupportsShouldProcess = $true)]
  19. param(
  20. [string]$RepoRoot,
  21. [string]$GitAuthorName = $null,
  22. [string]$GitAuthorEmail = $null,
  23. [string[]]$GitCommitArgs = @(),
  24. [switch]$NoCommit,
  25. [switch]$Force
  26. )
  27. $ErrorActionPreference = 'Stop'
  28. Set-StrictMode -Version 2
  29. if (-not $RepoRoot) {
  30. $RepoRoot = Resolve-Path "$PSScriptRoot\.."
  31. }
  32. Import-Module "$PSScriptRoot/common.psm1" -Scope Local -Force
  33. function Get-KoreBuildVersion {
  34. $lockFile = "$RepoRoot/korebuild-lock.txt"
  35. if (!(Test-Path $lockFile)) {
  36. return ''
  37. }
  38. $version = Get-Content $lockFile | Where-Object { $_ -like 'version:*' } | Select-Object -first 1
  39. if (!$version) {
  40. Write-Error "Failed to parse version from $lockFile. Expected a line that begins with 'version:'"
  41. }
  42. $version = $version.TrimStart('version:').Trim()
  43. return $version
  44. }
  45. Push-Location $RepoRoot
  46. try {
  47. Assert-Git
  48. $oldVersion = Get-KoreBuildVersion
  49. # Executes a command that no-ops. The only thing we really need is the updated version of korebuild-lock.txt
  50. & "$RepoRoot/run.ps1" -Update --help | Out-Null
  51. $newVersion = Get-KoreBuildVersion
  52. if ($oldVersion -eq $newVersion) {
  53. Write-Host -ForegroundColor Magenta 'No changes to build tools'
  54. exit 0
  55. }
  56. $deps = Get-Content "$RepoRoot/build/dependencies.props" `
  57. | % {
  58. if ($_ -like '*<InternalAspNetCoreSdkPackageVersion>*') {
  59. " <InternalAspNetCoreSdkPackageVersion>$newVersion</InternalAspNetCoreSdkPackageVersion>"
  60. } else {
  61. $_
  62. }
  63. }
  64. $deps | Set-Content -Encoding UTF8 "$RepoRoot/build/dependencies.props"
  65. Invoke-Block { git add "$RepoRoot/korebuild-lock.txt" }
  66. Invoke-Block { git add "$RepoRoot/build/dependencies.props" }
  67. $shortMessage = "Updating BuildTools from $oldVersion to $newVersion"
  68. # add this to the commit message to make it possible to filter commit triggers based on message
  69. $message = "$shortMessage`n`n[auto-updated: buildtools]"
  70. if (-not $NoCommit -and ($Force -or ($PSCmdlet.ShouldContinue($shortMessage, 'Create a new commit with these changes?')))) {
  71. $gitConfigArgs = @()
  72. if ($GitAuthorName) {
  73. $gitConfigArgs += '-c',"user.name=$GitAuthorName"
  74. }
  75. if ($GitAuthorEmail) {
  76. $gitConfigArgs += '-c',"user.email=$GitAuthorEmail"
  77. }
  78. Invoke-Block { git @gitConfigArgs commit -m $message @GitCommitArgs }
  79. }
  80. else {
  81. # If composing this script with others, return the message that would have been used
  82. return @{
  83. message = $message
  84. }
  85. }
  86. }
  87. finally {
  88. Pop-Location
  89. }