diff --git a/bin/dotfiles b/bin/dotfiles index 00bc7338a8..98e9825eca 100755 --- a/bin/dotfiles +++ b/bin/dotfiles @@ -127,21 +127,22 @@ function _prompt_menu_add() { _prompt_menu_result=("${_prompt_menu_result[@]}" "${menu_options[i]}") } -# Given a list of desired items and installed items, return a list -# of uninstalled items. Arrays in bash are insane (not in a good way). +# Given strings containing space-delimited words A and B, "setdiff A B" will +# return all words in A that do not exist in B. Arrays in bash are insane +# (and not in a good way). # From http://stackoverflow.com/a/1617303/142339 -function to_install() { - local debug oldifs desired installed remain a +function setdiff() { + local debug oldifs A B C x if [[ "$1" == 1 ]]; then debug=1; shift; fi # Convert args to arrays. - desired=($1); installed=($2); remain=() - for a in "${desired[@]}"; do - [[ "${installed[*]}" =~ (^| )$a($| ) ]] || remain=("${remain[@]}" "$a") + A=($1); B=($2); C=() + for x in "${A[@]}"; do + [[ "${B[*]}" =~ (^| )$x($| ) ]] || C=("${C[@]}" "$x") done - [[ "$debug" ]] && for a in desired installed remain; do - echo "$a ($(eval echo "\${#$a[*]}")) $(eval echo "\${$a[*]}")" 1>&2 + [[ "$debug" ]] && for x in A B C; do + echo "$x ($(eval echo "\${#$x[*]}")) $(eval echo "\${$x[*]}")" 1>&2 done - echo "${remain[@]}" + echo "${C[@]}" } # If this file was being sourced, exit now. diff --git a/init/20_osx_homebrew.sh b/init/20_osx_homebrew.sh index 2d4bc8cc53..eb87862ccd 100644 --- a/init/20_osx_homebrew.sh +++ b/init/20_osx_homebrew.sh @@ -18,7 +18,7 @@ brew update # Tap Homebrew kegs. function brew_tap_kegs() { - kegs=($(to_install "${kegs[*]}" "$(brew tap)")) + kegs=($(setdiff "${kegs[*]}" "$(brew tap)")) if [[ ${#kegs[@]} != 0 ]]; then e_header "Tapping Homebrew kegs: ${kegs[@]}" for keg in "${kegs[@]}"; do @@ -29,7 +29,7 @@ function brew_tap_kegs() { # Install Homebrew recipes. function brew_install_recipes() { - recipes=($(to_install "${recipes[*]}" "$(brew list)")) + recipes=($(setdiff "${recipes[*]}" "$(brew list)")) if [[ ${#recipes[@]} != 0 ]]; then e_header "Installing Homebrew recipes: ${recipes[@]}" for recipe in "${recipes[@]}"; do diff --git a/init/30_osx_homebrew_casks.sh b/init/30_osx_homebrew_casks.sh index 12eef577ee..6f9621c7c4 100644 --- a/init/30_osx_homebrew_casks.sh +++ b/init/30_osx_homebrew_casks.sh @@ -67,7 +67,7 @@ casks=( ) # Install Homebrew casks. -casks=($(to_install "${casks[*]}" "$(brew cask list 2>/dev/null)")) +casks=($(setdiff "${casks[*]}" "$(brew cask list 2>/dev/null)")) if [[ ${#casks[@]} != 0 ]]; then e_header "Installing Homebrew casks: ${casks[@]}" for cask in "${casks[@]}"; do diff --git a/init/50_node.sh b/init/50_node.sh index 6b283146ff..dafb92cd20 100644 --- a/init/50_node.sh +++ b/init/50_node.sh @@ -24,7 +24,7 @@ if [[ "$(type -P npm)" ]]; then npm update -g npm { pushd "$(npm config get prefix)/lib/node_modules"; installed=(*); popd; } > /dev/null - list="$(to_install "${npm_globals[*]}" "${installed[*]}")" + list="$(setdiff "${npm_globals[*]}" "${installed[*]}")" if [[ "$list" ]]; then e_header "Installing Npm modules: $list" npm install -g $list diff --git a/init/50_ruby.sh b/init/50_ruby.sh index 70010d8cdb..fb7fcd0130 100644 --- a/init/50_ruby.sh +++ b/init/50_ruby.sh @@ -5,7 +5,7 @@ source $DOTFILES/source/50_ruby.sh if [[ "$(type -P rbenv)" ]]; then versions=(2.1.3) # 2.0.0-p576 1.9.3-p547) - list="$(to_install "${versions[*]}" "$(rbenv whence ruby)")" + list="$(setdiff "${versions[*]}" "$(rbenv whence ruby)")" if [[ "$list" ]]; then e_header "Installing Ruby versions: $list" for version in $list; do rbenv install "$version"; done diff --git a/test/test_to_install.sh b/test/test_setdiff.sh similarity index 95% rename from test/test_to_install.sh rename to test/test_setdiff.sh index f386d2d402..0cb5653164 100755 --- a/test/test_to_install.sh +++ b/test/test_setdiff.sh @@ -4,7 +4,7 @@ source $DOTFILES/source/00_dotfiles.sh e_header "$(basename "$0" .sh)" function my_test() { - to_install "${desired[*]}" "${installed[*]}" + setdiff "${desired[*]}" "${installed[*]}" } desired=(a b c); installed=(); assert "a b c" my_test