-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
263 lines (220 loc) · 6.94 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 #
xyz_has() {
type "$1" >/dev/null 2>&1
}
xyz_echo() {
command printf %s\\n "$*" 2>/dev/null
}
xyz_grep() {
GREP_OPTIONS='' command grep "$@"
}
xyz_download() {
if xyz_has "curl"; then
curl --fail --compressed -q "$@"
elif xyz_has "wget"; then
# Emulate curl with wget
ARGS=$(xyz_echo "$@" | command sed -e 's/--progress-bar /--progress=bar /' \
-e 's/--compressed //' \
-e 's/--fail //' \
-e 's/-L //' \
-e 's/-I /--server-response /' \
-e 's/-s /-q /' \
-e 's/-sS /-nv /' \
-e 's/-o /-O /' \
-e 's/-C - /-c /')
# shellcheck disable=SC2086
eval wget $ARGS
fi
}
#
# Detect profile file if not specified as environment variable
# (eg: PROFILE=~/.myprofile)
# The echo'ed path is guaranteed to be an existing file
# Otherwise, an empty string is returned
#
xyz_profile_is_bash_or_zsh() {
local TEST_PROFILE
TEST_PROFILE="${1-}"
case "${TEST_PROFILE-}" in
*"/.bashrc" | *"/.bash_profile" | *"/.zshrc")
return
;;
*)
return 1
;;
esac
}
xyz_try_profile() {
if [ -z "${1-}" ] || [ ! -f "${1}" ]; then
return 1
fi
xyz_echo "${1}"
}
xyz_detect_profile() {
if [ "${PROFILE-}" = '/dev/null' ]; then
# the user has specifically requested NOT to have nvm touch their profile
return
fi
if [ -n "${PROFILE}" ] && [ -f "${PROFILE}" ]; then
xyz_echo "${PROFILE}"
return
fi
local DETECTED_PROFILE
DETECTED_PROFILE=''
if [ -n "${BASH_VERSION-}" ]; then
if [ -f "$HOME/.bashrc" ]; then
DETECTED_PROFILE="$HOME/.bashrc"
elif [ -f "$HOME/.bash_profile" ]; then
DETECTED_PROFILE="$HOME/.bash_profile"
fi
elif [ -n "${ZSH_VERSION-}" ]; then
DETECTED_PROFILE="$HOME/.zshrc"
fi
if [ -z "$DETECTED_PROFILE" ]; then
for EACH_PROFILE in ".profile" ".bashrc" ".bash_profile" ".zshrc"; do
if DETECTED_PROFILE="$(xyz_try_profile "${HOME}/${EACH_PROFILE}")"; then
break
fi
done
fi
if [ -n "$DETECTED_PROFILE" ]; then
xyz_echo "$DETECTED_PROFILE"
fi
}
xyz_default_install_path() {
[ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}" || printf %s "${XDG_CONFIG_HOME}"
}
xyz_install_path() {
if [ -n "$XYZ_PATH" ]; then
printf %s "${XYZ_PATH}"
else
xyz_default_install_path
fi
}
xyz_latest_version() {
xyz_echo "v1.2-beta"
}
xyz_file_name() {
xyz_echo ".git_aliases"
}
xyz_source() {
local XYZ_GITHUB_REPO
XYZ_GITHUB_REPO="${XYZ_INSTALL_GITHUB_REPO:-webcdn/git-aliases}"
local XYZ_VERSION
XYZ_VERSION="${XYZ_INSTALL_VERSION:-$(xyz_latest_version)}"
local XYZ_METHOD
XYZ_METHOD="$1"
local XYZ_SOURCE_URL
XYZ_SOURCE_URL="$XYZ_SOURCE"
if [ -z "$XYZ_SOURCE_URL" ]; then
if [ "_$XYZ_METHOD" = "_script" ]; then
XYZ_SOURCE_URL="https://raw.githubusercontent.com/${XYZ_GITHUB_REPO}/${XYZ_VERSION}/aliases.sh"
else
xyz_echo >&2 "Unexpected value \"$XYZ_METHOD\" for \$XYZ_METHOD"
return 1
fi
fi
xyz_echo "$XYZ_SOURCE_URL"
}
install_xyz_as_script() {
local XYZ_REMOTE_SRC
XYZ_REMOTE_SRC="$(xyz_source script)"
# Downloading to $XYZ_FULL_PATH
if [ -f "$XYZ_FULL_PATH" ]; then
xyz_echo "=> Script exists at '$XYZ_FULL_PATH', trying to update"
else
xyz_echo "=> Downloading script to '$XYZ_FULL_PATH'"
fi
xyz_download -s "$XYZ_REMOTE_SRC" -o "$XYZ_FULL_PATH" || {
xyz_echo >&2 "Failed to download '$XYZ_REMOTE_SRC'"
return 1
} &
# wait for downloading to finish
for job in $(jobs -p | command sort); do
wait "$job" || return $?
done
# marking file as executable
chmod a+x "$XYZ_FULL_PATH" || {
xyz_echo >&2 "Failed to mark '$XYZ_FULL_PATH' as executable"
return 3
}
}
xyz_do_install() {
local XYZ_FULL_PATH
XYZ_FULL_PATH="${XYZ_PATH:-$(xyz_install_path)/$(xyz_file_name)}"
# alert if directory exists with same name & exit
if [ -z "${XYZ_FULL_PATH-}" ]; then
xyz_echo >&2 "=> Unable to get the installation path."
exit 1
elif [ -d "${XYZ_FULL_PATH}" ]; then
xyz_echo >&2 "=> \"${XYZ_FULL_PATH}\" is a directory. This shouldn't exists."
exit 1
fi
# decide a method for installation
if [ -z "${METHOD}" ]; then
# Autodetect install method
if xyz_has xyz_download; then
install_xyz_as_script
else
xyz_echo >&2 'You need curl, or wget to add git_aliases'
exit 1
fi
else
xyz_echo >&2 "The environment variable \$METHOD is set to \"${METHOD}\", which is not recognized as a valid installation method."
exit 1
fi
# local PROFILE_INSTALL_DIR
# PROFILE_INSTALL_DIR="$(xyz_install_path | command sed "s:^$HOME:\$HOME:")"
local XYZ_INCLUDE_STRING
XYZ_INCLUDE_STRING="\\n# Load git-aliases\\n[ -s \"$XYZ_FULL_PATH\" ] && \\. \"$XYZ_FULL_PATH\" # this loads git_aliases\\n"
local XYZ_PROFILE
XYZ_PROFILE="$(xyz_detect_profile)"
# check & append xyz file to profile file, it will load with terminal
BASH_OR_ZSH=false
if [ -z "${XYZ_PROFILE-}" ]; then
local TRIED_PROFILE
if [ -n "${PROFILE}" ]; then
TRIED_PROFILE="${XYZ_PROFILE} (as defined in \$PROFILE), "
fi
xyz_echo "=> Profile not found. Tried ${TRIED_PROFILE-}~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile."
xyz_echo "=> Create one of them and run this script again"
xyz_echo " OR"
xyz_echo "=> Append the following lines to the correct file yourself:"
command printf "${XYZ_INCLUDE_STRING}"
xyz_echo
else
if xyz_profile_is_bash_or_zsh "${XYZ_PROFILE-}"; then
BASH_OR_ZSH=true
fi
if ! command grep -qc "$XYZ_FULL_PATH" "$XYZ_PROFILE"; then
xyz_echo "=> Appending source string to $XYZ_PROFILE"
command printf "${XYZ_INCLUDE_STRING}" >>"$XYZ_PROFILE"
else
xyz_echo "=> source string already in ${XYZ_PROFILE}"
fi
fi
# execute aliases
# shellcheck source=/dev/null
\. "$XYZ_FULL_PATH"
# warning
xyz_echo "=> Close and reopen your terminal to start using git_aliases or run the following to use it now:"
command printf "${XYZ_INCLUDE_STRING}"
if ${BASH_OR_ZSH}; then
command printf "${COMPLETION_STR}"
fi
# unsetting variables
xyz_reset
}
#
# Unsets the various functions defined
# during the execution of the install script
#
xyz_reset() {
unset -f xyz_has xyz_echo xyz_grep xyz_download xyz_profile_is_bash_or_zsh \
xyz_try_profile xyz_detect_profile xyz_default_install_path xyz_install_path \
xyz_latest_version xyz_file_name xyz_source install_xyz_as_script xyz_do_install \
xyz_reset
}
[ "_$XYZ_ENV" = "_testing" ] || xyz_do_install
} # this ensures the entire script is downloaded #