flutter 7.02 KB
Newer Older
1
#!/usr/bin/env bash
2 3 4 5
# Copyright 2015 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.

6 7 8 9

# ---------------------------------- NOTE ---------------------------------- #
#
# Please keep the logic in this file consistent with the logic in the
10 11
# `flutter.bat` script in the same directory to ensure that Flutter continues
# to work across all platforms!
12 13 14
#
# -------------------------------------------------------------------------- #

15 16
set -e

17 18
unset CDPATH

19
function follow_links() {
20
  cd -P "${1%/*}"
21
  local file="$PWD/${1##*/}"
22
  while [[ -h "$file" ]]; do
23
    # On Mac OS, readlink -f doesn't work.
24
    cd -P "${file%/*}"
25
    file="$(readlink "$file")"
26 27
    cd -P "${file%/*}"
    file="$PWD/${file##*/}"
28
  done
29
  echo "$PWD/${file##*/}"
30 31
}

32 33 34
# Convert a filesystem path to a format usable by Dart's URI parser.
function path_uri() {
  # Reduce multiple leading slashes to a single slash.
35
  echo "$1" | sed -E -e "s,^/+,/,"
36 37
}

38 39 40 41
function _rmlock () {
  [ -n "$FLUTTER_UPGRADE_LOCK" ] && rm -f "$FLUTTER_UPGRADE_LOCK"
}

42 43
function retry_upgrade {
  local total_tries="10"
44 45
  local remaining_tries=$((total_tries - 1))
  while [[ "$remaining_tries" -gt 0 ]]; do
46
    (cd "$FLUTTER_TOOLS_DIR" && "$PUB" upgrade "$VERBOSITY") && break
47
    echo "Error: Unable to 'pub upgrade' flutter tool. Retrying in five seconds... ($remaining_tries tries left)"
48
    remaining_tries=$((remaining_tries - 1))
49 50 51 52 53 54 55 56 57 58
    sleep 5
  done

  if [[ "$remaining_tries" == 0 ]]; then
    echo "Command 'pub upgrade' still failed after $total_tries tries, giving up."
    return 1
  fi
  return 0
}

59
function upgrade_flutter () {
60 61
  mkdir -p "$FLUTTER_ROOT/bin/cache"

62 63 64 65 66 67 68 69 70 71 72 73 74
  # This function is executed with a redirect that pipes the source of
  # this script into file descriptor 3.
  #
  # To ensure that we don't simultaneously update Dart in multiple
  # parallel instances, we try to obtain an exclusive lock on this
  # file descriptor (and thus this script's source file) while we are
  # updating Dart and compiling the script. To do this, we try to use
  # the command line program "flock", which is available on many
  # Unix-like platforms, in particular on most Linux distributions.
  # You give it a file descriptor, and it locks the corresponding
  # file, having inherited the file descriptor from the shell.
  #
  # Complicating matters, there are two major scenarios where this
75
  # will not work.
76
  #
77 78 79 80 81 82
  # The first is if the platform doesn't have "flock", for example on Mac.
  # There is not a direct equivalent, so on platforms that don't have flock,
  # we fall back to using a lockfile and spinlock with "shlock".  This
  # doesn't work as well over NFS as it relies on PIDs. Any platform
  # without either of these tools has no locking at all. To determine if we
  # have "flock" or "shlock" available, we abuse the "hash" shell built-in.
83 84 85 86 87 88 89 90 91 92
  #
  # The second complication is NFS. On NFS, to obtain an exclusive
  # lock you need a file descriptor that is open for writing, because
  # NFS implements exclusive locks by writing, or some such. Thus, we
  # ignore errors from flock. We do so by using the '|| true' trick,
  # since we are running in a 'set -e' environment wherein all errors
  # are fatal, and by redirecting all output to /dev/null, since
  # users will typically not care about errors from flock and are
  # more likely to be confused by them than helped.
  #
93 94 95
  # For "flock", the lock is released when the file descriptor goes out of
  # scope,  i.e. when this function returns.  The lock is released via
  # a trap when using "shlock".
96
  if hash flock 2>/dev/null; then
97
    flock 3 2>/dev/null || true
98 99 100 101
  elif hash shlock 2>/dev/null; then
    FLUTTER_UPGRADE_LOCK="$FLUTTER_ROOT/bin/cache/.upgrade_lock"
    while ! shlock -f "$FLUTTER_UPGRADE_LOCK" -p $$ ; do sleep .1 ; done
    trap _rmlock EXIT
102 103
  fi

104
  local revision="$(cd "$FLUTTER_ROOT"; git rev-parse HEAD)"
105 106 107 108 109 110

  # Invalidate cache if:
  #  * SNAPSHOT_PATH is not a file, or
  #  * STAMP_PATH is not a file with nonzero size, or
  #  * Contents of STAMP_PATH is not our local git HEAD revision, or
  #  * pubspec.yaml last modified after pubspec.lock
111
  if [[ ! -f "$SNAPSHOT_PATH" || ! -s "$STAMP_PATH" || "$(cat "$STAMP_PATH")" != "$revision" || "$FLUTTER_TOOLS_DIR/pubspec.yaml" -nt "$FLUTTER_TOOLS_DIR/pubspec.lock" ]]; then
112
    rm -f "$FLUTTER_ROOT/version"
113 114
    touch "$FLUTTER_ROOT/bin/cache/.dartignore"
    "$FLUTTER_ROOT/bin/internal/update_dart_sdk.sh"
115
    VERBOSITY="--verbosity=error"
116 117

    echo Building flutter tool...
118
    if [[ "$CI" == "true" || "$BOT" == "true" || "$CONTINUOUS_INTEGRATION" == "true" || "$CHROME_HEADLESS" == "1" ]]; then
119
      PUB_ENVIRONMENT="$PUB_ENVIRONMENT:flutter_bot"
120
      VERBOSITY="--verbosity=normal"
121 122 123
    fi
    export PUB_ENVIRONMENT="$PUB_ENVIRONMENT:flutter_install"

124
    if [[ -d "$FLUTTER_ROOT/.pub-cache" ]]; then
125 126 127
      export PUB_CACHE="${PUB_CACHE:-"$FLUTTER_ROOT/.pub-cache"}"
    fi

128 129
    retry_upgrade

130
    "$DART" $FLUTTER_TOOL_ARGS --snapshot="$SNAPSHOT_PATH" --packages="$FLUTTER_TOOLS_DIR/.packages" "$SCRIPT_PATH"
131 132
    echo "$revision" > "$STAMP_PATH"
  fi
133 134 135 136 137
  # The exit here is duplicitous since the function is run in a subshell,
  # but this serves as documentation that running the function in a
  # subshell is required to make sure any lockfile created by shlock
  # is cleaned up.
  exit $?
138 139
}

