• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 # Copyright 2017 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4 
5 
6 # ---------------------------------- NOTE ---------------------------------- #
7 #
8 # Please keep the logic in this file consistent with the logic in the
9 # `update_dart_sdk.sh` script in the same directory to ensure that Flutter
10 # continues to work across all platforms!
11 #
12 # -------------------------------------------------------------------------- #
13 
14 $ErrorActionPreference = "Stop"
15 
16 $progName = Split-Path -parent $MyInvocation.MyCommand.Definition
17 $flutterRoot = (Get-Item $progName).parent.parent.FullName
18 
19 $cachePath = "$flutterRoot\bin\cache"
20 $dartSdkPath = "$cachePath\dart-sdk"
21 $engineStamp = "$cachePath\engine-dart-sdk.stamp"
22 $engineVersion = (Get-Content "$flutterRoot\bin\internal\engine.version")
23 
24 $oldDartSdkPrefix = "dart-sdk.old"
25 
26 # Make sure that PowerShell has expected version.
27 $psMajorVersionRequired = 5
28 $psMajorVersionLocal = $PSVersionTable.PSVersion.Major
29 if ($psMajorVersionLocal -lt $psMajorVersionRequired) {
30     Write-Host "Flutter requires PowerShell $psMajorVersionRequired.0 or newer."
31     Write-Host "See https://flutter.dev/docs/get-started/install/windows for more."
32     return
33 }
34 
35 if ((Test-Path $engineStamp) -and ($engineVersion -eq (Get-Content $engineStamp))) {
36     return
37 }
38 
39 Write-Host "Downloading Dart SDK from Flutter engine $engineVersion..."
40 $dartSdkBaseUrl = $Env:FLUTTER_STORAGE_BASE_URL
41 if (-not $dartSdkBaseUrl) {
42     $dartSdkBaseUrl = "https://storage.googleapis.com"
43 }
44 $dartZipName = "dart-sdk-windows-x64.zip"
45 $dartSdkUrl = "$dartSdkBaseUrl/flutter_infra/flutter/$engineVersion/$dartZipName"
46 
47 if (Test-Path $dartSdkPath) {
48     # Move old SDK to a new location instead of deleting it in case it is still in use (e.g. by IntelliJ).
49     $oldDartSdkSuffix = 1
50     while (Test-Path "$cachePath\$oldDartSdkPrefix$oldDartSdkSuffix") { $oldDartSdkSuffix++ }
51     Rename-Item $dartSdkPath "$oldDartSdkPrefix$oldDartSdkSuffix"
52 }
53 New-Item $dartSdkPath -force -type directory | Out-Null
54 $dartSdkZip = "$cachePath\$dartZipName"
55 
56 Try {
57     Import-Module BitsTransfer
58     Start-BitsTransfer -Source $dartSdkUrl -Destination $dartSdkZip
59 }
60 Catch {
61     Write-Host "Downloading the Dart SDK using the BITS service failed, retrying with WebRequest..."
62     # Invoke-WebRequest is very slow when the progress bar is visible - a 28
63     # second download can become a 33 minute download. Disable it with
64     # $ProgressPreference and then restore the original value afterwards.
65     # https://github.com/flutter/flutter/issues/37789
66     $OriginalProgressPreference = $ProgressPreference
67     $ProgressPreference = 'SilentlyContinue'
68     Invoke-WebRequest -Uri $dartSdkUrl -OutFile $dartSdkZip
69     $ProgressPreference = $OriginalProgressPreference
70 }
71 
72 Write-Host "Unzipping Dart SDK..."
73 If (Get-Command 7z -errorAction SilentlyContinue) {
74     # The built-in unzippers are painfully slow. Use 7-Zip, if available.
75     & 7z x $dartSdkZip "-o$cachePath" -bd | Out-Null
76 } ElseIf (Get-Command 7za -errorAction SilentlyContinue) {
77     # Use 7-Zip's standalone version 7za.exe, if available.
78     & 7za x $dartSdkZip "-o$cachePath" -bd | Out-Null
79 } ElseIf (Get-Command Expand-Archive -errorAction SilentlyContinue) {
80     # Use PowerShell's built-in unzipper, if available (requires PowerShell 5+).
81     Expand-Archive $dartSdkZip -DestinationPath $cachePath
82 } Else {
83     # As last resort: fall back to the Windows GUI.
84     $shell = New-Object -com shell.application
85     $zip = $shell.NameSpace($dartSdkZip)
86     foreach($item in $zip.items()) {
87         $shell.Namespace($cachePath).copyhere($item)
88     }
89 }
90 
91 Remove-Item $dartSdkZip
92 $engineVersion | Out-File $engineStamp -Encoding ASCII
93 
94 # Try to delete all old SDKs.
95 Get-ChildItem -Path $cachePath | Where {$_.BaseName.StartsWith($oldDartSdkPrefix)} | Remove-Item -Recurse -ErrorAction SilentlyContinue
96