Ruby Rake load tasks from a gem

2019-03-18 14:48发布

问题:

i have been trying to include a gem located on github to my current app. The gem has a rake file that i want to able to access from my app. But i keep getting load errors.

load 'tasks/deploy.rake'

The gem file looks something like that

# -*- encoding: utf-8 -*-
require 'rake'

Gem::Specification.new do |gem|
  gem.authors       = %w(Hello World)
  gem.email         = %w(test@example.com)
  gem.description   = 'test'
  gem.summary       = 'test'
  gem.homepage      = 'https://github.com/..'
  gem.files         = FileList[ 'lib/**/*.rb', 'tasks/deploy.rake', 'README.md' ].to_a
  gem.name          = 'test'
  gem.require_paths = %w(lib)
  gem.version       = '0.0.1'
end

I want to be able to load ./tasks/deploy.rake to my app that includes this gem, how do i go on about it?

thanks

回答1:

Okay, I found a solution for this problem if anyone is interested:

# Rails.root/Rakefile

spec = Gem::Specification.find_by_name 'test'
load "#{spec.gem_dir}/tasks/deploy.rake"

That's all I needed to say in my Rakefile!