-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathbase-wrapper
More file actions
executable file
·234 lines (193 loc) · 6.94 KB
/
base-wrapper
File metadata and controls
executable file
·234 lines (193 loc) · 6.94 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/env bash
#
# base-wrapper
# Canonical Base wrapper for Bash commands.
#
# Supported modes:
# 1. Base-owned command dispatch by convention:
# base-wrapper setup.sh [args...]
# setup.sh [args...] # when setup.sh is a symlink to base-wrapper
#
# 2. Explicit script-path dispatch for wrapped scripts outside Base:
# base-wrapper ./path/to/script.sh [args...]
# ./path/to/script.sh # when the script uses #!/usr/bin/env base-wrapper
#
# In both cases, this wrapper:
# - initializes the shared Base CLI environment
# - preloads the Bash stdlib
# - exposes wrapper metadata through environment variables
# - sources the target command/script in the current shell
#
die() {
printf 'ERROR: %s\n' "$*" >&2
exit 1
}
ensure_supported_bash() {
local current_version="${BASH_VERSINFO[0]}${BASH_VERSINFO[1]}"
local candidate
((current_version >= 42)) && return 0
for candidate in /opt/homebrew/bin/bash /usr/local/bin/bash; do
[[ -x "$candidate" ]] || continue
[[ "$candidate" == "${BASH:-}" ]] && continue
exec "$candidate" "$0" "$@"
done
die "base-wrapper requires Bash 4.2 or newer; current version is ${BASH_VERSINFO[0]}.${BASH_VERSINFO[1]}."
}
resolve_script_path() {
local source_path="${1:-}" link_dir target
[[ -n "$source_path" ]] || return 1
if [[ "$source_path" != */* ]]; then
source_path="$(command -v -- "$source_path" 2>/dev/null || true)"
[[ -n "$source_path" ]] || return 1
fi
while [[ -L "$source_path" ]]; do
link_dir="$(cd -- "$(dirname -- "$source_path")" && pwd -P)"
target="$(readlink "$source_path")"
if [[ "$target" == /* ]]; then
source_path="$target"
else
source_path="$link_dir/$target"
fi
done
link_dir="$(cd -- "$(dirname -- "$source_path")" && pwd -P)"
printf '%s/%s\n' "$link_dir" "$(basename "$source_path")"
}
normalize_command_name() {
local command_name="${1:-}"
if [[ "$command_name" == *.sh && "$command_name" != ".sh" ]]; then
command_name="${command_name%.sh}"
fi
printf '%s\n' "$command_name"
}
list_commands() {
local commands_dir="$1"
local command_dir command_name found=0
[[ -d "$commands_dir" ]] || return 0
while IFS= read -r command_dir; do
[[ -d "$command_dir" ]] || continue
command_name="$(basename "$command_dir")"
if [[ -f "$command_dir/${command_name}.sh" ]]; then
printf ' %s.sh\n' "$command_name"
found=1
fi
done < <(find "$commands_dir" -mindepth 1 -maxdepth 1 -type d | sort)
((found)) || printf ' (none yet)\n'
}
print_usage() {
local wrapper_name="$1"
local commands_dir="$2"
cat <<EOF
Usage:
${wrapper_name} <command> [args...]
${wrapper_name} /path/to/script.sh [args...]
<command>.sh [args...]
Behavior:
- When invoked as 'base-wrapper', the first argument is treated as either:
* a Base command name resolved under cli/bash/commands/<name>/<name>.sh
* a script path when the argument contains a '/'
- Base entrypoint symlinks are expected to end in '.sh'; the wrapper maps '<name>.sh' to command '<name>'.
- When invoked through a symlink, the symlink name is treated as the command name.
Available Base-owned commands:
EOF
list_commands "$commands_dir"
}
resolve_base_paths() {
WRAPPER_PATH="$(resolve_script_path "$0")" || die "Unable to resolve wrapper path for '$0'."
WRAPPER_BIN_DIR="$(cd -- "$(dirname -- "$WRAPPER_PATH")" && pwd -P)"
REPO_ROOT="$(cd -- "$WRAPPER_BIN_DIR/.." && pwd -P)"
CLI_ROOT="$REPO_ROOT/cli"
BASH_ROOT="$CLI_ROOT/bash"
COMMANDS_DIR="$BASH_ROOT/commands"
ENV_SCRIPT="$CLI_ROOT/env/baseenv.sh"
}
resolve_dispatch_target() {
local invoked_as="$1"
local commands_dir="$2"
local script_arg=""
COMMAND_NAME=""
COMMAND_DIR=""
COMMAND_SCRIPT=""
COMMAND_ARGS=()
shift 2
if [[ "$invoked_as" == "base-wrapper" ]]; then
case "${1:-}" in
"" )
print_usage "$invoked_as" "$commands_dir"
exit 1
;;
-h|--help|help )
print_usage "$invoked_as" "$commands_dir"
exit 0
;;
--list|list )
list_commands "$commands_dir"
exit 0
;;
esac
if [[ "${1:-}" == */* ]]; then
script_arg="$1"
shift 1
else
COMMAND_NAME="$1"
shift 1
fi
else
COMMAND_NAME="$invoked_as"
fi
if [[ -n "$script_arg" ]]; then
COMMAND_SCRIPT="$(resolve_script_path "$script_arg")" || die "Wrapped script '$script_arg' was not found."
[[ -f "$COMMAND_SCRIPT" ]] || die "Wrapped script '$script_arg' was not found."
COMMAND_NAME="$(normalize_command_name "$(basename "$COMMAND_SCRIPT")")"
COMMAND_DIR="$(cd -- "$(dirname -- "$COMMAND_SCRIPT")" && pwd -P)"
else
COMMAND_NAME="$(normalize_command_name "$COMMAND_NAME")"
case "$COMMAND_NAME" in
""|.|..|*/* )
die "Invalid command name '$COMMAND_NAME'."
;;
esac
COMMAND_DIR="$commands_dir/$COMMAND_NAME"
if [[ -f "$COMMAND_DIR/${COMMAND_NAME}.sh" ]]; then
COMMAND_SCRIPT="$COMMAND_DIR/${COMMAND_NAME}.sh"
else
die "Command '$COMMAND_NAME' was not found under '$COMMAND_DIR'."
fi
fi
COMMAND_ARGS=("$@")
}
load_base_environment() {
[[ -f "$ENV_SCRIPT" ]] || die "Required environment bootstrap '$ENV_SCRIPT' was not found."
source "$ENV_SCRIPT" || die "Failed to initialize Base CLI environment from '$ENV_SCRIPT'."
BASH_ROOT="${BASE_BASH_ROOT:-$BASH_ROOT}"
CLI_ROOT="${BASE_CLI_ROOT:-$CLI_ROOT}"
REPO_ROOT="${BASE_REPO_ROOT:-$REPO_ROOT}"
BASH_BIN_DIR="${BASE_BASH_BIN_DIR:-$CLI_ROOT/bash/bin}"
BASH_LIB_DIR="${BASE_BASH_LIB_DIR:-$REPO_ROOT/lib/bash}"
COMMANDS_DIR="${BASE_BASH_COMMANDS_DIR:-$COMMANDS_DIR}"
ENV_SCRIPT="${BASE_CLI_ENV_SCRIPT:-$ENV_SCRIPT}"
BASE_REPO_ROOT="$REPO_ROOT"
BASE_CLI_ROOT="$CLI_ROOT"
BASE_BASH_ROOT="$BASH_ROOT"
BASE_BASH_BIN_DIR="$BASH_BIN_DIR"
BASE_BASH_COMMANDS_DIR="$COMMANDS_DIR"
BASE_CLI_ENV_SCRIPT="$ENV_SCRIPT"
BASE_BASH_COMMAND_NAME="$COMMAND_NAME"
BASE_BASH_COMMAND_DIR="$COMMAND_DIR"
BASE_BASH_COMMAND_SCRIPT="$COMMAND_SCRIPT"
STDLIB_PATH="$BASH_LIB_DIR/std/lib_std.sh"
[[ -f "$STDLIB_PATH" ]] || die "Required stdlib '$STDLIB_PATH' was not found."
}
main() {
local invoked_as
ensure_supported_bash "$@"
invoked_as="$(basename "$0")"
resolve_base_paths
resolve_dispatch_target "$invoked_as" "$COMMANDS_DIR" "$@"
set -- "${COMMAND_ARGS[@]}"
load_base_environment
BASE_BASH_BOOTSTRAP_SOURCE="$COMMAND_SCRIPT"
source "$STDLIB_PATH"
unset BASE_BASH_BOOTSTRAP_SOURCE
source "$COMMAND_SCRIPT" "$@"
}
main "$@"