You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.0 KiB
Ruby

#!/usr/bin/env ruby
def setup!
within_project_root do
test "ruby -v" => ruby_version
run "gem install bundler #{bundler_ver_arg} --no-document --conservative"
run "bundle check > /dev/null 2>&1 || bundle install"
run "yarn install" if File.exist?("yarn.lock")
run "overcommit --install" if overcommit_present?
run "bin/rails tmp:create db:prepare db:test:prepare db:seed"
end
end
require "fileutils"
begin
require "highline"
rescue LoadError
# If highline is missing, we'll gracefully omit ansi color output
end
def within_project_root(&block)
Dir.chdir(File.expand_path("..", __dir__), &block)
end
def overcommit_present?
system("which overcommit > /dev/null 2>&1")
end
def test(opts)
command, output = opts.first
log(:yellow, "test #{command}") do
unless `#{command}`.include?(output)
die("#{command} does not include #{output}")
end
end
end
def ruby_version
@ruby_version ||= begin
IO.read(".ruby-version").strip
end
end
def bundler_ver_arg
lock_file = File.expand_path("../Gemfile.lock", __dir__)
lock = File.exist?(lock_file) ? IO.read(lock_file) : ""
version = lock[/BUNDLED WITH\n (\S+)$/, 1]
version ? "-v #{version}" : nil
end
def run(command)
log(:blue, "run #{command}") do
with_clean_bundler_env do
system(command) or die("#{command} exited with non-zero status")
end
end
end
def with_clean_bundler_env(&block)
return block.call unless defined?(Bundler)
Bundler.with_clean_env(&block)
end
def copy(source, dest)
return if File.exist?(dest)
log(:magenta, "copy #{source}#{dest}") do
FileUtils.cp(source, dest)
end
end
def log(color, message, out=$stdout, &block)
if defined?(HighLine::String)
message.sub!(/^(\S*)/) { HighLine::String.new($1).public_send(color) }
end
if block_given?
out.print("#{message}")
yield
log(:green, "✔︎")
else
out.puts(message)
end
end
def die(message)
puts
log(:red, "FAIL #{message}", $stderr)
exit(1)
end
setup!