99# # See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010# #
1111# #===----------------------------------------------------------------------===##
12- $VSB = ' https://download.visualstudio.microsoft.com/download/pr/5536698c-711c-4834-876f-2817d31a2ef2/c792bdb0fd46155de19955269cac85d52c4c63c23db2cf43d96b9390146f9390/vs_BuildTools.exe'
13- $VSB_SHA256 = ' C792BDB0FD46155DE19955269CAC85D52C4C63C23DB2CF43D96B9390146F9390'
14- Set-Variable ErrorActionPreference Stop
15- Set-Variable ProgressPreference SilentlyContinue
16- Write-Host - NoNewLine (' Downloading {0} ... ' -f ${VSB} )
17- Invoke-WebRequest - Uri $VSB - OutFile $env: TEMP \vs_buildtools.exe
18- Write-Host ' SUCCESS'
19- Write-Host - NoNewLine (' Verifying SHA256 ({0}) ... ' -f $VSB_SHA256 )
20- $Hash = Get-FileHash $env: TEMP \vs_buildtools.exe - Algorithm sha256
21- if ($Hash.Hash -eq $VSB_SHA256 ) {
22- Write-Host ' SUCCESS'
23- } else {
24- Write-Host (' FAILED ({0})' -f $Hash.Hash )
25- exit 1
12+
13+ # Retry configuration
14+ $MaxRetries = 5
15+ $RetryDelay = 5
16+
17+ function Invoke-WebRequestWithRetry {
18+ param (
19+ [string ]$Uri ,
20+ [string ]$OutFile ,
21+ [int ]$TimeoutSec = 300
22+ )
23+
24+ $attempt = 1
25+
26+ while ($attempt -le $MaxRetries ) {
27+ try {
28+ if ($attempt -gt 1 ) {
29+ Write-Host " Retry attempt $attempt of $MaxRetries after ${RetryDelay} s delay..."
30+ Start-Sleep - Seconds $RetryDelay
31+ }
32+
33+ Write-Host " Attempt $attempt `: Downloading from $Uri "
34+
35+ # Clean up any existing partial download
36+ if (Test-Path $OutFile ) {
37+ Remove-Item - Force $OutFile - ErrorAction SilentlyContinue
38+ }
39+
40+ # Get expected file size from HTTP headers
41+ $headRequest = Invoke-WebRequest - Uri $Uri - Method Head - UseBasicParsing - TimeoutSec 30
42+ $expectedSize = $null
43+ if ($headRequest.Headers.ContainsKey (' Content-Length' )) {
44+ $expectedSize = [long ]$headRequest.Headers [' Content-Length' ][0 ]
45+ Write-Host " Expected file size: $ ( [math ]::Round($expectedSize / 1 MB , 2 )) MB"
46+ }
47+
48+ # Download with progress tracking disabled for better performance
49+ $webClient = New-Object System.Net.WebClient
50+ $webClient.DownloadFile ($Uri , $OutFile )
51+ $webClient.Dispose ()
52+
53+ # Verify file exists and has content
54+ if (-not (Test-Path $OutFile )) {
55+ throw " Download completed but file not found at $OutFile "
56+ }
57+
58+ $actualSize = (Get-Item $OutFile ).Length
59+ Write-Host " Downloaded file size: $ ( [math ]::Round($actualSize / 1 MB , 2 )) MB"
60+
61+ # Verify file size matches expected size
62+ if ($expectedSize -and $actualSize -ne $expectedSize ) {
63+ throw " File size mismatch. Expected: $expectedSize bytes, Got: $actualSize bytes"
64+ }
65+
66+ # Verify file is not corrupted by checking if it's a valid PE executable
67+ $fileBytes = [System.IO.File ]::ReadAllBytes($OutFile )
68+ if ($fileBytes.Length -lt 2 -or $fileBytes [0 ] -ne 0x4D -or $fileBytes [1 ] -ne 0x5A ) {
69+ throw " Downloaded file is not a valid executable (missing MZ header)"
70+ }
71+
72+ Write-Host " Download completed and verified successfully"
73+ return $true
74+ }
75+ catch {
76+ Write-Host " Download failed on attempt $attempt `: $ ( $_.Exception.Message ) "
77+
78+ # Clean up partial download if it exists
79+ if (Test-Path $OutFile ) {
80+ Remove-Item - Force $OutFile - ErrorAction SilentlyContinue
81+ }
82+
83+ if ($attempt -eq $MaxRetries ) {
84+ Write-Host " Download failed after $MaxRetries attempts"
85+ throw
86+ }
87+ }
88+
89+ $attempt ++
90+ }
91+
92+ return $false
2693}
27- Write-Host - NoNewLine ' Installing Visual Studio Build Tools ... '
28- $Process =
29- Start-Process $env: TEMP \vs_buildtools.exe - Wait - PassThru - NoNewWindow - ArgumentList @ (
30- ' --quiet' ,
31- ' --wait' ,
32- ' --norestart' ,
33- ' --nocache' ,
34- ' --add' , ' Microsoft.VisualStudio.Component.Windows11SDK.22000' ,
35- ' --add' , ' Microsoft.VisualStudio.Component.VC.Tools.x86.x64'
94+
95+ function Install-VisualStudioBuildTools {
96+ param (
97+ [string ]$Url ,
98+ [string ]$Sha256
3699 )
37- if ($Process.ExitCode -eq 0 -or $Process.ExitCode -eq 3010 ) {
38- Write-Host ' SUCCESS'
39- } else {
40- Write-Host (' FAILED ({0})' -f $Process.ExitCode )
41- exit 1
100+
101+ Set-Variable ErrorActionPreference Stop
102+ Set-Variable ProgressPreference SilentlyContinue
103+
104+ $installerPath = " $env: TEMP \vs_buildtools.exe"
105+
106+ Write-Host " Downloading Visual Studio Build Tools from $Url "
107+
108+ try {
109+ Invoke-WebRequestWithRetry - Uri $Url - OutFile $installerPath
110+ Write-Host ' Download SUCCESS'
111+ }
112+ catch {
113+ Write-Host " Download FAILED: $ ( $_.Exception.Message ) "
114+ exit 1
115+ }
116+
117+ Write-Host - NoNewLine (' Verifying SHA256 ({0}) ... ' -f $Sha256 )
118+ $Hash = Get-FileHash $installerPath - Algorithm sha256
119+ if ($Hash.Hash -eq $VSB_SHA256 ) {
120+ Write-Host ' SUCCESS'
121+ } else {
122+ Write-Host (' FAILED ({0})' -f $Hash.Hash )
123+ exit 1
124+ }
125+
126+ Write-Host - NoNewLine ' Installing Visual Studio Build Tools ... '
127+ try {
128+ $Process = Start-Process $installerPath - Wait - PassThru - NoNewWindow - ArgumentList @ (
129+ ' --quiet' ,
130+ ' --wait' ,
131+ ' --norestart' ,
132+ ' --nocache' ,
133+ ' --add' , ' Microsoft.VisualStudio.Component.Windows11SDK.22000' ,
134+ ' --add' , ' Microsoft.VisualStudio.Component.VC.Tools.x86.x64'
135+ )
136+ if ($Process.ExitCode -eq 0 -or $Process.ExitCode -eq 3010 ) {
137+ Write-Host ' SUCCESS'
138+ } else {
139+ Write-Host (' FAILED ({0})' -f $Process.ExitCode )
140+ exit 1
141+ }
142+ }
143+ catch {
144+ Write-Host " FAILED: $ ( $_.Exception.Message ) "
145+ Remove-Item - Force $installerPath - ErrorAction SilentlyContinue
146+ exit 1
147+ }
148+
149+ Remove-Item - Force $installerPath - ErrorAction SilentlyContinue
42150}
43- Remove-Item - Force $env: TEMP \vs_buildtools.exe
151+
152+ $VSB = ' https://download.visualstudio.microsoft.com/download/pr/5536698c-711c-4834-876f-2817d31a2ef2/c792bdb0fd46155de19955269cac85d52c4c63c23db2cf43d96b9390146f9390/vs_BuildTools.exe'
153+ $VSB_SHA256 = ' C792BDB0FD46155DE19955269CAC85D52C4C63C23DB2CF43D96B9390146F9390'
154+
155+ Install-VisualStudioBuildTools - Url $VSB - Sha256 $VSB_SHA256
0 commit comments