-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·263 lines (225 loc) · 9.16 KB
/
install.sh
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
#!/usr/bin/env bash
{ # this ensures the entire script is downloaded #
asapshell_has() {
type "$1" >/dev/null 2>&1
}
asapshell_echo() {
command printf %s\\n "$*" 2>/dev/null
}
if [ -z "${BASH_VERSION}" ] || [ -n "${ZSH_VERSION}" ]; then
# shellcheck disable=SC2016
asapshell_echo >&2 'Error: the install instructions explicitly say to pipe the install script to `bash`; please follow them'
exit 1
fi
asapshell_try_profile() {
if [ -z "${1-}" ] || [ ! -f "${1}" ]; then
return 1
fi
asapshell_echo "${1}"
}
asapshell_grep() {
GREP_OPTIONS='' command grep "$@"
}
asapshell_default_install_dir() {
[ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.asapshell" || printf %s "${XDG_CONFIG_HOME}/asapshell"
}
asapshell_install_dir() {
if [ -n "$ASAPSHELL_DIR" ]; then
printf %s "${ASAPSHELL_DIR}"
else
asapshell_default_install_dir
fi
}
asapshell_profile_is_bash_or_zsh() {
local TEST_PROFILE
TEST_PROFILE="${1-}"
case "${TEST_PROFILE-}" in
*"/.bashrc" | *"/.bash_profile" | *"/.zshrc")
return
;;
*)
return 1
;;
esac
}
asapshell_detect_profile() {
if [ "${PROFILE-}" = '/dev/null' ]; then
# the user has specifically requested NOT to have asapshell touch their profile
return
fi
if [ -n "${PROFILE}" ] && [ -f "${PROFILE}" ]; then
asapshell_echo "${PROFILE}"
return
fi
local DETECTED_PROFILE
DETECTED_PROFILE=''
if [ "${SHELL#*bash}" != "$SHELL" ]; then
if [ -f "$HOME/.bashrc" ]; then
DETECTED_PROFILE="$HOME/.bashrc"
elif [ -f "$HOME/.bash_profile" ]; then
DETECTED_PROFILE="$HOME/.bash_profile"
fi
elif [ "${SHELL#*zsh}" != "$SHELL" ]; then
if [ -f "$HOME/.zshrc" ]; then
DETECTED_PROFILE="$HOME/.zshrc"
fi
fi
if [ -z "$DETECTED_PROFILE" ]; then
for EACH_PROFILE in ".profile" ".bashrc" ".bash_profile" ".zshrc"; do
if DETECTED_PROFILE="$(asapshell_try_profile "${HOME}/${EACH_PROFILE}")"; then
break
fi
done
fi
if [ -n "$DETECTED_PROFILE" ]; then
asapshell_echo "$DETECTED_PROFILE"
fi
}
asapshell_latest_version() {
asapshell_echo "main"
}
install_asapshell_from_git() {
local INSTALL_DIR
INSTALL_DIR="$(asapshell_install_dir)"
local ASAPSHELL_VERSION
ASAPSHELL_VERSION="${ASAPSHELL_INSTALL_VERSION:-$(asapshell_latest_version)}"
local ASAPSHELL_SOURCE_URL
ASAPSHELL_SOURCE_URL="https://github.com/asapdotid/asapshell.git"
if [ -n "${ASAPSHELL_INSTALL_VERSION:-}" ]; then
# Check if version is an existing ref
if command git ls-remote "$ASAPSHELL_SOURCE_URL" "$ASAPSHELL_VERSION" | asapshell_grep -q "$ASAPSHELL_VERSION"; then
:
# Check if version is an existing changeset
elif ! asapshell_download -o /dev/null "$ASAPSHELL_SOURCE_URL"; then
asapshell_echo >&2 "Failed to find '$ASAPSHELL_VERSION' version."
exit 1
fi
fi
local fetch_error
if [ -d "$INSTALL_DIR/.git" ]; then
# Updating repo
asapshell_echo "=> asapshell is already installed in $INSTALL_DIR, trying to update using git"
command printf '\r=> '
fetch_error="Failed to update asapshell with $ASAPSHELL_VERSION, run 'git fetch' in $INSTALL_DIR yourself."
else
fetch_error="Failed to fetch origin with $ASAPSHELL_VERSION. Please report this!"
asapshell_echo "=> Downloading asapshell from git to '$INSTALL_DIR'"
command printf '\r=> '
mkdir -p "${INSTALL_DIR}"
if [ "$(ls -A "${INSTALL_DIR}")" ]; then
# Initializing repo
command git init "${INSTALL_DIR}" || {
asapshell_echo >&2 'Failed to initialize asapshell repo. Please report this!'
exit 2
}
command git --git-dir="${INSTALL_DIR}/.git" remote add origin "$ASAPSHELL_SOURCE_URL" 2>/dev/null ||
command git --git-dir="${INSTALL_DIR}/.git" remote set-url origin "$ASAPSHELL_SOURCE_URL" || {
asapshell_echo >&2 'Failed to add remote "origin" (or set the URL). Please report this!'
exit 2
}
else
# Cloning repo
command git clone "$ASAPSHELL_SOURCE_URL" --depth=1 "${INSTALL_DIR}" || {
asapshell_echo >&2 'Failed to clone asapsahell repo. Please report this!'
exit 2
}
fi
fi
[[ ! -f "${INSTALL_DIR}/.env" ]] && command cp -rf "${INSTALL_DIR}/.env.example" "${INSTALL_DIR}/.env"
# Try to fetch tag
if command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" fetch origin tag "$ASAPSHELL_VERSION" --depth=1 2>/dev/null; then
:
# Fetch given version
elif ! command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" fetch origin "$ASAPSHELL_VERSION" --depth=1; then
asapshell_echo >&2 "$fetch_error"
exit 1
fi
command git -c advice.detachedHead=false --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" checkout -f --quiet FETCH_HEAD || {
asapshell_echo >&2 "Failed to checkout the given version $ASAPSHELL_VERSION. Please report this!"
exit 2
}
if [ -n "$(command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" show-ref refs/heads/main)" ]; then
if command git --no-pager --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" branch --quiet 2>/dev/null; then
command git --no-pager --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" branch --quiet -D main >/dev/null 2>&1
else
asapshell_echo >&2 "Your version of git is out of date. Please update it!"
command git --no-pager --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" branch -D main >/dev/null 2>&1
fi
fi
asapshell_echo "=> Compressing and cleaning up git repository"
if ! command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" reflog expire --expire=now --all; then
asapshell_echo >&2 "Your version of git is out of date. Please update it!"
fi
if ! command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" gc --auto --aggressive --prune=now; then
asapshell_echo >&2 "Your version of git is out of date. Please update it!"
fi
return
}
asapshell_do_install() {
if [ -n "${ASAPSHELL_DIR-}" ] && ! [ -d "${ASAPSHELL_DIR}" ]; then
if [ -e "${ASAPSHELL_DIR}" ]; then
asapshell_echo >&2 "File \"${ASAPSHELL_DIR}\" has the same name as installation directory."
exit 1
fi
if [ "${ASAPSHELL_DIR}" = "$(asapshell_default_install_dir)" ]; then
mkdir "${ASAPSHELL_DIR}"
else
asapshell_echo >&2 "You have \$ASAPSHELL_DIR set to \"${ASAPSHELL_DIR}\", but that directory does not exist. Check your profile files and environment."
exit 1
fi
fi
if ! asapshell_has git; then
asapshell_echo >&2 "You need git to install asapshell"
exit 1
fi
install_asapshell_from_git
asapshell_echo
local ASAPSHELL_PROFILE
ASAPSHELL_PROFILE="$(asapshell_detect_profile)"
local PROFILE_INSTALL_DIR
PROFILE_INSTALL_DIR="$(asapshell_install_dir | command sed "s:^$HOME:\$HOME:")"
SOURCE_STR="\\nexport ASAPSHELL_DIR=\"${PROFILE_INSTALL_DIR}\"\\n[ -s \"\$ASAPSHELL_DIR/aliases\" ] && \\. \"\$ASAPSHELL_DIR/aliases\" # This loads asapshell\\n"
# shellcheck disable=SC2016
BASH_OR_ZSH=false
if [ -z "${ASAPSHELL_PROFILE-}" ]; then
local TRIED_PROFILE
if [ -n "${PROFILE}" ]; then
TRIED_PROFILE="${ASAPSHELL_PROFILE} (as defined in \$PROFILE), "
fi
asapshell_echo "=> Profile not found. Tried ${TRIED_PROFILE-}~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile."
asapshell_echo "=> Create one of them and run this script again"
asapshell_echo " OR"
asapshell_echo "=> Append the following lines to the correct file yourself:"
command printf "${SOURCE_STR}"
asapshell_echo
else
if asapshell_profile_is_bash_or_zsh "${ASAPSHELL_PROFILE-}"; then
BASH_OR_ZSH=true
fi
if ! command grep -qc '/aliases' "$ASAPSHELL_PROFILE"; then
asapshell_echo "=> Appending asapshell source string to $ASAPSHELL_PROFILE"
command printf "${SOURCE_STR}" >>"$ASAPSHELL_PROFILE"
else
asapshell_echo "=> Asapshell source string already in ${ASAPSHELL_PROFILE}"
fi
fi
if ${BASH_OR_ZSH} && [ -z "${ASAPSHELLPROFILE-}" ]; then
asapshell_echo "=> Please also append the following lines to the if you are using bash/zsh shell:"
command printf "${COMPLETION_STR}"
fi
# Source asapsahell
# shellcheck source=/dev/null
\. "$(asapshell_install_dir)/aliases"
asapshell_reset
asapshell_echo "=> Close and reopen your terminal to start using asapshell or run the following to use it now:"
command printf "${SOURCE_STR}"
}
asapshell_reset() {
unset -f asapshell_has asapshell_install_dir asapshell_latest_version \
asapshell_profile_is_bash_or_zsh asapshell_download install_asapshell_from_git \
asapshell_try_profile asapshell_detect_profile asapshell_do_install \
asapshell_reset asapshell_default_install_dir asapshell_grep
}
[ "_$ASAPSHELL_ENV" = "_testing" ] || asapshell_do_install
}
# this ensures the entire script is downloaded #