update_dart_sdk.sh 5.62 KB
Newer Older
1
#!/usr/bin/env bash
Ian Hickson's avatar
Ian Hickson committed
2
# Copyright 2014 The Flutter Authors. All rights reserved.
3 4 5
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

6 7 8 9 10 11 12 13 14

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

15 16
set -e

17
FLUTTER_ROOT="$(dirname "$(dirname "$(dirname "${BASH_SOURCE[0]}")")")"
18

19
DART_SDK_PATH="$FLUTTER_ROOT/bin/cache/dart-sdk"
20
DART_SDK_PATH_OLD="$DART_SDK_PATH.old"
21 22
ENGINE_STAMP="$FLUTTER_ROOT/bin/cache/engine-dart-sdk.stamp"
ENGINE_VERSION=`cat "$FLUTTER_ROOT/bin/internal/engine.version"`
23
OS="$(uname -s)"
24

25
if [ ! -f "$ENGINE_STAMP" ] || [ "$ENGINE_VERSION" != `cat "$ENGINE_STAMP"` ]; then
26
  command -v curl > /dev/null 2>&1 || {
27 28
    >&2 echo
    >&2 echo 'Missing "curl" tool. Unable to download Dart SDK.'
29
    case "$OS" in
30
      Darwin)
31
        >&2 echo 'Consider running "brew install curl".'
32 33
        ;;
      Linux)
34
        >&2 echo 'Consider running "sudo apt-get install curl".'
35 36
        ;;
      *)
37
        >&2 echo "Please install curl."
38 39 40 41 42
        ;;
    esac
    echo
    exit 1
  }
43
  command -v unzip > /dev/null 2>&1 || {
44 45
    >&2 echo
    >&2 echo 'Missing "unzip" tool. Unable to extract Dart SDK.'
46
    case "$OS" in
47 48 49 50 51 52 53 54 55 56 57 58 59
      Darwin)
        echo 'Consider running "brew install unzip".'
        ;;
      Linux)
        echo 'Consider running "sudo apt-get install unzip".'
        ;;
      *)
        echo "Please install unzip."
        ;;
    esac
    echo
    exit 1
  }
60

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
  # `uname -m` may be running in Rosetta mode, instead query sysctl
  if [ "$OS" = 'Darwin' ]; then
    # Allow non-zero exit so we can do control flow
    set +e
    # -n means only print value, not key
    QUERY="sysctl -n hw.optional.arm64"
    # Do not wrap $QUERY in double quotes, otherwise the args will be treated as
    # part of the command
    QUERY_RESULT=$($QUERY 2>/dev/null)
    if [ $? -eq 1 ]; then
      # If this command fails, we're certainly not on ARM
      ARCH='x64'
    elif [ "$QUERY_RESULT" = '0' ]; then
      # If this returns 0, we are also not on ARM
      ARCH='x64'
    elif [ "$QUERY_RESULT" = '1' ]; then
      ARCH='arm64'
    else
      >&2 echo "'$QUERY' returned unexpected output: '$QUERY_RESULT'"
      exit 1
    fi
    set -e
  else
    # On x64 stdout is "uname -m: x86_64"
    # On arm64 stdout is "uname -m: aarch64, arm64_v8a"
    case "$(uname -m)" in
      x86_64)
        ARCH="x64"
        ;;
      *)
        ARCH="arm64"
        ;;
    esac
  fi
95

96
  case "$OS" in
97
    Darwin)
98
      DART_ZIP_NAME="dart-sdk-darwin-${ARCH}.zip"
99
      IS_USER_EXECUTABLE="-perm +100"
100 101
      ;;
    Linux)
102
      DART_ZIP_NAME="dart-sdk-linux-${ARCH}.zip"
103
      IS_USER_EXECUTABLE="-perm /u+x"
104
      ;;
105
    MINGW*)
106 107 108
      DART_ZIP_NAME="dart-sdk-windows-x64.zip"
      IS_USER_EXECUTABLE="-perm /u+x"
      ;;
