update_dart_sdk.ps1 3.83 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# Copyright 2017 The Chromium Authors. All rights reserved.
# 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 22
$engineStamp = "$cachePath\engine-dart-sdk.stamp"
$engineVersion = (Get-Content "$flutterRoot\bin\internal\engine.version")
23

24 25
$oldDartSdkPrefix = "dart-sdk.old"

26 27 28 29 30
# 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."
31
    Write-Host "See https://flutter.dev/docs/get-started/install/windows for more."
32 33 34
    return
}

35
if ((Test-Path $engineStamp) -and ($engineVersion -eq (Get-Content $engineStamp))) {
36 37 38
    return
}

39
Write-Host "Downloading Dart SDK from Flutter engine $engineVersion..."
40 41 42 43
$dartSdkBaseUrl = $Env:FLUTTER_STORAGE_BASE_URL
if (-not $dartSdkBaseUrl) {
    $dartSdkBaseUrl = "https://storage.googleapis.com"
}
44 45
$dartZipName = "dart-sdk-windows-x64.zip"
$dartSdkUrl = "$dartSdkBaseUrl/flutter_infra/flutter/$engineVersion/$dartZipName"
46

47 48 49 50 51 52
if (Test-Path $dartSdkPath) {
    # 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++ }
    Rename-Item $dartSdkPath "$oldDartSdkPrefix$oldDartSdkSuffix"
}
53
New-Item $dartSdkPath -force -type directory | Out-Null
54
$dartSdkZip = "$cachePath\$dartZipName"
55 56 57 58 59 60 61

Try {
    Import-Module BitsTransfer
    Start-BitsTransfer -Source $dartSdkUrl -Destination $dartSdkZip
}
Catch {
    Write-Host "Downloading the Dart SDK using the BITS service failed, retrying with WebRequest..."
62 63 64 65 66 67
    # 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'
68
    Invoke-WebRequest -Uri $dartSdkUrl -OutFile $dartSdkZip
69
    $ProgressPreference = $OriginalProgressPreference
70
}
71 72

Write-Host "Unzipping Dart SDK..."
73 74
If (Get-Command 7z -errorAction SilentlyContinue) {
    # The built-in unzippers are painfully slow. Use 7-Zip, if available.
75
    & 7z x $dartSdkZip "-o$cachePath" -bd | Out-Null
76 77
} ElseIf (Get-Command 7za -errorAction SilentlyContinue) {
    # Use 7-Zip's standalone version 7za.exe, if available.
78
    & 7za x $dartSdkZip "-o$cachePath" -bd | Out-Null
79 80
} ElseIf (Get-Command Expand-Archive -errorAction SilentlyContinue) {
    # Use PowerShell's built-in unzipper, if available (requires PowerShell 5+).
81
    Expand-Archive $dartSdkZip -DestinationPath $cachePath
82 83
} Else {
    # As last resort: fall back to the Windows GUI.
84 85 86 87 88 89
    $shell = New-Object -com shell.application
    $zip = $shell.NameSpace($dartSdkZip)
    foreach($item in $zip.items()) {
        $shell.Namespace($cachePath).copyhere($item)
    }
}
90

91
Remove-Item $dartSdkZip
92
$engineVersion | Out-File $engineStamp -Encoding ASCII
93 94 95

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