I'm aware that Rake tasks can be defined in a number of places within a Ruby gem:
- inside a Rakefile
- inside
tasks/*.rake
- inside
lib/tasks/*.rake
I've read that the first two should be used when the tasks are to be executed on the gem itself. It seems the third option should be chosen when tasks are desired to be publicly available.
There are many tutorials online demonstrating a variety of methods to load Rake tasks from a gem using Rails, namely by utilising Rails::RailTie
.
However, I'd like to find a way of using a dependency gem's tasks within another gem without needing Rails.
Is there a simple solution to this? Would someone be kind enough to describe the proper approach, or outline what approaches would be viable?
UPDATE
I've tried creating a file bin/my-gem
to make available on the system for executing Rake tasks from my-gem
. I've put the following inside;
#!/usr/bin/env ruby
require 'rubygems'
require 'rake'
task=ARGV[0]
spec = Gem::Specification.find_by_name('dsi_core')
Dir["#{spec.gem_dir}/lib/tasks/*.rake"].each {|file| puts file and Rake::load_rakefile(file)}
Rake::Task.clear # Avoid tasks being loaded several times in dev mode
Rake::Task[task].reenable # Support re-execution of a task.
Rake::Task[task].invoke
Some of the content was based on this SO post.
Sadly I'm doing something wrong because upon installing the gem then running my-gem mytask
with mytask
defined in lib/test.rake
then the following is output:
/var/lib/gems/1.8/gems/rake-0.9.2/lib/rake/task_manager.rb:49:in `[]': Don't know how to build task 'mytest' (RuntimeError)
from /var/lib/gems/1.8/gems/rake-0.9.2/lib/rake/task.rb:298:in `[]'
from /var/lib/gems/1.8/gems/my_gem-0.0.1/bin/my_gem:8
from /usr/local/bin/my_gem:19:in `load'
from /usr/local/bin/my_gem:19
rake gem::task[args]
style execution method. – Creon