mirror of
https://github.com/actions/runner-images.git
synced 2025-12-11 11:37:00 +00:00
[macOS] Refactor Common.Helpers (#8924)
* [macOS] Refactor Common.Helpers * Update readme file * Remove unnecessary double quotes --------- Co-authored-by: Alexey Ayupov <“alexey.ayupov@akvelon.com”>
This commit is contained in:
@@ -7,6 +7,7 @@ function Get-CommandResult {
|
||||
# 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
|
||||
|
||||
return @{
|
||||
Output = If ($Multiline -eq $true) { $stdout } else { [string]$stdout }
|
||||
ExitCode = $exitCode
|
||||
@@ -14,21 +15,16 @@ function Get-CommandResult {
|
||||
}
|
||||
|
||||
# Gets path to the tool, analogue of 'which tool'
|
||||
function Get-WhichTool($tool) {
|
||||
function Get-ToolPath($tool) {
|
||||
return (Get-Command $tool).Path
|
||||
}
|
||||
|
||||
# Gets value of environment variable by the name
|
||||
function Get-EnvironmentVariable($variable) {
|
||||
return [System.Environment]::GetEnvironmentVariable($variable)
|
||||
}
|
||||
|
||||
# Returns the object with information about current OS
|
||||
# It can be used for OS-specific tests
|
||||
function Get-OSVersion {
|
||||
$osVersion = [Environment]::OSVersion
|
||||
$osVersionMajorMinor = $osVersion.Version.ToString(2)
|
||||
$processorArchitecture = arch
|
||||
|
||||
return [PSCustomObject]@{
|
||||
Version = $osVersion.Version
|
||||
Platform = $osVersion.Platform
|
||||
@@ -44,85 +40,11 @@ function Get-OSVersion {
|
||||
}
|
||||
}
|
||||
|
||||
function Get-ChildItemWithoutSymlinks {
|
||||
param (
|
||||
[Parameter(Mandatory)]
|
||||
[string] $Path,
|
||||
[string] $Filter
|
||||
)
|
||||
|
||||
$files = Get-ChildItem -Path $Path -Filter $Filter
|
||||
$files = $files | Where-Object { !$_.LinkType } # cut symlinks
|
||||
return $files
|
||||
}
|
||||
|
||||
# Get the value of specific toolset node
|
||||
# Example, invoke `Get-ToolsetValue "xamarin.bundles"` to get value of `$toolsetJson.xamarin.bundles`
|
||||
function Get-ToolsetValue {
|
||||
param (
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string] $KeyPath
|
||||
)
|
||||
# Get the value of the toolset
|
||||
function Get-ToolsetContent {
|
||||
$toolsetPath = Join-Path $env:HOME "image-generation" "toolset.json"
|
||||
$jsonNode = Get-Content -Raw $toolsetPath | ConvertFrom-Json
|
||||
|
||||
$pathParts = $KeyPath.Split(".")
|
||||
# try to walk through all arguments consequentially to resolve specific json node
|
||||
$pathParts | ForEach-Object {
|
||||
$jsonNode = $jsonNode.$_
|
||||
}
|
||||
return $jsonNode
|
||||
}
|
||||
|
||||
function Get-ToolcachePackages {
|
||||
$toolcachePath = Join-Path $env:HOME "image-generation" "toolcache.json"
|
||||
return Get-Content -Raw $toolcachePath | ConvertFrom-Json
|
||||
}
|
||||
|
||||
function Invoke-RestMethodWithRetry {
|
||||
param (
|
||||
[Parameter()]
|
||||
[string]
|
||||
$Url
|
||||
)
|
||||
Invoke-RestMethod $Url -MaximumRetryCount 10 -RetryIntervalSec 30
|
||||
}
|
||||
|
||||
function Invoke-ValidateCommand {
|
||||
param(
|
||||
[Parameter(Mandatory)]
|
||||
[string]$Command,
|
||||
[Uint] $Timeout = 0
|
||||
)
|
||||
|
||||
if ($Timeout -eq 0)
|
||||
{
|
||||
$output = Invoke-Expression -Command $Command
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
throw "Command '$Command' has finished with exit code $LASTEXITCODE"
|
||||
}
|
||||
return $output
|
||||
}
|
||||
else
|
||||
{
|
||||
$job = $command | Start-Job -ScriptBlock {
|
||||
$output = Invoke-Expression -Command $input
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
throw 'Command failed'
|
||||
}
|
||||
return $output
|
||||
}
|
||||
$waitObject = $job | Wait-Job -Timeout $Timeout
|
||||
if(-not $waitObject)
|
||||
{
|
||||
throw "Command '$Command' has timed out"
|
||||
}
|
||||
if($waitObject.State -eq 'Failed')
|
||||
{
|
||||
throw "Command '$Command' has failed"
|
||||
}
|
||||
Receive-Job -Job $job
|
||||
}
|
||||
$toolsetJson = Get-Content -Path $toolsetPath -Raw
|
||||
ConvertFrom-Json -InputObject $toolsetJson
|
||||
}
|
||||
|
||||
function Invoke-DownloadWithRetry {
|
||||
@@ -161,7 +83,7 @@ function Invoke-DownloadWithRetry {
|
||||
Write-Warning "Package download failed in $attemptSeconds seconds"
|
||||
Write-Warning $_.Exception.Message
|
||||
}
|
||||
|
||||
|
||||
if ($retries -eq 0) {
|
||||
$totalSeconds = [math]::Round(($(Get-Date) - $downloadStartTime).TotalSeconds, 2)
|
||||
throw "Package download failed after $totalSeconds seconds"
|
||||
@@ -174,37 +96,15 @@ function Invoke-DownloadWithRetry {
|
||||
return $Path
|
||||
}
|
||||
|
||||
function Add-EnvironmentVariable {
|
||||
param
|
||||
(
|
||||
[Parameter(Mandatory)] [string] $Name,
|
||||
[Parameter(Mandatory)] [string] $Value,
|
||||
[string] $FilePath = "${env:HOME}/.bashrc"
|
||||
)
|
||||
|
||||
$envVar = "export {0}={1}" -f $Name, $Value
|
||||
Add-Content -Path $FilePath -Value $envVar
|
||||
}
|
||||
|
||||
function isVeertu {
|
||||
return (Test-Path -Path "/Library/Application Support/Veertu")
|
||||
}
|
||||
|
||||
function Get-Architecture {
|
||||
$arch = arch
|
||||
if ($arch -ne "arm64")
|
||||
{
|
||||
if ($arch -ne "arm64") {
|
||||
$arch = "x64"
|
||||
}
|
||||
|
||||
return $arch
|
||||
}
|
||||
|
||||
function Test-CommandExists {
|
||||
param
|
||||
(
|
||||
[Parameter(Mandatory)] [string] $Command
|
||||
)
|
||||
|
||||
[boolean] (Get-Command $Command -ErrorAction 'SilentlyContinue')
|
||||
}
|
||||
|
||||
@@ -28,14 +28,15 @@ function Get-XcodeToolPath {
|
||||
}
|
||||
|
||||
function Get-XcodeVersionInfo {
|
||||
param(
|
||||
param (
|
||||
[Parameter(Mandatory)]
|
||||
[string]$XcodeRootPath
|
||||
[string] $XcodeRootPath
|
||||
)
|
||||
|
||||
$xcodebuildPath = Get-XcodeToolPath -XcodeRootPath $XcodeRootPath -ToolName "xcodebuild"
|
||||
[string]$output = Invoke-Expression "$xcodebuildPath -version"
|
||||
$versionOutputParts = $output.Split(" ")
|
||||
|
||||
return @{
|
||||
Version = [System.Version]::Parse($versionOutputParts[1])
|
||||
Build = $versionOutputParts[4]
|
||||
@@ -72,11 +73,12 @@ function Test-XcodeStableRelease {
|
||||
|
||||
$licenseInfoPlistPath = Join-Path $XcodeRootPath "Contents" "Resources" "LicenseInfo.plist"
|
||||
$releaseType = & defaults read $licenseInfoPlistPath "licenseType"
|
||||
|
||||
return -not ($releaseType -match "beta")
|
||||
}
|
||||
|
||||
function Get-XcodeSimulatorsInfo {
|
||||
param(
|
||||
param (
|
||||
[string] $Filter
|
||||
)
|
||||
|
||||
@@ -106,6 +108,7 @@ function Get-XcodeDevicesList {
|
||||
$result += "$runtimeName $deviceName"
|
||||
}
|
||||
}
|
||||
|
||||
return $result
|
||||
}
|
||||
|
||||
@@ -120,12 +123,13 @@ function Get-XcodePairsList {
|
||||
$phoneName = $_.Value.phone.name
|
||||
$result += "$watchName $phoneName"
|
||||
}
|
||||
|
||||
return $result
|
||||
}
|
||||
|
||||
#Helper function for execution of xcversion due to: https://github.com/fastlane/fastlane/issues/18161
|
||||
function Invoke-XCVersion {
|
||||
param(
|
||||
param (
|
||||
[Parameter(Mandatory)]
|
||||
[string] $Arguments,
|
||||
[Parameter()]
|
||||
@@ -133,7 +137,7 @@ function Invoke-XCVersion {
|
||||
[Parameter()]
|
||||
[int] $PauseDurationSecs = 1
|
||||
)
|
||||
|
||||
|
||||
$Command = "xcversion $Arguments"
|
||||
Write-Host "Execute [$Command]"
|
||||
for ($Attempt=1; $Attempt -le $RetryAttempts; $Attempt++) {
|
||||
@@ -150,7 +154,7 @@ function Invoke-XCVersion {
|
||||
}
|
||||
if ($result.ExitCode -ne 0) {
|
||||
throw "Command [$Command] has finished with non-zero exit code."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function Get-BrokenXcodeSimulatorsList {
|
||||
@@ -305,4 +309,4 @@ function Get-BrokenXcodeSimulatorsList {
|
||||
XcodeVersion = "14.2"
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,44 +2,41 @@ Import-Module "$PSScriptRoot/Common.Helpers.psm1"
|
||||
Import-Module "$PSScriptRoot/Xcode.Helpers.psm1"
|
||||
|
||||
function Install-XcodeVersion {
|
||||
param(
|
||||
param (
|
||||
[Parameter(Mandatory)]
|
||||
[string]$Version,
|
||||
[string] $Version,
|
||||
[Parameter(Mandatory)]
|
||||
[string]$LinkTo
|
||||
[string] $LinkTo
|
||||
)
|
||||
|
||||
$xcodeDownloadDirectory = "$env:HOME/Library/Caches/XcodeInstall"
|
||||
$xcodeTargetPath = Get-XcodeRootPath -Version $LinkTo
|
||||
$xcodeXipDirectory = Invoke-DownloadXcodeArchive -DownloadDirectory $xcodeDownloadDirectory -Version $Version
|
||||
Expand-XcodeXipArchive -DownloadDirectory $xcodeXipDirectory.FullName -TargetPath $xcodeTargetPath
|
||||
|
||||
Remove-Item -Path $xcodeXipDirectory -Force -Recurse
|
||||
}
|
||||
|
||||
function Invoke-DownloadXcodeArchive {
|
||||
param(
|
||||
param (
|
||||
[Parameter(Mandatory)]
|
||||
[string]$DownloadDirectory,
|
||||
[string] $DownloadDirectory,
|
||||
[Parameter(Mandatory)]
|
||||
[string]$Version
|
||||
[string] $Version
|
||||
)
|
||||
|
||||
Write-Host "Downloading Xcode $Version"
|
||||
|
||||
$tempXipDirectory = New-Item -Path $DownloadDirectory -Name "Xcode$Version" -ItemType "Directory"
|
||||
|
||||
$xcodeFileName = 'Xcode-{0}.xip' -f $Version
|
||||
$xcodeUri = '{0}{1}?{2}'-f ${env:XCODE_INSTALL_STORAGE_URL}, $xcodeFileName, ${env:XCODE_INSTALL_SAS}
|
||||
|
||||
Invoke-DownloadWithRetry -Url $xcodeUri -Path (Join-Path $tempXipDirectory.FullName $xcodeFileName) | Out-Null
|
||||
|
||||
return $tempXipDirectory
|
||||
}
|
||||
|
||||
function Resolve-ExactXcodeVersion {
|
||||
param(
|
||||
param (
|
||||
[Parameter(Mandatory)]
|
||||
[string]$Version
|
||||
[string] $Version
|
||||
)
|
||||
|
||||
# if toolset string contains spaces, consider it as a full name of Xcode
|
||||
@@ -50,6 +47,7 @@ function Resolve-ExactXcodeVersion {
|
||||
$semverVersion = [SemVer]::Parse($Version)
|
||||
$availableVersions = Get-AvailableXcodeVersions
|
||||
$satisfiedVersions = $availableVersions | Where-Object { $semverVersion -eq $_.stableSemver }
|
||||
|
||||
return $satisfiedVersions | Select-Object -Last 1 -ExpandProperty rawVersion
|
||||
}
|
||||
|
||||
@@ -75,18 +73,18 @@ function Get-AvailableXcodeVersions {
|
||||
}
|
||||
|
||||
function Expand-XcodeXipArchive {
|
||||
param(
|
||||
param (
|
||||
[Parameter(Mandatory)]
|
||||
[string]$DownloadDirectory,
|
||||
[string] $DownloadDirectory,
|
||||
[Parameter(Mandatory)]
|
||||
[string]$TargetPath
|
||||
[string] $TargetPath
|
||||
)
|
||||
|
||||
$xcodeXipPath = Get-ChildItem -Path $DownloadDirectory -Filter "Xcode-*.xip" | Select-Object -First 1
|
||||
|
||||
Write-Host "Extracting Xcode from '$xcodeXipPath'"
|
||||
Push-Location $DownloadDirectory
|
||||
if(Test-CommandExists 'unxip') {
|
||||
if ([boolean] (Get-Command 'unxip' -ErrorAction 'SilentlyContinue')) {
|
||||
Invoke-ValidateCommand "unxip $xcodeXipPath"
|
||||
} else {
|
||||
Invoke-ValidateCommand "xip -x $xcodeXipPath"
|
||||
@@ -107,9 +105,9 @@ function Expand-XcodeXipArchive {
|
||||
}
|
||||
|
||||
function Confirm-XcodeIntegrity {
|
||||
param(
|
||||
param (
|
||||
[Parameter(Mandatory)]
|
||||
[string]$Version
|
||||
[string] $Version
|
||||
)
|
||||
|
||||
$XcodeRootPath = Get-XcodeRootPath -Version $Version
|
||||
@@ -120,13 +118,12 @@ function Confirm-XcodeIntegrity {
|
||||
}
|
||||
|
||||
function Approve-XcodeLicense {
|
||||
param(
|
||||
param (
|
||||
[Parameter(Mandatory)]
|
||||
[string]$Version
|
||||
[string] $Version
|
||||
)
|
||||
|
||||
$os = Get-OSVersion
|
||||
|
||||
$XcodeRootPath = Get-XcodeRootPath -Version $Version
|
||||
Write-Host "Approving Xcode license for '$XcodeRootPath'..."
|
||||
$xcodeBuildPath = Get-XcodeToolPath -XcodeRootPath $XcodeRootPath -ToolName "xcodebuild"
|
||||
@@ -139,9 +136,9 @@ function Approve-XcodeLicense {
|
||||
}
|
||||
|
||||
function Install-XcodeAdditionalPackages {
|
||||
param(
|
||||
param (
|
||||
[Parameter(Mandatory)]
|
||||
[string]$Version
|
||||
[string] $Version
|
||||
)
|
||||
|
||||
Write-Host "Installing additional packages for Xcode $Version..."
|
||||
@@ -153,9 +150,9 @@ function Install-XcodeAdditionalPackages {
|
||||
}
|
||||
|
||||
function Invoke-XcodeRunFirstLaunch {
|
||||
param(
|
||||
param (
|
||||
[Parameter(Mandatory)]
|
||||
[string]$Version
|
||||
[string] $Version
|
||||
)
|
||||
|
||||
if ($Version.StartsWith("8") -or $Version.StartsWith("9")) {
|
||||
@@ -168,9 +165,9 @@ function Invoke-XcodeRunFirstLaunch {
|
||||
}
|
||||
|
||||
function Install-AdditionalSimulatorRuntimes {
|
||||
param(
|
||||
param (
|
||||
[Parameter(Mandatory)]
|
||||
[string]$Version
|
||||
[string] $Version
|
||||
)
|
||||
|
||||
Write-Host "Installing Simulator Runtimes for Xcode $Version ..."
|
||||
@@ -179,10 +176,10 @@ function Install-AdditionalSimulatorRuntimes {
|
||||
}
|
||||
|
||||
function Build-XcodeSymlinks {
|
||||
param(
|
||||
param (
|
||||
[Parameter(Mandatory)]
|
||||
[string]$Version,
|
||||
[string[]]$Symlinks
|
||||
[string] $Version,
|
||||
[string[]] $Symlinks
|
||||
)
|
||||
|
||||
$sourcePath = Get-XcodeRootPath -Version $Version
|
||||
@@ -193,10 +190,10 @@ function Build-XcodeSymlinks {
|
||||
}
|
||||
}
|
||||
|
||||
function Rebuild-XcodeLaunchServicesDb {
|
||||
param(
|
||||
function Initialize-XcodeLaunchServicesDb {
|
||||
param (
|
||||
[Parameter(Mandatory)]
|
||||
[string]$Version
|
||||
[string] $Version
|
||||
)
|
||||
|
||||
$xcodePath = Get-XcodeRootPath -Version $Version
|
||||
@@ -205,9 +202,9 @@ function Rebuild-XcodeLaunchServicesDb {
|
||||
}
|
||||
|
||||
function Build-ProvisionatorSymlink {
|
||||
param(
|
||||
param (
|
||||
[Parameter(Mandatory)]
|
||||
[string]$Version
|
||||
[string] $Version
|
||||
)
|
||||
|
||||
$sourcePath = Get-XcodeRootPath -Version $Version
|
||||
@@ -222,9 +219,9 @@ function Build-ProvisionatorSymlink {
|
||||
}
|
||||
|
||||
function Set-XcodeDeveloperDirEnvironmentVariables {
|
||||
param(
|
||||
param (
|
||||
[Parameter(Mandatory)]
|
||||
[string[]]$XcodeList
|
||||
[string[]] $XcodeList
|
||||
)
|
||||
|
||||
$exactVersionsList = $XcodeList | Where-Object { Test-XcodeStableRelease -Version $_ } | ForEach-Object {
|
||||
@@ -246,3 +243,37 @@ function Set-XcodeDeveloperDirEnvironmentVariables {
|
||||
"export ${variableName}=${variableValue}" | Out-File "$env:HOME/.bashrc" -Append
|
||||
}
|
||||
}
|
||||
|
||||
function Invoke-ValidateCommand {
|
||||
param (
|
||||
[Parameter(Mandatory)]
|
||||
[string] $Command,
|
||||
[Uint] $Timeout = 0
|
||||
)
|
||||
|
||||
if ($Timeout -eq 0) {
|
||||
$output = Invoke-Expression -Command $Command
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
throw "Command '$Command' has finished with exit code $LASTEXITCODE"
|
||||
}
|
||||
return $output
|
||||
} else {
|
||||
$job = $command | Start-Job -ScriptBlock {
|
||||
$output = Invoke-Expression -Command $input
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
throw 'Command failed'
|
||||
}
|
||||
return $output
|
||||
}
|
||||
$waitObject = $job | Wait-Job -Timeout $Timeout
|
||||
|
||||
if (-not $waitObject) {
|
||||
throw "Command '$Command' has timed out"
|
||||
}
|
||||
|
||||
if ($waitObject.State -eq 'Failed') {
|
||||
throw "Command '$Command' has failed"
|
||||
}
|
||||
Receive-Job -Job $job
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user