Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Common rake tasks I use on Rails projects
task :coverage do
sh "COVERAGE=true bundle exec rake"
Rake::Task["coverage:open"].invoke
end
namespace :coverage do
task :open do
require "launchy"
Dir["gems/*"].each(&method(:open_coverage))
Dir["engines/*"].each(&method(:open_coverage))
open_coverage(".")
end
def open_coverage(gem_dir)
chdir gem_dir do
Launchy.open("./coverage/index.html")
end
end
end
namespace :db do
desc "Kills open connections to the database"
task kill_postgres_conections: :environment do
exit_if_production
db_name = ActiveRecord::Base.connection_config[:database]
sh "ps xa | grep postgres: | grep #{db_name} | grep -v grep | awk '{print $1}' | xargs kill"
end
task drop: :kill_postgres_conections
desc "Load the dummy data from db/dummies.rb"
task dummy: [:sanity_check] do
dummies = Rails.root.join("db", "dummies.rb")
if File.exist?(dummies)
puts "\nLoading #{dummies}"
load(dummies)
end
end
desc "Reset your database, then load seed data, then dummy data"
task bounce: [:sanity_check, "migrate:reset", :seed, :dummy, "test:prepare"]
task sanity_check: :environment do
exit_if_production
end
def exit_if_production
return if ENV["FORCE_BOUNCE"].present?
return unless Rails.env.production?
puts "You're not allowed to do this in production!"
exit 1
end
end
namespace :test do
desc "Run tests for each gem in gems folder"
task :gems do
Dir["gems/*"].each(&method(:test_gems_run))
end
desc "Run tests for each gem in engines folder"
task :engines do
Dir["engines/*"].each do |dir|
test_gems_run(dir, database: true)
end
end
def test_gems_print_starting(suite)
puts \
ANSI::Code.magenta <<~MSG
----------------------------------------
Testing #{suite} ...
----------------------------------------
MSG
end
def test_gems_run(gem_dir, database: false)
test_gems_print_starting(gem_dir)
chdir gem_dir do
Bundler.with_clean_env do
sh "bundle check --path ../../vendor/bundle || bundle install --path ../../vendor/bundle"
sh "bundle exec rake db:migrate:reset db:test:prepare" if database
sh "bundle exec rake"
end
end
end
end
task default: ["test:gems", "test:engines"] do
test_gems_print_starting(Rails.application.class.parent_name)
end
namespace :test do
desc "Run tests for javascript assets"
task :javascripts do
# We can't just invoke the built-in teaspoon rake task here, because
# teaspoon requires that it runs in the development environment so a
# server can be booted, but the rest of the test suite runs in the test
# environment. Instead, we exec a new process to keep teaspoon in its own
# environment.
sh "RAILS_ENV=development bundle exec teaspoon"
end
end
task :default do
Rake::Task["test:javascripts"].invoke unless ENV["SKIP_JAVASCRIPT"]
end
begin
require 'rubocop/rake_task'
RuboCop::RakeTask.new
rescue LoadError => e
e
end
task :default do
Rake::Task[:rubocop].invoke
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment