Files
runner-images/images/macos/scripts/helpers/Common.Helpers.psm1
T

119 lines
3.9 KiB
PowerShell
Raw Normal View History

2020-09-10 14:34:08 +03:00
function Get-CommandResult {
param (
[Parameter(Mandatory=$true)]
[string] $Command,
[switch] $Multiline
)
# Bash trick to suppress and show error output because some commands write to stderr (for example, "python --version")
$stdout = & bash -c "$Command 2>&1"
$exitCode = $LASTEXITCODE
2023-12-04 12:13:08 +01:00
2020-09-10 14:34:08 +03:00
return @{
Output = If ($Multiline -eq $true) { $stdout } else { [string]$stdout }
ExitCode = $exitCode
}
}
# Gets path to the tool, analogue of 'which tool'
2023-12-04 12:13:08 +01:00
function Get-ToolPath($tool) {
2020-09-10 14:34:08 +03:00
return (Get-Command $tool).Path
}
# Returns the object with information about current OS
# It can be used for OS-specific tests
2020-09-10 14:34:08 +03:00
function Get-OSVersion {
$osVersion = [Environment]::OSVersion
$processorArchitecture = arch
2023-12-04 12:13:08 +01:00
2020-09-10 14:34:08 +03:00
return [PSCustomObject]@{
Version = $osVersion.Version
Platform = $osVersion.Platform
IsArm64 = $processorArchitecture -eq "arm64"
IsBigSur = $osVersion.Version.Major -eq "11"
IsMonterey = $osVersion.Version.Major -eq "12"
IsVentura = $($osVersion.Version.Major -eq "13")
IsVenturaArm64 = $($osVersion.Version.Major -eq "13" -and $processorArchitecture -eq "arm64")
IsVenturaX64 = $($osVersion.Version.Major -eq "13" -and $processorArchitecture -ne "arm64")
IsSonoma = $($osVersion.Version.Major -eq "14")
IsSonomaArm64 = $($osVersion.Version.Major -eq "14" -and $processorArchitecture -eq "arm64")
IsSonomaX64 = $($osVersion.Version.Major -eq "14" -and $processorArchitecture -ne "arm64")
2020-09-10 14:34:08 +03:00
}
}
2023-12-04 12:13:08 +01:00
function Get-ToolsetContent {
<#
.SYNOPSIS
Retrieves the content of the toolset.json file.
.DESCRIPTION
This function reads the toolset.json in path provided by IMAGE_FOLDER
environment variable and returns the content as a PowerShell object.
#>
$toolsetPath = Join-Path $env:IMAGE_FOLDER "toolset.json"
2023-12-04 12:13:08 +01:00
$toolsetJson = Get-Content -Path $toolsetPath -Raw
ConvertFrom-Json -InputObject $toolsetJson
}
function Invoke-DownloadWithRetry {
2020-10-27 09:45:22 +03:00
Param
(
[Parameter(Mandatory)]
[string] $Url,
[Alias("Destination")]
[string] $Path
2020-10-27 09:45:22 +03:00
)
if (-not $Path) {
$invalidChars = [IO.Path]::GetInvalidFileNameChars() -join ''
$re = "[{0}]" -f [RegEx]::Escape($invalidChars)
$fileName = [IO.Path]::GetFileName($Url) -replace $re
if ([String]::IsNullOrEmpty($fileName)) {
$fileName = [System.IO.Path]::GetRandomFileName()
}
$Path = Join-Path -Path "/tmp" -ChildPath $fileName
2020-10-27 09:45:22 +03:00
}
Write-Host "Downloading package from $Url to $Path..."
2020-10-27 09:45:22 +03:00
$interval = 30
$downloadStartTime = Get-Date
for ($retries = 20; $retries -gt 0; $retries--) {
try {
$attemptStartTime = Get-Date
(New-Object System.Net.WebClient).DownloadFile($Url, $Path)
$attemptSeconds = [math]::Round(($(Get-Date) - $attemptStartTime).TotalSeconds, 2)
Write-Host "Package downloaded in $attemptSeconds seconds"
2020-10-27 09:45:22 +03:00
break
} catch {
$attemptSeconds = [math]::Round(($(Get-Date) - $attemptStartTime).TotalSeconds, 2)
Write-Warning "Package download failed in $attemptSeconds seconds"
Write-Warning $_.Exception.Message
2020-10-27 09:45:22 +03:00
}
2023-12-04 12:13:08 +01:00
if ($retries -eq 0) {
$totalSeconds = [math]::Round(($(Get-Date) - $downloadStartTime).TotalSeconds, 2)
throw "Package download failed after $totalSeconds seconds"
2020-10-27 09:45:22 +03:00
}
Write-Warning "Waiting $interval seconds before retrying (retries left: $retries)..."
Start-Sleep -Seconds $interval
2020-10-27 09:45:22 +03:00
}
return $Path
2021-10-14 14:42:34 +03:00
}
function isVeertu {
return (Test-Path -Path "/Library/Application Support/Veertu")
}
2023-07-28 12:37:00 +02:00
function Get-Architecture {
$arch = arch
2023-12-04 12:13:08 +01:00
if ($arch -ne "arm64") {
2023-07-28 12:37:00 +02:00
$arch = "x64"
}
return $arch
}