Skip to content

Commit

Permalink
Rename to_install function to setdiff.
Browse files Browse the repository at this point in the history
  • Loading branch information
cowboy committed Oct 26, 2014
1 parent 79dfba9 commit 45688f9
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 16 deletions.
21 changes: 11 additions & 10 deletions bin/dotfiles
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions init/20_osx_homebrew.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion init/30_osx_homebrew_casks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion init/50_node.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion init/50_ruby.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/test_to_install.sh → test/test_setdiff.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 45688f9

Please sign in to comment.