blob: 778810570c47a7a9f67e8b76812328bca75cf518 [file] [log] [blame]
Julio Merino75d3d502017-02-02 15:42:28 +00001#!/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
29set -e
30
31die() {
32 echo "${@}" 1>&2
33 exit 1
34}
35
36get_optarg() {
aehlig672510e2017-07-18 16:25:46 +020037 expr -- "${1}" : "[^=]*=\\(.*\\)"
Julio Merino75d3d502017-02-02 15:42:28 +000038}
39
40append=
41bazel=
42javabase=
43output=
44prepend=
45while [ ${#} -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
56done
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
61tempdir="$(mktemp -d "${TMPDIR:-/tmp}/generate_bash_completion.XXXXXXXX")"
62trap "rm -rf '${tempdir}'" EXIT
63
64touch "${tempdir}/WORKSPACE"
65mkdir "${tempdir}/root"
66
67[ -z "${prepend}" ] || cat ${prepend} >>"${tempdir}/output"
68
cushonff6d3182018-09-05 08:51:29 -070069server_javabase_flag=
70[ -z "${javabase}" ] || server_javabase_flag="--server_javabase=${javabase}"
71"${bazel}" --output_user_root="${tempdir}/root" ${server_javabase_flag} \
Julio Merino75d3d502017-02-02 15:42:28 +000072 help completion >>"${tempdir}/output"
73
74[ -z "${append}" ] || cat ${append} >>"${tempdir}/output"
75
76rm -f "${output}"
77mv "${tempdir}/output" "${output}"