-
Notifications
You must be signed in to change notification settings - Fork 1
/
SwiftWindows.ps1
executable file
·123 lines (103 loc) · 4.99 KB
/
SwiftWindows.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
Param(
[String]$SwiftVersion = '5.4.2', # e.g. '5.4.2' '5.5' '2021-12-23-a'
[String]$Arch = 'x64', #
[String]$WinSDK = '' # e.g. '', '10.019041.0', '10.0.20348.0'
)
if (!$Env:GITHUB_ACTIONS) {
return 1
}
$PSVersionTable
### Ninja
# Install Ninja.
choco install ninja --yes --no-progress
ninja --version
### Swift Toolchain
# Remove the Swift minor version if it is 0.
if (-Not($SwiftVersion -match '\d{4}-\d{2}-\d{2}-\D')) {
$SplitVersion = $SwiftVersion.Split('.')
if (($SplitVersion.Length -eq 3) -and ($SplitVersion[2] -eq 0)) {
$SwiftVersion = $SplitVersion[0] + '.' + $SplitVersion[1]
}
}
# Check the Swift version whether it is already installed or not.
if ($NULL -ne (Get-Command swift -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Definition -First 1)) {
$CommandResult = swift --version | Select-Object -First 1
if ($CommandResult -cmatch " Swift version $SwiftVersion ") {
Write-Output "Swift $SwiftVersion is already installed."
swift --version
exit 0
}
}
# Install Swift Toolchain.
$exitCode = 0
if ($SwiftVersion -match '\d{4}-\d{2}-\d{2}-\D') {
Write-Output "Download Swift snapshot version: $SwiftVersion ..."
curl.exe -sL "https://download.swift.org/development/windows10/swift-DEVELOPMENT-SNAPSHOT-$SwiftVersion/swift-DEVELOPMENT-SNAPSHOT-$SwiftVersion-windows10.exe" -o "$Env:TEMP/swift-DEVELOPMENT-SNAPSHOT-$SwiftVersion-windows10.exe"
Write-Output "🎉 Successfully downloaded!"
$process = Start-Process -FilePath "$Env:TEMP/swift-DEVELOPMENT-SNAPSHOT-$SwiftVersion-windows10.exe" -ArgumentList -q -Wait -PassThru
$exitCode = $process.ExitCode
} else {
Write-Output "Download Swift release version: $SwiftVersion ..."
curl.exe -sL "https://download.swift.org/swift-$SwiftVersion-release/windows10/swift-$SwiftVersion-RELEASE/swift-$SwiftVersion-RELEASE-windows10.exe" -o "$Env:TEMP/swift-$SwiftVersion-RELEASE-windows10.exe"
Write-Output "🎉 Successfully downloaded!"
$process = Start-Process -FilePath "$Env:TEMP/swift-$SwiftVersion-RELEASE-windows10.exe" -ArgumentList -q -Wait -PassThru
$exitCode = $process.ExitCode
}
if (($exitCode -eq 0) -or ($exitCode -eq 3010)) {
Write-Output "🎉 Successfully installed!"
} else {
Write-Output "🚫 Failed to install..."
exit $exitCode
}
# Get environment variables in the current session.
foreach($level in 'Machine', 'User') {
[Environment]::GetEnvironmentVariables($level).GetEnumerator() | ForEach-Object {
if($_.Name -eq 'Path') {
$_.Value = ($((Get-Content "Env:$($_.Name)") + ";$($_.Value)") -split ';' | Select-Object -unique) -join ';'
}
$_
} | Set-Content -Path { "Env:$($_.Name)" }
}
# Set environment variables.
Write-Output "$Env:Path" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8
Get-ChildItem Env: | ForEach-Object { Write-Output "$($_.Name)=$($_.Value)" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf8 -Append }
### Visual Studio
# Set up Visual Studio.
$InstallationPath = Get-VSSetupInstance | Select-Object -ExpandProperty InstallationPath
$vcvarsallPath = Join-Path $InstallationPath 'VC\Auxiliary\Build\vcvarsall.bat'
cmd.exe -Verb runas /c "call `"$vcvarsallPath`" $Arch $WinSDK && set > %TEMP%\vcvars.txt"
Get-Content "$Env:TEMP\vcvars.txt" | Foreach-Object {
if ($_ -match "^(.*?)=(.*)$") {
$key=$matches[1].ToString()
$value=$matches[2].ToString()
# Sets environment variables for this step.
switch ($key) {
'UniversalCRTSdkDir' { $Env:UniversalCRTSdkDir = $value }
'UCRTVersion' { $Env:UCRTVersion = $value }
'VCToolsInstallDir' { $Env:VCToolsInstallDir = $value }
Default { }
}
# Sets environments variables for subsequent steps(not this step).
if ($key -eq 'Path') {
Write-Output "$Env:Path" | Out-File -FilePath $Env:GITHUB_PATH -Encoding utf-8 -Append
} else {
Write-Output "$key=$value" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf-8 -Append
}
}
}
Write-Output $Env:Path
Write-Output $Env:UniversalCRTSdkDir
Write-Output $Env:UCRTVersion
Write-Output $Env:VCToolsInstallDir
# Add supporting files.
Copy-Item -Path "$Env:SDKROOT\usr\share\ucrt.modulemap" -Destination "$Env:UniversalCRTSdkDir\Include\$Env:UCRTVersion\ucrt\module.modulemap" -Force
if (Test-Path -Path "$env:SDKROOT\usr\share\vcruntime.modulemap") {
Copy-Item -Path "$env:SDKROOT\usr\share\vcruntime.modulemap" -Destination "$env:VCToolsInstallDir\include\module.modulemap" -Force
Copy-Item -Path "$env:SDKROOT\usr\share\vcruntime.apinotes" -Destination "$env:VCToolsInstallDir\include\vcruntime.apinotes" -Force
} else {
Copy-Item -Path "$Env:SDKROOT\usr\share\visualc.modulemap" -Destination "$Env:VCToolsInstallDir\include\module.modulemap" -Force
Copy-Item -Path "$Env:SDKROOT\usr\share\visualc.apinotes" -Destination "$Env:VCToolsInstallDir\include\visualc.apinotes" -Force
}
Copy-Item -Path "$Env:SDKROOT\usr\share\winsdk.modulemap" -Destination "$Env:UniversalCRTSdkDir\Include\$Env:UCRTVersion\um\module.modulemap" -Force
# Output Swift version.
swift --version