-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup
executable file
·125 lines (100 loc) · 2.56 KB
/
setup
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
#!/usr/bin/env bash
print_in_color() {
local color="$1"
shift
if [[ -z ${NO_COLOR+x} ]]; then
printf "$color%b\e[0m\n" "$*"
else
printf "%b\n" "$*"
fi
}
red() { print_in_color "\e[31m" "$*"; }
green() { print_in_color "\e[32m" "$*"; }
green_bold() { print_in_color "\e[1;32m" "$*"; }
blue() { print_in_color "\e[34m" "$*"; }
section() {
printf "\n=== %s\n" "$(green_bold "$@")"
}
copy() {
printf "%s => %s\n" "$(blue "$(printf '%-25s' "$1")")" "$2"
cp "$1" "$2"
}
sudo_copy() {
printf "%s => %s\n" "$(blue "$(printf '%-25s' "$1")")" "$2"
$sudo cp "$1" "$2"
}
copy_executable() {
sudo_copy "$1" "/usr/local/bin/"
$sudo chmod a+x "/usr/local/bin/$1"
}
copy_man() {
if [[ ! -d "/usr/local/share/man/man1/" ]]; then return; fi
for file in "$1"/*.1; do
sudo_copy "$file" "/usr/local/share/man/man1/"
done
if command_exist makewhatis; then
$sudo makewhatis /usr/local/man
fi
}
install_completions() {
cmd="$1"
dir="$2"
if [[ -d "/usr/share/bash-completion/completions" ]]; then
compdir="/usr/share/bash-completion/completions"
elif [[ -d "/usr/local/etc/bash_completion.d" ]]; then
compdir="/usr/local/etc/bash_completion.d"
elif command -v brew &>/dev/null && [[ -d "$(brew --prefix)/etc/bash_completion.d" ]]; then
compdir="$(brew --prefix)/etc/bash_completion.d"
else
compdir=''
fi
if [[ -n $compdir ]]; then
printf "copying to %s\n" "$compdir"
echo "$cmd" | $sudo tee "${compdir}/${dir}" >/dev/null
else
printf "%s %s\n" "$(red WARN)" "skipping completions installation, compdir not found"
echo " install it manually by adding this to your startup script:"
echo " $cmd"
fi
}
command_exist() {
[[ -x "$(command -v "$1")" ]]
}
need() {
command_exist "$1" || fail "Cannot run $1"
printf "%s found\n" "$(blue " $1")"
}
onerror() {
local exit_status=$?
printf "\n=== %s\n" "$(red "Aborted with exit status $exit_status")"
exit $exit_status
}
fail() {
printf "$(red 'ERR') %s\n" "$*"
return 1
}
initialize() {
set -e
trap 'onerror' ERR
# Figure out if we need sudo or not
sudo=''
if [[ $EUID -ne 0 ]]; then
sudo='sudo'
fi
repo="$1"
repo_url="https://github.com/${repo}.git"
pushd "$(mktemp -d)" >/dev/null
}
section "Initializing"
initialize "DannyBen/rush"
section "Checking for pre-requisites"
need git
section "Cloning $repo"
git clone --depth 1 "$repo_url" .
section "Copying files"
copy_executable 'rush'
copy_man 'doc'
section "Installing bash completions"
install_completions "eval \"\$(rush completions)\"" "rush"
section "Done"
rush --version