From 64937b994499a4586077159653f24e975746bb14 Mon Sep 17 00:00:00 2001 From: Nikita Bykov <49442273+nikita-bykov@users.noreply.github.com> Date: Thu, 14 Oct 2021 14:42:34 +0300 Subject: [PATCH] [macOS] Add GOROOT env variables (#4220) --- images/macos/helpers/Common.Helpers.psm1 | 17 +++++- .../macos/helpers/SoftwareReport.Helpers.psm1 | 58 +++++++++++++++++++ .../provision/core/configure-toolset.ps1 | 47 +++++++++++++++ images/macos/provision/core/toolset.ps1 | 8 +-- .../SoftwareReport.Toolcache.psm1 | 19 ++++-- images/macos/templates/macOS-10.14.json | 5 +- images/macos/templates/macOS-10.15.json | 5 +- images/macos/templates/macOS-11.json | 5 +- images/macos/templates/macOS-12.json | 5 +- images/macos/toolsets/toolset-10.14.json | 1 + images/macos/toolsets/toolset-10.15.json | 1 + images/macos/toolsets/toolset-11.json | 1 + images/macos/toolsets/toolset-12.json | 1 + 13 files changed, 155 insertions(+), 18 deletions(-) create mode 100644 images/macos/provision/core/configure-toolset.ps1 diff --git a/images/macos/helpers/Common.Helpers.psm1 b/images/macos/helpers/Common.Helpers.psm1 index 50ed849d7..1d42cb0d8 100644 --- a/images/macos/helpers/Common.Helpers.psm1 +++ b/images/macos/helpers/Common.Helpers.psm1 @@ -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 -} \ No newline at end of file +} + +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 +} diff --git a/images/macos/helpers/SoftwareReport.Helpers.psm1 b/images/macos/helpers/SoftwareReport.Helpers.psm1 index b996f64b8..0f8b9abcd 100644 --- a/images/macos/helpers/SoftwareReport.Helpers.psm1 +++ b/images/macos/helpers/SoftwareReport.Helpers.psm1 @@ -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 } \ No newline at end of file diff --git a/images/macos/provision/core/configure-toolset.ps1 b/images/macos/provision/core/configure-toolset.ps1 new file mode 100644 index 000000000..d569eee69 --- /dev/null +++ b/images/macos/provision/core/configure-toolset.ps1 @@ -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 + } +} diff --git a/images/macos/provision/core/toolset.ps1 b/images/macos/provision/core/toolset.ps1 index c9ebc5250..e2c4efe8f 100644 --- a/images/macos/provision/core/toolset.ps1 +++ b/images/macos/provision/core/toolset.ps1 @@ -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 diff --git a/images/macos/software-report/SoftwareReport.Toolcache.psm1 b/images/macos/software-report/SoftwareReport.Toolcache.psm1 index 344807dbc..a31746cf7 100644 --- a/images/macos/software-report/SoftwareReport.Toolcache.psm1 +++ b/images/macos/software-report/SoftwareReport.Toolcache.psm1 @@ -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 diff --git a/images/macos/templates/macOS-10.14.json b/images/macos/templates/macOS-10.14.json index 6316bf328..3b17fda09 100644 --- a/images/macos/templates/macOS-10.14.json +++ b/images/macos/templates/macOS-10.14.json @@ -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", diff --git a/images/macos/templates/macOS-10.15.json b/images/macos/templates/macOS-10.15.json index 9bdd785aa..6c07559e3 100644 --- a/images/macos/templates/macOS-10.15.json +++ b/images/macos/templates/macOS-10.15.json @@ -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", diff --git a/images/macos/templates/macOS-11.json b/images/macos/templates/macOS-11.json index a1a87f424..b564327a3 100644 --- a/images/macos/templates/macOS-11.json +++ b/images/macos/templates/macOS-11.json @@ -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", diff --git a/images/macos/templates/macOS-12.json b/images/macos/templates/macOS-12.json index 3b6ae1835..596bc460d 100644 --- a/images/macos/templates/macOS-12.json +++ b/images/macos/templates/macOS-12.json @@ -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", diff --git a/images/macos/toolsets/toolset-10.14.json b/images/macos/toolsets/toolset-10.14.json index ff45ea7ba..b6ae1155d 100644 --- a/images/macos/toolsets/toolset-10.14.json +++ b/images/macos/toolsets/toolset-10.14.json @@ -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.*", diff --git a/images/macos/toolsets/toolset-10.15.json b/images/macos/toolsets/toolset-10.15.json index ba1b63ec0..1764687bd 100644 --- a/images/macos/toolsets/toolset-10.15.json +++ b/images/macos/toolsets/toolset-10.15.json @@ -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.*", diff --git a/images/macos/toolsets/toolset-11.json b/images/macos/toolsets/toolset-11.json index fa24efbe7..d5997a0b7 100644 --- a/images/macos/toolsets/toolset-11.json +++ b/images/macos/toolsets/toolset-11.json @@ -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.*", diff --git a/images/macos/toolsets/toolset-12.json b/images/macos/toolsets/toolset-12.json index 14cb184ef..82ddeda55 100644 --- a/images/macos/toolsets/toolset-12.json +++ b/images/macos/toolsets/toolset-12.json @@ -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.*",