flutter 3.83 KB
Newer Older
1 2 3 4 5
#!/bin/bash
# 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
# `flutter.bat` script in the same directory to ensure that Flutter continues
11 12 13 14
# to work across all platforms!
#
# -------------------------------------------------------------------------- #

15 16
set -e

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

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

36
PROG_NAME="$(path_uri "$(follow_links "$BASH_SOURCE")")"
37 38 39
BIN_DIR="$(cd "${PROG_NAME%/*}" ; pwd -P)"
export FLUTTER_ROOT="$(cd "${BIN_DIR}/.." ; pwd -P)"

40 41 42
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"
43
SCRIPT_PATH="$FLUTTER_TOOLS_DIR/bin/flutter_tools.dart"
44
DART_SDK_PATH="$FLUTTER_ROOT/bin/cache/dart-sdk"
45

46
DART="$DART_SDK_PATH/bin/dart"
47 48
PUB="$DART_SDK_PATH/bin/pub"

49 50
# Test if running as superuser – but don't warn if running within Docker
if [[ "$EUID" == "0" ]] && ! [[ -f /.dockerenv ]]; then
51 52 53 54 55 56
  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

57 58 59 60 61 62 63 64 65 66
# 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)
if [ ! -d "$FLUTTER_ROOT/.git" ]; then
  echo "Error: The Flutter directory is not a clone of the GitHub project."
  exit 1
fi
67

68 69 70
# To debug the tool, you can uncomment the following line to enable checked mode and set an observatory port:
# FLUTTER_TOOL_ARGS="--observe=65432 --checked"

71 72 73 74 75 76
(
  if hash flock 2>/dev/null; then
    flock 3 # ensures that we don't simultaneously update Dart in multiple parallel instances
    # some platforms (e.g. Mac) don't have flock or any reliable alternative
  fi
  REVISION=`(cd "$FLUTTER_ROOT"; git rev-parse HEAD)`
77
  if [ ! -f "$SNAPSHOT_PATH" ] || [ ! -s "$STAMP_PATH" ] || [ `cat "$STAMP_PATH"` != "$REVISION" ] || [ "$FLUTTER_TOOLS_DIR/pubspec.yaml" -nt "$FLUTTER_TOOLS_DIR/pubspec.lock" ]; then
Dan Rubel's avatar
Dan Rubel committed
78 79 80
    mkdir -p "$FLUTTER_ROOT/bin/cache"
    touch "$FLUTTER_ROOT/bin/cache/.dartignore"
    "$FLUTTER_ROOT/bin/internal/update_dart_sdk.sh"
81 82

    echo Building flutter tool...
83 84 85 86 87 88
    LOCAL_PUB_ENV="$PUB_ENVIRONMENT"
    if [ "$TRAVIS" == "true" ] || [ "$BOT" == "true" ] || [ "$CONTINUOUS_INTEGRATION" == "true" ] || [ "$CHROME_HEADLESS" == "1" ] || [ "$APPVEYOR" == "true" ] || [ "$CI" == "true" ]; then
      LOCAL_PUB_ENV="$LOCAL_PUB_ENV:flutter_bot"
    fi
    LOCAL_PUB_ENV="$LOCAL_PUB_ENV:flutter_install"
    (cd "$FLUTTER_TOOLS_DIR"; PUB_ENVIRONMENT=$LOCAL_PUB_ENV "$PUB" upgrade --verbosity=error --no-packages-dir)
89 90 91
    "$DART" --snapshot="$SNAPSHOT_PATH" --packages="$FLUTTER_TOOLS_DIR/.packages" "$SCRIPT_PATH"
    echo $REVISION > "$STAMP_PATH"
  fi
92
) 3< "$PROG_NAME"
93

94
set +e
95
"$DART" $FLUTTER_TOOL_ARGS "$SNAPSHOT_PATH" "$@"
96 97 98 99 100 101

# The VM exits with code 253 if the snapshot version is out-of-date.
# If it is, we need to snapshot it again.
EXIT_CODE=$?
if [ $EXIT_CODE != 253 ]; then
  exit $EXIT_CODE
Ian Hickson's avatar
Ian Hickson committed
102
fi
103 104

set -e
105
"$DART" --snapshot="$SNAPSHOT_PATH" --packages="$FLUTTER_TOOLS_DIR/.packages" "$SCRIPT_PATH"
106
"$DART" $FLUTTER_TOOL_ARGS "$SNAPSHOT_PATH" "$@"