class RailsConsoleCommands::Raker

Public Class Methods

new() click to toggle source
# File lib/rails_console_commands/raker.rb, line 9
def initialize
  load_rake_tasks
end

Public Instance Methods

rake(task = nil) click to toggle source
# File lib/rails_console_commands/raker.rb, line 13
def rake(task = nil)
  task.nil? ? print_rake_tasks : invoke_rake_task(task)
  'Completed'
rescue SystemExit, RuntimeError => e
  "Failed: #{e.message}"
end

Private Instance Methods

argv_arguments() click to toggle source
# File lib/rails_console_commands/raker.rb, line 54
def argv_arguments
  ARGV.each_with_object({}) do |arg, arguments|
    if arg =~ /^(\w+)=(.*)$/
      arguments[Regexp.last_match(1)] = Regexp.last_match(2)
    end
  end
end
expose_argv_arguments_via_env() { || ... } click to toggle source
# File lib/rails_console_commands/raker.rb, line 47
def expose_argv_arguments_via_env
  argv_arguments.each { |key, value| ENV[key] = value }
  yield
ensure
  argv_arguments.keys.each { |key| ENV.delete(key) }
end
invoke_rake_task(task) click to toggle source
# File lib/rails_console_commands/raker.rb, line 33
def invoke_rake_task(task)
  task, *options = task.split(' ')

  ARGV.replace options

  # FIXME: Before we can use this, we need a way to reset the options again
  # Rake.application.handle_options

  expose_argv_arguments_via_env { Rake::Task[task].invoke }
  Rake.application.tasks.each(&:reenable) # Rake by default only allows tasks to be run once per session
ensure
  ARGV.replace([])
end
load_rake_tasks() click to toggle source
# File lib/rails_console_commands/raker.rb, line 22
def load_rake_tasks
  Rake::TaskManager.record_task_metadata = true # needed to capture comments from define_task
  load Rails.root.join('Rakefile')
end
print_rake_tasks() click to toggle source