让 gem 的 rake 任务随处可用? [英] Make rake task from gem available everywhere?

查看:39
本文介绍了让 gem 的 rake 任务随处可用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在写一个小宝石,其中有一个/tasks"目录,其中包含一些特定的 rake 任务.如何在需要 gem 的任何地方自动使这些任务可用?例如,我希望我可以在安装 gem 后在我的 rails 根目录中运行 'rake mygemrake:task'.

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.

推荐答案

对于 Rails3 应用程序,您可能需要考虑为您的 gem 制作一个 Railtie.

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

你可以这样做:

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

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

尽管如此,在我的 railtie.rb 中要求 rake.task 文件时,我遇到了一些困难.我选择在 rake_tasks 块中定义我的 measley 一两个任务.

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.

这篇关于让 gem 的 rake 任务随处可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