install-tool.ps1 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <#
  2. .SYNOPSIS
  3. Install native tool
  4. .DESCRIPTION
  5. Install cmake native tool from Azure blob storage
  6. .PARAMETER InstallPath
  7. Base directory to install native tool to
  8. .PARAMETER BaseUri
  9. Base file directory or Url from which to acquire tool archives
  10. .PARAMETER CommonLibraryDirectory
  11. Path to folder containing common library modules
  12. .PARAMETER Force
  13. Force install of tools even if they previously exist
  14. .PARAMETER Clean
  15. Don't install the tool, just clean up the current install of the tool
  16. .PARAMETER DownloadRetries
  17. Total number of retry attempts
  18. .PARAMETER RetryWaitTimeInSeconds
  19. Wait time between retry attempts in seconds
  20. .NOTES
  21. Returns 0 if install succeeds, 1 otherwise
  22. #>
  23. [CmdletBinding(PositionalBinding=$false)]
  24. Param (
  25. [Parameter(Mandatory=$True)]
  26. [string] $ToolName,
  27. [Parameter(Mandatory=$True)]
  28. [string] $InstallPath,
  29. [Parameter(Mandatory=$True)]
  30. [string] $BaseUri,
  31. [Parameter(Mandatory=$True)]
  32. [string] $Version,
  33. [string] $CommonLibraryDirectory = $PSScriptRoot,
  34. [switch] $Force = $False,
  35. [switch] $Clean = $False,
  36. [int] $DownloadRetries = 5,
  37. [int] $RetryWaitTimeInSeconds = 30
  38. )
  39. . $PSScriptRoot\..\pipeline-logging-functions.ps1
  40. # Import common library modules
  41. Import-Module -Name (Join-Path $CommonLibraryDirectory "CommonLibrary.psm1")
  42. try {
  43. # Define verbose switch if undefined
  44. $Verbose = $VerbosePreference -Eq "Continue"
  45. $Arch = CommonLibrary\Get-MachineArchitecture
  46. $ToolOs = "win64"
  47. if($Arch -Eq "x32") {
  48. $ToolOs = "win32"
  49. }
  50. $ToolNameMoniker = "$ToolName-$Version-$ToolOs-$Arch"
  51. $ToolInstallDirectory = Join-Path $InstallPath "$ToolName\$Version\"
  52. $Uri = "$BaseUri/windows/$ToolName/$ToolNameMoniker.zip"
  53. $ShimPath = Join-Path $InstallPath "$ToolName.exe"
  54. if ($Clean) {
  55. Write-Host "Cleaning $ToolInstallDirectory"
  56. if (Test-Path $ToolInstallDirectory) {
  57. Remove-Item $ToolInstallDirectory -Force -Recurse
  58. }
  59. Write-Host "Cleaning $ShimPath"
  60. if (Test-Path $ShimPath) {
  61. Remove-Item $ShimPath -Force
  62. }
  63. $ToolTempPath = CommonLibrary\Get-TempPathFilename -Path $Uri
  64. Write-Host "Cleaning $ToolTempPath"
  65. if (Test-Path $ToolTempPath) {
  66. Remove-Item $ToolTempPath -Force
  67. }
  68. exit 0
  69. }
  70. # Install tool
  71. if ((Test-Path $ToolInstallDirectory) -And (-Not $Force)) {
  72. Write-Verbose "$ToolName ($Version) already exists, skipping install"
  73. }
  74. else {
  75. $InstallStatus = CommonLibrary\DownloadAndExtract -Uri $Uri `
  76. -InstallDirectory $ToolInstallDirectory `
  77. -Force:$Force `
  78. -DownloadRetries $DownloadRetries `
  79. -RetryWaitTimeInSeconds $RetryWaitTimeInSeconds `
  80. -Verbose:$Verbose
  81. if ($InstallStatus -Eq $False) {
  82. Write-PipelineTelemetryError "Installation failed" -Category "NativeToolsetBootstrapping"
  83. exit 1
  84. }
  85. }
  86. $ToolFilePath = Get-ChildItem $ToolInstallDirectory -Recurse -Filter "$ToolName.exe" | % { $_.FullName }
  87. if (@($ToolFilePath).Length -Gt 1) {
  88. Write-Error "There are multiple copies of $ToolName in $($ToolInstallDirectory): `n$(@($ToolFilePath | out-string))"
  89. exit 1
  90. } elseif (@($ToolFilePath).Length -Lt 1) {
  91. Write-Error "$ToolName was not found in $ToolFilePath."
  92. exit 1
  93. }
  94. # Generate shim
  95. # Always rewrite shims so that we are referencing the expected version
  96. $GenerateShimStatus = CommonLibrary\New-ScriptShim -ShimName $ToolName `
  97. -ShimDirectory $InstallPath `
  98. -ToolFilePath "$ToolFilePath" `
  99. -BaseUri $BaseUri `
  100. -Force:$Force `
  101. -Verbose:$Verbose
  102. if ($GenerateShimStatus -Eq $False) {
  103. Write-PipelineTelemetryError "Generate shim failed" -Category "NativeToolsetBootstrapping"
  104. return 1
  105. }
  106. exit 0
  107. }
  108. catch {
  109. Write-Host $_.ScriptStackTrace
  110. Write-PipelineTelemetryError -Category "NativeToolsetBootstrapping" -Message $_
  111. exit 1
  112. }