forked from DJToke1/laptop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mac
286 lines (236 loc) · 8.66 KB
/
mac
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
#!/bin/bash
# Welcome to the monfresh laptop script!
# Be prepared to turn your laptop (or desktop)
# into an awesome development machine.
fancy_echo() {
local fmt="$1"; shift
# shellcheck disable=SC2059
printf "\n$fmt\n" "$@"
}
append_to_file() {
local file="$1"
local text="$2"
if ! grep -qs "^$text$" "$file"; then
printf "\n%s\n" "$text" >> "$file"
fi
}
create_zshrc_and_set_it_as_shell_file() {
if [ ! -f "$HOME/.zshrc" ]; then
touch "$HOME/.zshrc"
fi
shell_file="$HOME/.zshrc"
}
create_bash_profile_and_set_it_as_shell_file() {
if [ ! -f "$HOME/.bash_profile" ]; then
touch "$HOME/.bash_profile"
fi
shell_file="$HOME/.bash_profile"
}
change_shell_to_zsh() {
fancy_echo "Please enter your password to continue."
echo "Note that there won't be visual feedback when you type your password."
echo "Type it slowly and press return, or press control-c to cancel the switch to zsh and exit the script."
create_zshrc_and_set_it_as_shell_file
chsh -s "$(which zsh)"
echo "Note that you can always switch back to ${bold}bash${normal} if you change your mind."
echo "The instructions for changing shells manually are available in the README."
}
# shellcheck disable=SC2154
trap 'ret=$?; test $ret -ne 0 && printf "failed\n\n" >&2; exit $ret' EXIT
set -e
if [ ! -d "$HOME/.bin/" ]; then
mkdir "$HOME/.bin"
fi
case "$SHELL" in
*/zsh) :
create_zshrc_and_set_it_as_shell_file
;;
*)
create_bash_profile_and_set_it_as_shell_file
if [ -z "$CI" ]; then
bold=$(tput bold)
normal=$(tput sgr0)
echo "Want to switch your shell from the default ${bold}bash${normal} to ${bold}zsh${normal}?"
echo "Both work fine for development, and ${bold}zsh${normal} has some extra "
echo "features for customization and tab completion."
echo "If you aren't sure or don't care, we recommend ${bold}zsh${normal}."
echo -n "Press ${bold}y${normal} to switch to zsh, ${bold}n${normal} to keep bash: "
read -r -n 1 response
if [ "$response" = "y" ]; then
fancy_echo "=== Getting ready to change your shell to zsh. ==="
if ! grep -qs "$(which zsh)" /etc/shells; then
echo "It looks like your version of zsh is missing from ${bold}/etc/shells${normal}."
echo "It must be added there manually before your shell can be changed."
echo "Open ${bold}/etc/shells${normal} with your favorite text editor,"
echo "then add ${bold}$(which zsh)${normal} in a new line and save the file."
echo -n "Once you've added it, press ${bold}y${normal} to continue, or ${bold}n${normal} to cancel: "
read -r -n 1 response
if [ "$response" = "y" ]; then
change_shell_to_zsh
else
fancy_echo "Shell will not be changed."
fi
else
change_shell_to_zsh
fi
else
fancy_echo "Shell will not be changed."
fi
else
fancy_echo "CI System detected, will not change shells"
fi
;;
esac
brew_is_installed() {
brew list -1 | grep -Fqx "$1"
}
tap_is_installed() {
brew tap -1 | grep -Fqx "$1"
}
gem_install_or_update() {
if gem list "$1" | grep "^$1 ("; then
fancy_echo "Updating %s ..." "$1"
gem update "$@"
else
fancy_echo "Installing %s ..." "$1"
gem install "$@"
fi
}
app_is_installed() {
local app_name
app_name=$(echo "$1" | cut -d'-' -f1)
find /Applications -iname "$app_name*" -maxdepth 1 | egrep '.*' > /dev/null
}
latest_installed_ruby() {
find "$HOME/.rubies" -maxdepth 1 -name 'ruby-*' | tail -n1 | egrep -o '\d+\.\d+\.\d+'
}
switch_to_latest_ruby() {
# shellcheck disable=SC1091
. /usr/local/share/chruby/chruby.sh
chruby "ruby-$(latest_installed_ruby)"
}
append_to_file "$shell_file" "alias laptop='bash <(curl -s https://raw.githubusercontent.com/monfresh/laptop/master/laptop)'"
# shellcheck disable=SC2016
append_to_file "$shell_file" 'export PATH="$HOME/.bin:$PATH"'
if ! command -v brew >/dev/null; then
fancy_echo "Installing Homebrew ..."
curl -fsS \
'https://raw.githubusercontent.com/Homebrew/install/master/install' | ruby
# shellcheck disable=SC2016
append_to_file "$shell_file" 'export PATH="/usr/local/bin:$PATH"'
else
fancy_echo "Homebrew already installed. Skipping ..."
fi
# Remove brew-cask since it is now installed as part of brew tap caskroom/cask.
# See https://github.com/caskroom/homebrew-cask/releases/tag/v0.60.0
if brew_is_installed 'brew-cask'; then
brew uninstall --force 'brew-cask'
fi
if tap_is_installed 'caskroom/versions'; then
brew untap caskroom/versions
fi
fancy_echo "Updating Homebrew ..."
cd "$(brew --repo)" && git fetch && git reset --hard origin/master && brew update
fancy_echo "Verifying the Homebrew installation..."
if brew doctor; then
fancy_echo "Your Homebrew installation is good to go."
else
fancy_echo "Your Homebrew installation reported some errors or warnings."
echo "Review the Homebrew messages to see if any action is needed."
fi
fancy_echo "Installing formulas and casks from the Brewfile ..."
if brew bundle --file="$HOME/Brewfile"; then
fancy_echo "All formulas and casks were installed successfully."
else
fancy_echo "Some formulas or casks failed to install."
echo "This is usually due to one of the Mac apps being already installed,"
echo "in which case, you can ignore these errors."
fi
# shellcheck disable=SC2016
append_to_file "$shell_file" 'eval "$(hub alias -s)"'
append_to_file "$HOME/.gemrc" 'gem: --no-document'
if command -v rbenv >/dev/null || command -v rvm >/dev/null; then
fancy_echo 'We recommend chruby and ruby-install over RVM or rbenv'
else
if ! brew_is_installed "chruby"; then
fancy_echo 'Installing chruby, ruby-install, and the latest Ruby...'
brew bundle --file=- <<EOF
brew 'chruby'
brew 'ruby-install'
EOF
append_to_file "$shell_file" 'source /usr/local/share/chruby/chruby.sh'
append_to_file "$shell_file" 'source /usr/local/share/chruby/auto.sh'
ruby-install ruby
append_to_file "$shell_file" "chruby ruby-$(latest_installed_ruby)"
switch_to_latest_ruby
else
brew bundle --file=- <<EOF
brew 'chruby'
brew 'ruby-install'
EOF
fancy_echo 'Checking if a newer version of Ruby is available...'
switch_to_latest_ruby
ruby-install --latest > /dev/null
latest_stable_ruby="$(cat < "$HOME/.cache/ruby-install/ruby/stable.txt" | tail -n1)"
if ! [ "$latest_stable_ruby" = "$(latest_installed_ruby)" ]; then
fancy_echo "Installing latest stable Ruby version: $latest_stable_ruby"
ruby-install ruby
else
fancy_echo 'You have the latest version of Ruby'
fi
fi
fi
fancy_echo 'Updating Rubygems...'
gem update --system
gem_install_or_update 'bundler'
fancy_echo "Configuring Bundler ..."
number_of_cores=$(sysctl -n hw.ncpu)
bundle config --global jobs $((number_of_cores - 1))
fancy_echo '...Finished Ruby installation checks.'
no_prompt_customizations_in_shell_file() {
! grep -qs -e "PS1=" -e "precmd" -e "PROMPT=" "$shell_file"
}
no_zsh_frameworks() {
[ ! -d "$HOME/.oh-my-zsh" ] && [ ! -d "$HOME/.zpresto" ] && [ ! -d "$HOME/.zim" ] && [ ! -d "$HOME/.zplug" ]
}
if [ -z "$CI" ] && no_zsh_frameworks && no_prompt_customizations_in_shell_file; then
echo -n "Would you like to customize your prompt to display the current directory and ruby version? [y/n]: "
read -r -n 1 response
if [ "$response" = "y" ]; then
if ! grep -qs "prompt_ruby_info()" "$shell_file"; then
cat <<EOT >> "$shell_file"
prompt_ruby_info() {
if [ -f ".ruby-version" ]; then
cat .ruby-version
fi
}
EOT
fi
# shellcheck disable=SC2016
append_to_file "$shell_file" 'GREEN=$(tput setaf 65)'
# shellcheck disable=SC2016
append_to_file "$shell_file" 'ORANGE=$(tput setaf 166)'
# shellcheck disable=SC2016
append_to_file "$shell_file" 'NORMAL=$(tput sgr0)'
# display pwd in orange, current ruby version in green,
# and set prompt character to $
# shellcheck disable=SC2016
append_to_file "$shell_file" 'precmd () { PS1="${ORANGE}[%~] ${GREEN}$(prompt_ruby_info) ${NORMAL}$ " }'
# display directories and files in different colors when running ls
append_to_file "$shell_file" 'export CLICOLOR=1;'
append_to_file "$shell_file" 'export LSCOLORS=exfxcxdxbxegedabagacad;'
else
fancy_echo "Skipping prompt customization."
fi
fi
if app_is_installed 'GitHub'; then
fancy_echo "It looks like you've already configured your GitHub SSH keys."
fancy_echo "If not, you can do it by signing in to the GitHub app on your Mac."
elif [ ! -f "$HOME/.ssh/github_rsa.pub" ]; then
open ~/Applications/GitHub\ Desktop.app
fi
if [ -f "$HOME/.laptop.local" ]; then
# shellcheck source=/dev/null
. "$HOME/.laptop.local"
fi
fancy_echo 'All done!'