|
| 1 | +#!/usr/bin/env ruby |
| 2 | + |
| 3 | +def setup |
| 4 | + log "Installing gems" |
| 5 | + # Only do bundle install if the much-faster |
| 6 | + # bundle check indicates we need to |
| 7 | + system! "bundle check || bundle install" |
| 8 | + |
| 9 | + log "Installing Node modules" |
| 10 | + # Only do yarn install if the much faster |
| 11 | + # yarn check indicates we need to. |
| 12 | + # --check-files is needed to force Yarn to actually |
| 13 | + # examine what's in node_modules |
| 14 | + system! "bin/yarn check --check-files || bin/yarn install" |
| 15 | + |
| 16 | + log "Dropping & recreating the development database" |
| 17 | + # Note that the very first this runs, db:reset |
| 18 | + # will fail, but this failure is fixed by |
| 19 | + # doing a db:migrate |
| 20 | + system! "bin/rails db:reset || bin/rails db:migrate" |
| 21 | + |
| 22 | + log "Dropping & recreating the test database" |
| 23 | + # Setting the RAILS_ENV explicitly to be sure |
| 24 | + # we actually reset the test database |
| 25 | + system!({"RAILS_ENV" => "test"}, "bin/rails db:reset") |
| 26 | + |
| 27 | + log "All set up." |
| 28 | + log "" |
| 29 | + log "To see commonly-needed commands, run:" |
| 30 | + log "" |
| 31 | + log " bin/setup help" |
| 32 | + log "" |
| 33 | +end |
| 34 | + |
| 35 | +def help |
| 36 | + log "Useful commands:" |
| 37 | + log "" |
| 38 | + log " bin/run" |
| 39 | + log " # runs app locally" |
| 40 | + log "" |
| 41 | + log "" |
| 42 | + log " LOGRAGE_IN_DEVELOPMENT=true bin/run" |
| 43 | + log " # run app locally using" |
| 44 | + log " # production-like logging" |
| 45 | + log "" |
| 46 | + log "" |
| 47 | + log " bin/ci" |
| 48 | + log " # runs all test and checks as CI would" |
| 49 | + log "" |
| 50 | + log " bin/rails test" |
| 51 | + log " # run non-system tests" |
| 52 | + log "" |
| 53 | + log " bin/rails test:system" |
| 54 | + log " # run system tests" |
| 55 | + log "" |
| 56 | + log " bin/setup help" |
| 57 | + log " # show this help" |
| 58 | +end |
| 59 | + |
| 60 | +# start of helpers |
| 61 | + |
| 62 | +# We don't want the setup method to have |
| 63 | +# to do all this error checking, and we |
| 64 | +# also want to explicitly log what we |
| 65 | +# are executing, so we use this method |
| 66 | +# instead of Kernel#system and frriends |
| 67 | + |
| 68 | +def system!(*args) |
| 69 | + log "Executing #{args}" |
| 70 | + if system(*args) |
| 71 | + log "#{args} succeeded" |
| 72 | + else |
| 73 | + log "#{args} failed" |
| 74 | + abort |
| 75 | + end |
| 76 | +end |
| 77 | + |
| 78 | +# It's helpful to know what messages came |
| 79 | +# from this script, so we'll use log |
| 80 | +# instead of puts to communicate with the user |
| 81 | +def log(message) |
| 82 | + puts "[ bin/setup ] #{message}" |
| 83 | +end |
| 84 | + |
| 85 | +# end of helpers |
| 86 | + |
| 87 | +if ARGV[0] == "help" |
| 88 | + help |
| 89 | +else |
| 90 | + setup |
| 91 | +end |
0 commit comments