-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathinstall.sh
More file actions
156 lines (134 loc) · 4.05 KB
/
Copy pathinstall.sh
File metadata and controls
156 lines (134 loc) · 4.05 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
#!/bin/sh
set -eu
REPOSITORY="microsoft/shell-use"
BINARY_NAME="shell-use"
fail() {
echo "Error: $*" >&2
exit 1
}
command -v uname >/dev/null 2>&1 || fail "uname is required."
command -v tar >/dev/null 2>&1 || fail "tar is required."
case "$(uname -s)" in
Darwin) OS="apple-darwin" ;;
Linux) OS="unknown-linux-musl" ;;
MINGW*|MSYS*|CYGWIN*)
fail "Use install.ps1 to install shell-use on Windows."
;;
*) fail "Unsupported operating system: $(uname -s)" ;;
esac
case "$(uname -m)" in
x86_64|amd64) ARCH="x86_64" ;;
arm64|aarch64) ARCH="aarch64" ;;
*) fail "Unsupported architecture: $(uname -m)" ;;
esac
TARGET="${ARCH}-${OS}"
ASSET="${BINARY_NAME}-${TARGET}.tar.gz"
VERSION="${SHELL_USE_VERSION:-latest}"
if [ -z "$VERSION" ] || [ "$VERSION" = "latest" ]; then
RELEASE_URL="https://github.com/${REPOSITORY}/releases/latest/download"
else
case "$VERSION" in
v*) ;;
*) VERSION="v${VERSION}" ;;
esac
RELEASE_URL="https://github.com/${REPOSITORY}/releases/download/${VERSION}"
fi
DOWNLOAD_URL="${RELEASE_URL}/${ASSET}"
TOKEN="${GITHUB_TOKEN:-${GH_TOKEN:-}}"
TMP_DIR="$(mktemp -d 2>/dev/null || mktemp -d -t shell-use)"
ARCHIVE_PATH="${TMP_DIR}/${ASSET}"
EXTRACT_DIR="${TMP_DIR}/extract"
cleanup() {
rm -rf -- "$TMP_DIR"
}
trap cleanup EXIT HUP INT TERM
download() {
url="$1"
destination="$2"
if command -v curl >/dev/null 2>&1; then
if [ -n "$TOKEN" ]; then
curl --proto '=https' --tlsv1.2 -fsSL \
-H "Authorization: Bearer ${TOKEN}" \
"$url" -o "$destination"
else
curl --proto '=https' --tlsv1.2 -fsSL "$url" -o "$destination"
fi
elif command -v wget >/dev/null 2>&1; then
if [ -n "$TOKEN" ]; then
wget -q --header="Authorization: Bearer ${TOKEN}" \
-O "$destination" "$url"
else
wget -q -O "$destination" "$url"
fi
else
fail "curl or wget is required."
fi
}
echo "Downloading shell-use for ${TARGET}..."
download "$DOWNLOAD_URL" "$ARCHIVE_PATH" ||
fail "Could not download ${DOWNLOAD_URL}"
ARCHIVE_CONTENTS="$(tar -tzf "$ARCHIVE_PATH")" ||
fail "Downloaded archive is invalid."
case "$ARCHIVE_CONTENTS" in
"$BINARY_NAME"|"./$BINARY_NAME") ;;
*) fail "Downloaded archive has unexpected contents." ;;
esac
mkdir -p "$EXTRACT_DIR"
tar -xzf "$ARCHIVE_PATH" -C "$EXTRACT_DIR"
if [ -n "${SHELL_USE_INSTALL_DIR:-}" ]; then
INSTALL_DIR="$SHELL_USE_INSTALL_DIR"
elif [ -n "${PREFIX:-}" ]; then
INSTALL_DIR="${PREFIX}/bin"
elif [ "$(id -u 2>/dev/null || echo 1)" -eq 0 ]; then
INSTALL_DIR="/usr/local/bin"
else
: "${HOME:?HOME is required when installing without root privileges.}"
INSTALL_DIR="${HOME}/.local/bin"
fi
mkdir -p "$INSTALL_DIR" ||
fail "Could not create ${INSTALL_DIR}. Set SHELL_USE_INSTALL_DIR to a writable directory."
DESTINATION="${INSTALL_DIR}/${BINARY_NAME}"
STAGED_DESTINATION="${INSTALL_DIR}/.${BINARY_NAME}.tmp.$$"
cp "${EXTRACT_DIR}/${BINARY_NAME}" "$STAGED_DESTINATION"
chmod 755 "$STAGED_DESTINATION"
mv -f "$STAGED_DESTINATION" "$DESTINATION"
echo "Installed shell-use to ${DESTINATION}"
case ":${PATH:-}:" in
*":${INSTALL_DIR}:"*) exit 0 ;;
esac
CURRENT_SHELL="$(basename "${SHELL:-/bin/sh}")"
case "$CURRENT_SHELL" in
zsh)
RC_FILE="${ZDOTDIR:-$HOME}/.zprofile"
PATH_LINE="export PATH=\"${INSTALL_DIR}:\$PATH\""
;;
bash)
if [ -f "$HOME/.bash_profile" ]; then
RC_FILE="$HOME/.bash_profile"
elif [ -f "$HOME/.bash_login" ]; then
RC_FILE="$HOME/.bash_login"
else
RC_FILE="$HOME/.profile"
fi
PATH_LINE="export PATH=\"${INSTALL_DIR}:\$PATH\""
;;
fish)
RC_FILE="${XDG_CONFIG_HOME:-$HOME/.config}/fish/conf.d/shell-use.fish"
PATH_LINE="fish_add_path \"${INSTALL_DIR}\""
;;
*)
RC_FILE="$HOME/.profile"
PATH_LINE="export PATH=\"${INSTALL_DIR}:\$PATH\""
;;
esac
case "${SHELL_USE_NO_MODIFY_PATH:-}" in
1|true|TRUE|yes|YES)
echo "Add ${INSTALL_DIR} to PATH."
exit 0
;;
esac
mkdir -p "$(dirname "$RC_FILE")"
if ! grep -Fqx "$PATH_LINE" "$RC_FILE" 2>/dev/null; then
printf "\n%s\n" "$PATH_LINE" >>"$RC_FILE"
echo "Added ${INSTALL_DIR} to PATH. Restart your shell."
fi