Last active
September 23, 2024 10:00
-
-
Save semick-dev/2bb327977ecaa987fa86041afa039237 to your computer and use it in GitHub Desktop.
A few convenience functions for the test-proxy.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Set-StrictMode -Version 4 | |
$AVAILABLE_TEST_PROXY_BINARIES = @{ | |
"Windows" = @{ | |
"AMD64" = @{ | |
"system" = "Windows" | |
"machine" = "AMD64" | |
"file_name" = "test-proxy-standalone-win-x64.zip" | |
"executable" = "Azure.Sdk.Tools.TestProxy.exe" | |
} | |
} | |
"Linux" = @{ | |
"X86_64" = @{ | |
"system" = "Linux" | |
"machine" = "X86_64" | |
"file_name" = "test-proxy-standalone-linux-x64.tar.gz" | |
"executable" = "Azure.Sdk.Tools.TestProxy" | |
} | |
"ARM64" = @{ | |
"system" = "Linux" | |
"machine" = "ARM64" | |
"file_name" = "test-proxy-standalone-linux-arm64.tar.gz" | |
"executable" = "Azure.Sdk.Tools.TestProxy" | |
} | |
} | |
"Darwin" = @{ | |
"X86_64" = @{ | |
"system" = "Darwin" | |
"machine" = "X86_64" | |
"file_name" = "test-proxy-standalone-osx-x64.zip" | |
"executable" = "Azure.Sdk.Tools.TestProxy" | |
} | |
"ARM64" = @{ | |
"system" = "Darwin" | |
"machine" = "ARM64" | |
"file_name" = "test-proxy-standalone-osx-arm64.zip" | |
"executable" = "Azure.Sdk.Tools.TestProxy" | |
} | |
} | |
} | |
function Get-Proxy-Meta () { | |
$ErrorActionPreferenceDefault = $ErrorActionPreference | |
$ErrorActionPreference = "Stop" | |
$os = "unknown" | |
$machine = "unknown" | |
# awkwardly long but dependable | |
if ($IsWindows) { | |
$os = "Windows" | |
} elseif ($IsLinux) { | |
$os = "Linux" | |
} elseif ($IsMacOS) { | |
$os = "Darwin" | |
} | |
# Determine the architecture | |
$architecture = (Get-CimInstance Win32_OperatingSystem).OSArchitecture | |
# this is a bit awkward, but should do what we need reliably on the platforms we support | |
if ($architecture -match "ARM64") { | |
$machine = "ARM64" | |
} elseif ($architecture -match "64") { | |
$machine = "X86_64" | |
if ($IsWindows) { | |
$machine = "AMD64" | |
} | |
} else { | |
$machine = "unknown" | |
} | |
$ErrorActionPreference = $ErrorActionPreferenceDefault | |
return $AVAILABLE_TEST_PROXY_BINARIES[$os][$machine] | |
} | |
function Get-Proxy-Url ( | |
[Parameter(mandatory=$true)]$Version | |
) { | |
$systemDetails = Get-Proxy-Meta | |
$file = $systemDetails.file_name | |
$url = "https://github.com/Azure/azure-sdk-tools/releases/download/Azure.Sdk.Tools.TestProxy_$Version/$file" | |
return $url | |
} | |
Export-ModuleMember -Function Get-Proxy-Url | |
function Cleanup-Directory ($path) { | |
if (Test-Path -Path $path) { | |
Remove-Item -Path $path -Recurse -Force | |
} | |
New-Item -ItemType Directory -Path $path -Force | |
} | |
function Is-Work-Necessary ( | |
[Parameter(mandatory=$true)] | |
$Version, | |
[Parameter(mandatory=$true)] | |
$Directory | |
) { | |
$savedVersionTxt = Join-Path $Directory "downloaded_version.txt" | |
if (Test-Path $savedVersionTxt) { | |
$result = (Get-Content -Raw $savedVersionTxt).Trim() | |
if ($result -eq $Version) { | |
return $false | |
} | |
} | |
return $true | |
} | |
<# | |
.SYNOPSIS | |
Installs the test-proxy as a dotnet tool installation. | |
.COMPONENT | |
locker | |
.PARAMETER Version | |
The version of the proxy to install. Defaults to latest release. | |
.PARAMETER Feed | |
The feed from which to install the test-proxy. Defaults to public azure sdk for net official feed. | |
.PARAMETER TargetDirectory | |
If provided, the tool will be installed to this directory, rather than globally. | |
#> | |
function Install-TestProxy-Tool ( | |
$Version = "1.0.0-dev*", | |
$Feed = "https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-net/nuget/v3/index.json", | |
$TargetDirectory = $null | |
) { | |
$adjustableArgs = "--global" | |
if ($TargetDirectory -ne $null) { | |
$adjustableArgs = "--tool-path `"$TargetDirectory`"" | |
} | |
Write-Host "dotnet tool update azure.sdk.tools.testproxy $adjustableArgs --add-source $Feed --version $Version --ignore-failed-sources" | |
dotnet tool update azure.sdk.tools.testproxy $adjustableArgs --add-source $Feed --version $Version --ignore-failed-sources | |
} | |
Export-ModuleMember -Function Install-TestProxy-Tool | |
<# | |
.SYNOPSIS | |
Installs a local copy of the test-proxy. | |
.COMPONENT | |
locker | |
.PARAMETER ProjectDirectory | |
Where does the solution or project directory exist? Defaults to ".". | |
.PARAMETER TargetDirectory | |
If provided, the tool will be installed to this directory, rather than globally. | |
#> | |
function Install-Local-TestProxy ( | |
$ProjectDirectory = ".", | |
$TargetDirectory = $null | |
) { | |
$tmpDir = Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath ([System.Guid]::NewGuid().ToString()) | |
New-Item -ItemType Directory -Path $tmpDir | |
$adjustableArgs = "--global" | |
if ($TargetDirectory -ne $null) { | |
$adjustableArgs = "--tool-path `"$TargetDirectory`"" | |
} | |
dotnet pack /p:ArtifactsPackagesDir=$tmpDir -c Release $ProjectDirectory | |
Write-Host "Built Test-Proxy located in $tmpDir" | |
dotnet tool update azure.sdk.tools.testproxy $adjustableArgs --prerelease --add-source $tmpDir | |
} | |
Export-ModuleMember -Function Install-Local-TestProxy | |
<# | |
.SYNOPSIS | |
Installs a standalone version of the test-proxy. | |
.COMPONENT | |
locker | |
.PARAMETER Version | |
The version of the proxy to install. Requires a full version to be provided. EG "1.0.0-dev.20240617.1" | |
.PARAMETER Directory | |
The directory within which the test-proxy exe will exist after this function invokes. Defaults to "." | |
#> | |
function Install-TestProxy-Standalone ( | |
[Parameter(mandatory=$true)] | |
$Version, | |
$Directory="." | |
) { | |
$ErrorActionPreference = "Stop" | |
$systemDetails = Get-Proxy-Meta | |
$downloadFolder = Resolve-Path $Directory | |
$downloadUrl = Get-Proxy-Url $Version | |
$downloadFile = $downloadUrl.Split('/')[-1] | |
$downloadLocation = Join-Path $downloadFolder $downloadFile | |
$savedVersionTxt = Join-Path $downloadFolder "downloaded_version.txt" | |
if (Is-Work-Necessary $version $downloadFolder) { | |
Write-Host "Commencing installation of `"$Version`" to `"$downloadFolder`" from $downloadUrl." | |
Invoke-WebRequest -Uri $downloadUrl -OutFile $downloadLocation | |
if ($downloadFile -like "*.zip") { | |
Expand-Archive -Path $downloadLocation -DestinationPath $downloadFolder -Force | |
} elseif ($downloadFile -like "*.tar.gz") { | |
tar -xzf $downloadLocation -C $downloadFolder | |
} else { | |
throw "Unsupported file format" | |
} | |
# Remove the downloaded file after extraction | |
Remove-Item -Path $downloadLocation -Force | |
# Record downloaded version | |
Set-Content -Path $savedVersionTxt -Value $Version | |
# Set executable permissions if on macOS (Darwin) | |
$executable_path = Join-Path $downloadFolder $systemDetails.executable | |
if ($IsMacOS) { | |
chmod 755 $executable_path | |
} | |
} | |
else { | |
Write-Host "Target version `"$Version`" already present in target directory `"$downloadFolder.`"" | |
} | |
} | |
Export-ModuleMember -Function Install-TestProxy-Standalone |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment