Ruby:从没有 Rails 的 gem 访问 rake 任务 [英] Ruby: Accessing rake task from a gem without Rails

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

问题描述

我知道可以在 Ruby gem 中的多个位置定义 Rake 任务:

I'm aware that Rake tasks can be defined in a number of places within a Ruby gem:

  • 在 Rakefile 中
  • tasks/*.rake
  • lib/tasks/*.rake

我已经读过,当要在 gem 本身上执行任务时,应该使用前两个.当任务需要公开可用时,似乎应该选择第三个选项.

I've read that the first two should be used when the tasks are to be executed on the gem itself. It seems the third option should be chosen when tasks are desired to be publicly available.

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

There are many tutorials online demonstrating a variety of methods to load Rake tasks from a gem using Rails, namely by utilising Rails::RailTie.

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

However, I'd like to find a way of using a dependency gem's tasks within another gem without needing Rails.

有没有简单的解决方案?有人愿意描述正确的方法,或概述哪些方法可行吗?

Is there a simple solution to this? Would someone be kind enough to describe the proper approach, or outline what approaches would be viable?

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

I've tried creating a file bin/my-gem to make available on the system for executing Rake tasks from my-gem. I've put the following inside;

#!/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

部分内容基于这篇 SO 帖子.

遗憾的是我做错了什么,因为在安装 gem 后运行 my-gem mytasklib/test.rakemytask> 然后输出如下:

Sadly I'm doing something wrong because upon installing the gem then running my-gem mytask with mytask defined in lib/test.rake then the following is output:

/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

推荐答案

我找到了解决方案的正文 此处.我对其进行了修改以支持带参数的任务规范,并添加了对 cucumber 的支持.

I found the body of the solution here. I've modified it to support specification of tasks with arguments and added support for cucumber.

所以..

在你的 gem 中创建 bin/my_gem

Within your gem create bin/my_gem

将这篇文章底部的脚本粘贴到其中.有关示例用法,请参阅评论.

Paste the script at bottom of this post into it. See comments for example usage.

你的 rake 任务必须在你的 Rakefile 中.

Your rake tasks must be in your Rakefile.

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

Alternatively, add your tasks e.g. to lib/tasks/*.rake then add the following into your 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

宾果游戏!:-)

这篇关于Ruby:从没有 Rails 的 gem 访问 rake 任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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