如何将条件 ruby​​gem 要求添加到 gem 规范中? [英] How do I add conditional rubygem requirements to a gem specification?

查看:20
本文介绍了如何将条件 ruby​​gem 要求添加到 gem 规范中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以仅在该人使用特定版本的 ruby​​ 时添加 gem 依赖项?

Is it possible to add a gem dependency only if the person is using a certain version of ruby?

背景:我正在开发一个一个项目的分支使用 Test::Unit::Autorunner 等.它们是 ruby​​ 1.8 标准库的一部分,但不是 1.9.1 标准库的一部分,而是在测试单元"gem 中.我想添加一个依赖项,说明如果有人使用 ruby​​ 1.9.1 或更高版本,请安装test-unit"gem,但如果他们使用的是 1.8 或更早版本,则无需安装任何东西.

Background: I'm working on a fork of a project that uses Test::Unit::Autorunner and the like. They are part of the standard library in ruby 1.8, but aren't part of the standard library in 1.9.1, and is instead in the "test-unit" gem. I want to add a dependency that says that if someone's using ruby 1.9.1 or later, install the "test-unit" gem, but if they're using 1.8 or earlier, they don't need to install anything.

推荐答案

我为一个项目做了这件事.诀窍是将脚本添加为扩展,然后在安装时在用户机器上执行.

I did this exact thing for a project. The trick is to add the script as an extension, which will then get executed at install time on the user's machine.

这里是代码片段和我们 github 的链接:

Here are code snippets and links to our github:

首先,在 gemspec 中(我们实际上是使用 Rakefile 来生成它,但结果是一样的)来源

First, when in the gemspec (we're actually using a Rakefile to generate it, but the result ends up the same) source

# This file *needs* to be named this, there are other files 
#  for other extension processors
s.extensions << 'ext/mkrf_conf.rb'

然后是 mkrf_conf.rb 中的相关行 来源

And then the relevant lines in that mkrf_conf.rb source

require 'rubygems/dependency_installer.rb' 
inst = Gem::DependencyInstaller.new
inst.install "test-unit" if RUBY_VERSION > "1.9"

这篇关于如何将条件 ruby​​gem 要求添加到 gem 规范中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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