blob: 42743f7ae56339bd7c3c91e316c6ac7b0fa06f2f [file] [log] [blame]
Christopher Fujino06cd79d2020-05-18 12:38:09 -07001#!/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
14set -e
15
Greg Spencer379e11b2020-05-27 15:30:46 -070016# Needed because if it is set, cd may print the path it changed to.
Christopher Fujino06cd79d2020-05-18 12:38:09 -070017unset CDPATH
18
Greg Spencer379e11b2020-05-27 15:30:46 -070019# 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.
34function follow_links() (
35 cd -P "$(dirname -- "$1")"
36 file="$PWD/$(basename -- "$1")"
Christopher Fujino06cd79d2020-05-18 12:38:09 -070037 while [[ -h "$file" ]]; do
Greg Spencer379e11b2020-05-27 15:30:46 -070038 cd -P "$(dirname -- "$file")"
39 file="$(readlink -- "$file")"
40 cd -P "$(dirname -- "$file")"
41 file="$PWD/$(basename -- "$file")"
Christopher Fujino06cd79d2020-05-18 12:38:09 -070042 done
Greg Spencer379e11b2020-05-27 15:30:46 -070043 echo "$file"
44)
Christopher Fujino06cd79d2020-05-18 12:38:09 -070045
Greg Spencer6db22112020-06-18 13:29:24 -070046PROG_NAME="$(follow_links "${BASH_SOURCE[0]}")"
Christopher Fujino06cd79d2020-05-18 12:38:09 -070047BIN_DIR="$(cd "${PROG_NAME%/*}" ; pwd -P)"
Greg Spencer6db22112020-06-18 13:29:24 -070048OS="$(uname -s)"
49
Jonah Williamsfac0d262021-03-15 12:08:03 -070050# If we're on Windows, invoke the batch script instead to get proper locking.
51if [[ $OS =~ MINGW.* || $OS =~ CYGWIN.* ]]; then
Greg Spencer6db22112020-06-18 13:29:24 -070052 exec "${BIN_DIR}/dart.bat" "$@"
53fi
Christopher Fujino06cd79d2020-05-18 12:38:09 -070054
55# To define `shared::execute()` function
Christopher Fujino30fed042020-05-29 16:03:02 -070056source "$BIN_DIR/internal/shared.sh"
Christopher Fujino06cd79d2020-05-18 12:38:09 -070057
58shared::execute "$@"