#!/bin/bash PLATFORM= HOMEBREW= readonly OSX_GITHUB="https://github.com/nobv/provisioning.git" readonly PROVISIONINGPATH=~/.provisioning readonly DOTPATH=~/.dotfiles function info() { printf "\e[37;1m%s\e[m\n" "$*" } function successs() { printf "\e[36m%s\e[m\n" "✔ $*" } function error() { printf "\e[31;2m%s\e[m\n" "✖ $*" } function is_exists() { which "$1" >/dev/null 2>&1 return $? } function os_type() { uname | tr "[:upper:]" "[:lower:]" } function os_judgment() { case "$(os_type)" in *'linux'*) PLATFORM='linux' HOMEBREW='sh -c "$(curl -fsSL https://raw.githubusercontent.com/Linuxbrew/install/master/install.sh)"' ;; *'darwin'*) PLATFORM='osx' HOMEBREW='/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"' ;; *'bsd'*) PLATFORM='bsd' HOMEBREW='' ;; *) PLATFORM='unknown' HOMEBREW='' ;; esac export PLATFORM } function is_osx() { os_judgment if [ "${PLATFORM}" = "osx" ]; then return 0 else return 1 fi } function is_linux() { os_judgment if [ "${PLATFORM}" = "linux" ]; then return 0 else return 1 fi } function print_in_color() { printf "%b" \ "$(tput setaf "$2" 2> /dev/null)" \ "$1" \ "$(tput sgr0 2> /dev/null)" } function print_in_green() { print_in_color "$1" 2 } function print_successs() { print_in_green " [✔] $1\n" } function are_xcode_command_line_tools_installed() { xcode-select --print-path &> /dev/null } function kill_all_subprocesses() { local i="" for i in $(jobs -p); do kill "$i" wait "$i" &> /dev/null done } function print_result() { if [ "$1" -eq 0 ]; then print_successs "$2" else print_error "$2" fi return "$1" } function print_error_stream() { while read -r line; do print_error "↳ ERROR: $line" done } function execute() { local -r CMDS="$1" local -r MSG="${2:-$1}" local -r TMP_FILE="$(mktemp /tmp/XXXXX)" local exitCode=0 local cmdsPID="" # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # If the current process is ended, # also end all its subprocesses. set_trap "EXIT" "kill_all_subprocesses" # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Execute commands in background eval "$CMDS" \ &> /dev/null \ 2> "$TMP_FILE" & cmdsPID=$! # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Show a spinner if the commands # require more time to complete. show_spinner "$cmdsPID" "$CMDS" "$MSG" # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Wait for the commands to no longer be executing # in the background, and then get their exit code. wait "$cmdsPID" &> /dev/null exitCode=$? # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Print output based on what happened. print_result $exitCode "$MSG" if [ $exitCode -ne 0 ]; then print_error_stream < "$TMP_FILE" fi rm -rf "$TMP_FILE" # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - return $exitCode } function set_trap() { trap -p "$1" | grep "$2" &> /dev/null \ || trap '$2' "$1" } function show_spinner() { local -r FRAMES='/-\|' # shellcheck disable=SC2034 local -r NUMBER_OR_FRAMES=${#FRAMES} local -r CMDS="$2" local -r MSG="$3" local -r PID="$1" local i=0 local frameText="" # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Note: In order for the Travis CI site to display # things correctly, it needs special treatment, hence, # the "is Travis CI?" checks. if [ "$TRAVIS" != "true" ]; then # Provide more space so that the text hopefully # doesn't reach the bottom line of the terminal window. # # This is a workaround for escape sequences not tracking # the buffer position (accounting for scrolling). # # See also: https://unix.stackexchange.com/a/278888 printf "\n\n\n" tput cuu 3 tput sc fi # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Display spinner while the commands are being executed. while kill -0 "$PID" &>/dev/null; do frameText=" [${FRAMES:i++%NUMBER_OR_FRAMES:1}] $MSG" # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Print frame text. if [ "$TRAVIS" != "true" ]; then printf "%s\n" "$frameText" else printf "%s" "$frameText" fi sleep 0.2 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Clear frame text. if [ "$TRAVIS" != "true" ]; then tput rc else printf "\r" fi done } function install_xcode_command_line_tools() { # If necessary, prompt user to install # the `Xcode Command Line Tools`. xcode-select --install &> /dev/null # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Wait until the `Xcode Command Line Tools` are installed. execute \ "until are_xcode_command_line_tools_installed; do \ sleep 5; \ done" \ "Xcode Command Line Tools" } function initialize() { os_judgment if ! is_exsits "brew"; then info "==> installing Homebrew..." eval ${HOMEBREW} brew update brew upgrade --all --cleanup brew -v success "Homebrew" fi if [ ! -d "${DOTPATH}" ]; then info "==> Downloading dotfiles..." cd ${HOME} echo ${PWD} #bash -c "$(curl -L https://github.com/Nobv/dotfiles.git)" git clone https://github.com/nobv/dotfiles.git ${DOTPATH} cd ${DOTPATH} sh install.sh success "dotfiles" fi } # Download function download() { if [ -d ${PROVISIONINGPATH} ];then error "${PROVISIONINGPATH} is already exists." exit 1 fi install_xcode_command_line_tools # TODO: when git is not exists info "Downloading provisioning file..." git clone "${OSX_GITHUB}" ${PROVISIONINGPATH} success "Download" } function install() { download initialize cd ${PROVISIONINGPATH} info "Starting install from anyenv..." make anyenv success "anyenv" info "Starting install from brew, cask and mas..." make bundle success "bundle" info "Starting install from other..." make download success "download" info "Starting macos settings..." make macos_setting success "macos settings" #info "Starting macos update..." #make macos_update } install