[macOS] Add GOROOT env variables (#4220)

This commit is contained in:
Nikita Bykov
2021-10-14 14:42:34 +03:00
committed by GitHub
parent 3ec2449ade
commit 64937b9944
13 changed files with 155 additions and 18 deletions

View File

@@ -104,8 +104,7 @@ function Invoke-ValidateCommand {
return $output
}
function Start-DownloadWithRetry
{
function Start-DownloadWithRetry {
Param
(
[Parameter(Mandatory)]
@@ -147,4 +146,16 @@ function Start-DownloadWithRetry
}
return $filePath
}
}
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
}

View File

@@ -59,4 +59,62 @@ function Get-BrewPackageVersion {
$packageVersion = $Matches.Version
return $packageVersion
}
function Get-CachedToolInstances {
<#
.SYNOPSIS
Returns hashtable of installed cached tools.
.DESCRIPTION
Return hashtable that contains versions and architectures for the selected cached tool.
.PARAMETER Name
Name of cached tool.
.PARAMETER VersionCommand
Optional parameter. Command to return version of system default tool.
.EXAMPLE
Get-CachedToolInstances -Name "Python" -VersionCommand "--version"
#>
param
(
[String] $Name,
[String] $VersionCommand
)
$toolInstances = @()
$toolPath = Join-Path -Path $env:AGENT_TOOLSDIRECTORY -ChildPath $Name
# Get all installed versions from TOOLSDIRECTORY folder
$versions = Get-ChildItem $toolPath | Sort-Object { [System.Version]$_.Name }
foreach ($version in $versions) {
$instanceInfo = @{}
# Create instance hashtable
[string]$instanceInfo.Path = Join-Path -Path $toolPath -ChildPath $version.Name
[string]$instanceInfo.Version = $version.Name
# Get all architectures for current version
[array]$instanceInfo.Architecture_Array = Get-ChildItem $version.FullName -Name -Directory | Where-Object { $_ -match "^x[0-9]{2}$" }
[string]$instanceInfo.Architecture = $instanceInfo.Architecture_Array -Join ", "
# Add (default) postfix to version name, in case if current version is in environment path
if (-not ([string]::IsNullOrEmpty($VersionCommand))) {
$defaultVersion = $(& ($Name.ToLower()) $VersionCommand 2>&1)
$defaultToolVersion = $defaultVersion | Select-String -Pattern "\d+\.\d+\.\d+" -AllMatches `
| ForEach-Object { $_.Matches.Value }
if ([version]$version.Name -eq [version]$defaultToolVersion) {
$instanceInfo.Version += " (Default)"
}
}
$toolInstances += $instanceInfo
}
return $toolInstances
}

View File

@@ -0,0 +1,47 @@
################################################################################
## File: Configure-Toolset.ps1
## Team: CI-Build
## Desc: Configure toolset
################################################################################
Import-Module "~/image-generation/helpers/Common.Helpers.psm1"
function Get-ToolsetToolFullPath
{
param
(
[Parameter(Mandatory)] [string] $ToolName,
[Parameter(Mandatory)] [string] $ToolVersion,
[Parameter(Mandatory)] [string] $ToolArchitecture
)
$toolPath = Join-Path -Path $env:AGENT_TOOLSDIRECTORY -ChildPath $toolName
$toolPathVersion = Join-Path -Path $toolPath -ChildPath $toolVersion
$foundVersion = Get-Item $toolPathVersion | Sort-Object -Property {[version]$_.name} -Descending | Select-Object -First 1
$installationDir = Join-Path -Path $foundVersion -ChildPath $toolArchitecture
return $installationDir
}
$toolcache = Get-ToolsetValue "toolcache"
foreach ($tool in $toolcache)
{
$toolName = $tool.name
$toolArch = $tool.arch
$toolEnvironment = $tool.variable_template
if (-not $toolEnvironment)
{
continue
}
foreach ($toolVersion in $tool.versions)
{
Write-Host "Set $toolName $toolVersion environment variable..."
$toolPath = Get-ToolsetToolFullPath -ToolName $toolName -ToolVersion $toolVersion -ToolArchitecture $toolArch
$envName = $toolEnvironment -f $toolVersion.split(".")
# Add environment variable name=value
Add-EnvironmentVariable -Name $envName -Value $toolPath
}
}

View File

@@ -6,12 +6,6 @@
Import-Module "~/image-generation/helpers/Tests.Helpers.psm1"
Import-Module "~/image-generation/helpers/Common.Helpers.psm1"
Function Get-ToolcacheFromToolset {
$toolsetPath = Join-Path $env:HOME "image-generation" "toolset.json"
$toolsetJson = Get-Content -Raw $toolsetPath | ConvertFrom-Json
return $toolsetJson.toolcache
}
Function Install-Asset {
param(
[Parameter(Mandatory=$true)]
@@ -36,7 +30,7 @@ Function Install-Asset {
# Get toolcache content from toolset
$toolsToInstall = @("Python", "Node", "Go")
$tools = Get-ToolcacheFromToolset | Where-Object {$ToolsToInstall -contains $_.Name}
$tools = Get-ToolsetValue "toolcache" | Where-Object {$toolsToInstall -contains $_.Name}
foreach ($tool in $tools) {
# Get versions manifest for current tool

View File

@@ -28,9 +28,20 @@ function Get-ToolcacheNodeVersions {
return Get-ChildItem $toolcachePath -Name | Sort-Object { [Version]$_ }
}
function Get-ToolcacheGoVersions {
$toolcachePath = Join-Path $env:HOME "hostedtoolcache" "Go"
return Get-ChildItem $toolcachePath -Name | Sort-Object { [Version]$_ }
function Get-ToolcacheGoTable {
$ToolInstances = Get-CachedToolInstances -Name "Go" -VersionCommand "version"
foreach ($Instance in $ToolInstances) {
$Version = [System.Version]($Instance.Version -Split(" "))[0]
$Instance."Environment Variable" = "GOROOT_$($Version.major)_$($Version.minor)_X64"
}
$Content = $ToolInstances | New-MDTable -Columns ([ordered]@{
Version = "left";
Architecture = "left";
"Environment Variable" = "left"
})
return $Content
}
function Build-ToolcacheSection {
@@ -47,7 +58,7 @@ function Build-ToolcacheSection {
$output += New-MDHeader "Node.js" -Level 4
$output += New-MDList -Lines (Get-ToolcacheNodeVersions) -Style Unordered
$output += New-MDHeader "Go" -Level 4
$output += New-MDList -Lines (Get-ToolcacheGoVersions) -Style Unordered
$output += Get-ToolcacheGoTable
}
return $output

View File

@@ -192,7 +192,10 @@
{
"type": "shell",
"execute_command": "chmod +x {{ .Path }}; {{ .Vars }} pwsh -f {{ .Path }}",
"scripts": "./provision/core/toolset.ps1"
"scripts": [
"./provision/core/toolset.ps1",
"./provision/core/configure-toolset.ps1"
]
},
{
"type": "shell",

View File

@@ -195,7 +195,10 @@
{
"type": "shell",
"execute_command": "chmod +x {{ .Path }}; {{ .Vars }} pwsh -f {{ .Path }}",
"scripts": "./provision/core/toolset.ps1"
"scripts": [
"./provision/core/toolset.ps1",
"./provision/core/configure-toolset.ps1"
]
},
{
"type": "shell",

View File

@@ -198,7 +198,10 @@
{
"type": "shell",
"execute_command": "chmod +x {{ .Path }}; {{ .Vars }} pwsh -f {{ .Path }}",
"scripts": "./provision/core/toolset.ps1"
"scripts": [
"./provision/core/toolset.ps1",
"./provision/core/configure-toolset.ps1"
]
},
{
"type": "shell",

View File

@@ -193,7 +193,10 @@
{
"type": "shell",
"execute_command": "chmod +x {{ .Path }}; {{ .Vars }} pwsh -f {{ .Path }}",
"scripts": "./provision/core/toolset.ps1"
"scripts": [
"./provision/core/toolset.ps1",
"./provision/core/configure-toolset.ps1"
]
},
{
"type": "shell",

View File

@@ -331,6 +331,7 @@
"url" : "https://raw.githubusercontent.com/actions/go-versions/main/versions-manifest.json",
"arch": "x64",
"platform" : "darwin",
"variable_template" : "GOROOT_{0}_{1}_X64",
"versions": [
"1.13.*",
"1.14.*",

View File

@@ -283,6 +283,7 @@
"url" : "https://raw.githubusercontent.com/actions/go-versions/main/versions-manifest.json",
"arch": "x64",
"platform" : "darwin",
"variable_template" : "GOROOT_{0}_{1}_X64",
"versions": [
"1.13.*",
"1.14.*",

View File

@@ -227,6 +227,7 @@
"url" : "https://raw.githubusercontent.com/actions/go-versions/main/versions-manifest.json",
"arch": "x64",
"platform" : "darwin",
"variable_template" : "GOROOT_{0}_{1}_X64",
"versions": [
"1.15.*",
"1.16.*",

View File

@@ -132,6 +132,7 @@
"url" : "https://raw.githubusercontent.com/actions/go-versions/main/versions-manifest.json",
"arch": "x64",
"platform" : "darwin",
"variable_template" : "GOROOT_{0}_{1}_X64",
"versions": [
"1.15.*",
"1.16.*",