mbigras
mbigras

Reputation: 8055

Invoke namespaced rake task in ruby code

Given some ruby file foo.rb:

require 'rake'

namespace :tmp do
  desc "Foo bar baz.."
  task :some_task do
    puts "running some task..."
  end
end

How can I invoke the namespaced task tmp:some_task task in ruby?

I've tried:

require_relative 'foo'
#=> true
Rake::Task[tmp:some_task].invoke
#=> NameError: undefined local variable or method `some_task' for main:Object

Upvotes: 0

Views: 269

Answers (2)

Haseeb A
Haseeb A

Reputation: 6112

The Accepted answer is correct. But, from rails, to invoke any rake task from Rails. you need to load them first.

require 'rake'
Rails.application.load_tasks
Rake::Task['my_task'].invoke

Upvotes: 0

Andrey Deineko
Andrey Deineko

Reputation: 52347

You are calling the task incorrectly - pass the name of the task as string:

Rake::Task['tmp:some_task'].invoke

Upvotes: 2

Related Questions