update_dart_sdk.sh 4.15 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

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

61 62 63 64 65 66 67 68 69 70 71
  # 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

72 73
  case "$(uname -s)" in
    Darwin)
74
      DART_ZIP_NAME="dart-sdk-darwin-x64.zip"
75
      IS_USER_EXECUTABLE="-perm +100"
76 77
      ;;
    Linux)
78
      DART_ZIP_NAME="dart-sdk-linux-${ARCH}.zip"
79
      IS_USER_EXECUTABLE="-perm /u+x"
80
      ;;
81
    MINGW*)
82 83 84
      DART_ZIP_NAME="dart-sdk-windows-x64.zip"
      IS_USER_EXECUTABLE="-perm /u+x"
      ;;
85 86 87 88 89 90
    *)
      echo "Unknown operating system. Cannot install Dart SDK."
      exit 1
      ;;
  esac

91 92 93 94 95 96 97
  # Use the default find if possible.
  if [ -e /usr/bin/find ]; then
    FIND=/usr/bin/find
  else
    FIND=find
  fi

98
  DART_SDK_BASE_URL="${FLUTTER_STORAGE_BASE_URL:-https://storage.googleapis.com}"
99
  DART_SDK_URL="$DART_SDK_BASE_URL/flutter_infra_release/flutter/$ENGINE_VERSION/$DART_ZIP_NAME"
100

101 102 103 104 105 106 107
  # 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
108
  rm -rf -- "$DART_SDK_PATH"
109
  mkdir -m 755 -p -- "$DART_SDK_PATH"
110
  DART_SDK_ZIP="$FLUTTER_ROOT/bin/cache/$DART_ZIP_NAME"
111

112 113 114 115 116 117 118
  # 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 || {
119 120 121 122 123
    >&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
124 125 126
    rm -f -- "$DART_SDK_ZIP"
    exit 1
  }
127
  unzip -o -q "$DART_SDK_ZIP" -d "$FLUTTER_ROOT/bin/cache" || {
128 129 130 131 132
    >&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:"
    >&2 echo "  https://github.com/flutter/flutter/issues/new?template=ACTIVATION.md"
    >&2 echo
133 134 135
    rm -f -- "$DART_SDK_ZIP"
    exit 1
  }
136
  rm -f -- "$DART_SDK_ZIP"
137 138
  $FIND "$DART_SDK_PATH" -type d -exec chmod 755 {} \;
  $FIND "$DART_SDK_PATH" -type f $IS_USER_EXECUTABLE -exec chmod a+x,a+r {} \;
139
  echo "$ENGINE_VERSION" > "$ENGINE_STAMP"
140 141 142 143 144

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