|
9 | 9 | ## See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
10 | 10 | ## |
11 | 11 | ##===----------------------------------------------------------------------===## |
| 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 | + # Use -Resume to support partial downloads if the connection drops |
| 34 | + Invoke-WebRequest -Uri $Uri -OutFile $OutFile -TimeoutSec $TimeoutSec -UseBasicParsing |
| 35 | + |
| 36 | + Write-Host "Download completed successfully" |
| 37 | + return $true |
| 38 | + } |
| 39 | + catch { |
| 40 | + Write-Host "Download failed on attempt $attempt`: $($_.Exception.Message)" |
| 41 | + |
| 42 | + # Clean up partial download if it exists |
| 43 | + if (Test-Path $OutFile) { |
| 44 | + Remove-Item -Force $OutFile -ErrorAction SilentlyContinue |
| 45 | + } |
| 46 | + |
| 47 | + if ($attempt -eq $MaxRetries) { |
| 48 | + Write-Host "Download failed after $MaxRetries attempts" |
| 49 | + throw |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + $attempt++ |
| 54 | + } |
| 55 | + |
| 56 | + return $false |
| 57 | +} |
| 58 | + |
12 | 59 | function Install-Swift { |
13 | 60 | param ( |
14 | 61 | [string]$Url, |
15 | 62 | [string]$Sha256 |
16 | 63 | ) |
17 | 64 | Set-Variable ErrorActionPreference Stop |
18 | 65 | Set-Variable ProgressPreference SilentlyContinue |
19 | | - Write-Host -NoNewLine ('Downloading {0} ... ' -f $url) |
20 | | - Invoke-WebRequest -Uri $url -OutFile installer.exe |
21 | | - Write-Host 'SUCCESS' |
| 66 | + |
| 67 | + Write-Host "Downloading $Url ... " |
| 68 | + |
| 69 | + try { |
| 70 | + Invoke-WebRequestWithRetry -Uri $Url -OutFile installer.exe |
| 71 | + Write-Host 'SUCCESS' |
| 72 | + } |
| 73 | + catch { |
| 74 | + Write-Host "FAILED: $($_.Exception.Message)" |
| 75 | + exit 1 |
| 76 | + } |
| 77 | + |
22 | 78 | Write-Host -NoNewLine ('Verifying SHA256 ({0}) ... ' -f $Sha256) |
23 | 79 | $Hash = Get-FileHash installer.exe -Algorithm sha256 |
24 | 80 | if ($Hash.Hash -eq $Sha256 -or $Sha256 -eq "") { |
|
0 commit comments