blob: c45e411be8829d452d3e453a0c176f68f256b020 [file] [log] [blame]
#!/bin/bash
# Copyright 2018 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=Software delivery
### build Fuchsia and push to device
## usage: fx run [--no-build|[NINJA OPTION,...]] [--no-pkg-check] [--no-kill] TARGET [ARG,...]
##
## Build ALL targets.
## Execute 'fx shell killall TARGET'.
## Execute 'fx shell run TARGET [ARGS].
##
## --no-build do not execute a build
## --no-pkg-check do not check for a package server
## --no-kill do not kill all instances of matching components
##
## NINJA OPTION
## The following ninja options are passed to the build if invoked:
## -C, -f, -j, -k, -l, -t, -w.
## See fx ninja --help for more information on these flags.
##
## TARGET is any form that 'run' on the target device accepts, as such it may
## be a package url, or a non-ambiguous short-form.
## ARG is passed to target without interpretation. All arguments after TARGET
## are passed to TARGET.
set -e
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/lib/vars.sh || exit $?
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/lib/updates.sh || exit $?
fx-config-read
function main {
local ninja_args=()
local targets=()
local build=true
local pkg_check=true
local kill_components=true
while (($#)); do
case "$1" in
--no-build)
build=false
;;
--no-pkg-check)
pkg_check=false
;;
--no-kill)
kill_components=false
;;
-C|-f|-j|-k|-l|-t|-w)
ninja_args+=("$1")
shift
ninja_args+=("$1")
;;
-n|-v)
ninja_args+=("$1")
;;
*)
target+=("$1")
shift
break
esac
shift
done
if $build; then
fx-command-run build "${ninja_args[@]}"
fi
if $pkg_check; then
check-for-package-server || return 1
fi
local r=0
# As running components are typically named after their cmx, the
# basename here avoids the case where a user has provided a full
# URL such as `fx run
# fuchsia-pkg://fuchsia.com/rolldice#meta/rolldice.cmx`. This
# strategy could be improved if implemented in a more featureful
# environment.
if $kill_components; then
echo >&2 "Stopping ${target} with killall $(basename "${target}") ..."
fx-command-run shell killall "$(basename "${target}")" || true
fi
echo >&2 "Running ${target} with run "${target}" ..."
fx-command-run shell run "${target}" "$@"
((r += $?))
echo >&2
fx-info "If this component exposes protocols, you may want to restart its clients."
fx-info "Use `ffx session restart` to restart the session or `ffx component reload`"
fx-info "to reload a component."
return r
}
main "$@"