aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Julio Merino <jmmv@google.com>2017-02-02 15:42:28 +0000
committerGravatar Yun Peng <pcloudy@google.com>2017-02-02 17:00:38 +0000
commit75d3d509ce7b01418421d596d10490c5d0baa64c (patch)
tree7d3cb5f007be4720a6a97a36df49be94dcb012fd
parent5b1941417e7cb519b0e1b0b2e6c201c0340af957 (diff)
Make the bash completion generation easier to customize per product, take 2.
Refactor the code to generate the bash completions file by moving the logic to a separate script and keeping the genrule as a dumb invocation of this script. This allows us to more easily customize the rules for Blaze. -- PiperOrigin-RevId: 146362379 MOS_MIGRATED_REVID=146362379
-rw-r--r--scripts/BUILD15
-rwxr-xr-xscripts/generate_bash_completion.sh77
2 files changed, 85 insertions, 7 deletions
diff --git a/scripts/BUILD b/scripts/BUILD
index decf702cfc..7c782cc760 100644
--- a/scripts/BUILD
+++ b/scripts/BUILD
@@ -8,15 +8,16 @@ genrule(
name = "bash_completion",
srcs = ["bazel-complete-template.bash"],
outs = ["bazel-complete.bash"],
- cmd = "cat $(SRCS) > $@\n" +
- "touch err.log\n" +
- "TMP=`mktemp -d $${TMPDIR:-/tmp}/tmp.XXXXXXXXXX`\n" +
- "trap \"rm -fr err.log $${TMP}\" EXIT\n" +
- "$(location //src:bazel) " +
- " --batch --output_user_root=$${TMP} --host_javabase=$(JAVABASE) " +
- " help completion 2>err.log >>$@ || { cat err.log; exit 1; }",
+ cmd = " ".join([
+ "$(location :generate_bash_completion.sh)",
+ "--bazel=$(location //src:bazel)",
+ "--javabase=$(JAVABASE)",
+ "--output=$@",
+ "--prepend=$(location bazel-complete-template.bash)",
+ ]),
output_to_bindir = 1,
tools = [
+ ":generate_bash_completion.sh",
"//src:bazel",
"//tools/defaults:jdk",
],
diff --git a/scripts/generate_bash_completion.sh b/scripts/generate_bash_completion.sh
new file mode 100755
index 0000000000..83851e39ff
--- /dev/null
+++ b/scripts/generate_bash_completion.sh
@@ -0,0 +1,77 @@
+#!/bin/sh
+#
+# Copyright 2017 The Bazel Authors. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+#
+# Generates the Bash completion script for Bazel.
+#
+# At a minimum, you must pass the --bazel and --output flags to specify the path
+# to the Bazel binary to use and the output file to generate.
+#
+# Callers can customize the completion script by passing additional files with
+# the --prepend and --append flags, which are stitched together to generate the
+# final completion script. Prepended files can override built-in variables and
+# appended files can override built-in functions.
+#
+
+set -e
+
+die() {
+ echo "${@}" 1>&2
+ exit 1
+}
+
+get_optarg() {
+ expr "${1}" : "[^=]*=\\(.*\\)"
+}
+
+append=
+bazel=
+javabase=
+output=
+prepend=
+while [ ${#} -gt 0 ]; do
+ case "${1}" in
+ --append=*) append="${append} $(get_optarg "${1}")" ;;
+ --bazel=*) bazel="$(get_optarg "${1}")" ;;
+ --javabase=*) javabase="$(get_optarg "${1}")" ;;
+ --output=*) output="$(get_optarg "${1}")" ;;
+ --prepend=*) prepend="${prepend} $(get_optarg "${1}")" ;;
+ --*) die "Unknown option ${1}" ;;
+ *) break ;;
+ esac
+ shift
+done
+[ ${#} -eq 0 ] || die "No arguments allowed"
+[ -n "${bazel}" ] || die "--bazel required but not provided"
+[ -n "${output}" ] || die "--output required but not provided"
+
+tempdir="$(mktemp -d "${TMPDIR:-/tmp}/generate_bash_completion.XXXXXXXX")"
+trap "rm -rf '${tempdir}'" EXIT
+
+touch "${tempdir}/WORKSPACE"
+mkdir "${tempdir}/root"
+
+[ -z "${prepend}" ] || cat ${prepend} >>"${tempdir}/output"
+
+host_javabase_flag=
+[ -z "${javabase}" ] || host_javabase_flag="--host_javabase=${javabase}"
+"${bazel}" --output_user_root="${tempdir}/root" ${host_javabase_flag} \
+ help completion >>"${tempdir}/output"
+
+[ -z "${append}" ] || cat ${append} >>"${tempdir}/output"
+
+rm -f "${output}"
+mv "${tempdir}/output" "${output}"