-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
325 lines (270 loc) · 8.09 KB
/
install.sh
File metadata and controls
325 lines (270 loc) · 8.09 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
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_NAME="install.sh"
REPO_OWNER="a5c-ai"
REPO_NAME="o"
REPO_REF_DEFAULT="main"
MANAGED_BEGIN="# --- a5c-ai/o install.sh managed ---"
MANAGED_END="# --- end a5c-ai/o install.sh managed ---"
usage() {
cat <<'EOF'
Install `o` + `.a5c/` templates into a target directory.
Usage:
install.sh [--to DIR] [--force] [--no-gitignore]
install.sh --smoke-test
install.sh --help
Options:
--to DIR Target directory (default: current directory)
--force Overwrite existing installed files/directories
--no-gitignore Do not create/update target .gitignore
--smoke-test Run local smoke tests in a temp dir
--help Show this help
Notes:
- macOS/Linux supported. On Windows, use WSL2 or Git Bash/MSYS2.
- This installer downloads the repo tarball from GitHub (no git required).
EOF
}
log() { printf '%s\n' "$*"; }
die() { printf 'Error: %s\n' "$*" >&2; exit 1; }
require_cmd() {
command -v "$1" >/dev/null 2>&1 || die "Missing required command: $1"
}
mktemp_dir() {
if command -v mktemp >/dev/null 2>&1; then
mktemp -d 2>/dev/null || mktemp -d -t "o-install"
else
die "mktemp not found"
fi
}
abs_path() {
# Prints an absolute path without requiring realpath(1).
local p="$1"
if [[ "$p" == /* ]]; then
printf '%s\n' "$p"
return 0
fi
printf '%s/%s\n' "$(pwd -P)" "$p"
}
script_dir_or_empty() {
local src="${BASH_SOURCE[0]:-}"
[[ -n "$src" && -f "$src" ]] || return 1
(cd "$(dirname "$src")" && pwd -P)
}
local_source_root_or_empty() {
# If install.sh is being run from a repo checkout (not piped via stdin),
# use the adjacent files as the installation source.
local sdir
sdir="$(script_dir_or_empty 2>/dev/null || true)"
[[ -n "$sdir" ]] || return 1
[[ -f "${sdir%/}/o" ]] || return 1
[[ -f "${sdir%/}/.a5c/o.md" ]] || return 1
[[ -d "${sdir%/}/.a5c/functions" ]] || return 1
[[ -d "${sdir%/}/.a5c/processes" ]] || return 1
printf '%s\n' "$sdir"
}
download_tarball() {
local dest_tgz="$1"
local ref="${2:-$REPO_REF_DEFAULT}"
local url="https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/tarball/${ref}"
if command -v curl >/dev/null 2>&1; then
curl -fsSL "$url" -o "$dest_tgz"
elif command -v wget >/dev/null 2>&1; then
wget -qO "$dest_tgz" "$url"
else
die "Need curl or wget to download $url"
fi
}
extract_tarball() {
local tgz="$1"
local dest_dir="$2"
mkdir -p "$dest_dir"
tar -xzf "$tgz" -C "$dest_dir"
local top
top="$(cd "$dest_dir" && ls -1d */ 2>/dev/null | head -n 1 || true)"
[[ -n "$top" ]] || die "Failed to locate extracted source directory"
printf '%s\n' "${dest_dir%/}/${top%/}"
}
copy_item() {
local src="$1"
local dst="$2"
local force="$3"
if [[ -e "$dst" ]]; then
if [[ "$force" == "1" ]]; then
rm -rf "$dst"
else
die "Refusing to overwrite existing path: $dst (use --force)"
fi
fi
local dst_parent
dst_parent="$(dirname "$dst")"
mkdir -p "$dst_parent"
if [[ -d "$src" ]]; then
cp -R "$src" "$dst"
else
cp "$src" "$dst"
fi
}
ensure_executable() {
local path="$1"
if chmod +x "$path" 2>/dev/null; then
:
else
log "Warning: could not mark executable: $path"
fi
}
render_gitignore_block() {
cat <<EOF
$MANAGED_BEGIN
.a5c/creds.env
.a5c/creds.env.tmp.*
.a5c/runs/
$MANAGED_END
EOF
}
update_gitignore() {
local target_dir="$1"
local gitignore_path="${target_dir%/}/.gitignore"
local tmp_path="${gitignore_path}.tmp.$$"
local block
block="$(render_gitignore_block)"
if [[ -f "$gitignore_path" ]]; then
awk -v begin="$MANAGED_BEGIN" -v end="$MANAGED_END" '
$0 == begin {inblock=1; next}
$0 == end {inblock=0; next}
!inblock {print}
' "$gitignore_path" >"$tmp_path"
else
: >"$tmp_path"
fi
# Ensure file ends with a newline before appending.
if [[ -s "$tmp_path" ]]; then
local last_byte
last_byte="$(tail -c 1 "$tmp_path" 2>/dev/null || true)"
if [[ "$last_byte" != $'\n' ]]; then
printf '\n' >>"$tmp_path"
fi
fi
printf '%s\n' "$block" >>"$tmp_path"
mv -f "$tmp_path" "$gitignore_path"
}
install_into() {
(
set -euo pipefail
local to_dir="$1"
local force="$2"
local manage_gitignore="$3"
local ref="${4:-$REPO_REF_DEFAULT}"
mkdir -p "$to_dir"
local src_root=""
if [[ "${O_INSTALL_DISABLE_LOCAL_SOURCE:-}" != "1" ]]; then
src_root="$(local_source_root_or_empty 2>/dev/null || true)"
fi
local tmp=""
if [[ -z "$src_root" ]]; then
require_cmd tar
tmp="$(mktemp_dir)"
trap 'rm -rf "$tmp"' EXIT
local tgz="${tmp%/}/src.tgz"
local src_extract="${tmp%/}/src"
log "Downloading ${REPO_OWNER}/${REPO_NAME}@${ref}..."
download_tarball "$tgz" "$ref"
log "Extracting..."
src_root="$(extract_tarball "$tgz" "$src_extract")"
else
log "Using local source: $src_root"
fi
local src_o="${src_root%/}/o"
local src_a5c="${src_root%/}/.a5c"
[[ -f "$src_o" ]] || die "Source missing: o"
[[ -d "$src_a5c" ]] || die "Source missing: .a5c/"
local dst_o="${to_dir%/}/o"
local dst_a5c="${to_dir%/}/.a5c"
mkdir -p "$dst_a5c"
copy_item "$src_o" "$dst_o" "$force"
ensure_executable "$dst_o"
copy_item "${src_a5c%/}/o.md" "${dst_a5c%/}/o.md" "$force"
copy_item "${src_a5c%/}/functions" "${dst_a5c%/}/functions" "$force"
copy_item "${src_a5c%/}/processes" "${dst_a5c%/}/processes" "$force"
if [[ "$manage_gitignore" == "1" ]]; then
update_gitignore "$to_dir"
fi
log "Installed:"
log " ${dst_o}"
log " ${dst_a5c}/o.md"
log " ${dst_a5c}/functions"
log " ${dst_a5c}/processes"
if [[ "$manage_gitignore" == "1" ]]; then
log " ${to_dir%/}/.gitignore (updated)"
fi
)
}
smoke_test() {
(
set -euo pipefail
local tmp
tmp="$(mktemp_dir)"
trap 'rm -rf "$tmp"' EXIT
log "Smoke test temp dir: $tmp"
printf '%s\n' "# keep me" >"${tmp%/}/.gitignore"
printf '%s\n' "unmanaged-line" >>"${tmp%/}/.gitignore"
log "Running install (first time)..."
install_into "$tmp" "0" "1" "$REPO_REF_DEFAULT"
[[ -f "${tmp%/}/o" ]] || die "Smoke test: missing o"
[[ -f "${tmp%/}/.a5c/o.md" ]] || die "Smoke test: missing .a5c/o.md"
[[ -d "${tmp%/}/.a5c/functions" ]] || die "Smoke test: missing .a5c/functions"
[[ -d "${tmp%/}/.a5c/processes" ]] || die "Smoke test: missing .a5c/processes"
log "Verifying ./o help..."
(cd "$tmp" && ./o help >/dev/null)
log "Running install (second time, expect failure without --force)..."
if install_into "$tmp" "0" "1" "$REPO_REF_DEFAULT" >/dev/null 2>&1; then
die "Smoke test: second install unexpectedly succeeded without --force"
fi
log "Running install (third time, with --force)..."
install_into "$tmp" "1" "1" "$REPO_REF_DEFAULT" >/dev/null
log "Verifying managed .gitignore block (exactly one; preserves unmanaged lines)..."
local gi="${tmp%/}/.gitignore"
local begin_count end_count
begin_count="$(grep -cF "$MANAGED_BEGIN" "$gi" || true)"
end_count="$(grep -cF "$MANAGED_END" "$gi" || true)"
[[ "$begin_count" == "1" && "$end_count" == "1" ]] || die "Smoke test: managed block markers not exactly once"
grep -qF "# keep me" "$gi" || die "Smoke test: unmanaged line missing (# keep me)"
grep -qF "unmanaged-line" "$gi" || die "Smoke test: unmanaged line missing (unmanaged-line)"
log "Smoke test OK"
)
}
main() {
local to_dir="."
local force="0"
local manage_gitignore="1"
local run_smoke="0"
while [[ $# -gt 0 ]]; do
case "$1" in
--to)
shift
[[ $# -gt 0 ]] || die "--to requires a directory"
to_dir="$1"
;;
--force) force="1" ;;
--no-gitignore) manage_gitignore="0" ;;
--smoke-test) run_smoke="1" ;;
--help|-h) usage; exit 0 ;;
*)
die "Unknown argument: $1 (use --help)"
;;
esac
shift
done
if [[ "$run_smoke" == "1" ]]; then
smoke_test
return 0
fi
install_into "$(abs_path "$to_dir")" "$force" "$manage_gitignore" "$REPO_REF_DEFAULT"
cat <<EOF
Next:
1) cd "$(abs_path "$to_dir")"
2) ./o help
3) ./o init
4) ./o doctor
EOF
}
main "$@"