Ruby: Accessing rake task from a gem without Rails
I found the body of the solution here. I've modified it to support specification of tasks with arguments and added support for cucumber
.
So..
Within your gem create bin/my_gem
Paste the script at bottom of this post into it. See comments for example usage.
Your rake
tasks must be in your Rakefile
.
Alternatively, add your tasks e.g. to lib/tasks/*.rake
then add the following into your Rakefile
:
Dir.glob('lib/tasks/*.rake').each {|r| import r}
Here's the secret sauce:
#!/usr/bin/env ruby
# Run rake tasks and cucumber features
# from my_gem once it's installed.
#
# Example:
#
# my_gem rake some-task
# my_gem rake some-task[args]
# my_gem cucumber feature1 feature2
#
# Note: cucumber features have '.feature' appended automatically,
# no need for you to do it ;)
#
# Author:: N David Brown
gem_dir = File.expand_path("..",File.dirname(__FILE__))
$LOAD_PATH.unshift gem_dir# Look in gem directory for resources first.
exec_type = ARGV[0]
if exec_type == 'rake' then
require 'rake'
require 'pp'
pwd=Dir.pwd
Dir.chdir(gem_dir) # We'll load rakefile from the gem's dir.
Rake.application.init
Rake.application.load_rakefile
Dir.chdir(pwd) # Revert to original pwd for any path args passed to task.
Rake.application.invoke_task(ARGV[1])
elsif exec_type == 'cucumber' then
require 'cucumber'
features = ARGV[1,].map{|feature| "#{gem_dir}/features/#{feature}.feature"}.join(' ')
runtime = Cucumber::Runtime.new
runtime.load_programming_language('rb')
pwd=Dir.pwd
Dir.chdir(gem_dir) # We'll load features from the gem's dir.
Cucumber::Cli::Main.new([features]).execute!(runtime)
Dir.chdir(pwd) # Revert to original pwd for convenience.
end
Bingo! :-)
How do I include gems in Rake files without specifying them in a Gemfile?
Rails applications by default use Bundler, so you need to add gem "curb"
to your Gemfile and then ”install” (register) the new gem to Rails bundle with gem install
.
Load rake tasks from ruby gem
I've just restarted spring and new rake tasks appeared in my rake -T
list. :)
bin stub - missing dependencies from gem (executing rake tasks within gem)
Ultimately, I needed a call to Bundler.setup
to resolve dependencies.
After cleanup, the file below is the only thing I needed to call a rake task with a bin file inside a gem (external bin stub utilizes this file):
bin/elastic-beanstalk
#!/usr/bin/env ruby
require 'rake'
require 'bundler'
raise "Bundler is required. Please install bundler with 'gem install bundler'" unless defined?(Bundler)
#
# Example:
#
# elastic-beanstalk eb:show_config
# elastic-beanstalk eb:show_config[1.1.1]
#
# init dependencies
Bundler.setup
# init rake
Rake.application.init
# load the rake tasks
gem_dir = File.expand_path('..',File.dirname(__FILE__))
load "#{gem_dir}/lib/elastic/beanstalk/tasks/eb.rake"
# invoke the given task
Rake.application.invoke_task(ARGV[0])
Related Topics
Is It Possible for Rspec to Expect Change in Two Tables
Split String into a List, But Keeping the Split Pattern
Keep Form Fields Filled After an Error (Ror)
Base64 Photo and Paperclip -Rails
Ruby on Rails: How to Run Things in the Background
How to Set Up Simple Http Authentication for a Ruby on Rails App on Heroku
Paperclip Renaming Files After They'Re Saved
Is There Equivalent for PHP's Print_R in Ruby/Rails
How to Preserve Request Url With Nginx Proxy_Pass
Ruby: Dynamically Generate Attribute_Accessor
Rspec/Capybara Loading in Progress, Circular Require Considered Harmful
How to Dynamically Alter Inheritance in Ruby
How to Check a Word Is Already All Uppercase
Using Memcache Client with Ruby
Welcome/Home Page in Ruby on Rails - Best Practice