109 110 111 112 113 114
    *)
      echo "Unknown operating system. Cannot install Dart SDK."
      exit 1
      ;;
  esac

115 116
  >&2 echo "Downloading $OS $ARCH Dart SDK from Flutter engine $ENGINE_VERSION..."

117 118 119 120 121 122 123
  # Use the default find if possible.
  if [ -e /usr/bin/find ]; then
    FIND=/usr/bin/find
  else
    FIND=find
  fi

124
  DART_SDK_BASE_URL="${FLUTTER_STORAGE_BASE_URL:-https://storage.googleapis.com}"
125
  DART_SDK_URL="$DART_SDK_BASE_URL/flutter_infra_release/flutter/$ENGINE_VERSION/$DART_ZIP_NAME"
126

127 128 129 130 131 132 133
  # if the sdk path exists, copy it to a temporary location
  if [ -d "$DART_SDK_PATH" ]; then
    rm -rf "$DART_SDK_PATH_OLD"
    mv "$DART_SDK_PATH" "$DART_SDK_PATH_OLD"
  fi

  # install the new sdk
134
  rm -rf -- "$DART_SDK_PATH"
135
  mkdir -m 755 -p -- "$DART_SDK_PATH"
136
  DART_SDK_ZIP="$FLUTTER_ROOT/bin/cache/$DART_ZIP_NAME"
137

138 139 140 141 142 143 144
  # Conditionally set verbose flag for LUCI
  verbose_curl=""
  if [[ -n "$LUCI_CI" ]]; then
    verbose_curl="--verbose"
  fi

  curl ${verbose_curl} --retry 3 --continue-at - --location --output "$DART_SDK_ZIP" "$DART_SDK_URL" 2>&1 || {
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
    curlExitCode=$?
    # Handle range errors specially: retry again with disabled ranges (`--continue-at -` argument)
    # When this could happen:
    # - missing support of ranges in proxy servers
    # - curl with broken handling of completed downloads
    #   This is not a proper fix, but doesn't require any user input
    # - mirror of flutter storage without support of ranges
    #
    # 33  HTTP range error. The range "command" didn't work.
    # https://man7.org/linux/man-pages/man1/curl.1.html#EXIT_CODES
    if [ $curlExitCode != 33 ]; then
      return $curlExitCode
    fi
    curl ${verbose_curl} --retry 3 --location --output "$DART_SDK_ZIP" "$DART_SDK_URL" 2>&1
  } || {
160 161 162 163 164
    >&2 echo
    >&2 echo "Failed to retrieve the Dart SDK from: $DART_SDK_URL"
    >&2 echo "If you're located in China, please see this page:"
    >&2 echo "  https://flutter.dev/community/china"
    >&2 echo
165 166 167
    rm -f -- "$DART_SDK_ZIP"
    exit 1
  }
168
  unzip -o -q "$DART_SDK_ZIP" -d "$FLUTTER_ROOT/bin/cache" || {
169 170 171
    >&2 echo
    >&2 echo "It appears that the downloaded file is corrupt; please try again."
    >&2 echo "If this problem persists, please report the problem at:"
172
    >&2 echo "  https://github.com/flutter/flutter/issues/new?template=1_activation.md"
173
    >&2 echo
174 175 176
    rm -f -- "$DART_SDK_ZIP"
    exit 1
  }
177
  rm -f -- "$DART_SDK_ZIP"
178 179
  $FIND "$DART_SDK_PATH" -type d -exec chmod 755 {} \;
  $FIND "$DART_SDK_PATH" -type f $IS_USER_EXECUTABLE -exec chmod a+x,a+r {} \;
180
  echo "$ENGINE_VERSION" > "$ENGINE_STAMP"
181 182 183 184 185

  # delete any temporary sdk path
  if [ -d "$DART_SDK_PATH_OLD" ]; then
    rm -rf "$DART_SDK_PATH_OLD"
  fi
186
fi