從寶石到處都可以進行rake任務嗎?

[英]Make rake task from gem available everywhere?


So I'm writing a small gem and I have a '/tasks' dir in it with some specific rake tasks. How do I make those tasks available automatically everywhere, where the gem is required? For example I wish I could run 'rake mygemrake:task' inside my rails root dir after I have the gem installed.

所以我正在寫一個小寶石,我有一個'/ tasks'目錄,里面有一些特定的rake任務。如何在需要寶石的地方自動提供這些任務?例如,我希望在安裝gem之后可以在rails root目錄中運行'rake mygemrake:task'。

5 个解决方案

#1


23  

For Rails3 applications, you might want to look into making a Railtie for your gem.

對於Rails3應用程序,您可能希望研究為您的gem制作Railtie。

You can do so with:

您可以這樣做:

lib/your_gem/railtie.rb

LIB / your_gem / railtie.rb

require 'your_gem'
require 'rails'
module YourGem
  class Railtie < Rails::Railtie
    rake_tasks do
      require 'path/to/rake.task'
    end
  end
end

lib/your_gem.rb

LIB / your_gem.rb

module YourGem
  require "lib/your_gem/railtie" if defined?(Rails)
end

Though, I had my share of difficulties with requiring the rake.task file in my railtie.rb. I opted to just define my measley one or two tasks within the rake_tasks block.

雖然,我在railtie.rb中要求rake.task文件時遇到了一些困難。我選擇在rake_tasks塊中定義我的一個或兩個任務。

#2


2  

Check out the rdoctask in rake for an example of how to define a task provided by a gem. The task is defined in ruby instead of the rake build language and can be required like so:

查看rake中的rdoctask,了解如何定義gem提供的任務。該任務是用ruby而不是rake構建語言定義的,可以這樣定義:

require 'rake'             # the gem
require 'rake/rdoctask'    # the task

#3


0  

You have to import those tasks in application's Rakefile. This is how it looks in mine (I am using bundler08 to manage my gems):

您必須在應用程序的Rakefile中導入這些任務。這就是我的樣子(我使用bundler08來管理我的寶石):

%w(gem1 gem2 gem3).each do |g|
  Dir[File.dirname(__FILE__) + "/vendor/bundler_gems/**/#{g}*/tasks/*.rake"].each do |f|
    import f
  end
end

#4


0  

You can write normal rake tasks for a gem and load them like this:

您可以為gem編寫正常的rake任務並加載它們,如下所示:

require 'rake'
load 'path/to/your/tasks.rake'

Also, take a look at thor vs. rake.

另外,看看托爾與耙子。

#5


0  

That's what Sake is for. Datamapper and Merb have been using Sake with success.

這就是Sake的用途。 Datamapper和Merb一直在使用Sake。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/04/12/72511bea94f802276e8f30759a758623.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com