init-tools-native.ps1 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <#
  2. .SYNOPSIS
  3. Entry point script for installing native tools
  4. .DESCRIPTION
  5. Reads $RepoRoot\global.json file to determine native assets to install
  6. and executes installers for those tools
  7. .PARAMETER BaseUri
  8. Base file directory or Url from which to acquire tool archives
  9. .PARAMETER InstallDirectory
  10. Directory to install native toolset. This is a command-line override for the default
  11. Install directory precedence order:
  12. - InstallDirectory command-line override
  13. - NETCOREENG_INSTALL_DIRECTORY environment variable
  14. - (default) %USERPROFILE%/.netcoreeng/native
  15. .PARAMETER Clean
  16. Switch specifying to not install anything, but cleanup native asset folders
  17. .PARAMETER Force
  18. Clean and then install tools
  19. .PARAMETER DownloadRetries
  20. Total number of retry attempts
  21. .PARAMETER RetryWaitTimeInSeconds
  22. Wait time between retry attempts in seconds
  23. .PARAMETER GlobalJsonFile
  24. File path to global.json file
  25. .NOTES
  26. #>
  27. [CmdletBinding(PositionalBinding=$false)]
  28. Param (
  29. [string] $BaseUri = 'https://netcorenativeassets.blob.core.windows.net/resource-packages/external',
  30. [string] $InstallDirectory,
  31. [switch] $Clean = $False,
  32. [switch] $Force = $False,
  33. [int] $DownloadRetries = 5,
  34. [int] $RetryWaitTimeInSeconds = 30,
  35. [string] $GlobalJsonFile
  36. )
  37. if (!$GlobalJsonFile) {
  38. $GlobalJsonFile = Join-Path (Get-Item $PSScriptRoot).Parent.Parent.FullName 'global.json'
  39. }
  40. Set-StrictMode -version 2.0
  41. $ErrorActionPreference='Stop'
  42. . $PSScriptRoot\pipeline-logging-functions.ps1
  43. Import-Module -Name (Join-Path $PSScriptRoot 'native\CommonLibrary.psm1')
  44. try {
  45. # Define verbose switch if undefined
  46. $Verbose = $VerbosePreference -Eq 'Continue'
  47. $EngCommonBaseDir = Join-Path $PSScriptRoot 'native\'
  48. $NativeBaseDir = $InstallDirectory
  49. if (!$NativeBaseDir) {
  50. $NativeBaseDir = CommonLibrary\Get-NativeInstallDirectory
  51. }
  52. $Env:CommonLibrary_NativeInstallDir = $NativeBaseDir
  53. $InstallBin = Join-Path $NativeBaseDir 'bin'
  54. $InstallerPath = Join-Path $EngCommonBaseDir 'install-tool.ps1'
  55. # Process tools list
  56. Write-Host "Processing $GlobalJsonFile"
  57. If (-Not (Test-Path $GlobalJsonFile)) {
  58. Write-Host "Unable to find '$GlobalJsonFile'"
  59. exit 0
  60. }
  61. $NativeTools = Get-Content($GlobalJsonFile) -Raw |
  62. ConvertFrom-Json |
  63. Select-Object -Expand 'native-tools' -ErrorAction SilentlyContinue
  64. if ($NativeTools) {
  65. $NativeTools.PSObject.Properties | ForEach-Object {
  66. $ToolName = $_.Name
  67. $ToolVersion = $_.Value
  68. $LocalInstallerArguments = @{ ToolName = "$ToolName" }
  69. $LocalInstallerArguments += @{ InstallPath = "$InstallBin" }
  70. $LocalInstallerArguments += @{ BaseUri = "$BaseUri" }
  71. $LocalInstallerArguments += @{ CommonLibraryDirectory = "$EngCommonBaseDir" }
  72. $LocalInstallerArguments += @{ Version = "$ToolVersion" }
  73. if ($Verbose) {
  74. $LocalInstallerArguments += @{ Verbose = $True }
  75. }
  76. if (Get-Variable 'Force' -ErrorAction 'SilentlyContinue') {
  77. if($Force) {
  78. $LocalInstallerArguments += @{ Force = $True }
  79. }
  80. }
  81. if ($Clean) {
  82. $LocalInstallerArguments += @{ Clean = $True }
  83. }
  84. Write-Verbose "Installing $ToolName version $ToolVersion"
  85. Write-Verbose "Executing '$InstallerPath $($LocalInstallerArguments.Keys.ForEach({"-$_ '$($LocalInstallerArguments.$_)'"}) -join ' ')'"
  86. & $InstallerPath @LocalInstallerArguments
  87. if ($LASTEXITCODE -Ne "0") {
  88. $errMsg = "$ToolName installation failed"
  89. if ((Get-Variable 'DoNotAbortNativeToolsInstallationOnFailure' -ErrorAction 'SilentlyContinue') -and $DoNotAbortNativeToolsInstallationOnFailure) {
  90. $showNativeToolsWarning = $true
  91. if ((Get-Variable 'DoNotDisplayNativeToolsInstallationWarnings' -ErrorAction 'SilentlyContinue') -and $DoNotDisplayNativeToolsInstallationWarnings) {
  92. $showNativeToolsWarning = $false
  93. }
  94. if ($showNativeToolsWarning) {
  95. Write-Warning $errMsg
  96. }
  97. $toolInstallationFailure = $true
  98. } else {
  99. Write-PipelineTelemetryError -Category 'NativeToolsBootstrap' -Message $errMsg
  100. exit 1
  101. }
  102. }
  103. }
  104. if ((Get-Variable 'toolInstallationFailure' -ErrorAction 'SilentlyContinue') -and $toolInstallationFailure) {
  105. Write-PipelineTelemetryError -Category 'NativeToolsBootstrap' -Message 'Native tools bootstrap failed'
  106. exit 1
  107. }
  108. }
  109. else {
  110. Write-Host 'No native tools defined in global.json'
  111. exit 0
  112. }
  113. if ($Clean) {
  114. exit 0
  115. }
  116. if (Test-Path $InstallBin) {
  117. Write-Host 'Native tools are available from ' (Convert-Path -Path $InstallBin)
  118. Write-Host "##vso[task.prependpath]$(Convert-Path -Path $InstallBin)"
  119. return $InstallBin
  120. }
  121. else {
  122. Write-PipelineTelemetryError -Category 'NativeToolsBootstrap' -Message 'Native tools install directory does not exist, installation failed'
  123. exit 1
  124. }
  125. exit 0
  126. }
  127. catch {
  128. Write-Host $_.ScriptStackTrace
  129. Write-PipelineTelemetryError -Category 'NativeToolsBootstrap' -Message $_
  130. ExitWithExitCode 1
  131. }