UpdateDependencies.ps1 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/env pwsh -c
  2. <#
  3. .PARAMETER BuildXml
  4. The URL or file path to a build.xml file that defines package versions to be used
  5. #>
  6. [CmdletBinding()]
  7. param(
  8. [Parameter(Mandatory = $true)]
  9. $BuildXml,
  10. [string[]]$ConfigVars = @()
  11. )
  12. $ErrorActionPreference = 'Stop'
  13. Import-Module -Scope Local -Force "$PSScriptRoot/common.psm1"
  14. Set-StrictMode -Version 1
  15. $depsPath = Resolve-Path "$PSScriptRoot/../build/dependencies.props"
  16. [xml] $dependencies = LoadXml $depsPath
  17. if ($BuildXml -like 'http*') {
  18. $url = $BuildXml
  19. New-Item -Type Directory "$PSScriptRoot/../obj/" -ErrorAction Ignore
  20. $localXml = "$PSScriptRoot/../obj/build.xml"
  21. Write-Verbose "Downloading from $url to $BuildXml"
  22. Invoke-WebRequest -OutFile $localXml $url
  23. }
  24. [xml] $remoteDeps = LoadXml $localXml
  25. $count = 0
  26. $variables = @{}
  27. foreach ($package in $remoteDeps.SelectNodes('//Package')) {
  28. $packageId = $package.Id
  29. $packageVersion = $package.Version
  30. $varName = PackageIdVarName $packageId
  31. Write-Verbose "Found {id: $packageId, version: $packageVersion, varName: $varName }"
  32. if ($variables[$varName]) {
  33. if ($variables[$varName].Where( {$_ -eq $packageVersion}, 'First').Count -eq 0) {
  34. $variables[$varName] += $packageVersion
  35. }
  36. }
  37. else {
  38. $variables[$varName] = @($packageVersion)
  39. }
  40. }
  41. $updatedVars = @{}
  42. foreach ($varName in ($variables.Keys | sort)) {
  43. $packageVersions = $variables[$varName]
  44. if ($packageVersions.Length -gt 1) {
  45. Write-Warning "Skipped $varName. Multiple version found. { $($packageVersions -join ', ') }."
  46. continue
  47. }
  48. $packageVersion = $packageVersions | Select-Object -First 1
  49. $depVarNode = $dependencies.SelectSingleNode("//PropertyGroup[`@Label=`"Package Versions: Auto`"]/$varName")
  50. if ($depVarNode -and $depVarNode.InnerText -ne $packageVersion) {
  51. $depVarNode.InnerText = $packageVersion
  52. $count++
  53. Write-Host -f DarkGray " Updating $varName to $packageVersion"
  54. $updatedVars[$varName] = $packageVersion
  55. }
  56. }
  57. if ($count -gt 0) {
  58. Write-Host -f Cyan "Updating $count version variables in $depsPath"
  59. SaveXml $dependencies $depsPath
  60. # Ensure dotnet is installed
  61. & "$PSScriptRoot\..\run.ps1" install-tools
  62. $ProjectPath = "$PSScriptRoot\update-dependencies\update-dependencies.csproj"
  63. $ConfigVars += "--BuildXml"
  64. $ConfigVars += $BuildXml
  65. $ConfigVars += "--UpdatedVersions"
  66. $varString = ""
  67. foreach ($updatedVar in $updatedVars.GetEnumerator()) {
  68. $varString += "$($updatedVar.Name)=$($updatedVar.Value);"
  69. }
  70. $ConfigVars += $varString
  71. # Restore and run the app
  72. Write-Host "Invoking App $ProjectPath..."
  73. Invoke-Expression "dotnet run -p `"$ProjectPath`" @ConfigVars"
  74. if ($LASTEXITCODE -ne 0) { throw "Build failed" }
  75. }
  76. else {
  77. Write-Host -f Green "No changes found"
  78. }