Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·182 lines (161 loc) · 5.58 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·182 lines (161 loc) · 5.58 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
#!/bin/sh
# curl -fsSL https://raw.githubusercontent.com/justdeko/zopf/main/install.sh | sh
# main() runs last so a partial download does nothing
set -eu
REPO="justdeko/zopf"
PREFIX="${ZOPF_PREFIX:-$HOME/.local}"
VERSION="${ZOPF_VERSION:-}"
MODIFY_PATH=1
say() { printf '%s\n' "$*"; }
note() { printf '%s\n' "$*" >&2; }
err() {
printf 'error: %s\n' "$*" >&2
exit 1
}
usage() {
cat <<'EOF'
Installs the zopf command line into ~/.local (override with --prefix or ZOPF_PREFIX).
--version <x.y.z> a specific release, rather than the newest published one
--prefix <dir> install under <dir>/opt and link <dir>/bin/zopf
--no-modify-path never touch a shell rc file, just say what to add
--help
Piping into sh takes arguments after -s --, e.g.
curl -fsSL <url> | sh -s -- --version <x.y.z>
EOF
}
parse_args() {
while [ $# -gt 0 ]; do
case "$1" in
--version)
[ $# -ge 2 ] || err "--version needs a value."
VERSION="$2"
shift 2
;;
--prefix)
[ $# -ge 2 ] || err "--prefix needs a value."
PREFIX="$2"
shift 2
;;
--no-modify-path)
MODIFY_PATH=0
shift
;;
--help | -h)
usage
exit 0
;;
*) err "unknown option: $1" ;;
esac
done
[ -n "$PREFIX" ] || err "--prefix cannot be empty."
}
check_platform() {
[ "$(uname -s)" = "Darwin" ] || err "zopf is macOS only."
command -v curl >/dev/null 2>&1 || err "curl is required and isn't on your PATH."
command -v tar >/dev/null 2>&1 || err "tar is required and isn't on your PATH."
}
# /releases/latest redirects to the newest tag
resolve_version() {
[ -z "$VERSION" ] || return 0
url=$(curl -sSLI -o /dev/null -w '%{url_effective}' "https://github.com/$REPO/releases/latest" 2>/dev/null) ||
err "couldn't reach GitHub to ask what the newest release is."
case "$url" in
*/releases/tag/v*) VERSION="${url##*/releases/tag/v}" ;;
*) err "github.com/$REPO has no published release to install. Pass --version to name one." ;;
esac
}
download() {
asset="zopf-cli-$VERSION.tar.gz"
base="https://github.com/$REPO/releases/download/v$VERSION"
say "Downloading zopf $VERSION"
curl -fsSL "$base/$asset" -o "$TMP/$asset" ||
err "couldn't download $asset — is $VERSION a published release?"
if curl -fsSL "$base/$asset.sha256" -o "$TMP/$asset.sha256" 2>/dev/null; then
expected=$(cut -d' ' -f1 <"$TMP/$asset.sha256")
actual=$(shasum -a 256 "$TMP/$asset" | cut -d' ' -f1)
[ "$expected" = "$actual" ] ||
err "the download was damaged, so nothing was installed. Try again."
else
note "note: $VERSION has no checksum, so the download wasn't checked."
fi
}
unpack() {
dest="$PREFIX/opt/zopf-cli-$VERSION"
mkdir -p "$PREFIX/opt" "$PREFIX/bin"
rm -rf "$dest"
tar xzf "$TMP/$asset" -C "$PREFIX/opt"
[ -x "$dest/bin/zopf" ] || err "this release is broken. Please report it on GitHub."
ln -sf "$dest/bin/zopf" "$PREFIX/bin/zopf"
# older releases only take up space once the link has moved on
for old in "$PREFIX"/opt/zopf-cli-*; do
[ -d "$old" ] && [ "$old" != "$dest" ] && rm -rf "$old"
done
}
rc_file() {
case "$(basename "${SHELL:-/bin/sh}")" in
zsh) printf '%s' "${ZDOTDIR:-$HOME}/.zshrc" ;;
bash) printf '%s' "$HOME/.bash_profile" ;;
fish) printf '%s' "$HOME/.config/fish/config.fish" ;;
*) printf '%s' "$HOME/.profile" ;;
esac
}
path_line() {
case "$(basename "${SHELL:-/bin/sh}")" in
fish) printf 'fish_add_path %s' "$PREFIX/bin" ;;
*) printf 'export PATH="%s:$PATH"' "$PREFIX/bin" ;;
esac
}
# a child can't change its parent's PATH
ensure_on_path() {
case ":$PATH:" in
*":$PREFIX/bin:"*) return 0 ;;
esac
line=$(path_line)
if [ "$MODIFY_PATH" -eq 0 ]; then
note "$PREFIX/bin is not on your PATH. Add this yourself:"
note " $line"
return 0
fi
rc=$(rc_file)
mkdir -p "$(dirname "$rc")"
if [ -f "$rc" ] && grep -Fq "$PREFIX/bin" "$rc"; then
RESTART_HINT="$rc already adds it — open a new terminal to pick it up."
return 0
fi
printf '\n# added by the zopf installer\n%s\n' "$line" >>"$rc"
RESTART_HINT="Added $PREFIX/bin to $rc — run 'exec \$SHELL' or open a new terminal."
}
# the tarball ships no JDK
check_java() {
java_bin="java"
[ -n "${JAVA_HOME:-}" ] && [ -x "$JAVA_HOME/bin/java" ] && java_bin="$JAVA_HOME/bin/java"
command -v "$java_bin" >/dev/null 2>&1 || {
note ""
note "zopf needs a JDK 21 or newer and there is no java on your PATH."
note " brew install temurin"
return 0
}
major=$("$java_bin" -version 2>&1 | sed -n '1s/.*version "\([0-9][0-9]*\).*/\1/p')
[ -n "$major" ] && [ "$major" -ge 21 ] 2>/dev/null && return 0
note ""
note "zopf needs a JDK 21 or newer; the java on your PATH reports ${major:-an unreadable version}."
note " brew install temurin"
}
main() {
parse_args "$@"
check_platform
resolve_version
TMP=$(mktemp -d)
trap 'rm -rf "$TMP"' EXIT INT TERM
download
unpack
RESTART_HINT=""
ensure_on_path
say "Installed zopf $VERSION to $PREFIX/bin/zopf"
[ -n "$RESTART_HINT" ] && say "$RESTART_HINT"
check_java
say ""
say " zopf --help what it can do"
say " zopf list the workflows in the workspace you're standing in"
}
main "$@"