Cody Schroeder | fe728dc | 2016-01-22 21:48:08 +0000 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # Copyright 2016 The Bazel Authors. All rights reserved. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | # |
| 17 | USAGE='bazel-run.sh [<bazel option>...] <target> [ -- [<target option>]... ]' |
| 18 | DESCRIPTION=' |
| 19 | Builds and runs the command generated by "bazel run" in the calling |
| 20 | terminal. The command is run as a grandchild of the current shell, |
| 21 | not from the Bazel server. Therefore, the program will have a controlling terminal, and |
| 22 | the Bazel lock is released before running the command.' |
| 23 | |
| 24 | function usage() { |
| 25 | echo "$USAGE" "$DESCRIPTION" >&2 |
| 26 | } |
| 27 | |
| 28 | function die() { |
| 29 | echo "$1" |
| 30 | exit 1 |
| 31 | } |
| 32 | |
| 33 | function cleanup() { |
| 34 | rm "$runcmd" |
| 35 | } |
| 36 | |
| 37 | [ $# -gt 0 ] || { usage; exit 1; } |
| 38 | |
| 39 | runcmd="$(mktemp /tmp/bazel-run.XXXXXX)" || die "Could not create tmp file" |
| 40 | trap "cleanup" EXIT |
| 41 | |
| 42 | bazel run --script_path="$runcmd" "$@" || exit $? |
| 43 | [ -x "$runcmd" ] || die "File $runcmd not executable" |
| 44 | |
| 45 | "$runcmd" |
| 46 | |