Skip to content

Instantly share code, notes, and snippets.

@pokka
Created January 28, 2014 07:59
Show Gist options
  • Save pokka/8663754 to your computer and use it in GitHub Desktop.
Save pokka/8663754 to your computer and use it in GitHub Desktop.
Run rake task in console
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