-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap
executable file
·216 lines (170 loc) · 5.85 KB
/
bootstrap
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
#!/bin/bash
# Heavily based on thoughtbot's laptop script https://github.com/thoughtbot/laptop/blob/master/mac
OSX_BOOTSTRAP=${OSX_BOOTSTRAP:-"$HOME/.osx-bootstrap"}
fancy_echo() {
local fmt="$1"; shift
# shellcheck disable=SC2059
printf "\n$fmt\n" "$@"
}
shellrc_path() {
local path current_shell
current_shell="$( basename "$SHELL" )"
if [[ $current_shell = "zsh" ]]; then
if [[ -w "$HOME/.zshrc.local" ]]; then
path="$HOME/.zshrc.local"
else
path="$HOME/.zshrc"
fi
elif [[ "$current_shell" = "bash" ]]; then
path="$HOME/.bash_profile"
else
echo "/dev/stdout"
return 1
fi
if [[ ! -e "$path" ]]; then
touch "$path"
fi
if [[ ! -w "$path" ]]; then
fancy_echo "Cannot write to $path"
echo "/dev/stdout"
return 1
fi
echo "$path"
}
append_to_shellrc() {
local text="$1" rcpath
local skip_new_line="${2:-0}"
rcpath="$(shellrc_path)"
if ! grep -Fqs "$text" "$rcpath" >/dev/null 2>&1; then
if [[ "$skip_new_line" -eq 1 ]]; then
printf "%s\n" "$text" >> "$rcpath"
else
printf "\n%s\n" "$text" >> "$rcpath"
fi
fi
}
# die MESSAGE: logs MESSAGE and then exits non-zero
#
# Example:
#
# writable "$directory" || die "not writable: $directory"
die() {
local message
# ${FUNCNAME[1]} evaluates to the name of the calling function.
message="${FUNCNAME[1]}: fatal error: *** $1"
echo "$message" 1>&2
exit 1
}
announce_editing_shell_runconfig() {
startup_path=$(shellrc_path)
# shellcheck disable=SC2181
if [[ "$?" -ne 0 ]]; then
fancy_echo "Don't know how to hack your $SHELL rcfile. Intended changes will be logged using bash syntax; feel free to adapt to your shell."
else
fancy_echo "Shell startup configuration will be added to: $startup_path"
fi
append_to_shellrc '# Added by Big Nerd Ranch iOS/Mac Machine Bootstrap Script'
}
ensure_homebrew_prefix_usable() {
echo "$FUNCNAME: Your Homebrew prefix is ${HOMEBREW_PREFIX:-/usr/local}. Let's ensure it's usable by Homebrew (it exists as a directory, and you have read access)."
HOMEBREW_PREFIX="$(brew --prefix)"
if [[ ! -e "$HOMEBREW_PREFIX" ]]; then
HOMEBREW_PREFIX="/usr/local"
fi
current_group_name="$(id -ng)"
if [[ -d "$HOMEBREW_PREFIX" ]]; then
# brew update requires that HOMEBREW_PREFIX be readable.
if [[ ! -r "$HOMEBREW_PREFIX" ]]; then
# chown -R was taking a glacial age to run,
# especially when the actual problem was the Google App Engine installer
# had decided that /usr/local itself should be owned by root:wheel.
# Thanks, Google!
#
# The -R looks like it works bottom-up, since it hadn't affected the
# actual target directory when I bailed, so let's hit that first
# in case the user decides to bail out during.
fancy_echo "Your password is required to run:
chown $LOGNAME:$current_group_name $HOMEBREW_PREFIX
&& chown -R $LOGNAME:$current_group_name $HOMEBREW_PREFIX/*"
sudo chown "$LOGNAME:$current_group_name" "$HOMEBREW_PREFIX" \
&& sudo chown -R "$LOGNAME:$current_group_name" "$HOMEBREW_PREFIX" \
|| die "failed to change ownership of $HOMEBREW_PREFIX"
fi
else
fancy_echo "Your password is required to run:
mkdir $HOMEBREW_PREFIX
&& chflags norestricted $HOMEBREW_PREFIX
&& chown -R $LOGNAME:$current_group_name $HOMEBREW_PREFIX"
sudo mkdir "$HOMEBREW_PREFIX" \
&& sudo chflags norestricted "$HOMEBREW_PREFIX" \
&& sudo chown -R "$LOGNAME:$current_group_name" "$HOMEBREW_PREFIX" \
|| die "failed to make $HOMEBREW_PREFIX usable"
fi
echo "$FUNCNAME: $HOMEBREW_PREFIX should be usable by homebrew now."
}
update_homebrew() {
if ! command -v brew >/dev/null; then
fancy_echo "$FUNCNAME: Installing Homebrew, a handy OS X package manager (see http://brew.sh)."
curl -fsS \
'https://raw.githubusercontent.com/Homebrew/install/master/install' | ruby
append_to_shellrc '# recommended by brew doctor'
# shellcheck disable=SC2016
append_to_shellrc 'export PATH="$HOMEBREW_PREFIX/bin:$PATH"' 1
export PATH="$HOMEBREW_PREFIX/bin:$PATH"
fi
if brew list | grep -Fq brew-cask; then
fancy_echo "$FUNCNAME: Looks like you still have the old brew-cask formula installed. Since December 2015, brew-cask is updated along with Homebrew itself, and the old formula should be installed. Let's take care of that. (If you want to read more, see: https://github.com/caskroom/homebrew-cask/pull/15381)"
fancy_echo "Uninstalling old Homebrew-Cask ..."
brew uninstall --force brew-cask
fi
echo "$FUNCNAME: Updating local package repository"
brew update
}
find_latest_ruby() {
rbenv install -l | grep -v - | tail -1 | sed -e 's/^ *//'
}
clone_repo(){
if [ ! -d "$OSX_BOOTSTRAP" ]
then
git clone -b master https://github.com/austek/osx-bootstrap.git "$OSX_BOOTSTRAP"
fi
}
install_packages() {
brew install git
clone_repo
cd "$OSX_BOOTSTRAP" || (echo "$OSX_BOOTSTRAP couldn't be found exiting" && exit)
echo "$FUNCNAME: Installing/updating essentials:"
cat ./Brewfile
brew bundle --file=./Brewfile --verbose
if [[ "$1" -eq "sample" ]]
then
cp "$OSX_BOOTSTRAP/sample/.Brewfile" "$OSX_BOOTSTRAP/sample/.bootstrap.local" ~/
fi
if [[ -r "$HOME/.Brewfile" ]]; then
echo "$FUNCNAME: Installing/upgrading formulae listed in $HOME/.Brewfile."
brew bundle --global --verbose
fi
}
run_bootstrap_local() {
if [[ -f "$HOME/.bootstrap.local" ]]; then
fancy_echo "Running your customizations from ~/.bootstrap.local..."
# shellcheck disable=SC1091
. "$HOME/.bootstrap.local"
fi
}
install_or_update_gem() {
if gem list "$1" --installed > /dev/null; then
gem update "$@"
else
gem install "$@"
fi
rbenv rehash
}
main() {
announce_editing_shell_runconfig
ensure_homebrew_prefix_usable
update_homebrew
install_packages "$@"
run_bootstrap_local
}
main "$@"