-
Notifications
You must be signed in to change notification settings - Fork 1
/
subzelle.bash.in
146 lines (129 loc) · 4.75 KB
/
subzelle.bash.in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env bash
# 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.
@@GENERATED_MESSAGE@@
set -euo pipefail
# env
# pwd
# find .
RUNNER_LABEL=@@RUNNER_LABEL@@
GAZELLE_SHORT_PATH=@@GAZELLE_SHORT_PATH@@
GAZELLE_LABEL=@@GAZELLE_LABEL@@
ARGS=@@ARGS@@
GOTOOL=@@GOTOOL@@
export SUBZELLE_PLUGIN_NAME=@@SUBZELLE_PLUGIN_NAME@@
export SUBZELLE_PLUGIN_ADDRESS=@@SUBZELLE_PLUGIN_ADDRESS@@
export SUBZELLE_PLUGIN_PATH=@@SUBZELLE_PLUGIN_PATH@@
# find_runfile prints the location of a runfile in the source workspace,
# either by reading the symbolic link or reading the runfiles manifest.
function find_runfile {
local runfile=$1
if [ -f "$runfile" ]; then
readlink "$runfile"
return
fi
runfile=$(echo "$runfile" | sed -e 's!^\(\.\./\|external/\)!!')
if grep -q "^$runfile" MANIFEST; then
grep "^$runfile" MANIFEST | head -n 1 | cut -d' ' -f2
return
fi
# printing nothing indicates failure
}
# bazel_build_get_path builds a given target and prints the absolute path
# to the generated binary. This only works for rules that produce a single file.
function bazel_build_get_path {
local build_log=$(mktemp gazelle_build.XXXX.json --tmpdir)
bazel build --build_event_json_file="$build_log" "$1"
grep "^{\"id\":{\"targetCompleted\":{\"label\":\"$1\"" "$build_log" | \
sed -e 's!^.*file://\([^"]*\).*$!\1!'
rm -f "$build_log"
}
# set_goroot attempts to set GOROOT to the SDK used by rules_go. gazelle
# invokes tools inside the Go SDK for dependency management. It's good to
# use the SDK used by the workspace in case the Go SDK is not installed
# on the host system or is a different version.
function set_goroot {
local gotool=$(find_runfile "$GOTOOL")
if [ -z "$gotool" ]; then
echo "$0: warning: could not locate GOROOT used by rules_go" >&2
return
fi
export GOROOT=$(cd "$(dirname "$gotool")/.."; pwd)
if type cygpath >/dev/null 2>&1; then
# On Windows, convert the path to something usable outside of bash.
GOROOT=$(cygpath -w "$GOROOT")
fi
}
# set_pluginroot attempts to set pluginROOT to the SDK used by rules_go. gazelle
# invokes tools inside the Go SDK for dependency management. It's good to
# use the SDK used by the workspace in case the Go SDK is not installed
# on the host system or is a different version.
function set_pluginroot {
local tool=$(realpath "./$SUBZELLE_PLUGIN_PATH")
if [ -z "$tool" ]; then
echo "$0: warning: could not locate SUBZELLE_PLUGIN_ROOT used by subzelle" >&2
return
fi
# export SUBZELLE_PLUGIN_ROOT=$(cd "$(dirname "$tool")/.."; pwd)
export SUBZELLE_PLUGIN_ABSPATH="$tool"
if type cygpath >/dev/null 2>&1; then
# On Windows, convert the path to something usable outside of bash.
SUBZELLE_PLUGIN_ABSPATH=$(cygpath -w "$SUBZELLE_PLUGIN_ABSPATH")
fi
}
# Check whether the script was executed by Bazel. The gazelle macro prepends
# an argument that tells us this.
is_bazel_run=false
if [ "${1-}" = "-bazel_run" ]; then
is_bazel_run=true
shift
fi
# If arguments were provided on the command line, either replace or augment
# the generated args.
if [ "${1-}" = "-args" ]; then
shift
ARGS+=("$@")
elif [ $# -ne 0 ]; then
ARGS=("$@")
fi
if [ "$is_bazel_run" = true ]; then
# If the script was invoked by "bazel run", jump out of the execroot, into
# the workspace before running Gazelle.
# TODO(jayconrod): detect when a command can't be run this way.
set_goroot
set_pluginroot
# set_runfiles
gazelle_short_path=$(find_runfile "$GAZELLE_SHORT_PATH")
if [ -z "$gazelle_short_path" ]; then
echo "error: could not locate gazelle binary" >&2
exit 1
fi
if [ -z "${BUILD_WORKSPACE_DIRECTORY-}" ]; then
echo "error: BUILD_WORKSPACE_DIRECOTRY not set" >&2
exit 1
fi
cd "$BUILD_WORKSPACE_DIRECTORY"
"$gazelle_short_path" "${ARGS[@]}"
else
# If the script was invoked directly, check whether the script is out of
# date before proceeding.
new_runner_script=$(bazel_build_get_path "$RUNNER_LABEL")
if ! diff "$new_runner_script" "$0" &>/dev/null; then
cat - >&2 <<EOF
error: $0: script is out of date. Refresh it with the command:
bazel build $RUNNER_LABEL && cp -fv "$new_runner_script" "$0"
EOF
exit 1
fi
# Rebuild and run Gazelle.
gazelle_exe=$(bazel_build_get_path "$GAZELLE_LABEL")
"$gazelle_exe" "${ARGS[@]}"
fi