Christopher Fujino | 06cd79d | 2020-05-18 12:38:09 -0700 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | # Copyright 2014 The Flutter Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | # ---------------------------------- NOTE ---------------------------------- # |
| 7 | # |
| 8 | # Please keep the logic in this file consistent with the logic in the |
| 9 | # `dart.bat` script in the same directory to ensure that Flutter & Dart continue |
| 10 | # to work across all platforms! |
| 11 | # |
| 12 | # -------------------------------------------------------------------------- # |
| 13 | |
| 14 | set -e |
| 15 | |
Greg Spencer | 379e11b | 2020-05-27 15:30:46 -0700 | [diff] [blame] | 16 | # Needed because if it is set, cd may print the path it changed to. |
Christopher Fujino | 06cd79d | 2020-05-18 12:38:09 -0700 | [diff] [blame] | 17 | unset CDPATH |
| 18 | |
Greg Spencer | 379e11b | 2020-05-27 15:30:46 -0700 | [diff] [blame] | 19 | # On Mac OS, readlink -f doesn't work, so follow_links traverses the path one |
| 20 | # link at a time, and then cds into the link destination and find out where it |
| 21 | # ends up. |
| 22 | # |
| 23 | # The returned filesystem path must be a format usable by Dart's URI parser, |
| 24 | # since the Dart command line tool treats its argument as a file URI, not a |
| 25 | # filename. For instance, multiple consecutive slashes should be reduced to a |
| 26 | # single slash, since double-slashes indicate a URI "authority", and these are |
| 27 | # supposed to be filenames. There is an edge case where this will return |
| 28 | # multiple slashes: when the input resolves to the root directory. However, if |
| 29 | # that were the case, we wouldn't be running this shell, so we don't do anything |
| 30 | # about it. |
| 31 | # |
| 32 | # The function is enclosed in a subshell to avoid changing the working directory |
| 33 | # of the caller. |
| 34 | function follow_links() ( |
| 35 | cd -P "$(dirname -- "$1")" |
| 36 | file="$PWD/$(basename -- "$1")" |
Christopher Fujino | 06cd79d | 2020-05-18 12:38:09 -0700 | [diff] [blame] | 37 | while [[ -h "$file" ]]; do |
Greg Spencer | 379e11b | 2020-05-27 15:30:46 -0700 | [diff] [blame] | 38 | cd -P "$(dirname -- "$file")" |
| 39 | file="$(readlink -- "$file")" |
| 40 | cd -P "$(dirname -- "$file")" |
| 41 | file="$PWD/$(basename -- "$file")" |
Christopher Fujino | 06cd79d | 2020-05-18 12:38:09 -0700 | [diff] [blame] | 42 | done |
Greg Spencer | 379e11b | 2020-05-27 15:30:46 -0700 | [diff] [blame] | 43 | echo "$file" |
| 44 | ) |
Christopher Fujino | 06cd79d | 2020-05-18 12:38:09 -0700 | [diff] [blame] | 45 | |
Greg Spencer | 6db2211 | 2020-06-18 13:29:24 -0700 | [diff] [blame] | 46 | PROG_NAME="$(follow_links "${BASH_SOURCE[0]}")" |
Christopher Fujino | 06cd79d | 2020-05-18 12:38:09 -0700 | [diff] [blame] | 47 | BIN_DIR="$(cd "${PROG_NAME%/*}" ; pwd -P)" |
Greg Spencer | 6db2211 | 2020-06-18 13:29:24 -0700 | [diff] [blame] | 48 | OS="$(uname -s)" |
| 49 | |
Jonah Williams | fac0d26 | 2021-03-15 12:08:03 -0700 | [diff] [blame] | 50 | # If we're on Windows, invoke the batch script instead to get proper locking. |
| 51 | if [[ $OS =~ MINGW.* || $OS =~ CYGWIN.* ]]; then |
Greg Spencer | 6db2211 | 2020-06-18 13:29:24 -0700 | [diff] [blame] | 52 | exec "${BIN_DIR}/dart.bat" "$@" |
| 53 | fi |
Christopher Fujino | 06cd79d | 2020-05-18 12:38:09 -0700 | [diff] [blame] | 54 | |
| 55 | # To define `shared::execute()` function |
Christopher Fujino | 30fed04 | 2020-05-29 16:03:02 -0700 | [diff] [blame] | 56 | source "$BIN_DIR/internal/shared.sh" |
Christopher Fujino | 06cd79d | 2020-05-18 12:38:09 -0700 | [diff] [blame] | 57 | |
| 58 | shared::execute "$@" |