140
PROG_NAME="$(path_uri "$(follow_links "$BASH_SOURCE")")"
141 142 143
BIN_DIR="$(cd "${PROG_NAME%/*}" ; pwd -P)"
export FLUTTER_ROOT="$(cd "${BIN_DIR}/.." ; pwd -P)"

144 145 146
FLUTTER_TOOLS_DIR="$FLUTTER_ROOT/packages/flutter_tools"
SNAPSHOT_PATH="$FLUTTER_ROOT/bin/cache/flutter_tools.snapshot"
STAMP_PATH="$FLUTTER_ROOT/bin/cache/flutter_tools.stamp"
147
SCRIPT_PATH="$FLUTTER_TOOLS_DIR/bin/flutter_tools.dart"
148
DART_SDK_PATH="$FLUTTER_ROOT/bin/cache/dart-sdk"
149

150
DART="$DART_SDK_PATH/bin/dart"
151 152
PUB="$DART_SDK_PATH/bin/pub"

153
# Test if running as superuser – but don't warn if running within Docker
154
if [[ "$EUID" == "0" && ! -f /.dockerenv ]]; then
155 156 157 158 159 160
  echo "   Woah! You appear to be trying to run flutter as root."
  echo "   We strongly recommend running the flutter tool without superuser privileges."
  echo "  /"
  echo "📎"
fi

161 162 163 164 165 166
# Test if Git is available on the Host
if ! hash git 2>/dev/null; then
  echo "Error: Unable to find git in your PATH."
  exit 1
fi
# Test if the flutter directory is a git clone (otherwise git rev-parse HEAD would fail)
167
if [[ ! -e "$FLUTTER_ROOT/.git" ]]; then
168
  echo "Error: The Flutter directory is not a clone of the GitHub project."
169 170
  echo "       The flutter tool requires Git in order to operate properly;"
  echo "       to set up Flutter, run the following command:"
171
  echo "       git clone -b stable https://github.com/flutter/flutter.git"
172 173
  exit 1
fi
174

175 176
# To debug the tool, you can uncomment the following lines to enable checked mode and set an observatory port:
# FLUTTER_TOOL_ARGS="--checked $FLUTTER_TOOL_ARGS"
177
# FLUTTER_TOOL_ARGS="$FLUTTER_TOOL_ARGS --observe=65432"
178

179
(upgrade_flutter) 3< "$PROG_NAME"
180

181 182
# FLUTTER_TOOL_ARGS isn't quoted below, because it is meant to be considered as
# separate space-separated args.
183
"$DART" $FLUTTER_TOOL_ARGS "$SNAPSHOT_PATH" "$@"