-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·385 lines (328 loc) · 12.3 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·385 lines (328 loc) · 12.3 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/usr/bin/env bash
# Base shell standards require explicit error handling instead of shell strict mode.
BASE_DEFAULT_HOMEBREW_INSTALLER_URL="https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh"
install_usage() {
cat <<'EOF'
Usage:
install.sh [options]
Options:
--dir <path> Install or update Base at this path. Defaults to ~/work/base.
--repo-url <url> Git repository URL to clone. Defaults to https://github.com/basefoundry/base.git.
--branch <name> Clone a specific branch when installing into a new directory.
--no-profile Skip basectl update-profile after setup.
--dry-run Print planned actions without making changes.
-h, --help Show this help text.
Install or update Base, run basectl setup, and optionally update shell startup files.
EOF
}
install_log() {
printf '%s\n' "$*"
}
install_die() {
printf 'ERROR: %s\n' "$*" >&2
exit 1
}
install_expand_path() {
local path="$1"
case "$path" in
"~") printf '%s\n' "$HOME" ;;
"~/"*) printf '%s/%s\n' "$HOME" "${path#"~/"}" ;;
*) printf '%s\n' "$path" ;;
esac
}
install_run() {
if [[ "${BASE_INSTALL_DRY_RUN:-false}" == "true" ]]; then
printf '[DRY-RUN] Would run:'
printf ' %q' "$@"
printf '\n'
return 0
fi
"$@"
}
install_bash_version_number() {
printf '%s\n' "${BASE_INSTALL_TEST_BASH_VERSION:-${BASH_VERSINFO[0]}${BASH_VERSINFO[1]}}"
}
install_find_supported_bash() {
local candidate
local candidates="${BASE_INSTALL_BASH_CANDIDATES:-/opt/homebrew/bin/bash:/usr/local/bin/bash}"
local current_version
local -a candidate_paths
current_version="$(install_bash_version_number)"
if [[ "$current_version" -ge 42 ]]; then
printf '%s\n' "${BASH:-bash}"
return 0
fi
IFS=: read -ra candidate_paths <<< "$candidates"
for candidate in "${candidate_paths[@]}"; do
[[ -n "$candidate" ]] || continue
[[ -x "$candidate" ]] || continue
printf '%s\n' "$candidate"
return 0
done
return 1
}
install_find_brew() {
local candidate
local candidates="${BASE_INSTALL_BREW_CANDIDATES:-/opt/homebrew/bin/brew:/usr/local/bin/brew}"
local -a candidate_paths
if [[ -n "${BASE_INSTALL_BREW_BIN:-}" && -x "${BASE_INSTALL_BREW_BIN:-}" ]]; then
printf '%s\n' "$BASE_INSTALL_BREW_BIN"
return 0
fi
if command -v brew >/dev/null 2>&1; then
command -v brew
return 0
fi
IFS=: read -ra candidate_paths <<< "$candidates"
for candidate in "${candidate_paths[@]}"; do
[[ -n "$candidate" ]] || continue
[[ -x "$candidate" ]] || continue
printf '%s\n' "$candidate"
return 0
done
return 1
}
install_homebrew_pinned_selected() {
[[ -n "${BASE_HOMEBREW_INSTALLER_URL+x}" ||
-n "${BASE_INSTALL_HOMEBREW_INSTALLER_URL+x}" ||
-n "${BASE_HOMEBREW_INSTALLER_SHA256+x}" ||
-n "${BASE_INSTALL_HOMEBREW_INSTALLER_SHA256+x}" ]]
}
install_homebrew_pinned_url_selected() {
[[ -n "${BASE_HOMEBREW_INSTALLER_URL+x}" ||
-n "${BASE_INSTALL_HOMEBREW_INSTALLER_URL+x}" ]]
}
install_homebrew_pinned_sha256_selected() {
[[ -n "${BASE_HOMEBREW_INSTALLER_SHA256+x}" ||
-n "${BASE_INSTALL_HOMEBREW_INSTALLER_SHA256+x}" ]]
}
install_homebrew_installer_url() {
if [[ -n "${BASE_HOMEBREW_INSTALLER_URL+x}" ]]; then
printf '%s\n' "$BASE_HOMEBREW_INSTALLER_URL"
return 0
fi
if [[ -n "${BASE_INSTALL_HOMEBREW_INSTALLER_URL+x}" ]]; then
printf '%s\n' "$BASE_INSTALL_HOMEBREW_INSTALLER_URL"
return 0
fi
printf '%s\n' "$BASE_DEFAULT_HOMEBREW_INSTALLER_URL"
}
install_homebrew_installer_sha256() {
if [[ -n "${BASE_HOMEBREW_INSTALLER_SHA256+x}" ]]; then
printf '%s\n' "$BASE_HOMEBREW_INSTALLER_SHA256"
return 0
fi
if [[ -n "${BASE_INSTALL_HOMEBREW_INSTALLER_SHA256+x}" ]]; then
printf '%s\n' "$BASE_INSTALL_HOMEBREW_INSTALLER_SHA256"
return 0
fi
}
install_log_homebrew_mutable_policy() {
install_log "Homebrew installer trust policy: using Homebrew's official mutable installer without checksum verification."
install_log "Set BASE_HOMEBREW_INSTALLER_URL and BASE_HOMEBREW_INSTALLER_SHA256 to use a pinned verified installer."
}
install_fetch_homebrew_installer() {
local installer_url="$1"
local target="$2"
local installer_path
case "$installer_url" in
file://*)
installer_path="${installer_url#file://}"
cp "$installer_path" "$target"
;;
/*|./*|../*)
cp "$installer_url" "$target"
;;
*)
command -v curl >/dev/null 2>&1 || return 127
curl -fsSL "$installer_url" -o "$target"
;;
esac
}
install_run_verified_homebrew_installer() {
local installer_url="$1"
local expected_sha256="$2"
local installer_file
local checksum
local actual_sha256
local exit_code
installer_file="$(mktemp "${TMPDIR:-/tmp}/base-homebrew-installer.XXXXXX")" || install_die "Failed to create a temporary Homebrew installer file."
install_fetch_homebrew_installer "$installer_url" "$installer_file" || {
rm -f "$installer_file"
install_die "Failed to read pinned Homebrew installer content from '$installer_url'."
}
command -v shasum >/dev/null 2>&1 || {
rm -f "$installer_file"
install_die "shasum is required to verify pinned Homebrew installer content."
}
checksum="$(shasum -a 256 "$installer_file")" || {
rm -f "$installer_file"
install_die "Failed to compute Homebrew installer checksum."
}
actual_sha256="${checksum%% *}"
if [[ "$actual_sha256" != "$expected_sha256" ]]; then
rm -f "$installer_file"
install_die "Homebrew installer checksum mismatch (expected $expected_sha256, got $actual_sha256)."
fi
/bin/bash "$installer_file"
exit_code=$?
rm -f "$installer_file"
[[ "$exit_code" -eq 0 ]] || install_die "Homebrew installer failed."
}
install_homebrew() {
local installer
local installer_url
local installer_sha256
installer_url="$(install_homebrew_installer_url)"
installer_sha256="$(install_homebrew_installer_sha256)"
install_log "Installing Homebrew."
if install_homebrew_pinned_selected; then
install_homebrew_pinned_url_selected &&
install_homebrew_pinned_sha256_selected &&
[[ -n "$installer_url" && -n "$installer_sha256" ]] ||
install_die "Pinned Homebrew installer URL and SHA-256 are both required."
install_log "Using pinned Homebrew installer from $installer_url."
if [[ "${BASE_INSTALL_DRY_RUN:-false}" == "true" ]]; then
install_log "[DRY-RUN] Would verify Homebrew installer SHA-256 $installer_sha256"
install_log "[DRY-RUN] Would run: /bin/bash <verified Homebrew installer from $installer_url>"
return 0
fi
install_run_verified_homebrew_installer "$installer_url" "$installer_sha256"
return 0
fi
install_log_homebrew_mutable_policy
if [[ "${BASE_INSTALL_DRY_RUN:-false}" == "true" ]]; then
install_log "[DRY-RUN] Would run: /bin/bash -c <Homebrew installer from $installer_url>"
return 0
fi
command -v curl >/dev/null 2>&1 || install_die "curl is required to install Homebrew."
installer="$(curl -fsSL "$installer_url")" || install_die "Failed to download the Homebrew installer."
/bin/bash -c "$installer" || install_die "Homebrew installer failed."
}
install_ensure_homebrew() {
if install_find_brew >/dev/null 2>&1; then
return 0
fi
install_homebrew || install_die "Homebrew installation failed."
}
install_ensure_supported_bash() {
local brew_bin
if install_find_supported_bash >/dev/null 2>&1; then
return 0
fi
install_log "A supported Bash was not found; bootstrapping Homebrew Bash before running basectl."
install_ensure_homebrew || install_die "Homebrew installation failed."
brew_bin="$(install_find_brew || true)"
if [[ -z "$brew_bin" && "${BASE_INSTALL_DRY_RUN:-false}" == "true" ]]; then
brew_bin=brew
fi
[[ -n "$brew_bin" ]] || install_die "Homebrew was installed, but 'brew' was not found."
install_run "$brew_bin" install bash || install_die "Failed to install Bash through Homebrew."
if [[ "${BASE_INSTALL_DRY_RUN:-false}" == "true" ]]; then
return 0
fi
install_find_supported_bash >/dev/null 2>&1 || install_die "Bash was installed, but a supported Bash was not found."
}
install_run_basectl() {
local install_dir="$1"
shift
local bash_bin
bash_bin="$(install_find_supported_bash || true)"
if [[ -z "$bash_bin" && "${BASE_INSTALL_DRY_RUN:-false}" == "true" ]]; then
bash_bin="${BASE_INSTALL_DRY_RUN_BASH:-/opt/homebrew/bin/bash}"
fi
[[ -n "$bash_bin" ]] || install_die "A supported Bash was not found."
install_run "$bash_bin" "$install_dir/bin/basectl" "$@" || install_die "basectl $* failed."
}
install_clone_or_update() {
local repo_url="$1"
local install_dir="$2"
local branch="$3"
if [[ "${BASE_INSTALL_DRY_RUN:-false}" != "true" ]] && ! command -v git >/dev/null 2>&1; then
install_die "Git is required to install Base."
fi
if [[ -d "$install_dir/.git" ]]; then
install_log "Updating existing Base checkout at '$install_dir'."
install_run git -C "$install_dir" pull --ff-only || install_die "Failed to update existing Base checkout."
return 0
fi
if [[ -e "$install_dir" ]]; then
install_die "Install path '$install_dir' exists but is not a Git checkout."
fi
install_log "Cloning Base into '$install_dir'."
install_run mkdir -p "$(dirname "$install_dir")" || install_die "Failed to create install parent directory."
if [[ -n "$branch" ]]; then
install_run git clone --branch "$branch" "$repo_url" "$install_dir" || install_die "Failed to clone Base repository."
else
install_run git clone "$repo_url" "$install_dir" || install_die "Failed to clone Base repository."
fi
}
install_run_base_setup() {
local install_dir="$1"
install_log "Running basectl setup."
install_ensure_supported_bash
install_run_basectl "$install_dir" setup
}
install_run_update_profile() {
local install_dir="$1"
install_log "Updating shell startup files."
install_run_basectl "$install_dir" update-profile
}
install_main() {
local repo_url="${BASE_INSTALL_REPO_URL:-https://github.com/basefoundry/base.git}"
local install_dir="${BASE_INSTALL_DIR:-${BASE_HOME:-$HOME/work/base}}"
local branch="${BASE_INSTALL_BRANCH:-}"
local update_profile="${BASE_INSTALL_UPDATE_PROFILE:-true}"
BASE_INSTALL_DRY_RUN="${BASE_INSTALL_DRY_RUN:-false}"
while (($# > 0)); do
case "$1" in
-h|--help)
install_usage
return 0
;;
--dir)
[[ -n "${2:-}" ]] || install_die "Option '--dir' requires an argument."
install_dir="$2"
shift 2
;;
--repo-url)
[[ -n "${2:-}" ]] || install_die "Option '--repo-url' requires an argument."
repo_url="$2"
shift 2
;;
--branch)
[[ -n "${2:-}" ]] || install_die "Option '--branch' requires an argument."
branch="$2"
shift 2
;;
--no-profile)
update_profile=false
shift
;;
--dry-run)
BASE_INSTALL_DRY_RUN=true
shift
;;
*)
install_usage >&2
install_die "Unknown option '$1'."
;;
esac
done
install_dir="$(install_expand_path "$install_dir")"
install_log "Base installer"
install_log "Repository: $repo_url"
install_log "Install path: $install_dir"
install_clone_or_update "$repo_url" "$install_dir" "$branch"
install_run_base_setup "$install_dir"
if [[ "$update_profile" == "true" ]]; then
install_run_update_profile "$install_dir"
fi
install_log "Base installation is complete."
if [[ "$update_profile" == "true" ]]; then
install_log "Restart your shell with: exec \"\$SHELL\" -l"
fi
}
if [[ "${BASE_INSTALL_TESTING:-false}" != "true" ]]; then
install_main "$@"
fi