Skip to content

Instantly share code, notes, and snippets.

@ppdeassis
Last active March 6, 2019 12:32
Show Gist options
  • Save ppdeassis/eccd32133bdfe0476ffa09b0e4832fce to your computer and use it in GitHub Desktop.
Save ppdeassis/eccd32133bdfe0476ffa09b0e4832fce to your computer and use it in GitHub Desktop.
Setup dev environment on macOS
#!/bin/bash
#
# \curl https://gist.githubusercontent.com/ppdeassis/601d9191c76337b94909/raw/92f4af629ee7d8d58022c4711b77e1fbd0f560e4/ruby_osx_setup.sh | bash
#
# If any command fails, we should immediately fail too
set -e
#
#
BASH_PROFILE="$HOME/.profile"
add_to_bash_profile() {
local line="$1"
if ! grep --quiet "$line" "$BASH_PROFILE"; then
echo "$line" >> "$BASH_PROFILE"
fi
}
brew_cask_install() {
local cask="$1"
if ! brew cask list | grep --quiet --line-regexp "$cask"; then
echo "Installing cask $cask"
brew cask install "$cask"
else
echo "Cask $cask already installed. Skipping..."
fi
}
brew_install() {
local formula="$1"
if ! brew list | grep --quiet --line-regexp "$formula"; then
echo "Installing formula $formula"
brew install "$formula"
else
echo "Formula $formula already installed. Skipping..."
fi
}
generate_ssh_keys() {
DIRECTORY=~/.ssh
KEY_PATH=~/.ssh/id_rsa
if [ ! -d "$DIRECTORY" ]; then
mkdir $DIRECTORY
chmod -R 0700 $DIRECTORY
fi
if [ -f "$KEY_PATH" ]; then
echo "SSH keys already exists, skipping..."
else
ssh-keygen -t rsa -f "$KEY_PATH" -N ''
fi
}
install_command_line_tools() {
if xcode-select -p > /dev/null; then
echo "Command Line Tools already installed. Skipping..."
else
# From https://github.com/rtrouton/rtrouton_scripts/blob/master/rtrouton_scripts/install_xcode_command_line_tools/install_xcode_command_line_tools.sh
local macos_version=$(sw_vers -productVersion | awk -F "." '{print $2}')
local tmp_file="/tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress"
# Create the placeholder file which is checked by the softwareupdate tool
# before allowing the installation of the Xcode command line tools.
touch "$tmp_file"
# Identify the correct update in the Software Update feed with "Command Line Tools" in the name for the OS version in question.
if [[ "$macos_version" -gt 9 ]]; then
cmd_line_tools=$(softwareupdate -l | awk '/\*\ Command Line Tools/ { $1=$1;print }' | grep "$macos_version" | sed 's/^[[ \t]]*//;s/[[ \t]]*$//;s/*//' | cut -c 2-)
elif [[ "$macos_version" -eq 9 ]]; then
cmd_line_tools=$(softwareupdate -l | awk '/\*\ Command Line Tools/ { $1=$1;print }' | grep "Mavericks" | sed 's/^[[ \t]]*//;s/[[ \t]]*$//;s/*//' | cut -c 2-)
fi
#Install the command line tools
softwareupdate -i "$cmd_line_tools" --verbose
# Remove the temp file
if [[ -f "$tmp_file" ]]; then
rm "$tmp_file"
fi
fi # end installing commnad line tools
}
install_fonts() {
brew tap caskroom/fonts
brew_cask_install font-hack
brew_cask_install font-open-sans
}
install_homebrew() {
if ! command -v brew > /dev/null; then
echo "Installing Homebrew"
curl --silent \
--fail \
--show-error \
--location \
https://raw.githubusercontent.com/Homebrew/install/master/install | ruby
source "$BASH_PROFILE"
brew doctor
else
echo "Homebrew already installed. Skipping..."
fi
}
install_postgres() {
if ! brew list | grep --quiet --line-regexp "postgresql"; then
brew_install postgresql
local pgdatadir=/usr/local/var/postgres
# removing brew initialized cluster
rm -rf $pgdatadir
# creating a new one, with custom encoding and locale
initdb --encoding=UTF-8 --locale=pt_BR.UTF-8 --lc-messages=en_US.UTF-8 --username=postgres $pgdatadir
# creating 'caiena' role
pg_ctl -D $pgdatadir start
# waiting for server to start
sleep 5
psql --username=postgres --command="CREATE ROLE caiena SUPERUSER CREATEDB CREATEROLE LOGIN"
pg_ctl -D $pgdatadir stop
fi
}
install_rvm() {
if [ ! -d $HOME/.rvm ]; then
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
curl -sSL https://get.rvm.io | bash -s stable --autolibs=homebrew
source ~/.rvm/scripts/rvm
rvm install ruby --latest
# skipping doc when installing gems
touch ~/.gemrc
local nodoc="gem: --no-ri --no-rdoc --no-document"
if ! grep --quiet "$nodoc" ~/.gemrc; then
echo "$nodoc" >> ~/.gemrc
fi
else
echo "rvm already installed. Skipping..."
fi
}
install_terminal_solarized() {
echo "Installing Solarized Dark color theme for Terminal.app"
echo " - It will open a new terminal window with the theme."
echo " - Feel free to close it."
curl -sSL https://raw.githubusercontent.com/altercation/solarized/master/osx-terminal.app-colors-solarized/Solarized%20Dark%20ansi.terminal > /tmp/"Solarized Dark.terminal"
open /tmp/"Solarized Dark.terminal"
}
# we expect a .profile to exist
touch "$BASH_PROFILE"
install_command_line_tools
install_homebrew
brew_install git
brew_install sqlite
brew_install imagemagick
brew_install bash-completion
brew_install yarn
add_to_bash_profile "# yarn - adding to path"
add_to_bash_profile 'export PATH="$PATH:`yarn global bin`'
# extra config needed for bash-completion to work properly
if ! grep --quiet "etc/bash_completion" "$BASH_PROFILE"; then
cat <<-'EOF' >> ~/.profile
# enabling bash-completion
if [ -f $(brew --prefix)/etc/bash_completion ]; then
source $(brew --prefix)/etc/bash_completion
fi
EOF
fi
brew_install gpg
install_postgres
brew_install phantomjs
brew_install caskroom/cask/brew-cask
brew tap caskroom/versions
brew_cask_install firefox
brew_cask_install google-chrome
brew_cask_install slack
brew_cask_install sublime-text3
install_rvm
generate_ssh_keys
install_fonts
# adding solarized to terminal
install_terminal_solarized
# disable accentuation on key holding
defaults write -g ApplePressAndHoldEnabled -bool false
# Fast keyboard repeat rate
defaults write NSGlobalDomain KeyRepeat -int 2
# Shorter delay until key repeat
# defaults write NSGlobalDomain InitialKeyRepeat -int 20
# Enable inspect element in web views
# defaults write NSGlobalDomain WebKitDeveloperExtras -bool true
# Allow using tabs to cycle between prompts buttons
defaults write NSGlobalDomain AppleKeyboardUIMode -int 3
# Always show file extensions
defaults write NSGlobalDomain AppleShowAllExtensions -bool true
# Don't create DS_Store when connecting to network volumes
defaults write com.apple.desktopservices DSDontWriteNetworkStores -bool true
# Don't always ask for using connected external drive with Timemachine
defaults write com.apple.TimeMachine DoNotOfferNewDisksForBackup -bool true
# Turn off keyboard illumination when it is not touched for five minutes
# defaults write com.apple.BezelServices kDimTime -int 300
# Disabling smart quotes substitution
defaults write NSGlobalDomain NSAutomaticQuoteSubstitutionEnabled -bool false
defaults write com.apple.TextEdit SmartQuotes -bool false
# Disabling smart dashes substitution
defaults write NSGlobalDomain NSAutomaticDashSubstitutionEnabled -bool false
defaults write com.apple.TextEdit SmartDashes -bool false
# Disabling autocorrect
defaults write -g NSAutomaticSpellingCorrectionEnabled -bool false
# Disabling Gatekeeper (will prompt for password)
sudo spctl --master-disable
echo "Setup complete. Enjoy! :)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment