|
| 1 | +Set-StrictMode -Version 4 |
| 2 | +$AVAILABLE_TEST_PROXY_BINARIES = @{ |
| 3 | + "Windows" = @{ |
| 4 | + "AMD64" = @{ |
| 5 | + "system" = "Windows" |
| 6 | + "machine" = "AMD64" |
| 7 | + "file_name" = "test-proxy-standalone-win-x64.zip" |
| 8 | + "executable" = "Azure.Sdk.Tools.TestProxy.exe" |
| 9 | + } |
| 10 | + } |
| 11 | + "Linux" = @{ |
| 12 | + "X86_64" = @{ |
| 13 | + "system" = "Linux" |
| 14 | + "machine" = "X86_64" |
| 15 | + "file_name" = "test-proxy-standalone-linux-x64.tar.gz" |
| 16 | + "executable" = "Azure.Sdk.Tools.TestProxy" |
| 17 | + } |
| 18 | + "ARM64" = @{ |
| 19 | + "system" = "Linux" |
| 20 | + "machine" = "ARM64" |
| 21 | + "file_name" = "test-proxy-standalone-linux-arm64.tar.gz" |
| 22 | + "executable" = "Azure.Sdk.Tools.TestProxy" |
| 23 | + } |
| 24 | + } |
| 25 | + "Darwin" = @{ |
| 26 | + "X86_64" = @{ |
| 27 | + "system" = "Darwin" |
| 28 | + "machine" = "X86_64" |
| 29 | + "file_name" = "test-proxy-standalone-osx-x64.zip" |
| 30 | + "executable" = "Azure.Sdk.Tools.TestProxy" |
| 31 | + } |
| 32 | + "ARM64" = @{ |
| 33 | + "system" = "Darwin" |
| 34 | + "machine" = "ARM64" |
| 35 | + "file_name" = "test-proxy-standalone-osx-arm64.zip" |
| 36 | + "executable" = "Azure.Sdk.Tools.TestProxy" |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +function Get-SystemArchitecture { |
| 42 | + $unameOutput = uname -m |
| 43 | + switch ($unameOutput) { |
| 44 | + "x86_64" { return "X86_64" } |
| 45 | + "aarch64" { return "ARM64" } |
| 46 | + "arm64" { return "ARM64" } |
| 47 | + default { throw "Unable to determine system architecture. uname -m returned $unameOutput." } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +function Get-Proxy-Meta () { |
| 52 | + $ErrorActionPreferenceDefault = $ErrorActionPreference |
| 53 | + $ErrorActionPreference = "Stop" |
| 54 | + |
| 55 | + $os = "unknown" |
| 56 | + $machine = Get-SystemArchitecture |
| 57 | + |
| 58 | + if ($IsWindows) { |
| 59 | + $os = "Windows" |
| 60 | + # we only support x64 on windows, if that doesn't work the platform is unsupported |
| 61 | + $machine = "AMD64" |
| 62 | + } elseif ($IsLinux) { |
| 63 | + $os = "Linux" |
| 64 | + } elseif ($IsMacOS) { |
| 65 | + $os = "Darwin" |
| 66 | + } |
| 67 | + |
| 68 | + $ErrorActionPreference = $ErrorActionPreferenceDefault |
| 69 | + |
| 70 | + return $AVAILABLE_TEST_PROXY_BINARIES[$os][$machine] |
| 71 | +} |
| 72 | + |
| 73 | +function Get-Proxy-Url ( |
| 74 | + [Parameter(mandatory=$true)]$Version |
| 75 | +) { |
| 76 | + $systemDetails = Get-Proxy-Meta |
| 77 | + |
| 78 | + $file = $systemDetails.file_name |
| 79 | + $url = "https://github.com/Azure/azure-sdk-tools/releases/download/Azure.Sdk.Tools.TestProxy_$Version/$file" |
| 80 | + |
| 81 | + return $url |
| 82 | +} |
| 83 | + |
| 84 | +function Cleanup-Directory ($path) { |
| 85 | + if (Test-Path -Path $path) { |
| 86 | + Remove-Item -Path $path -Recurse -Force |
| 87 | + } |
| 88 | + New-Item -ItemType Directory -Path $path -Force |
| 89 | +} |
| 90 | + |
| 91 | +function Is-Work-Necessary ( |
| 92 | + [Parameter(mandatory=$true)] |
| 93 | + $Version, |
| 94 | + [Parameter(mandatory=$true)] |
| 95 | + $Directory |
| 96 | +) { |
| 97 | + $savedVersionTxt = Join-Path $Directory "downloaded_version.txt" |
| 98 | + if (Test-Path $savedVersionTxt) { |
| 99 | + $result = (Get-Content -Raw $savedVersionTxt).Trim() |
| 100 | + |
| 101 | + if ($result -eq $Version) { |
| 102 | + return $false |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return $true |
| 107 | +} |
| 108 | + |
| 109 | +<# |
| 110 | +.SYNOPSIS |
| 111 | +Installs a standalone version of the test-proxy. |
| 112 | +.PARAMETER Version |
| 113 | +The version of the proxy to install. Requires a full version to be provided. EG "1.0.0-dev.20240617.1" |
| 114 | +.PARAMETER Directory |
| 115 | +The directory within which the test-proxy exe will exist after this function invokes. Defaults to "." |
| 116 | +#> |
| 117 | +function Install-Standalone-TestProxy ( |
| 118 | + [Parameter(mandatory=$true)] |
| 119 | + $Version, |
| 120 | + $Directory="." |
| 121 | +) { |
| 122 | + $ErrorActionPreference = "Stop" |
| 123 | + $systemDetails = Get-Proxy-Meta |
| 124 | + |
| 125 | + if (!(Test-Path $Directory) -and $Directory -ne ".") { |
| 126 | + New-Item -ItemType Directory -Path $Directory -Force |
| 127 | + } |
| 128 | + |
| 129 | + $downloadFolder = Resolve-Path $Directory |
| 130 | + $downloadUrl = Get-Proxy-Url $Version |
| 131 | + $downloadFile = $downloadUrl.Split('/')[-1] |
| 132 | + $downloadLocation = Join-Path $downloadFolder $downloadFile |
| 133 | + $savedVersionTxt = Join-Path $downloadFolder "downloaded_version.txt" |
| 134 | + |
| 135 | + if (Is-Work-Necessary $version $downloadFolder) { |
| 136 | + Write-Host "Commencing installation of `"$Version`" to `"$downloadFolder`" from $downloadUrl." |
| 137 | + Invoke-WebRequest -Uri $downloadUrl -OutFile $downloadLocation |
| 138 | + |
| 139 | + if ($downloadFile -like "*.zip") { |
| 140 | + Expand-Archive -Path $downloadLocation -DestinationPath $downloadFolder -Force |
| 141 | + } elseif ($downloadFile -like "*.tar.gz") { |
| 142 | + tar -xzf $downloadLocation -C $downloadFolder |
| 143 | + } else { |
| 144 | + throw "Unsupported file format" |
| 145 | + } |
| 146 | + |
| 147 | + # Remove the downloaded file after extraction |
| 148 | + Remove-Item -Path $downloadLocation -Force |
| 149 | + |
| 150 | + # Record downloaded version |
| 151 | + Set-Content -Path $savedVersionTxt -Value $Version |
| 152 | + |
| 153 | + # Set executable permissions if on macOS (Darwin) |
| 154 | + $executable_path = Join-Path $downloadFolder $systemDetails.executable |
| 155 | + if ($IsMacOS) { |
| 156 | + chmod 755 $executable_path |
| 157 | + } |
| 158 | + } |
| 159 | + else { |
| 160 | + Write-Host "Target version `"$Version`" already present in target directory `"$downloadFolder.`"" |
| 161 | + } |
| 162 | +} |
0 commit comments