Skip to content

Instantly share code, notes, and snippets.

@davetron5000
Last active February 18, 2023 17:04
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davetron5000/02c9ff175b8a67b016a1cd3ce4c46271 to your computer and use it in GitHub Desktop.
Save davetron5000/02c9ff175b8a67b016a1cd3ce4c46271 to your computer and use it in GitHub Desktop.
def setup(options)
if !verify_no_credentials_in_checked_in_files!
exit 1
end
log "💎 Installing gems"
# Only do bundle install if the much-faster
# bundle check indicates we need to
bundle_install = "bundle install --jobs=4 --retry=3"
system! "bundle check || #{bundle_install}"
log "☕️ Installing Node modules"
system! "bin/yarn install"
log "🪑 Checking your .local configurations"
check_for_local_configuration!
system! "bin/db-reset"
log "💰 Flushing all Redis databases"
system!("bin/rails dev:redis_reset")
log "🎉 All set up! 🎉"
log ""
log "To see commonly-needed commands, run:"
log ""
log " bin/setup help"
log ""
end
def system!(*args, ignore_failure: false)
log "🐈 Executing #{args}"
if system(*args)
log "😸 #{args} succeeded"
else
log "😿 #{args} failed"
yield if block_given?
if ignore_failure
log "😿 Ignoring the failure"
else
abort
end
end
end
def log(message)
puts "[ bin/setup ] #{message}"
end
def verify_no_credentials_in_checked_in_files!
rails_root = Pathname(__FILE__).dirname / ".."
found_at_least_one_problem = false
[
".env.development",
".env.development.local",
".env.test",
".env.test.local",
].each do |dotenv_file|
path_to_dotenv_file = rails_root / dotenv_file
if File.exists?(path_to_dotenv_file)
contents = File.read(path_to_dotenv_file).split(/\n/).reject{ |line|
line =~ /^#/
}
database_urls = contents.select { |line|
line =~ /^[^=]*DATABASE_URL=/
}
database_urls.each do |database_url|
var_name, connection_string = database_url.split("=",2)
if connection_string =~ /amazonaws.com/
found_at_least_one_problem = true
log "❗️ #{var_name} in #{dotenv_file} seems to point to a non-local database"
end
end
end
end
if found_at_least_one_problem
log "‼️ There was at least one problem with your existing .env files"
end
!found_at_least_one_problem
end
def check_for_local_configuration!
rails_root = Pathname(__FILE__).dirname / ".."
path_to_dot_env_development_local = rails_root / ".env.development.local"
if !File.exists?(path_to_dot_env_development_local)
system! "touch .env.development.local"
end
contents = File.read(path_to_dot_env_development_local).split(/\n/).reject { |line|
line =~ /^#/
}
need_google_keys = false
if contents.detect { |line| line =~ /^GOOGLE_CLIENT_ID=\S+$/ }.nil?
log "❗️ .env.development.local Missing GOOGLE_CLIENT_ID"
need_google_keys = true
end
if contents.detect { |line| line =~ /^GOOGLE_CLIENT_SECRET=\S+$/ }.nil?
log "❗️ .env.development.local Missing GOOGLE_CLIENT_SECRET"
need_google_keys = true
end
if need_google_keys
log "🔑 You need the Google OAuth keys to run the app locally"
log "🔑 Here is how to get them:"
log ""
log "1️⃣ Open up 1Password and locate the Secure Note titled"
log " \"Google OAuth Credentials for Hermes Local Development \""
log "2️⃣ Copy and paste them into your .env.development.local"
log " The names are in the note, but they should be GOOGLE_CLIENT_ID an GOOGLE_CLIENT_SECRET"
log "3️⃣ Re-run this script"
log ""
exit 1
end
path_to_dot_env_test_local = rails_root / ".env.test.local"
if !File.exists?(path_to_dot_env_test_local)
system! "touch .env.test.local"
end
contents = File.read(path_to_dot_env_test_local).split(/\n/).reject { |line|
line =~ /^#/
}
end
# config/initializers/00_appconfig.rb
class AppConfig
def self.methods_to_check
@methods_to_check ||= []
end
def self.check(method_name)
methods_to_check << method_name
end
def initialize
self.class.methods_to_check.each do |method_name|
self.send(method_name)
end
end
check def google_client_id
ENV.fetch("GOOGLE_CLIENT_ID")
end
check def google_client_secret
ENV.fetch("GOOGLE_CLIENT_SECRET")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment