Julio Merino | 75d3d50 | 2017-02-02 15:42:28 +0000 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # Copyright 2017 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 | # |
| 18 | # Generates the Bash completion script for Bazel. |
| 19 | # |
| 20 | # At a minimum, you must pass the --bazel and --output flags to specify the path |
| 21 | # to the Bazel binary to use and the output file to generate. |
| 22 | # |
| 23 | # Callers can customize the completion script by passing additional files with |
| 24 | # the --prepend and --append flags, which are stitched together to generate the |
| 25 | # final completion script. Prepended files can override built-in variables and |
| 26 | # appended files can override built-in functions. |
| 27 | # |
| 28 | |
| 29 | set -e |
| 30 | |
| 31 | die() { |
| 32 | echo "${@}" 1>&2 |
| 33 | exit 1 |
| 34 | } |
| 35 | |
| 36 | get_optarg() { |
aehlig | 672510e | 2017-07-18 16:25:46 +0200 | [diff] [blame] | 37 | expr -- "${1}" : "[^=]*=\\(.*\\)" |
Julio Merino | 75d3d50 | 2017-02-02 15:42:28 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | append= |
| 41 | bazel= |
| 42 | javabase= |
| 43 | output= |
| 44 | prepend= |
| 45 | while [ ${#} -gt 0 ]; do |
| 46 | case "${1}" in |
| 47 | --append=*) append="${append} $(get_optarg "${1}")" ;; |
| 48 | --bazel=*) bazel="$(get_optarg "${1}")" ;; |
| 49 | --javabase=*) javabase="$(get_optarg "${1}")" ;; |
| 50 | --output=*) output="$(get_optarg "${1}")" ;; |
| 51 | --prepend=*) prepend="${prepend} $(get_optarg "${1}")" ;; |
| 52 | --*) die "Unknown option ${1}" ;; |
| 53 | *) break ;; |
| 54 | esac |
| 55 | shift |
| 56 | done |
| 57 | [ ${#} -eq 0 ] || die "No arguments allowed" |
| 58 | [ -n "${bazel}" ] || die "--bazel required but not provided" |
| 59 | [ -n "${output}" ] || die "--output required but not provided" |
| 60 | |
| 61 | tempdir="$(mktemp -d "${TMPDIR:-/tmp}/generate_bash_completion.XXXXXXXX")" |
| 62 | trap "rm -rf '${tempdir}'" EXIT |
| 63 | |
| 64 | touch "${tempdir}/WORKSPACE" |
| 65 | mkdir "${tempdir}/root" |
| 66 | |
| 67 | [ -z "${prepend}" ] || cat ${prepend} >>"${tempdir}/output" |
| 68 | |
cushon | ff6d318 | 2018-09-05 08:51:29 -0700 | [diff] [blame] | 69 | server_javabase_flag= |
| 70 | [ -z "${javabase}" ] || server_javabase_flag="--server_javabase=${javabase}" |
| 71 | "${bazel}" --output_user_root="${tempdir}/root" ${server_javabase_flag} \ |
Julio Merino | 75d3d50 | 2017-02-02 15:42:28 +0000 | [diff] [blame] | 72 | help completion >>"${tempdir}/output" |
| 73 | |
| 74 | [ -z "${append}" ] || cat ${append} >>"${tempdir}/output" |
| 75 | |
| 76 | rm -f "${output}" |
| 77 | mv "${tempdir}/output" "${output}" |