-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·234 lines (185 loc) · 4.98 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·234 lines (185 loc) · 4.98 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
#!/bin/zsh
set -euo pipefail
DRY_RUN=0
REPO_URL="https://github.com/oullin/git-diff"
DEFAULT_REPO_DEST="$HOME/Sites/git-diff"
for arg in "$@"; do
case "$arg" in
--dry-run)
DRY_RUN=1
;;
esac
done
log() {
printf '==> %s\n' "$1"
}
die() {
printf 'error: %s\n' "$1" >&2
exit 1
}
run() {
if [[ "$DRY_RUN" -eq 1 ]]; then
printf 'would run:'
printf ' %q' "$@"
printf '\n'
return 0
fi
"$@"
}
start_sudo_keepalive() {
if [[ "$DRY_RUN" -eq 1 ]]; then
log "would validate sudo access with sudo -v"
return 0
fi
log "validating sudo access"
sudo -v || die "administrator access is required"
while true; do
sudo -n -v
sleep 60
done 2>/dev/null &
SUDO_KEEPALIVE_PID=$!
trap 'kill "$SUDO_KEEPALIVE_PID" 2>/dev/null || true' EXIT INT TERM
}
ensure_macos() {
if [[ "$(uname -s)" != "Darwin" ]]; then
die "setup only supports macOS"
fi
}
ensure_canonical_location() {
local root
root="$(cd "$(dirname "$0")" && pwd)"
if [[ -d "$root/.git" ]]; then
return 0
fi
log "warning: setup is running from $root, which is not a git checkout"
log "if this is an unzipped download, edits to the repo and stow links will drift"
log "setup must continue from a canonical git clone"
if [[ "$DRY_RUN" -eq 1 ]]; then
log "would prompt for destination repo path, defaulting to $DEFAULT_REPO_DEST"
log "would clone $REPO_URL into the selected destination if needed"
log "would re-run setup from the selected destination"
return 0
fi
printf 'Destination repo path [%s]: ' "$DEFAULT_REPO_DEST"
local destination
read -r destination || destination=""
if [[ -z "$destination" ]]; then
destination="$DEFAULT_REPO_DEST"
fi
destination="${destination/#\~/$HOME}"
if [[ -e "$destination" ]]; then
if [[ ! -d "$destination/.git" ]]; then
die "$destination exists but is not a git checkout"
fi
local origin
origin="$(git -C "$destination" remote get-url origin 2>/dev/null || true)"
if [[ "$origin" != "$REPO_URL" && "$origin" != "git@github.com:oullin/git-diff.git" ]]; then
die "$destination is a git checkout but origin is $origin, expected $REPO_URL"
fi
else
log "cloning $REPO_URL into $destination"
mkdir -p "$(dirname "$destination")"
run git clone "$REPO_URL" "$destination"
fi
log "re-running setup from $destination"
cd "$destination"
exec ./setup.sh "$@"
}
ensure_command_line_tools() {
if xcode-select -p >/dev/null 2>&1; then
log "Xcode Command Line Tools found"
else
log "Xcode Command Line Tools are missing"
if [[ "$DRY_RUN" -eq 1 ]]; then
log "would open Apple's Command Line Tools installer with xcode-select --install"
log "would wait until xcode-select -p succeeds"
return 0
fi
xcode-select --install 2>/dev/null || true
printf 'Complete the Apple Command Line Tools installer dialog to continue.\n'
until xcode-select -p >/dev/null 2>&1; do
sleep 10
printf '.'
done
printf '\n'
log "Xcode Command Line Tools installed"
fi
if [[ "$DRY_RUN" -eq 1 ]]; then
log "would check Xcode Command Line Tools license status"
return 0
fi
if ! xcodebuild -license check >/tmp/git-diff-xcode-license.log 2>&1; then
if grep -qiE 'license|agree' /tmp/git-diff-xcode-license.log; then
cat /tmp/git-diff-xcode-license.log >&2
die "Xcode license needs attention; run 'sudo xcodebuild -license' and accept Apple's prompts, then rerun setup"
fi
fi
}
load_homebrew() {
local brew_bin=""
if command -v brew >/dev/null 2>&1; then
brew_bin="$(command -v brew)"
elif [[ -x /opt/homebrew/bin/brew ]]; then
brew_bin="/opt/homebrew/bin/brew"
elif [[ -x /usr/local/bin/brew ]]; then
brew_bin="/usr/local/bin/brew"
fi
if [[ -n "$brew_bin" ]]; then
eval "$("$brew_bin" shellenv)"
fi
}
ensure_homebrew() {
load_homebrew
if command -v brew >/dev/null 2>&1; then
log "Homebrew found"
return 0
fi
log "Homebrew is missing"
if [[ "$DRY_RUN" -eq 1 ]]; then
log "would install Homebrew with the official install script"
return 0
fi
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
load_homebrew
command -v brew >/dev/null 2>&1 || die "Homebrew installed but brew is not on PATH"
}
ensure_go() {
if command -v go >/dev/null 2>&1; then
log "Go found"
return 0
fi
log "Go is missing"
run brew install go
}
ensure_git() {
if command -v git >/dev/null 2>&1; then
log "Git found"
return 0
fi
log "Git is missing"
run brew install git
}
prepare_workflow_ui() {
local root
root="$(cd "$(dirname "$0")" && pwd)"
log "preparing api workflow backend"
if [[ "$DRY_RUN" -eq 1 ]]; then
printf 'would run: go run ./packages/api/cmd help\n'
return 0
fi
cd "$root"
go run ./packages/api/cmd help >/dev/null
if command -v pnpm >/dev/null 2>&1; then
log "Electron UI available: pnpm --filter ui build && pnpm --filter ui start"
else
log "Electron UI requires pnpm; enable Corepack after Node is installed"
fi
}
ensure_macos
ensure_command_line_tools
ensure_homebrew
ensure_git
ensure_canonical_location "$@"
start_sudo_keepalive
ensure_go
prepare_workflow_ui "$@"