#!/usr/bin/env bash set -eu set -o pipefail : "${DOTFILES_PATH:="$HOME/dotfiles"}" : "${DOTFILES_BRANCH:=master}" : "${DOTFILES_NOEDIT_SECRETS:=0}" #=== Steps #============================================================================================== print_header() { printf "\e[34m" echo '--------------------------------------------------------------------------------' echo ' ' echo ' 888 888 .d888 d8b 888 ' echo ' 888 888 d88P" Y8P 888 ' echo ' 888 888 888 888 ' echo ' .d88888 .d88b. 888888 888888 888 888 .d88b. .d8888b ' echo ' d88" 888 d88""88b 888 888 888 888 d8P Y8b 88K ' echo ' 888 888 888 888 888 888 888 888 88888888 "Y8888b. ' echo ' Y88b 888 Y88..88P Y88b. 888 888 888 Y8b. X88 ' echo ' "Y88888 "Y88P" "Y888 888 888 888 "Y8888 88888P" ' echo ' ' echo ' Harder, Better, Faster, Stronger ' echo ' ' echo ' github.com/creasty/dotfiles ' echo ' ' echo '--------------------------------------------------------------------------------' printf "\e[0m\n" } check_os() { if [ "$(uname -s)" != "Darwin" ]; then echo "Sorry, this script is intended only for OS X" exit 1 fi } clone_or_update_repo() { local git_dir="$DOTFILES_PATH/.git" if [ -d "$git_dir" ]; then echo 'Updating repo...' if [ "$(git --git-dir="$git_dir" symbolic-ref --short HEAD 2> /dev/null)" != "master" ]; then echo 'skip (working on a non-master branch)' return fi if ! git --git-dir="$git_dir" diff --no-ext-diff --quiet --exit-code > /dev/null 2>&1; then echo 'skip (unstaged changes present)' return fi if ! git --git-dir="$git_dir" diff-index --cached --quiet HEAD -- > /dev/null 2>&1; then echo 'skip (uncommitted changes present)' return fi git --git-dir="$git_dir" pull origin master git --git-dir="$git_dir" submodule sync echo 'done' elif ! [ -d "$DOTFILES_PATH" ]; then echo 'Cloning repo...' git clone --recursive https://github.com/creasty/dotfiles.git --branch "$DOTFILES_BRANCH" "$DOTFILES_PATH" echo 'done' fi } load_envs() { export ANYENV_INIT_CACHE_DISABLED=1 source "$DOTFILES_PATH/shell/profile" } check_xcode_license_approved() { echo 'Agreeing to Xcode license...' if ! [[ "$(/usr/bin/xcrun clang 2>&1 || true)" =~ 'license' ]]; then echo 'skip (already approved)' return fi sudo xcodebuild -license accept echo 'done' } install_homebrew() { command -v 'brew' > /dev/null 2>&1 && return echo 'Installing homebrew...' NONINTERACTIVE=1 HAVE_SUDO_ACCESS=1 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" echo 'done' } install_ansible() { command -v 'ansible' > /dev/null 2>&1 && return echo 'Installing ansible...' brew install ansible echo 'done' } install_rosetta2() { echo 'Installing Rosetta 2...' if [ -d /usr/libexec/rosetta ]; then echo 'skip (already installed)' return fi sudo softwareupdate --install-rosetta --agree-to-license echo 'done' } run_provisioning() { echo 'Provisioning...' cd "$DOTFILES_PATH/provisioning" if ! [ -f ./secrets.yml ]; then cp ./secrets{.sample,}.yml [ "$DOTFILES_NOEDIT_SECRETS" -eq 0 ] && vim ./secrets.yml fi ansible-playbook \ -i 'localhost,' \ --extra-vars "home_path=$HOME" \ --extra-vars "dotfiles_path=$DOTFILES_PATH" \ --extra-vars "homebrew_prefix=$HOMEBREW_PREFIX" \ --extra-vars '@config.yml' \ --extra-vars '@secrets.yml' \ "$@" \ playbook.yml echo 'done' } #=== Entrypoint #============================================================================================== main() { print_header check_os clone_or_update_repo check_xcode_license_approved load_envs install_homebrew install_ansible install_rosetta2 run_provisioning "$@" } main "$@"