forked from dodola/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fx
executable file
·216 lines (189 loc) · 5.47 KB
/
fx
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/bin/bash
# Copyright 2017 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
function get_build_dir {
(source "${vars_sh}" && fx-config-read-if-present && echo "${FUCHSIA_BUILD_DIR}")
}
function commands {
local cmds="$(ls "${fuchsia_dir}/scripts/devshell" | grep -v lib)"
local newline=$'\n'
local build_dir=$(get_build_dir)
if [[ -n "${build_dir}" ]]; then
cmds="${cmds}${newline}$(ls "${build_dir}/tools" 2>/dev/null)"
fi
for tool in ${buildtools_whitelist}; do
cmds="${cmds}${newline}${tool}"
done
echo "$(echo "${cmds}" | sort)"
}
function find_command {
local cmd=$1
local command_path="${fuchsia_dir}/scripts/devshell/${cmd}"
if [[ -x "${command_path}" ]]; then
echo "${command_path}"
return 0
fi
local build_dir=$(get_build_dir)
if [[ -n "${build_dir}" ]]; then
local command_path="${build_dir}/tools/${cmd}"
if [[ -x "${command_path}" ]]; then
echo "${command_path}"
return 0
fi
fi
for tool in ${buildtools_whitelist}; do
if [[ "$cmd" != "$tool" ]]; then
continue
fi
local command_path="${fuchsia_dir}/buildtools/${tool}"
if [[ -x "${command_path}" ]]; then
echo "${command_path}"
return 0
fi
done
return 1
}
function help {
local cmd="$1"
if [[ -z "${cmd}" ]]; then
for cmd in $(commands); do
local cmd_path="$(find_command "${cmd}")"
if [[ $(file -b --mime "${cmd_path}" | cut -d / -f 1) == "text" ]]; then
echo "${cmd} | $(sed -n '1,/^###/s/^### //p' < "${cmd_path}")"
else
echo "${cmd}"
fi
done | column -t -s '|' -c 2
else
local cmd_path="$(find_command "${cmd}")"
if [[ -z "$cmd_path" ]]; then
echo "Command not found"
elif [[ $(file -b --mime "${cmd_path}" | cut -d / -f 1) == "text" ]]; then
fx-print-command-help "${cmd_path}"
else
echo "No help found. Try \`fx ${cmd} -h\`"
fi
fi
}
function usage {
cat <<END
usage: fx [--config CONFIG_FILE | --dir BUILD_DIR] [-x] COMMAND [...]
Run Fuchsia development commands. Must be run with either a current working
directory that is contained in a Fuchsia source tree or the FUCHSIA_DIR
environment variable set to the root of a Fuchsia source tree.
commands:
$(help)
optional arguments:
--config=CONFIG_FILE Path to the config file use when running COMMAND.
Defaults to FUCHSIA_CONFIG if set in the
environment and //.config otherwise. The config
file determines which build directory (and therefore
build configuration) is used by COMMAND.
--dir=BUILD_DIR Path to the build directory to use when running COMMAND.
If specified, FILE is ignored.
-x Print commands and their arguments as they are executed.
optional shell extensions:
fx-go
fx-update-path
fx-set-prompt
To use these shell extensions, first source fx-env.sh into your shell:
$ source scripts/fx-env.sh
END
}
buildtools_whitelist="gn ninja"
fuchsia_dir="${FUCHSIA_DIR}"
if [[ -z "${fuchsia_dir}" ]]; then
# We walk the parent directories looking for .jiri_root rather than using
# BASH_SOURCE so that we find the fuchsia_dir enclosing the current working
# directory instead of the one containing this file in case the user has
# multiple Fuchsia source trees and is picking up this file from another one.
fuchsia_dir="$(pwd)"
while [[ ! -d "${fuchsia_dir}/.jiri_root" ]]; do
fuchsia_dir="$(dirname "${fuchsia_dir}")"
if [[ "${fuchsia_dir}" == "/" ]]; then
echo >& 2 "error: Cannot find Fuchsia source tree containing $(pwd)"
exit 1
fi
done
fi
declare -r vars_sh="${fuchsia_dir}/scripts/devshell/lib/vars.sh"
source "${vars_sh}"
while [[ $# -ne 0 ]]; do
case $1 in
--config=*|--dir=*)
# Turn --switch=value into --switch value.
arg="$1"
shift
set -- "${arg%%=*}" "${arg#*=}" "$@"
continue
;;
--config)
if [[ $# -lt 2 ]]; then
usage
echo >&2 "ERROR: Missing path to config file for --config argument"
exit 1
fi
shift # Removes --config.
export FUCHSIA_CONFIG="$1"
;;
--dir)
if [[ $# -lt 2 ]]; then
usage
echo >&2 "ERROR: Missing path to build directory for --dir argument"
exit 1
fi
shift # Removes --dir.
export FUCHSIA_BUILD_DIR="$1"
if [[ "$FUCHSIA_BUILD_DIR" == //* ]]; then
FUCHSIA_BUILD_DIR="${fuchsia_dir}/${FUCHSIA_BUILD_DIR#//}"
fi
if [[ ! -d "$FUCHSIA_BUILD_DIR" ]]; then
echo >&2 "ERROR: Build directory $FUCHSIA_BUILD_DIR does not exist"
exit 1
fi
# This tells fx-config-read not to use the file.
export FUCHSIA_CONFIG=-
;;
-x)
export FUCHSIA_DEVSHELL_VERBOSITY=1
;;
--)
shift
break
;;
help)
if [[ $# -gt 1 ]]; then
shift
help "$@"
exit
else
usage
exit 1
fi
;;
-*)
usage
echo >& 2 "error: Unknown global argument $1"
exit 1
;;
*)
break
;;
esac
shift
done
if [[ $# -lt 1 ]]; then
usage
echo >& 2 "error: Missing command name"
exit 1
fi
command_name="$1"
command_path="$(find_command ${command_name})"
if [[ $? -ne 0 ]]; then
usage
echo >& 2 "error: Unknown command ${command_name}"
exit 1
fi
shift # Removes the command name.
exec "${command_path}" "$@"