mirror of
https://github.com/actions/runner-images.git
synced 2026-01-06 18:19:54 +08:00
move source code to public
This commit is contained in:
76
images/macos/helpers/Common.Helpers.psm1
Normal file
76
images/macos/helpers/Common.Helpers.psm1
Normal file
@@ -0,0 +1,76 @@
|
||||
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
|
||||
return @{
|
||||
Output = If ($Multiline -eq $true) { $stdout } else { [string]$stdout }
|
||||
ExitCode = $exitCode
|
||||
}
|
||||
}
|
||||
|
||||
# Gets path to the tool, analogue of 'which tool'
|
||||
function Get-WhichTool($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
|
||||
return [PSCustomObject]@{
|
||||
Version = $osVersion.Version
|
||||
Platform = $osVersion.Platform
|
||||
IsHighSierra = $osVersion.Version.Major -eq 17
|
||||
IsMojave = $osVersion.Version.Major -eq 18
|
||||
IsCatalina = $osVersion.Version.Major -eq 19
|
||||
IsBigSur = $osVersion.Version.Major -eq 20
|
||||
IsLessThanCatalina = $osVersion.Version.Major -lt 19
|
||||
IsLessThanBigSur = $osVersion.Version.Major -lt 20
|
||||
IsHigherThanMojave = $osVersion.Version.Major -gt 18
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
)
|
||||
$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
|
||||
}
|
||||
32
images/macos/helpers/SoftwareReport.Helpers.psm1
Normal file
32
images/macos/helpers/SoftwareReport.Helpers.psm1
Normal file
@@ -0,0 +1,32 @@
|
||||
function Run-Command {
|
||||
param (
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string] $Command,
|
||||
[switch] $SuppressStderr
|
||||
)
|
||||
# Bash trick to suppress and show error output because some commands write to stderr (for example, "python --version")
|
||||
$redirectOutputArguments = If ($SuppressStderr) { "2> /dev/null" } Else { "2>&1" }
|
||||
$stdout = & bash -c "${Command} ${redirectOutputArguments}"
|
||||
|
||||
return $stdout
|
||||
}
|
||||
|
||||
function Take-Part {
|
||||
param (
|
||||
[Parameter(ValueFromPipeline)]
|
||||
[string] $toolOutput,
|
||||
[string] $Delimiter = " ",
|
||||
[int[]] $Part
|
||||
)
|
||||
$parts = $toolOutput.Split($Delimiter, [System.StringSplitOptions]::RemoveEmptyEntries)
|
||||
$selectedParts = $parts[$Part]
|
||||
return [string]::Join($Delimiter, $selectedParts)
|
||||
}
|
||||
|
||||
function New-MDNewLine {
|
||||
param (
|
||||
[int] $Count = 1
|
||||
)
|
||||
$newLineSymbol = [System.Environment]::NewLine
|
||||
return $newLineSymbol * $Count
|
||||
}
|
||||
87
images/macos/helpers/Tests.Helpers.psm1
Normal file
87
images/macos/helpers/Tests.Helpers.psm1
Normal file
@@ -0,0 +1,87 @@
|
||||
# Invokes command and validate that the exit code is 0
|
||||
function Validate-ZeroExitCode($command) {
|
||||
$result = Get-CommandResult $command
|
||||
$result.ExitCode | Should -Be 0 -Because $result.Output
|
||||
}
|
||||
|
||||
# Validates that tool is installed and in PATH
|
||||
function Validate-ToolExist($tool) {
|
||||
Get-Command $tool -ErrorAction SilentlyContinue | Should -BeTrue
|
||||
}
|
||||
|
||||
function Validate-ArrayWithoutDuplicates {
|
||||
param (
|
||||
[AllowEmptyCollection()]
|
||||
[Parameter(Mandatory = $true)]
|
||||
[array] $Items,
|
||||
[string] $Because
|
||||
)
|
||||
$uniqueList = @()
|
||||
$Items | ForEach-Object {
|
||||
$uniqueList | Should -Not -Contain $_ -Because $Because
|
||||
$uniqueList += $_
|
||||
}
|
||||
}
|
||||
|
||||
function Validate-Url {
|
||||
param (
|
||||
[Parameter(Mandatory)]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[string] $Url
|
||||
)
|
||||
|
||||
$result = $true
|
||||
try {
|
||||
$requestResult = Invoke-WebRequest $Url -DisableKeepAlive -UseBasicParsing -Method Head
|
||||
$result = ($requestResult.StatusCode -eq 200)
|
||||
} catch {
|
||||
$result = $false
|
||||
}
|
||||
|
||||
$result | Should -BeTrue -Because "'$Url' should be available, but it is not"
|
||||
}
|
||||
|
||||
function Validate-IdenticalFileContent {
|
||||
param (
|
||||
[Parameter(Mandatory)]
|
||||
[string] $File1,
|
||||
[Parameter(Mandatory)]
|
||||
[string] $File2
|
||||
)
|
||||
|
||||
$File1 | Should -Exist -Because "The content of '$File1' should be identical with content of '$File2' but '$File1' doesn't exist"
|
||||
$File2 | Should -Exist -Because "The content of '$File1' should be identical with content of '$File2' but '$File2' doesn't exist"
|
||||
|
||||
$content1 = Get-Content -Raw $File1
|
||||
$content2 = Get-Content -Raw $File2
|
||||
$content1 | Should -Be $content2 -Because "The content of '$File1' should be identical with content of '$File2' but they are different"
|
||||
}
|
||||
|
||||
function ShouldReturnZeroExitCode {
|
||||
Param(
|
||||
[String] $ActualValue,
|
||||
[switch] $Negate,
|
||||
[string] $Because # This parameter is unused by we need it to match Pester asserts signature
|
||||
)
|
||||
|
||||
$result = Get-CommandResult $ActualValue
|
||||
|
||||
[bool]$succeeded = $result.ExitCode -eq 0
|
||||
if ($Negate) { $succeeded = -not $succeeded }
|
||||
|
||||
if (-not $succeeded)
|
||||
{
|
||||
$commandOutputIndent = " " * 4
|
||||
$commandOutput = ($result.Output | ForEach-Object { "${commandOutputIndent}${_}" }) -join "`n"
|
||||
$failureMessage = "Command '${ActualValue}' has finished with exit code ${actualExitCode}`n${commandOutput}"
|
||||
}
|
||||
|
||||
return [PSCustomObject] @{
|
||||
Succeeded = $succeeded
|
||||
FailureMessage = $failureMessage
|
||||
}
|
||||
}
|
||||
|
||||
If (Get-Command -Name Add-AssertionOperator -ErrorAction SilentlyContinue) {
|
||||
Add-AssertionOperator -Name ReturnZeroExitCode -InternalName ShouldReturnZeroExitCode -Test ${function:ShouldReturnZeroExitCode}
|
||||
}
|
||||
94
images/macos/helpers/Xcode.Helpers.psm1
Normal file
94
images/macos/helpers/Xcode.Helpers.psm1
Normal file
@@ -0,0 +1,94 @@
|
||||
function Get-XcodeRootPath {
|
||||
Param (
|
||||
[Parameter(Mandatory)]
|
||||
[string] $Version
|
||||
)
|
||||
|
||||
return "/Applications/Xcode_$Version.app"
|
||||
}
|
||||
|
||||
function Get-DefaultXcodeRootPath {
|
||||
$defaultXcodePath = "/Applications/Xcode.app"
|
||||
$defaultXcodeItem = Get-Item -Path $defaultXcodePath
|
||||
return $defaultXcodeItem.Target
|
||||
}
|
||||
|
||||
function Get-XcodeToolPath {
|
||||
param (
|
||||
[Parameter(ParameterSetName = 'Version')]
|
||||
[string] $Version,
|
||||
[Parameter(ParameterSetName = 'Path')]
|
||||
[string] $XcodeRootPath,
|
||||
[string] $ToolName
|
||||
)
|
||||
|
||||
if ($PSCmdlet.ParameterSetName -eq "Version") {
|
||||
$XcodeRootPath = Get-XcodeRootPath $Version
|
||||
}
|
||||
|
||||
return Join-Path $XcodeRootPath "Contents/Developer/usr/bin" $ToolName
|
||||
}
|
||||
|
||||
function Switch-Xcode {
|
||||
param (
|
||||
[Parameter(ParameterSetName = 'Version')]
|
||||
[string] $Version,
|
||||
[Parameter(ParameterSetName = 'Path')]
|
||||
[string] $XcodeRootPath
|
||||
)
|
||||
|
||||
if ($PSCmdlet.ParameterSetName -eq "Version") {
|
||||
$XcodeRootPath = Get-XcodeRootPath $Version
|
||||
}
|
||||
|
||||
Write-Verbose "Switching Xcode to '${XcodeRootPath}'"
|
||||
Invoke-Expression "sudo xcode-select --switch ${XcodeRootPath}"
|
||||
}
|
||||
|
||||
function Get-XcodeSimulatorsInfo {
|
||||
param(
|
||||
[string] $Filter
|
||||
)
|
||||
|
||||
[string]$rawSimulatorsInfo = Invoke-Expression "xcrun simctl list --json"
|
||||
$jsonSimulatorsInfo = $rawSimulatorsInfo | ConvertFrom-Json
|
||||
|
||||
if ($Filter) {
|
||||
return $jsonSimulatorsInfo | Select-Object -ExpandProperty $Filter
|
||||
}
|
||||
|
||||
return $jsonSimulatorsInfo
|
||||
}
|
||||
|
||||
function Get-XcodeDevicesList {
|
||||
$result = @()
|
||||
|
||||
$runtimes = Get-XcodeSimulatorsInfo -Filter "devices"
|
||||
$runtimes.PSObject.Properties | ForEach-Object {
|
||||
$runtimeName = $_.Name
|
||||
$devices = $_.Value
|
||||
$devices | Where-Object {
|
||||
$availability = $_.availability
|
||||
$isAvailable = $_.isAvailable
|
||||
return (($availability -eq "(available)") -or ($isAvailable -eq "YES") -or ($isAvailable -eq $true))
|
||||
} | ForEach-Object {
|
||||
$deviceName = $_.name
|
||||
$result += "$runtimeName $deviceName"
|
||||
}
|
||||
}
|
||||
return $result
|
||||
}
|
||||
|
||||
function Get-XcodePairsList {
|
||||
$result = @()
|
||||
|
||||
$runtimes = Get-XcodeSimulatorsInfo -Filter "pairs"
|
||||
$runtimes.PSObject.Properties | Where-Object {
|
||||
return $_.Value.state -match "active"
|
||||
} | ForEach-Object {
|
||||
$watchName = $_.Value.watch.name
|
||||
$phoneName = $_.Value.phone.name
|
||||
$result += "$watchName $phoneName"
|
||||
}
|
||||
return $result
|
||||
}
|
||||
Reference in New Issue
Block a user