Ruby:在没有Rails的情况下从gem访问rake任务

时间:2012-12-13 11:05:29

标签: ruby-on-rails ruby rake task

我知道可以在Ruby gem中的许多地方定义Rake任务:

  • 在Rakefile里面
  • tasks/*.rake
  • lib/tasks/*.rake

我已经读过,当要在gem本身执行任务时,应该使用前两个。当希望公开任务时,似乎应该选择第三种选择。

网上有很多教程演示了使用Rails从gem加载Rake任务的各种方法,即利用Rails::RailTie

但是,我想找到一种在不需要Rails的情况下在另一个gem中使用依赖gem的任务的方法。

有一个简单的解决方案吗?有人会善意地描述正确的方法,或概述哪些方法可行?

更新

我尝试创建一个文件bin/my-gem,以便在系统上使用my-gem执行Rake任务。我把以下内容放在里面;

#!/usr/bin/env ruby
require 'rubygems'
require 'rake'
task=ARGV[0]
spec = Gem::Specification.find_by_name('dsi_core')
Dir["#{spec.gem_dir}/lib/tasks/*.rake"].each {|file| puts file and Rake::load_rakefile(file)}
Rake::Task.clear # Avoid tasks being loaded several times in dev mode
Rake::Task[task].reenable # Support re-execution of a task.
Rake::Task[task].invoke

部分内容基于this SO post

可悲的是,我做错了,因为在安装gem之后运行my-gem mytask mytask并在lib/test.rake中定义,然后输出以下内容:

/var/lib/gems/1.8/gems/rake-0.9.2/lib/rake/task_manager.rb:49:in `[]': Don't know how to build task 'mytest' (RuntimeError)
    from /var/lib/gems/1.8/gems/rake-0.9.2/lib/rake/task.rb:298:in `[]'
    from /var/lib/gems/1.8/gems/my_gem-0.0.1/bin/my_gem:8
    from /usr/local/bin/my_gem:19:in `load'
    from /usr/local/bin/my_gem:19

2 个答案:

答案 0 :(得分:13)

我找到了解决方案here的正文。我修改了它以支持带参数的任务规范,并增加了对cucumber的支持。

所以..

在您的宝石中创建bin/my_gem

将此帖子底部的脚本粘贴到其中。有关示例用法,请参阅注释。

您的rake任务必须位于Rakefile

或者,添加您的任务,例如到lib/tasks/*.rake然后将以下内容添加到Rakefile

Dir.glob('lib/tasks/*.rake').each {|r| import r}

这是秘诀:

#!/usr/bin/env ruby

# Run rake tasks and cucumber features
# from my_gem once it's installed.
#
# Example:
#
#   my_gem rake some-task
#   my_gem rake some-task[args]
#   my_gem cucumber feature1 feature2
#
# Note: cucumber features have '.feature' appended automatically,
#       no need for you to do it ;)
#
# Author:: N David Brown
gem_dir = File.expand_path("..",File.dirname(__FILE__))
$LOAD_PATH.unshift gem_dir# Look in gem directory for resources first.
exec_type = ARGV[0]
if exec_type == 'rake' then
    require 'rake'
    require 'pp'
    pwd=Dir.pwd
    Dir.chdir(gem_dir) # We'll load rakefile from the gem's dir.
    Rake.application.init
    Rake.application.load_rakefile
    Dir.chdir(pwd) # Revert to original pwd for any path args passed to task.
    Rake.application.invoke_task(ARGV[1])
elsif exec_type == 'cucumber' then
    require 'cucumber'
    features = ARGV[1,].map{|feature| "#{gem_dir}/features/#{feature}.feature"}.join(' ')
    runtime = Cucumber::Runtime.new 
    runtime.load_programming_language('rb') 
    pwd=Dir.pwd
    Dir.chdir(gem_dir) # We'll load features from the gem's dir.
    Cucumber::Cli::Main.new([features]).execute!(runtime)
    Dir.chdir(pwd) # Revert to original pwd for convenience.
end

宾果! : - )

答案 1 :(得分:2)

这篇文章给了我一些提示,但也许对其他人来说,我遇到了一些依赖性问题。我终于找到了如何用bin / binstub做这个,而不需要rakefile。 My solution is posted on another SO question.