从ruby gem加载rake任务

时间:2015-01-18 13:27:13

标签: ruby-on-rails ruby rake rakefile

我正在编写一个gem,我希望能够从我的rails应用程序访问gem rake任务。为此,我使用了Railties。

我有lib/frontrockets-rails/railtie.rb

require 'frontrockets-rails'
require 'rails'

module FrontrocketsRails
  class Railtie < Rails::Railtie
    railtie_name :frontrockets

    rake_tasks do
      load 'tasks/frontrockets.rake'
    end
  end  
end

lib/frontrockets-rails.rb

module FrontrocketsRails
  require 'frontrockets-rails/railtie' if defined?(Rails)
end

当然还有lib/tasks/frontrockets.rake

namespace :frontrockets do
  desc 'Create .bowerrc file'
  task :create_bowerrc do
    touch '.bowerrc'
  end

  task :install => [:create_bowerrc] do
  end
end

但是当我在我的Rails应用程序中安装此gem时,我仍然无法执行此rake任务,rake -T列表中没有它们。

1 个答案:

答案 0 :(得分:3)

我刚刚重新启动了弹簧,我的rake -T列表中出现了新的rake任务。 :)