-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap
More file actions
executable file
·135 lines (108 loc) · 3.01 KB
/
Copy pathbootstrap
File metadata and controls
executable file
·135 lines (108 loc) · 3.01 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
#!/usr/bin/env sh
if (return 0 2>/dev/null); then
printf '%s\n' "bootstrap: do not source this script; run it as ./bootstrap or sh ./bootstrap" >&2
return 1
fi
set -eu
DEFAULT_REPO_URL="https://github.com/davidstosik/dotfiles-next.git"
DEFAULT_REPO_REF="main"
export HOMEBREW_NO_AUTO_UPDATE=1
ROOT=
log() { printf '%s\n' "$*"; }
fail() {
log "bootstrap: $*" >&2
exit 1
}
command_exists() { command -v "$1" >/dev/null 2>&1; }
run() {
log "+ $*"
"$@"
}
find_checkout_root() {
[ -f "$0" ] || return 1
git_root=$(git -C "$(dirname -- "$0")" rev-parse --show-toplevel 2>/dev/null || true)
[ -n "$git_root" ] || return 1
[ -x "$git_root/bootstrap" ] || return 1
[ -f "$git_root/dotfiles" ] || return 1
ROOT=$git_root
}
load_macos_homebrew_shellenv() {
if command_exists brew; then
return 0
fi
if [ -x /opt/homebrew/bin/brew ]; then
eval "$(/opt/homebrew/bin/brew shellenv)"
elif [ -x /usr/local/bin/brew ]; then
eval "$(/usr/local/bin/brew shellenv)"
else
return 1
fi
}
ensure_sudo() {
log "Validating sudo credentials..."
run sudo -v
}
ensure_macos() {
if [ "$(uname -s)" != "Darwin" ]; then
fail "This bootstrap is for macOS."
fi
}
install_homebrew() {
log "Ensuring Homebrew is installed..."
if load_macos_homebrew_shellenv; then
run brew update
return 0
fi
installer=$(mktemp)
trap 'rm -f "$installer"' EXIT
run curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh -o "$installer"
run env NONINTERACTIVE=1 /bin/bash "$installer"
rm -f "$installer"
trap - EXIT
load_macos_homebrew_shellenv || fail "Homebrew installed but could not be loaded"
}
install_bootstrap_tools() {
log "Installing bootstrap tools..."
run brew install git mise
}
ensure_checkout() {
if find_checkout_root; then
return 0
fi
: "${HOME:?HOME is required}"
repo_url=${DOTFILES_REPO_URL:-$DEFAULT_REPO_URL}
repo_ref=${DOTFILES_REPO_REF:-$DEFAULT_REPO_REF}
install_dir=${DOTFILES_INSTALL_DIR:-"$HOME/.dotfiles"}
if [ -d "$install_dir/.git" ]; then
log "Updating dotfiles repository in $install_dir..."
run git -C "$install_dir" fetch --prune origin "$repo_ref"
run git -C "$install_dir" checkout "$repo_ref"
run git -C "$install_dir" reset --hard "origin/$repo_ref"
else
[ ! -e "$install_dir" ] || fail "$install_dir already exists and is not a git checkout"
log "Cloning dotfiles repository into $install_dir..."
run mkdir -p "$(dirname -- "$install_dir")"
run git clone --branch "$repo_ref" "$repo_url" "$install_dir"
fi
ROOT=$install_dir
[ -x "$ROOT/bootstrap" ] || fail "dotfiles checkout does not contain an executable bootstrap"
}
install_ruby() {
log "Ensuring Ruby is installed..."
run mise trust -y "$ROOT/.mise.toml"
run mise install -y --cd "$ROOT"
}
run_dotfiles() {
log "Running dotfiles..."
run mise exec --cd "$ROOT" -- ruby "$ROOT/dotfiles"
}
main() {
ensure_macos
ensure_sudo
install_homebrew
install_bootstrap_tools
ensure_checkout
install_ruby
run_dotfiles
}
main