mirror of
https://github.com/actions/runner-images-sangeeth.git
synced 2025-12-16 06:46:48 +00:00
[ubuntu] Refactor Common.Helpers (#8910)
* [ubuntu] Refactor Common.Helpers * Move Get-AndroidPackages function from BeforeAll * Fix ParameterBinding
This commit is contained in:
committed by
GitHub
parent
92e22bd8c6
commit
0c03739e50
@@ -135,7 +135,7 @@ function Get-HomebrewVersion {
|
||||
}
|
||||
|
||||
function Get-CpanVersion {
|
||||
$result = Get-CommandResult "cpan --version" -ExpectExitCode @(25, 255)
|
||||
$result = Get-CommandResult "cpan --version" -ExpectedExitCode @(25, 255)
|
||||
$result.Output -match "version (?<version>\d+\.\d+) " | Out-Null
|
||||
return $Matches.version
|
||||
}
|
||||
|
||||
@@ -39,3 +39,16 @@ function Get-AptSourceRepository {
|
||||
$sourceUrl = Get-Content "$PSScriptRoot/../helpers/apt-sources.txt" | Select-String -Pattern $PackageName | Take-OutputPart -Part (1..3)
|
||||
return $sourceUrl
|
||||
}
|
||||
|
||||
function Get-OSVersionShort {
|
||||
$(Get-OSVersionFull) | Take-OutputPart -Delimiter '.' -Part 0,1
|
||||
}
|
||||
|
||||
function Get-OSVersionFull {
|
||||
lsb_release -ds | Take-OutputPart -Part 1, 2
|
||||
}
|
||||
|
||||
function Get-KernelVersion {
|
||||
$kernelVersion = uname -r
|
||||
return $kernelVersion
|
||||
}
|
||||
|
||||
@@ -1,8 +1,33 @@
|
||||
function Get-CommandResult {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Runs a command in bash and returns the output and exit code.
|
||||
|
||||
.DESCRIPTION
|
||||
Function runs a provided command in bash and returns the output and exit code as hashtable.
|
||||
|
||||
.PARAMETER Command
|
||||
The command to run.
|
||||
|
||||
.PARAMETER ExpectedExitCode
|
||||
The expected exit code. If the actual exit code does not match, an exception is thrown.
|
||||
|
||||
.PARAMETER Multiline
|
||||
If true, the output is returned as an array of strings. Otherwise, the output is returned as a single string.
|
||||
|
||||
.PARAMETER ValidateExitCode
|
||||
If true, the actual exit code is compared to the expected exit code.
|
||||
|
||||
.EXAMPLE
|
||||
$result = Get-CommandResult "ls -la"
|
||||
|
||||
This command runs "ls -la" in bash and returns the output and exit code as hashtable.
|
||||
|
||||
#>
|
||||
param (
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string] $Command,
|
||||
[int[]] $ExpectExitCode = 0,
|
||||
[int[]] $ExpectedExitCode = 0,
|
||||
[switch] $Multiline,
|
||||
[bool] $ValidateExitCode = $true
|
||||
)
|
||||
@@ -12,7 +37,7 @@ function Get-CommandResult {
|
||||
$exitCode = $LASTEXITCODE
|
||||
|
||||
if ($ValidateExitCode) {
|
||||
if ($ExpectExitCode -notcontains $exitCode) {
|
||||
if ($ExpectedExitCode -notcontains $exitCode) {
|
||||
try {
|
||||
throw "StdOut: '$stdout' ExitCode: '$exitCode'"
|
||||
} catch {
|
||||
@@ -24,24 +49,11 @@ function Get-CommandResult {
|
||||
}
|
||||
|
||||
return @{
|
||||
Output = If ($Multiline -eq $true) { $stdout } else { [string]$stdout }
|
||||
Output = If ($Multiline -eq $true) { $stdout } else { [string] $stdout }
|
||||
ExitCode = $exitCode
|
||||
}
|
||||
}
|
||||
|
||||
function Get-OSVersionShort {
|
||||
$(Get-OSVersionFull) | Take-OutputPart -Delimiter '.' -Part 0,1
|
||||
}
|
||||
|
||||
function Get-OSVersionFull {
|
||||
lsb_release -ds | Take-OutputPart -Part 1, 2
|
||||
}
|
||||
|
||||
function Get-KernelVersion {
|
||||
$kernelVersion = uname -r
|
||||
return $kernelVersion
|
||||
}
|
||||
|
||||
function Test-IsUbuntu20 {
|
||||
return (lsb_release -rs) -eq "20.04"
|
||||
}
|
||||
@@ -56,6 +68,23 @@ function Get-ToolsetContent {
|
||||
}
|
||||
|
||||
function Get-ToolsetValue {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
This function retrieves the value of a specific toolset.
|
||||
|
||||
.DESCRIPTION
|
||||
The Get-ToolsetValue is used to retrieve the value of a specific toolset.
|
||||
The toolset is a collection of tools or settings in json format.
|
||||
|
||||
.PARAMETER KeyPath
|
||||
The path to the toolset value in json notation.
|
||||
|
||||
.EXAMPLE
|
||||
Get-ToolsetValue -Toolset "tool.arch.versions"
|
||||
|
||||
This command returns the value of the nodes named "tool", "arch" and "versions" as PSCustomObject.
|
||||
|
||||
#>
|
||||
param (
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string] $KeyPath
|
||||
@@ -64,30 +93,9 @@ function Get-ToolsetValue {
|
||||
$jsonNode = Get-ToolsetContent
|
||||
|
||||
$pathParts = $KeyPath.Split(".")
|
||||
# try to walk through all arguments consequentially to resolve specific json node
|
||||
# walk through all arguments consequentially to resolve specific json node
|
||||
$pathParts | ForEach-Object {
|
||||
$jsonNode = $jsonNode.$_
|
||||
}
|
||||
return $jsonNode
|
||||
}
|
||||
|
||||
function Get-AndroidPackages {
|
||||
$packagesListFile = "/usr/local/lib/android/sdk/packages-list.txt"
|
||||
|
||||
if (-Not (Test-Path -Path $packagesListFile -PathType Leaf)) {
|
||||
(/usr/local/lib/android/sdk/cmdline-tools/latest/bin/sdkmanager --list --verbose 2>&1) |
|
||||
Where-Object { $_ -Match "^[^\s]" } |
|
||||
Where-Object { $_ -NotMatch "^(Loading |Info: Parsing |---|\[=+|Installed |Available )" } |
|
||||
Where-Object { $_ -NotMatch "^[^;]*$" } |
|
||||
Out-File -FilePath $packagesListFile
|
||||
|
||||
Write-Host Android packages list:
|
||||
Get-Content $packagesListFile
|
||||
}
|
||||
|
||||
return Get-Content $packagesListFile
|
||||
}
|
||||
|
||||
function Get-EnvironmentVariable($variable) {
|
||||
return [System.Environment]::GetEnvironmentVariable($variable)
|
||||
}
|
||||
@@ -1,15 +1,101 @@
|
||||
Describe "Android" {
|
||||
BeforeAll {
|
||||
function Test-AndroidPackage {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
This function tests existance of an Android package.
|
||||
|
||||
.DESCRIPTION
|
||||
The Test-AndroidPackage function is used to test an existance of Android package in ANDROID_HOME path.
|
||||
|
||||
.PARAMETER PackageName
|
||||
The name of the Android package to test.
|
||||
|
||||
.EXAMPLE
|
||||
Test-AndroidPackage
|
||||
|
||||
This command tests the Android package.
|
||||
|
||||
#>
|
||||
param (
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string] $PackageName
|
||||
)
|
||||
|
||||
# Convert 'cmake;3.6.4111459' -> 'cmake/3.6.4111459'
|
||||
$PackageName = $PackageName.Replace(";", "/")
|
||||
$targetPath = Join-Path $env:ANDROID_HOME $PackageName
|
||||
$targetPath | Should -Exist
|
||||
}
|
||||
}
|
||||
|
||||
function Get-AndroidPackages {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
This function returns a list of available Android packages.
|
||||
|
||||
.DESCRIPTION
|
||||
The Get-AndroidPackages function checks if a list of packages is already available in a file.
|
||||
If not, it uses the sdkmanager to generate a list of available packages and saves it to a file.
|
||||
It then returns the content of this file.
|
||||
|
||||
.PARAMETER SDKRootPath
|
||||
The root path of the Android SDK installation.
|
||||
If not specified, the function uses the ANDROID_HOME environment variable.
|
||||
|
||||
.EXAMPLE
|
||||
Get-AndroidPackages -SDKRootPath "/usr/local/lib/android/sdk"
|
||||
|
||||
This command returns a list of available Android packages for the specified SDK root path.
|
||||
|
||||
.NOTES
|
||||
This function requires the Android SDK to be installed.
|
||||
#>
|
||||
param (
|
||||
[Parameter(Mandatory=$false)]
|
||||
[string] $SDKRootPath
|
||||
)
|
||||
|
||||
if (-not $SDKRootPath) {
|
||||
$SDKRootPath = $env:ANDROID_HOME
|
||||
}
|
||||
|
||||
$packagesListFile = "$SDKRootPath/packages-list.txt"
|
||||
|
||||
if (-not (Test-Path -Path $packagesListFile -PathType Leaf)) {
|
||||
(/usr/local/lib/android/sdk/cmdline-tools/latest/bin/sdkmanager --list --verbose 2>&1) |
|
||||
Where-Object { $_ -Match "^[^\s]" } |
|
||||
Where-Object { $_ -NotMatch "^(Loading |Info: Parsing |---|\[=+|Installed |Available )" } |
|
||||
Where-Object { $_ -NotMatch "^[^;]*$" } |
|
||||
Out-File -FilePath $packagesListFile
|
||||
|
||||
Write-Host "Android packages list:"
|
||||
Get-Content $packagesListFile
|
||||
}
|
||||
|
||||
return Get-Content $packagesListFile
|
||||
}
|
||||
|
||||
$androidSdkManagerPackages = Get-AndroidPackages
|
||||
[int]$platformMinVersion = Get-ToolsetValue "android.platform_min_version"
|
||||
[version]$buildToolsMinVersion = Get-ToolsetValue "android.build_tools_min_version"
|
||||
[array]$ndkVersions = Get-ToolsetValue "android.ndk.versions"
|
||||
$ndkFullVersions = $ndkVersions | ForEach-Object { (Get-ChildItem "/usr/local/lib/android/sdk/ndk/${_}.*" | Select-Object -Last 1).Name } | ForEach-Object { "ndk/${_}" }
|
||||
$ndkFullVersions = $ndkVersions |
|
||||
ForEach-Object { (Get-ChildItem "/usr/local/lib/android/sdk/ndk/${_}.*" |
|
||||
Select-Object -Last 1).Name } | ForEach-Object { "ndk/${_}" }
|
||||
# Platforms starting with a letter are the preview versions, which is not installed on the image
|
||||
$platformVersionsList = ($androidSdkManagerPackages | Where-Object { "$_".StartsWith("platforms;") }) -replace 'platforms;android-', '' | Where-Object { $_ -match "^\d" } | Sort-Object -Unique
|
||||
$platformsInstalled = $platformVersionsList | Where-Object { [int]($_.Split("-")[0]) -ge $platformMinVersion } | ForEach-Object { "platforms/android-${_}" }
|
||||
$platformVersionsList = ($androidSdkManagerPackages |
|
||||
Where-Object { "$_".StartsWith("platforms;") }) -replace 'platforms;android-', '' |
|
||||
Where-Object { $_ -match "^\d" } | Sort-Object -Unique
|
||||
$platformsInstalled = $platformVersionsList |
|
||||
Where-Object { [int]($_.Split("-")[0]) -ge $platformMinVersion } |
|
||||
ForEach-Object { "platforms/android-${_}" }
|
||||
|
||||
$buildToolsList = ($androidSdkManagerPackages | Where-Object { "$_".StartsWith("build-tools;") }) -replace 'build-tools;', ''
|
||||
$buildTools = $buildToolsList | Where-Object { $_ -match "\d+(\.\d+){2,}$"} | Where-Object { [version]$_ -ge $buildToolsMinVersion } | Sort-Object -Unique |
|
||||
$buildTools = $buildToolsList |
|
||||
Where-Object { $_ -match "\d+(\.\d+){2,}$"} |
|
||||
Where-Object { [version]$_ -ge $buildToolsMinVersion } |
|
||||
Sort-Object -Unique |
|
||||
ForEach-Object { "build-tools/${_}" }
|
||||
|
||||
$androidPackages = @(
|
||||
@@ -23,22 +109,6 @@ Describe "Android" {
|
||||
|
||||
$androidPackages = $androidPackages | ForEach-Object { $_ }
|
||||
|
||||
BeforeAll {
|
||||
function Validate-AndroidPackage {
|
||||
param (
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string]$PackageName
|
||||
)
|
||||
|
||||
# Convert 'm2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-beta1' ->
|
||||
# 'm2repository/com/android/support/constraint/constraint-layout-solver/1.0.0-beta1'
|
||||
# 'cmake;3.6.4111459' -> 'cmake/3.6.4111459'
|
||||
$PackageName = $PackageName.Replace(";", "/")
|
||||
$targetPath = Join-Path $env:ANDROID_HOME $PackageName
|
||||
$targetPath | Should -Exist
|
||||
}
|
||||
}
|
||||
|
||||
Context "SDKManagers" {
|
||||
$testCases = @(
|
||||
@{
|
||||
@@ -61,7 +131,7 @@ Describe "Android" {
|
||||
|
||||
It "<PackageName>" -TestCases $testCases {
|
||||
param ([string] $PackageName)
|
||||
Validate-AndroidPackage $PackageName
|
||||
Test-AndroidPackage $PackageName
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,8 +8,8 @@ Describe "Java" {
|
||||
[array]$testCases = $jdkVersions | ForEach-Object { @{Version = $_ } }
|
||||
|
||||
It "Java <DefaultJavaVersion> is default" -TestCases @{ DefaultJavaVersion = $defaultVersion } {
|
||||
$actualJavaPath = Get-EnvironmentVariable "JAVA_HOME"
|
||||
$expectedJavaPath = Get-EnvironmentVariable "JAVA_HOME_${DefaultJavaVersion}_X64"
|
||||
$actualJavaPath = [System.Environment]::GetEnvironmentVariable("JAVA_HOME")
|
||||
$expectedJavaPath = [System.Environment]::GetEnvironmentVariable("JAVA_HOME_${DefaultJavaVersion}_X64")
|
||||
|
||||
$actualJavaPath | Should -Not -BeNullOrEmpty
|
||||
$expectedJavaPath | Should -Not -BeNullOrEmpty
|
||||
@@ -28,7 +28,7 @@ Describe "Java" {
|
||||
It "Gradle" {
|
||||
"gradle -version" | Should -ReturnZeroExitCode
|
||||
|
||||
$gradleVariableValue = Get-EnvironmentVariable "GRADLE_HOME"
|
||||
$gradleVariableValue = [System.Environment]::GetEnvironmentVariable("GRADLE_HOME")
|
||||
$gradleVariableValue | Should -BeLike "/usr/share/gradle-*"
|
||||
|
||||
$gradlePath = Join-Path $env:GRADLE_HOME "bin/gradle"
|
||||
@@ -36,7 +36,7 @@ Describe "Java" {
|
||||
}
|
||||
|
||||
It "Java <Version>" -TestCases $testCases {
|
||||
$javaVariableValue = Get-EnvironmentVariable "JAVA_HOME_${Version}_X64"
|
||||
$javaVariableValue = [System.Environment]::GetEnvironmentVariable("JAVA_HOME_${Version}_X64")
|
||||
$javaVariableValue | Should -Not -BeNullOrEmpty
|
||||
$javaPath = Join-Path $javaVariableValue "bin/java"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user