Created
January 28, 2014 07:59
-
-
Save pokka/8663754 to your computer and use it in GitHub Desktop.
Run rake task in console
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rake' | |
#cake == console rake | |
module Cake | |
attr :trace | |
attr :alias_tasks | |
attr :current_task | |
@alias_tasks = {} | |
@trace = false | |
def trace= t | |
@trace = t | |
end | |
def current_task | |
@alias_tasks[@current_task] || @current_task | |
end | |
def invoke task | |
started_at = Time.now | |
ActiveRecord::Base.transaction do | |
begin | |
Rake::Task[task].invoke | |
rescue Exception => e | |
log '' | |
log 'ERROR:' | |
log e | |
log '' | |
log e.backtrace if @trace | |
log '' | |
log "Abort, cost: #{Time.now - started_at} (s)" | |
raise ActiveRecord::Rollback | |
end | |
end | |
finished_at = Time.now | |
log "Started at: #{started_at}, End at: #{finished_at}; Cost: #{finished_at - started_at} (s)" | |
end | |
def refresh task | |
Rake::Task[task].reenable | |
true | |
end | |
def run task,aliaz = nil | |
@current_task = if aliaz.present? | |
@alias_tasks[aliaz] = task | |
aliaz | |
else | |
task | |
end | |
refresh current_task | |
invoke current_task | |
end | |
def reload | |
Rake::Task.clear | |
Console::Application.load_tasks | |
@current_task = nil | |
'Done' | |
end | |
def log msg | |
$stdout.puts msg | |
end | |
extend self | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment