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