Ruby Rake从gem中加载任务

Max*_*Max 11 ruby rake load include

我一直在尝试将位于github上的gem添加到我当前的应用程序中.gem有一个rake文件,我希望能够从我的应用程序访问.但我不断加载错误.

load 'tasks/deploy.rake'
Run Code Online (Sandbox Code Playgroud)

gem文件看起来像那样

# -*- 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
Run Code Online (Sandbox Code Playgroud)

我希望能够将./tasks/deploy.rake加载到包含此gem的应用程序中,我该如何继续呢?

谢谢

Max*_*Max 23

好的,如果有人有兴趣,我找到了解决这个问题的方法:

# Rails.root/Rakefile

spec = Gem::Specification.find_by_name 'test'
load "#{spec.gem_dir}/tasks/deploy.rake"
Run Code Online (Sandbox Code Playgroud)

这就是我在Rakefile中需要说的全部内容!

  • 对我来说,工作在应用程序的Rakefile中放置`require'<gem name>/rake_tasks'`(不是宝石) (2认同)