-
Notifications
You must be signed in to change notification settings - Fork 80
/
install-bashhub.sh
executable file
·339 lines (281 loc) · 8.51 KB
/
install-bashhub.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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/bin/bash -e
#
# Bashhub.com Installation shell script
#
# Ryan Caloras (ryan@bashhub.com)
#
# http://www.gnu.org/s/hello/manual/autoconf/Portable-Shell.html
#
# The only shell it won't ever work on is cmd.exe.
set -e
bash_profile_hook='
### Bashhub.com Installation.
### This Should be at the EOF. https://bashhub.com/docs
if [ -f ~/.bashhub/bashhub.sh ]; then
source ~/.bashhub/bashhub.sh
fi
'
zsh_profile_hook='
### Bashhub.com Installation
if [ -f ~/.bashhub/bashhub.zsh ]; then
source ~/.bashhub/bashhub.zsh
fi
'
fish_config_hook='
### Bashhub.com Installation
if [ -f "$HOME/.bashhub/bashhub.fish" ]
source "$HOME/.bashhub/bashhub.fish"
end
'
bash_config_source='
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
'
PYTHON_VERSION_COMMAND='
import sys
if (3, 6, 0) < sys.version_info < (3, 12, 0):
sys.exit(0)
elif (2, 7, 8) < sys.version_info < (3,0):
sys.exit(0)
else:
sys.exit(-1)'
# Prefer Python 3 versions over Python 2
PYTHON_VERSION_ARRAY=(
"/usr/bin/python3"
"python3"
"python3.11"
"python3.10"
"python3.9"
"python3.8"
"python3.7"
"python3.6"
"python"
"python2.7"
"python27"
"python2"
)
bashhub_config=~/.bashhub/config
backup_config=~/.bashhub.config.backup
zshprofile=~/.zshrc
fish_config="${XDG_CONFIG_HOME:-~/.config}/fish/config.fish"
# Optional parameter to specify a github branch
# to pull from.
github_branch=${1:-'2.4.1'}
install_bashhub() {
check_dependencies
check_already_installed
setup_bashhub_files
}
get_and_check_python_version() {
for python_version in "${PYTHON_VERSION_ARRAY[@]}"; do
if type "$python_version" &> /dev/null; then
if "$python_version" -c "$PYTHON_VERSION_COMMAND"; then
echo "$python_version"
return 0
fi
fi
done
return 1
}
# Boostrap virtualenv via zipapp
# Details https://virtualenv.pypa.io/en/latest/installation.html#via-zipapp
download_and_install_env() {
local python_command=$(get_and_check_python_version)
if [[ -z "$python_command" ]]; then
die "\n Sorry you need to have python 3.6-3.11 or 2.7.9+ installed. Please install it and rerun this script." 1
fi
# Set to whatever python interpreter you want for your first environment
PYTHON=$(which $python_command)
echo "Using Python path $PYTHON"
VERSION=20.10.0
VERSION_URL="https://github.com/pypa/get-virtualenv/raw/$VERSION/public/virtualenv.pyz"
# Alternatively use latest url for most recent that should be 2.7-3.9+
LATEST_URL="https://bootstrap.pypa.io/virtualenv/2.7/virtualenv.pyz"
curl -OL $VERSION_URL
# Create the first "bootstrap" environment.
$PYTHON virtualenv.pyz -q env
rm virtualenv.pyz
}
check_dependencies() {
if [ -z "$(get_and_check_python_version)" ]; then
die "\n Sorry can't seem to find a version of python 3.6-3.11 or 2.7.9+ installed" 1
fi
if [ -z "$(detect_shell_type)" ]; then
die "\n Sorry, couldn't detect your shell type. Bashhub only supports bash or zsh. Your defualt shell is $SHELL." 1
fi;
}
check_already_installed() {
if [ -e ~/.bashhub ]; then
echo -e "\nLooks like Bashhub is already installed.
\nLets go ahead and update it.\n"
# Copy our user credentials so we don't have to ask you for them again.
if [ -e "$bashhub_config" ]; then
cp "$bashhub_config" "$backup_config"
fi
rm -r ~/.bashhub
fi
}
install_hooks_for_zsh() {
# If we're using zsh, install our zsh hooks
if [ ! -e ~/.zshrc ]; then
die "No zshfile (.zshrc could be found)" 1
fi
# Add our file to our bashprofile if it doesn't exist yet
if grep -q "source ~/.bashhub/bashhub.zsh" "$zshprofile"
then
:
else
echo "$zsh_profile_hook" >> "$zshprofile"
fi
}
install_hooks_for_fish() {
if [ ! -e $fish_config ]; then
die "No fish config cound be found" 1
fi
if grep -q "source ~/.bashhub/bashhub.fish" "$fish_config"
then
:
else
echo "$fish_config_hook" >> "$fish_config"
fi
}
# Create two config files .bashrc and .bash_profile since
# OS X and Linux shells use them diffferently. Source .bashrc
# from .bash_profile and everything should work the same now.
generate_bash_config_file() {
touch ~/.bashrc
touch ~/.bash_profile
echo "$bash_config_source" >> ~/.bash_profile
echo "Created ~/.bash_profile and ~/.bashrc"
}
install_hooks_for_bash() {
local bashprofile=$(find_users_bash_file)
# If we don't have a bash profile ask if we should generate one.
if [ -z "$bashprofile" ]; then
echo "Couldn't find a bash confg file."
while true; do
read -p "Would you like to generate one? (y/n): " yn
case $yn in
[Yy]* ) generate_bash_config_file; bashprofile="$HOME/.bashrc"; break;;
[Nn]* ) break;;
* ) echo "Please answer yes or no.";;
esac
done
fi
# Have to have this. Error out otherwise.
if [ -z "$bashprofile" ]; then
die "No bashfile (e.g. .profile, .bashrc, etc) could be found." 1
fi
# Add our file to our bashprofile if it doesn't exist yet
if grep -q "source ~/.bashhub/bashhub.sh" $bashprofile
then
:
else
echo "$bash_profile_hook" >> $bashprofile
fi
}
detect_shell_type() {
if [ -n "$ZSH_VERSION" ]; then
echo 'zsh'
elif [ -n "$FISH_VERSION" ]; then
echo 'fish'
elif [ -n "$BASH_VERSION" ]; then
echo 'bash'
else
:
fi
}
install_hooks_for_shell() {
local shell_type
shell_type=$(detect_shell_type)
case $shell_type in
"zsh")
install_hooks_for_zsh
;;
"fish")
install_hooks_for_fish
;;
"bash")
install_hooks_for_bash
;;
*)
die "\n Bashhub only supports bash, fish, or zsh. Your default shell is $SHELL." 1
esac
}
setup_bashhub_files() {
mkdir -p ~/.bashhub
cd ~/.bashhub
download_and_install_env
# Grab the code from master off github.
echo "Pulling down bashhub-client from ${github_branch} branch"
curl -sL https://github.com/rcaloras/bashhub-client/archive/${github_branch}.tar.gz -o client.tar.gz
tar -xf client.tar.gz
cd bashhub-client*
# Copy over our dependencies.
cp -r bashhub/shell/deps ~/.bashhub/
# Copy over our bashhub sh and zsh files.
cp bashhub/shell/bashhub.* ~/.bashhub/
install_hooks_for_shell
# install our packages. bashhub and dependencies.
echo "Pulling down a few dependencies...(this may take a moment)"
../env/bin/pip -qq install .
# Check if we already have a config. If not run setup.
if [ -e $backup_config ]; then
cp "$backup_config" "$bashhub_config"
rm "$backup_config"
if ! ../env/bin/bashhub util update_system_info; then
# Run setup if we run into any issues updating our system info
../env/bin/bashhub setup
fi
else
# Setup our config file
../env/bin/bashhub setup
fi
# Wire up our bin directory
mkdir -p ~/.bashhub/bin
ln -sf ../env/bin/bashhub ~/.bashhub/bin/bashhub
ln -sf ../env/bin/bh ~/.bashhub/bin/bh
# Clean up what we downloaded
cd ~/.bashhub
rm client.tar.gz
rm -r bashhub-client*
# Make sure our config is only readable to us.
chmod 600 "$bashhub_config"
chmod 700 ~/.bashhub
if [ -e "$bashhub_config" ]; then
echo "Should be good to go! Please close and restart your terminal session."
else
echo "Please run 'bashhub setup' after restarting your terminal session."
fi
}
#
# Find a users active bash file based on
# which looks the largest. The idea being the
# largest is probably the one they actively use.
#
find_users_bash_file() {
bash_file_array=( ~/.bash_profile ~/.bashrc ~/.profile)
local largest_file_size=0
for file in "${bash_file_array[@]}"
do
if [ -e $file ]; then
# Get our file size.
local file_size=$(wc -c "$file" | awk '{print $1}')
if [ $file_size -gt $largest_file_size ]; then
local largest_file_size=$file_size
local largest_file=$file
fi
fi
done
# If we found the largest file, return it
if [ -n "$largest_file" ]; then
echo $largest_file
return 0
fi
}
die () { echo -e $1; exit $2; }
# Run our install so long as we're not in test.
if [[ -z "$bashhub_install_test" ]]; then
install_bashhub
fi;