Install-BuildDependencies.ps1 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. function Install-BuildDependencies {
  2. <#
  3. .SYNOPSIS
  4. Installs required build dependencies.
  5. .DESCRIPTION
  6. Additional packages might be needed for successful builds. This module contains additional
  7. dependencies available for installation via winget and, if possible, adds their locations
  8. to the environment path for future invocation.
  9. .EXAMPLE
  10. Install-BuildDependencies
  11. #>
  12. param(
  13. [string] $WingetFile = "$PSScriptRoot/.Wingetfile"
  14. )
  15. if ( ! ( Test-Path function:Log-Warning ) ) {
  16. . $PSScriptRoot/Logger.ps1
  17. }
  18. $Prefixes = @{
  19. 'x64' = ${env:ProgramFiles}
  20. 'x86' = ${env:ProgramFiles(x86)}
  21. 'arm64' = ${env:ProgramFiles(arm)}
  22. }
  23. $Paths = $env:Path -split [System.IO.Path]::PathSeparator
  24. $WingetOptions = @('install', '--accept-package-agreements', '--accept-source-agreements')
  25. if ( $script:Quiet ) {
  26. $WingetOptions += '--silent'
  27. }
  28. Log-Group 'Check Windows build requirements'
  29. Get-Content $WingetFile | ForEach-Object {
  30. $_, $Package, $_, $Path, $_, $Binary, $_, $Version = $_ -replace ',','' -split " +(?=(?:[^\']*\'[^\']*\')*[^\']*$)" -replace "'",''
  31. $Prefixes.GetEnumerator() | ForEach-Object {
  32. $Prefix = $_.value
  33. $FullPath = "${Prefix}\${Path}"
  34. if ( ( Test-Path $FullPath ) -and ! ( $Paths -contains $FullPath ) ) {
  35. $Paths = @($FullPath) + $Paths
  36. $env:Path = $Paths -join [System.IO.Path]::PathSeparator
  37. }
  38. }
  39. Log-Debug "Checking for command ${Binary}"
  40. $Found = Get-Command -ErrorAction SilentlyContinue $Binary
  41. if ( $Found ) {
  42. Log-Status "Found dependency ${Binary} as $($Found.Source)"
  43. } else {
  44. Log-Status "Installing package ${Package} $(if ( $Version -ne $null ) { "Version: ${Version}" } )"
  45. if ( $Version -ne $null ) {
  46. $WingGetOptions += @('--version', ${Version})
  47. }
  48. try {
  49. $Params = $WingetOptions + $Package
  50. winget @Params
  51. } catch {
  52. throw "Error while installing winget package ${Package}: $_"
  53. }
  54. }
  55. }
  56. Log-Group
  57. }