|
1 | 1 | #!/usr/bin/env ruby
|
| 2 | + |
2 | 3 | # frozen_string_literal: true
|
3 | 4 |
|
| 5 | +# This script is a way to setup or update your development environment |
| 6 | +# automatically. This script is idempotent, so that you can run it at anytime |
| 7 | +# and get an expectable outcome. Add necessary setup steps to this file. |
| 8 | + |
4 | 9 | require 'fileutils'
|
| 10 | +require 'securerandom' |
5 | 11 |
|
6 |
| -# path to your application root. |
7 |
| -APP_ROOT = File.expand_path('..', __dir__) |
| 12 | +def setup |
| 13 | + unless File.exist?('.env') |
| 14 | + log "Copying environment config files" |
| 15 | + FileUtils.cp '.env.sample', '.env' |
| 16 | + log "Copied .env.sample to .env\n" |
8 | 17 |
|
9 |
| -def system!(*args) |
10 |
| - system(*args) || abort("\n== Command #{args} failed ==") |
| 18 | + log "Generating SECRET_KEY_BASE for .env" |
| 19 | + secret = SecureRandom.hex(64) |
| 20 | + env_file = File.read(".env") |
| 21 | + env_file_with_secret = env_file.gsub(/SECRET_KEY_BASE=.+/, "SECRET_KEY_BASE=\"#{secret}\"") |
| 22 | + File.open(".env", "w") { |file| file.puts env_file_with_secret } |
| 23 | + log "Generated SECRET_KEY_BASE for .env" |
| 24 | + log "IMPORTANT: Please set DATABASE_URL and DATABASE_TEST_URL based on your environment\n" |
| 25 | + end |
| 26 | + |
| 27 | + log "Installing gems" |
| 28 | + # Only do bundle install if the much-faster bundle check indicates we need to |
| 29 | + system! "gem install bundler --conservative" |
| 30 | + system! "bundle check || bundle install" |
| 31 | + log "Gems installed\n" |
| 32 | + |
| 33 | + log "Ensuring binstubs are created" |
| 34 | + system! "bundle binstubs --force rubocop rspec-core guard" |
| 35 | + log "Binstubs created\n" |
| 36 | + |
| 37 | + log "Removing old logs and tempfiles" |
| 38 | + system! 'bin/rails log:clear tmp:clear' |
| 39 | + log "Temp files removed\n" |
| 40 | + |
| 41 | + log "Dropping & recreating the development database" |
| 42 | + # Note that the very first time this runs, db:reset will fail, but this |
| 43 | + # failure is fixed by doing a db:migrate |
| 44 | + system! "bin/rails db:reset || bin/rails db:migrate" |
| 45 | + log "Development database recreated\n" |
| 46 | + |
| 47 | + log "Dropping & recreating the test database" |
| 48 | + # Setting the RAILS_ENV explicitly to be sure we actually reset the test |
| 49 | + # database |
| 50 | + system!({ "RAILS_ENV" => "test" }, "bin/rails db:reset") |
| 51 | + log "Test database recreated\n" |
| 52 | + |
| 53 | + log "All set up." |
| 54 | + log "" |
| 55 | + log "To see commonly-needed commands, run:" |
| 56 | + log "" |
| 57 | + log " bin/setup help" |
| 58 | + log "" |
11 | 59 | end
|
12 | 60 |
|
13 |
| -FileUtils.chdir APP_ROOT do |
14 |
| - # This script is a way to setup or update your development environment automatically. |
15 |
| - # This script is idempotent, so that you can run it at anytime and get an expectable outcome. |
16 |
| - # Add necessary setup steps to this file. |
| 61 | +def help |
| 62 | + log "Useful commands:" |
| 63 | + log "" |
| 64 | + log " bin/run" |
| 65 | + log " # run app locally" |
| 66 | + log "" |
| 67 | + log " bin/ci" |
| 68 | + log " # runs all tests and checks as CI would" |
| 69 | + log "" |
| 70 | + log " bin/rubocop" |
| 71 | + log " # run linter" |
| 72 | + log "" |
| 73 | + log " bin/rspec" |
| 74 | + log " # run unit tests" |
| 75 | + log "" |
| 76 | + log " bin/guard" |
| 77 | + log " # run unit tests in watch mode" |
| 78 | + log "" |
| 79 | + log " bin/setup help" |
| 80 | + log " # show this help message" |
| 81 | + log "" |
| 82 | +end |
17 | 83 |
|
18 |
| - puts '== Installing dependencies ==' |
19 |
| - system! 'gem install bundler --conservative' |
20 |
| - system('bundle check') || system!('bundle install') |
| 84 | +# start of helpers |
21 | 85 |
|
22 |
| - # puts "\n== Copying sample files ==" |
23 |
| - # unless File.exist?('config/database.yml') |
24 |
| - # FileUtils.cp 'config/database.yml.sample', 'config/database.yml' |
25 |
| - # end |
| 86 | +# It's helpful to know what messages come from this script, so we'll use log |
| 87 | +# instead of `puts` |
| 88 | +def log(message) |
| 89 | + puts "[ bin/setup ] #{message}" |
| 90 | + puts if message.end_with?("\n") |
| 91 | +end |
26 | 92 |
|
27 |
| - puts "\n== Preparing database ==" |
28 |
| - system! 'bin/rails db:prepare' |
| 93 | +# We don't want the setup method to have to do all this error checking, and we |
| 94 | +# also want to explicitly log what we are executing. Thus, we use this method |
| 95 | +# instead of Kernel#system |
| 96 | +def system!(*args) |
| 97 | + log "Executing #{args}" |
| 98 | + if system(*args) |
| 99 | + log "#{args} succeeded" |
| 100 | + else |
| 101 | + log "#{args} failed" |
| 102 | + abort |
| 103 | + end |
| 104 | +end |
29 | 105 |
|
30 |
| - puts "\n== Removing old logs and tempfiles ==" |
31 |
| - system! 'bin/rails log:clear tmp:clear' |
| 106 | +# end of helpers |
32 | 107 |
|
33 |
| - puts "\n== Restarting application server ==" |
34 |
| - system! 'bin/rails restart' |
| 108 | +# path to your application root. |
| 109 | +APP_ROOT = File.expand_path('..', __dir__) |
| 110 | + |
| 111 | +FileUtils.chdir APP_ROOT do |
| 112 | + if ARGV[0] == "help" |
| 113 | + help |
| 114 | + else |
| 115 | + setup |
| 116 | + end |
35 | 117 | end
|
0 commit comments