blob: 571e674e9679ca59484515a05d84bccf42b25b11 [file] [log] [blame]
#!/bin/bash
# Copyright 2019 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#### CATEGORY=Run, inspect and debug
### run fuchsia devtools
## usage: fx fdt [VERSION]
## VERSION Download (if necessary) and run a specific version of
## Fuchsia devtools. If not specified, the current version
## in //prebuilt will be used.
## VERSION is either a CIPD hash or "latest".
# shellcheck source=/dev/null
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/lib/vars.sh || exit $?
set -e
# Fills $FX_DEVICE_NAME, $FX_DEVICE_ADDR, $FX_SSH_ADDR, and $FX_SSH_PORT if
# there is one device currently attached.
fx-export-device-address
PREBUILT_DIR="$FUCHSIA_DIR/prebuilt/gui_tools"
DOWNLOAD_DIR="$FUCHSIA_DIR/prebuilt/gui_tools/download"
if [[ $HOST_PLATFORM == "mac-x64" ]]; then
BINARY="Fuchsia DevTools.app"
ZIP="macos.zip"
ZIP_OLD="macos/macos.zip"
DOWNLOAD_ZIP="$DOWNLOAD_DIR/$ZIP"
DOWNLOAD_ZIP_OLD="$DOWNLOAD_DIR/$ZIP_OLD"
DOWNLOAD_UNZIP_DIR="$DOWNLOAD_DIR/fuchsia_devtools/macos-extracted"
DOWNLOAD="$DOWNLOAD_UNZIP_DIR/$BINARY"
PREBUILT_ZIP="$PREBUILT_DIR/$ZIP"
PREBUILT_ZIP_OLD="$PREBUILT_DIR/$ZIP_OLD"
PREBUILT_UNZIP_DIR="$PREBUILT_DIR/fuchsia_devtools/macos-extracted"
PREBUILT="$PREBUILT_UNZIP_DIR/$BINARY"
else
BINARY_OLD="linux/fuchsia_devtools"
PREBUILT_OLD="$PREBUILT_DIR/$BINARY_OLD"
DOWNLOAD_OLD="$DOWNLOAD_DIR/$BINARY_OLD"
BINARY="fuchsia_devtools"
PREBUILT="$PREBUILT_DIR/$BINARY"
DOWNLOAD="$DOWNLOAD_DIR/$BINARY"
fi
# Use prebuilt version if no arguments are passed.
if [[ $# == 0 ]]; then
if [[ $HOST_PLATFORM == "mac-x64" ]]; then
temp_dir="${PREBUILT_UNZIP_DIR}-temp"
# On the mac if we have a prebuilt zip then we are good to go.
if [[ -e "${PREBUILT_ZIP}" ]]; then
rm -rf "${PREBUILT_UNZIP_DIR}"
mkdir -p "${temp_dir}"
if ! unzip -qq "${PREBUILT_ZIP}" -d "${temp_dir}"; then
rm -rf "${temp_dir}"
echo "Prebuilt archive failed to extract."
exit 1
fi
mv "${temp_dir}" "${PREBUILT_UNZIP_DIR}"
open "$PREBUILT"
exit 0
elif [[ -e "${PREBUILT_ZIP_OLD}" ]]; then
rm -rf "${PREBUILT_UNZIP_DIR}"
mkdir -p "${temp_dir}"
if ! unzip -qq "${PREBUILT_ZIP_OLD}" -d "${temp_dir}"; then
rm -rf "${temp_dir}"
echo "Prebuilt archive failed to extract."
exit 1
fi
mv "${temp_dir}" "${PREBUILT_UNZIP_DIR}"
open "$PREBUILT"
exit 0
fi
else
# else we are on linux
# If the new prebuilt binary exists, run it.
if [[ -x "$PREBUILT" ]]; then
# Maybe add ` > /dev/null 2>&1 &` in the future.
exec "$PREBUILT"
# If the old prebuilt binary exists, run it.
elif [[ $HOST_PLATFORM == "linux-x64" ]] && [[ -x "$PREBUILT_OLD" ]]; then
# Maybe add ` > /dev/null 2>&1 &` in the future.
exec "$PREBUILT_OLD"
fi
fi
# We are on mac and don't have a zip file or we are linux and are missing
# a binary with either the old or new name. This should only happen if the
# jiri fetch for the prebuilt is not configured properly or has not run.
cat << end
To use fdt, JIRI should download it. Please run the following commands:
jiri init -fetch-optional=fuchsia_devtools && jiri fetch-packages --local-manifest=true
end
exit 1;
fi
# If a specific version of FDT is requested, it can be downloaded directly.
# Check that the fuchsia_devtools are downloaded and then run it.
# If the version is already downloaded, it will not be re-downloaded.
# Using "latest" or a CIPD hash.
version="$1"
shift
# Check fuchsia/gui_tools/... first as that is the new location
# shellcheck disable=SC2016
if ! echo 'fuchsia/gui_tools/fuchsia_devtools/${platform}' "$version" | cipd ensure -ensure-file - -root "$DOWNLOAD_DIR"; then
# Fallback to checking fuchsia_internal/gui_tools...
# shellcheck disable=SC2016
if ! echo 'fuchsia_internal/gui_tools/fuchsia_devtools/${platform}' "$version" | cipd ensure -ensure-file - -root "$DOWNLOAD_DIR"; then
echo "Failed to download $version."
exit 1
fi
fi
if [[ $HOST_PLATFORM == "mac-x64" ]]; then
rm -rf "${DOWNLOAD_UNZIP_DIR}"
temp_dir="${DOWNLOAD_UNZIP_DIR}-temp"
if [[ -e "${DOWNLOAD_ZIP}" ]]; then
mkdir -p "${temp_dir}"
if ! unzip -qq "${DOWNLOAD_ZIP}" -d "${temp_dir}"; then
rm -rf "${temp_dir}"
echo "Downloaded archive for $version failed to extract."
exit 1
fi
mv "${temp_dir}" "${DOWNLOAD_UNZIP_DIR}"
open "$DOWNLOAD"
elif [[ -e "${DOWNLOAD_ZIP_OLD}" ]]; then
mkdir -p "${temp_dir}"
if ! unzip -qq "${DOWNLOAD_ZIP_OLD}" -d "${temp_dir}"; then
rm -rf "${temp_dir}"
echo "Downloaded archive for $version failed to extract."
exit 1
fi
mv "${temp_dir}" "${DOWNLOAD_UNZIP_DIR}"
open "$DOWNLOAD"
fi
else
# In the future, we should add the following to eliminate the terminal spam:
# > /dev/null 2>&1 &
# For the near term, using this for debug output is important.
if [[ -x "$DOWNLOAD" ]]; then
"$DOWNLOAD" "$@"
elif [[ -x "$DOWNLOAD_OLD" ]]; then
"$DOWNLOAD_OLD" "$@"
else
echo "Failed to find Fuchsia DevTools binary."
exit 1
fi
fi