|
1 |
| -#!/bin/sh |
| 1 | +#!/usr/bin/env bash |
2 | 2 |
|
3 |
| -if ! bundle -v &>/dev/null; then |
4 |
| - echo "You don't seem to have Bundler installed. Install it first and then re-run this script." |
5 |
| - exit 1 |
6 |
| -fi |
| 3 | +set -euo pipefail |
7 | 4 |
|
8 |
| -bundle install |
9 |
| -bundle exec appraisal install |
| 5 | +### PROJECT SETUP ############################################################## |
| 6 | + |
| 7 | +provision-project() { |
| 8 | + bundle exec appraisal install |
| 9 | +} |
| 10 | + |
| 11 | +### DON'T MODIFY ANYTHING BELOW THIS LINE! ##################################### |
| 12 | + |
| 13 | +# This setup script was generated with setup_script_generator 0.2.4, |
| 14 | +# available on RubyGems. |
| 15 | +# |
| 16 | +# To regenerate this section, install the gem and run: |
| 17 | +# |
| 18 | +# generate-setup -p ruby |
| 19 | +# |
| 20 | + |
| 21 | +# --- SETUP -------------------------------------------------------------------- |
| 22 | + |
| 23 | +something_already_printed=0 |
| 24 | + |
| 25 | +determine-platform() { |
| 26 | + local uname=$(uname) |
| 27 | + |
| 28 | + if [[ $uname == 'Darwin' ]]; then |
| 29 | + echo 'mac' |
| 30 | + else |
| 31 | + echo 'linux' |
| 32 | + fi |
| 33 | +} |
| 34 | + |
| 35 | +banner() { |
| 36 | + print-with-color 34 "== $@ ==" |
| 37 | +} |
| 38 | + |
| 39 | +success() { |
| 40 | + print-with-color 32 "$@" |
| 41 | +} |
| 42 | + |
| 43 | +warning() { |
| 44 | + print-with-color 33 "$@" |
| 45 | +} |
| 46 | + |
| 47 | +error() { |
| 48 | + print-with-color 31 "$@" |
| 49 | +} |
| 50 | + |
| 51 | +print-with-color() { |
| 52 | + pad-from-existing-output |
| 53 | + echo -ne "\033[${1}m" |
| 54 | + echo -n "${@:2}" |
| 55 | + echo -e "\033[0m" |
| 56 | + something_already_printed=1 |
| 57 | +} |
| 58 | + |
| 59 | +print-wrapped() { |
| 60 | + pad-from-existing-output |
| 61 | + echo -n "$@" | fmt -w 80 | cat |
| 62 | + something_already_printed=1 |
| 63 | +} |
| 64 | + |
| 65 | +pad-from-existing-output() { |
| 66 | + if [[ $something_already_printed -eq 1 ]]; then |
| 67 | + echo |
| 68 | + fi |
| 69 | +} |
| 70 | + |
| 71 | +print() { |
| 72 | + pad-from-existing-output |
| 73 | + echo "$@" |
| 74 | + something_already_printed=1 |
| 75 | +} |
| 76 | + |
| 77 | +has-executable() { |
| 78 | + type "$1" &>/dev/null |
| 79 | +} |
| 80 | + |
| 81 | +is-running() { |
| 82 | + pgrep "$1" >/dev/null |
| 83 | +} |
| 84 | + |
| 85 | +start() { |
| 86 | + if has-executable brew; then |
| 87 | + brew services start "$1" |
| 88 | + else |
| 89 | + sudo service "${2:-$1}" start |
| 90 | + fi |
| 91 | +} |
| 92 | + |
| 93 | +install() { |
| 94 | + local apt_package="" |
| 95 | + local rpm_package="" |
| 96 | + local brew_package="" |
| 97 | + local default_package="" |
| 98 | + local package="" |
| 99 | + |
| 100 | + for arg in "$@"; do |
| 101 | + case $arg in |
| 102 | + apt=*) |
| 103 | + apt_package="${arg#apt=}" |
| 104 | + ;; |
| 105 | + rpm=*) |
| 106 | + rpm_package="${arg#rpm=}" |
| 107 | + ;; |
| 108 | + brew=*) |
| 109 | + brew_package="${arg#brew=}" |
| 110 | + ;; |
| 111 | + *) |
| 112 | + default_package="$arg" |
| 113 | + ;; |
| 114 | + esac |
| 115 | + done |
| 116 | + |
| 117 | + if has-executable brew; then |
| 118 | + package="${brew_package:-$default_package}" |
| 119 | + |
| 120 | + if [[ -n $package ]]; then |
| 121 | + brew install "$package" |
| 122 | + fi |
| 123 | + elif has-executable apt-get; then |
| 124 | + package="${apt_package:-$default_package}" |
| 125 | + |
| 126 | + if [[ -n $package ]]; then |
| 127 | + sudo apt-get install -y "$package" |
| 128 | + fi |
| 129 | + elif has-executable yum; then |
| 130 | + package="${rpm_package:-$default_package}" |
| 131 | + |
| 132 | + if [[ -n $package ]]; then |
| 133 | + sudo yum install -y "$package" |
| 134 | + fi |
| 135 | + else |
| 136 | + error "Sorry, I'm not sure how to install $default_package." |
| 137 | + exit 1 |
| 138 | + fi |
| 139 | +} |
| 140 | + |
| 141 | +check-for-package-manager() { |
| 142 | + local platform=$(determine-platform) |
| 143 | + |
| 144 | + if [[ $platform == "linux" ]] && ! has-executable apt-get && ! has-executable yum; then |
| 145 | + # TODO: Check if build-essential is installed on Debian? |
| 146 | + # TODO: Check if 'Development Tools' group is installed on RedHat? |
| 147 | + |
| 148 | + error "You don't seem to have a package manager installed." |
| 149 | + print-wrapped "\ |
| 150 | +This setup script assumes you're using a flavor of Linux derived from Debian or |
| 151 | +RedHat (i.e. something with Apt or Yum). If this is not the case, then we would |
| 152 | +gladly take a PR fixing this!" |
| 153 | + exit 1 |
| 154 | + elif [[ $platform == "mac" ]] && ! has-executable brew; then |
| 155 | + # TODO: Check that OS X Command Line Tools are installed? |
| 156 | + |
| 157 | + error "You don't seem to have Homebrew installed." |
| 158 | + print-wrapped "\ |
| 159 | +Visit <https://brew.sh> and follow the instructions there, then re-run this |
| 160 | +script." |
| 161 | + exit 1 |
| 162 | + fi |
| 163 | +} |
| 164 | + |
| 165 | +install-development-libraries() { |
| 166 | + install rpm=zlib-devel |
| 167 | +} |
| 168 | + |
| 169 | +setup() { |
| 170 | + cd "$(dirname "$(dirname "$0")")" |
| 171 | + check-for-package-manager |
| 172 | + install-development-libraries |
| 173 | + run-provisions |
| 174 | + if type provision-project &>/dev/null; then |
| 175 | + provision-project |
| 176 | + fi |
| 177 | + success "Setup complete!" |
| 178 | +} |
| 179 | + |
| 180 | +# --- RUBY --------------------------------------------------------------------- |
| 181 | + |
| 182 | +provision-ruby() { |
| 183 | + USE_BUNDLER_1=0 |
| 184 | + |
| 185 | + if [[ -f .tool-versions ]]; then |
| 186 | + REQUIRED_RUBY_VERSION=$(cat .tool-versions | grep '^ruby ' | sed -Ee 's/^ruby (.+)$/\1/') |
| 187 | + elif [[ -f .ruby-version ]]; then |
| 188 | + REQUIRED_RUBY_VERSION=$(cat .ruby-version) |
| 189 | + else |
| 190 | + error "You don't seem to have a Ruby version set in your project." |
| 191 | + print-wrapped "\ |
| 192 | +You'll need to create either a .tool-versions file or .ruby-version file in your |
| 193 | +project before you can run this script." |
| 194 | + exit 1 |
| 195 | + fi |
| 196 | + |
| 197 | + ensure-ruby-development-libraries-installed |
| 198 | + ensure-ruby-installed |
| 199 | + ensure-project-ruby-dependencies-installed |
| 200 | +} |
| 201 | + |
| 202 | +ensure-ruby-development-libraries-installed() { |
| 203 | + local platform=$(determine-platform) |
| 204 | + |
| 205 | + if [[ $platform == "linux" ]]; then |
| 206 | + banner "Installing Ruby development libraries" |
| 207 | + install apt=ruby-dev rpm=ruby-devel |
| 208 | + fi |
| 209 | +} |
| 210 | + |
| 211 | +ensure-ruby-installed() { |
| 212 | + if has-executable asdf; then |
| 213 | + if ! (asdf current ruby | grep $REQUIRED_RUBY_VERSION'\>' &>/dev/null); then |
| 214 | + banner "Installing Ruby $REQUIRED_RUBY_VERSION with asdf" |
| 215 | + asdf install ruby $REQUIRED_RUBY_VERSION |
| 216 | + fi |
| 217 | + elif has-executable rbenv; then |
| 218 | + if ! (rbenv versions | grep $REQUIRED_RUBY_VERSION'\>' &>/dev/null); then |
| 219 | + banner "Installing Ruby $REQUIRED_RUBY_VERSION with rbenv" |
| 220 | + rbenv install --skip-existing "$REQUIRED_RUBY_VERSION" |
| 221 | + fi |
| 222 | + elif has-executable chruby-exec; then |
| 223 | + PREFIX='' source /usr/local/share/chruby/chruby.sh |
| 224 | + if ! (chruby '' | grep $REQUIRED_RUBY_VERSION'\>' &>/dev/null); then |
| 225 | + if has-executable install-ruby; then |
| 226 | + banner "Installing Ruby $REQUIRED_RUBY_VERSION with install-ruby" |
| 227 | + install-ruby "$REQUIRED_RUBY_VERSION" |
| 228 | + else |
| 229 | + error "Please use chruby to install Ruby $REQUIRED_RUBY_VERSION!" |
| 230 | + fi |
| 231 | + fi |
| 232 | + elif has-executable rvm; then |
| 233 | + if ! (rvm list | grep $required_ruby_version'\>' &>/dev/null); then |
| 234 | + banner "Installing Ruby $required_ruby_version with rvm" |
| 235 | + rvm install $required_ruby_version |
| 236 | + rvm use $required_ruby_version |
| 237 | + fi |
| 238 | + else |
| 239 | + error "You don't seem to have a Ruby manager installed." |
| 240 | + print-wrapped "\ |
| 241 | +We recommend using asdf. You can find instructions to install it here: |
| 242 | +
|
| 243 | + https://asdf-vm.com |
| 244 | +
|
| 245 | +When you're done, close and re-open this terminal tab and re-run this script." |
| 246 | + exit 1 |
| 247 | + fi |
| 248 | +} |
| 249 | + |
| 250 | +has-bundler() { |
| 251 | + has-executable bundle && bundle -v &>/dev/null |
| 252 | +} |
| 253 | + |
| 254 | +ensure-project-ruby-dependencies-installed() { |
| 255 | + banner 'Installing Ruby dependencies' |
| 256 | + |
| 257 | + if [[ $USE_BUNDLER_1 -eq 1 ]] && (! has-bundler || [[ $(bundle -v) =~ '^1\.' ]]); then |
| 258 | + gem install bundler -v '~> 1.0' |
| 259 | + elif ! has-bundler; then |
| 260 | + gem install bundler |
| 261 | + fi |
| 262 | + |
| 263 | + bundle check || bundle install |
| 264 | +} |
| 265 | + |
| 266 | +run-provisions() { |
| 267 | + provision-ruby |
| 268 | +} |
| 269 | + |
| 270 | +# --- FIN ---------------------------------------------------------------------- |
| 271 | + |
| 272 | +setup |
0 commit comments