Skip to content

Commit 5092329

Browse files
update environment variables after installing Swift on Windows
1 parent 6c6512c commit 5092329

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

.github/workflows/scripts/windows/swift/install-swift.ps1

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,50 @@ function Install-Swift {
3838
Write-Host ('FAILED ({0})' -f $Process.ExitCode)
3939
exit 1
4040
}
41+
Write-Host -NoNewLine 'Adding Swift to PATH ... '
42+
# Older toolchains modify the Machine PATH to include \Library\Developer\Toolchains
43+
foreach ($Item in [Environment]::GetEnvironmentVariables('Machine')['Path'].Split(';')) {
44+
if ($Item.Contains('Swift') -or $Item.Contains('Library\Developer\Toolchains')) {
45+
GitHubActions-AddPath -Item $Item
46+
}
47+
}
48+
# Newer toolchains modify the User PATH to include a path to a Swift directory
49+
foreach ($Item in [Environment]::GetEnvironmentVariables('User')['Path'].Split(';')) {
50+
if ($Item.Contains('Swift')) {
51+
GitHubActions-AddPath -Item $Item
52+
}
53+
}
54+
# Find and export the SDKROOT environment variable
55+
$MachineSDKROOT = [Environment]::GetEnvironmentVariables('Machine')['SDKROOT']
56+
$UserSDKROOT = [Environment]::GetEnvironmentVariables('User')['SDKROOT']
57+
if ($MachineSDKROOT) {
58+
GitHubActions-ExportVariable -Name "SDKROOT" -Value $MachineSDKROOT
59+
} elseif ($UserSDKROOT) {
60+
GitHubActions-ExportVariable -Name "SDKROOT" -Value $UserSDKROOT
61+
}
62+
Write-Host 'SUCCESS'
4163
Remove-Item -Force installer.exe
42-
}
64+
}
65+
66+
function GitHubActions-AddPath {
67+
param (
68+
[string]$Item
69+
)
70+
$FilePath = $env:GITHUB_PATH
71+
if ($FilePath) {
72+
Add-Content -Path "$FilePath" -Value "$Item`r`n"
73+
}
74+
$env:Path = $Item + ";" + $env:Path
75+
}
76+
77+
function GitHubActions-ExportVariable {
78+
param (
79+
[string]$Name,
80+
[string]$Value
81+
)
82+
$FilePath = $env:GITHUB_ENV
83+
if ($FilePath) {
84+
Add-Content -Path "$FilePath" -Value "$Name=$Value`r`n"
85+
}
86+
Set-Content env:\$Name $Value
87+
}

.github/workflows/swift_package_test.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,4 @@ jobs:
731731
- name: Build / Test
732732
timeout-minutes: 60
733733
if: ${{ !inputs.enable_windows_docker }}
734-
run: |
735-
Import-Module $env:ChocolateyInstall\helpers\chocolateyProfile.psm1
736-
RefreshEnv
737-
powershell.exe -NoLogo -File $env:TEMP\test-script\run.ps1; exit $LastExitCode
734+
run: powershell.exe -NoLogo -File $env:TEMP\test-script\run.ps1; exit $LastExitCode

0 commit comments

Comments
 (0)