|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# brew-cask |
| 4 | +# |
| 5 | +# bash shim to invoke brew-cask-cmd.rb |
| 6 | +# |
| 7 | + |
| 8 | +### |
| 9 | +### settings |
| 10 | +### |
| 11 | + |
| 12 | +set -e |
| 13 | +set -o pipefail |
| 14 | +set +o histexpand |
| 15 | +set -o nounset |
| 16 | +shopt -s nocasematch |
| 17 | +shopt -s nullglob |
| 18 | +shopt -s dotglob |
| 19 | + |
| 20 | +### |
| 21 | +### functions |
| 22 | +### |
| 23 | + |
| 24 | +warn () { |
| 25 | + local message="$@" |
| 26 | + message="${message//\\t/$'\011'}" |
| 27 | + message="${message//\\n/$'\012'}" |
| 28 | + message="${message%"${message##*[![:space:]]}"}" |
| 29 | + printf "%s\n" "$message" 1>&2 |
| 30 | +} |
| 31 | + |
| 32 | +die () { |
| 33 | + warn "$@" |
| 34 | + exit 1 |
| 35 | +} |
| 36 | + |
| 37 | +resolve_dir () { |
| 38 | + local resolved="$(for dir; do cd "$dir"; done && /bin/pwd -P)" |
| 39 | + if [[ -d "$resolved" ]]; then |
| 40 | + printf "%s" "$resolved" |
| 41 | + else |
| 42 | + die "cannot resolve: '$@'" |
| 43 | + fi |
| 44 | +} |
| 45 | + |
| 46 | +ensure_dir () { |
| 47 | + local dir="$1" |
| 48 | + local message |
| 49 | + shift |
| 50 | + |
| 51 | + if [ "$#" -gt 0 ]; then |
| 52 | + message="$@" |
| 53 | + else |
| 54 | + message="brew-cask: no such directory: '$dir'" |
| 55 | + fi |
| 56 | + if ! [[ -d "$dir" ]]; then |
| 57 | + die "$message" |
| 58 | + fi |
| 59 | +} |
| 60 | + |
| 61 | +ensure_file () { |
| 62 | + local file="$1" |
| 63 | + local message |
| 64 | + shift |
| 65 | + |
| 66 | + if [ "$#" -gt 0 ]; then |
| 67 | + message="$@" |
| 68 | + else |
| 69 | + message="brew-cask: no such file: '$file'" |
| 70 | + fi |
| 71 | + if ! [[ -f "$file" ]]; then |
| 72 | + die "$message" |
| 73 | + fi |
| 74 | +} |
| 75 | + |
| 76 | +ensure_executable () { |
| 77 | + local executable="$1" |
| 78 | + local message |
| 79 | + shift |
| 80 | + |
| 81 | + if [ "$#" -gt 0 ]; then |
| 82 | + message="$@" |
| 83 | + else |
| 84 | + message="brew-cask: no such executable: '$executable'" |
| 85 | + fi |
| 86 | + if ! [[ -f "$executable" ]] || ! [[ -x "$executable" ]]; then |
| 87 | + die "$message" |
| 88 | + fi |
| 89 | +} |
| 90 | + |
| 91 | +find_ruby_2_plus () { |
| 92 | + declare -a rubies |
| 93 | + local favorite_ruby="/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby" |
| 94 | + local version_str |
| 95 | + |
| 96 | + if [[ -x "$favorite_ruby" ]]; then |
| 97 | + printf "%s" "$favorite_ruby" |
| 98 | + else |
| 99 | + IFS=$'\n' rubies=( $(/usr/bin/type -aP ruby) \ |
| 100 | + "/usr/local/bin/ruby" \ |
| 101 | + "$(brew --prefix 2>/dev/null)/bin/ruby" ) |
| 102 | + for ruby in "${rubies[@]}"; do |
| 103 | + version_str="$("$ruby" --version 2>/dev/null)" |
| 104 | + if [[ "$version_str" =~ ^ruby.2 ]]; then |
| 105 | + printf "%s" "$ruby" |
| 106 | + break |
| 107 | + fi |
| 108 | + done |
| 109 | + fi |
| 110 | +} |
| 111 | + |
| 112 | +### |
| 113 | +### main |
| 114 | +### |
| 115 | + |
| 116 | +_brew_cask () { |
| 117 | + local script_dir |
| 118 | + local symlink_target |
| 119 | + local symlink_target_dir |
| 120 | + local brewcask_lib_dir |
| 121 | + local brewcask_command |
| 122 | + local interpreter |
| 123 | + declare -a ruby_opts |
| 124 | + |
| 125 | + ruby_opts=( "-W0" "-EUTF-8:UTF-8" ) |
| 126 | + |
| 127 | + script_dir="$(resolve_dir "${0%/*}")" |
| 128 | + ensure_dir "$script_dir" \ |
| 129 | + "brew-cask: could not resolve script directory" |
| 130 | + |
| 131 | + # return "." in case we are not a link |
| 132 | + symlink_target="$(/usr/bin/readlink "$0" || echo ".")" |
| 133 | + |
| 134 | + # redefine script_dir because we are likely to be a relative link |
| 135 | + symlink_target_dir="$(/usr/bin/dirname "$symlink_target")" |
| 136 | + script_dir="$(resolve_dir "$script_dir" "$symlink_target_dir")" |
| 137 | + ensure_dir "$script_dir" \ |
| 138 | + "brew-cask: could not resolve script directory" |
| 139 | + |
| 140 | + # The Homebrew install process replaces the lib element below with rubylib |
| 141 | + brewcask_lib_dir="$(resolve_dir "$script_dir"/../lib)" |
| 142 | + ensure_dir "$brewcask_lib_dir" \ |
| 143 | + "brew-cask: could not resolve homebrew-cask library directory" |
| 144 | + |
| 145 | + brewcask_command="$brewcask_lib_dir/brew-cask-cmd.rb" |
| 146 | + ensure_file "$brewcask_command" \ |
| 147 | + "brew-cask: could not find brew-cask-cmd.rb in '$brewcask_lib_dir'" |
| 148 | + |
| 149 | + interpreter="$(find_ruby_2_plus)" |
| 150 | + ensure_executable "$interpreter" \ |
| 151 | + "brew-cask: could not find Ruby 2.0 or greater. Try 'brew install ruby'." |
| 152 | + |
| 153 | + exec "$interpreter" "${ruby_opts[@]}" "$brewcask_command" "${@}" |
| 154 | +} |
| 155 | + |
| 156 | +### |
| 157 | +### initialization |
| 158 | +### |
| 159 | + |
| 160 | +unset GEM_HOME |
| 161 | +unset GEM_PATH |
| 162 | + |
| 163 | +### |
| 164 | +### dispatch |
| 165 | +### |
| 166 | + |
| 167 | +_brew_cask "${@:-}" |
| 168 | + |
| 169 | +# |
0 commit comments