update_dart_sdk.ps1 4.71 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
# Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.


# ---------------------------------- NOTE ---------------------------------- #
#
# Please keep the logic in this file consistent with the logic in the
# `update_dart_sdk.sh` script in the same directory to ensure that Flutter
# continues to work across all platforms!
#
# -------------------------------------------------------------------------- #

$ErrorActionPreference = "Stop"

16
$progName = Split-Path -parent $MyInvocation.MyCommand.Definition
17
$flutterRoot = (Get-Item $progName).parent.parent.FullName
18

19 20
$cachePath = "$flutterRoot\bin\cache"
$dartSdkPath = "$cachePath\dart-sdk"
21
$dartSdkLicense = "$cachePath\LICENSE.dart_sdk_archive.md"
22 23
$engineStamp = "$cachePath\engine-dart-sdk.stamp"
$engineVersion = (Get-Content "$flutterRoot\bin\internal\engine.version")
24
$engineRealm = (Get-Content "$flutterRoot\bin\internal\engine.realm")
25

26 27
$oldDartSdkPrefix = "dart-sdk.old"

28 29 30 31 32
# Make sure that PowerShell has expected version.
$psMajorVersionRequired = 5
$psMajorVersionLocal = $PSVersionTable.PSVersion.Major
if ($psMajorVersionLocal -lt $psMajorVersionRequired) {
    Write-Host "Flutter requires PowerShell $psMajorVersionRequired.0 or newer."
33
    Write-Host "See https://flutter.dev/docs/get-started/install/windows for more."
34 35 36
    Write-Host "Current version is $psMajorVersionLocal."
    # Use exit code 2 to signal that shared.bat should exit immediately instead of retrying.
    exit 2
37 38
}

39
if ((Test-Path $engineStamp) -and ($engineVersion -eq (Get-Content $engineStamp))) {
40 41 42
    return
}

43 44 45 46
$dartSdkBaseUrl = $Env:FLUTTER_STORAGE_BASE_URL
if (-not $dartSdkBaseUrl) {
    $dartSdkBaseUrl = "https://storage.googleapis.com"
}
47 48 49
if ($engineRealm) {
    $dartSdkBaseUrl = "$dartSdkBaseUrl/$engineRealm"
}
50
$dartZipName = "dart-sdk-windows-x64.zip"
51
$dartSdkUrl = "$dartSdkBaseUrl/flutter_infra_release/flutter/$engineVersion/$dartZipName"
52

53
if ((Test-Path $dartSdkPath) -or (Test-Path $dartSdkLicense)) {
54 55 56
    # Move old SDK to a new location instead of deleting it in case it is still in use (e.g. by IntelliJ).
    $oldDartSdkSuffix = 1
    while (Test-Path "$cachePath\$oldDartSdkPrefix$oldDartSdkSuffix") { $oldDartSdkSuffix++ }
57 58 59 60 61 62 63 64

    if (Test-Path $dartSdkPath) {
        Rename-Item $dartSdkPath "$oldDartSdkPrefix$oldDartSdkSuffix"
    }

    if (Test-Path $dartSdkLicense) {
        Rename-Item $dartSdkLicense "$oldDartSdkPrefix$oldDartSdkSuffix.LICENSE.md"
    }
65
}
66
New-Item $dartSdkPath -force -type directory | Out-Null
67
$dartSdkZip = "$cachePath\$dartZipName"
68 69 70

Try {
    Import-Module BitsTransfer
71
    $ProgressPreference = 'SilentlyContinue'
72
    Start-BitsTransfer -Source $dartSdkUrl -Destination $dartSdkZip -ErrorAction Stop
73 74 75
}
Catch {
    Write-Host "Downloading the Dart SDK using the BITS service failed, retrying with WebRequest..."
76 77 78 79 80 81
    # Invoke-WebRequest is very slow when the progress bar is visible - a 28
    # second download can become a 33 minute download. Disable it with
    # $ProgressPreference and then restore the original value afterwards.
    # https://github.com/flutter/flutter/issues/37789
    $OriginalProgressPreference = $ProgressPreference
    $ProgressPreference = 'SilentlyContinue'
82
    Invoke-WebRequest -Uri $dartSdkUrl -OutFile $dartSdkZip
83
    $ProgressPreference = $OriginalProgressPreference
84
}
85

86
If (Get-Command 7z -errorAction SilentlyContinue) {
87
    Write-Host "Expanding downloaded archive with 7z..."
88
    # The built-in unzippers are painfully slow. Use 7-Zip, if available.
89
    & 7z x $dartSdkZip "-o$cachePath" -bd | Out-Null
90
} ElseIf (Get-Command 7za -errorAction SilentlyContinue) {
91
    Write-Host "Expanding downloaded archive with 7za..."
92
    # Use 7-Zip's standalone version 7za.exe, if available.
93
    & 7za x $dartSdkZip "-o$cachePath" -bd | Out-Null
94
} ElseIf (Get-Command Microsoft.PowerShell.Archive\Expand-Archive -errorAction SilentlyContinue) {
95
    Write-Host "Expanding downloaded archive with PowerShell..."
96
    # Use PowerShell's built-in unzipper, if available (requires PowerShell 5+).
97
    $global:ProgressPreference='SilentlyContinue'
98
    Microsoft.PowerShell.Archive\Expand-Archive $dartSdkZip -DestinationPath $cachePath
99
} Else {
100
    Write-Host "Expanding downloaded archive with Windows..."
101
    # As last resort: fall back to the Windows GUI.
102 103 104 105 106 107
    $shell = New-Object -com shell.application
    $zip = $shell.NameSpace($dartSdkZip)
    foreach($item in $zip.items()) {
        $shell.Namespace($cachePath).copyhere($item)
    }
}
108

109
Remove-Item $dartSdkZip
110
$engineVersion | Out-File $engineStamp -Encoding ASCII
111

112
# Try to delete all old SDKs and license files.
113
Get-ChildItem -Path $cachePath | Where {$_.BaseName.StartsWith($oldDartSdkPrefix)} | Remove-Item -Recurse -ErrorAction SilentlyContinue