Skip to content

Instantly share code, notes, and snippets.

@LolWalid
Created February 22, 2016 18:11
Show Gist options
  • Save LolWalid/641ae6454118af73c511 to your computer and use it in GitHub Desktop.
Save LolWalid/641ae6454118af73c511 to your computer and use it in GitHub Desktop.
Action before and after rake task
# lib/rake/hooks.rb
module Rake::Hooks
def before(*task_names, before_task_name)
task_names.each do |task_name|
task = Rake::Task[task_name]
task.enhance([before_task_name])
end
end
def after(*task_names, after_task_name)
task_names.each do |task_name|
task = Rake::Task[task_name]
after_task_name = Rake::Task[after_task_name]
task.enhance do
after_task_name.invoke
end
end
end
def arround(*task_names, before_task_name, after_task_name)
before(*task_names, before_task_name)
after(*task_names, after_task_name)
end
end
# Rake::DSL.send(:include, Rake::Hooks) if defined?(Rake::DSL)
include Rake::Hooks unless self.class.included_modules.include?(Rake::Hooks)
# lib/tasks/update.rake
namespace :namespace do
task task1: :environment do
puts 'inside task1'
end
task task2: :environment do
puts 'inside task2'
end
end
namespace :hooks do
task :after_party do
puts 'after_party'
end
task :before_party do
puts 'before_party'
end
end
arround 'namespace:task1', 'hooks:before_party', 'hooks:after_party'
before 'namespace:task2', 'hooks:before_party'
after 'namespace:task2', 'hooks:after_party'
require File.expand_path('../lib/tasks/hooks', __FILE__)
require File.expand_path('../config/application', __FILE__)
Rails.application.load_tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment