-
Notifications
You must be signed in to change notification settings - Fork 553
Calling Rake tasks with Thor
Rajasegar Chandran edited this page Sep 20, 2019
·
2 revisions
To call a Rake task from a Thorfile, or a Ruby script loading the Thor libraries, a little more is needed than just calling the thor/rake_compat
library. The Thorfile
will need to have a task created to call it.
This example comes from the RDoc documentation for Thor::RakeCompat
require 'thor/rake_compat'
require 'rspec/core/rake_task'
class Default < Thor
include Thor::RakeCompat
RSpec::Core::RakeTask.new(:spec) do |t|
t.rspec_opts = ['--options', './.rspec']
end
desc 'spec', 'Run RSpec tests'
def spec
Rake::Task['spec'].invoke
end
end
This creates the rake spec
task, but it needs to be called using the Rake::Task['spec'].invoke
method.
This example is coming almost directly from the Thor Spec tests for rake_compat_spec.rb
.
require 'thor/rake_compat'
require "rake/tasklib"
class RakeTask < Rake::TaskLib
def initialize
define
end
def define
instance_eval do
desc "Say it's cool"
task :cool do
puts "COOL"
end
namespace :hiper_mega do
task :super do
puts "HIPER MEGA SUPER"
end
end
end
end
end
class ThorTask < Thor
include Thor::RakeCompat
RakeTask.new
desc 'cool', 'say cool'
def cool
Rake::Task['cool'].invoke
puts ThorTask.tasks['cool'].description
end
end
The result is:
thor thor_task:cool
COOL
say cool