我可以根据命令行输入为我的 gem 指定动态依赖项吗?
介绍:我正在开发一个 gem,默认情况下,它会从一些 XML 数据中提取信息并对文档进行某种处理。我正在使用 nokogiri 来解析 XML。但是,我希望允许用户自己解析 XML 并传入运行我的数据处理方法所需的信息,以防他们不想安装 nokogiri 或已经解析了 XML。
问题:有什么方法可以让用户在 gem 安装过程中指定他们不希望安装 nokogiri 依赖项?例如(这里非常手动),
gem install crazy_gem --no-nokogiri
在 gemspec 中也许类似
Gem::Specification.new do |s|
...
s.add_dependency 'nokogiri' unless Proc.new { install_flags('no-nokogiri') }
...
end
[编辑] 我不想过多关注上面的 gemspec 代码,因为我知道它不起作用 - 这只是一个例子我想做的事情。 [/edit]
gem install mad_gem --ignore-dependencies
在存在其他依赖项之前效果很好。
Intro: I'm working on a gem that, by default, will pull information out of some XML data and do some sort of processing on the document. I'm using nokogiri to parse the XML. However, I wish to allow the user to parse the XML themselves and pass in the necessary information for my data processing methods to run, in case they don't want to install nokogiri or have already parsed the XML.
Question: Is there any way to allow the user to specify, during gem installation, that they don't wish to install the nokogiri dependency? For example (very hand-wavey here),
gem install crazy_gem --no-nokogiri
and in the gemspec perhaps something like
Gem::Specification.new do |s|
...
s.add_dependency 'nokogiri' unless Proc.new { install_flags('no-nokogiri') }
...
end
[edit] I don't want to focus too much on the gemspec code above, as I know it doesn't work--it's just an example of the kind of thing I want to do. [/edit]
gem install crazy_gem --ignore-dependencies
works great until there are additional dependencies.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不认为你可以完全按照你的要求去做,但是如果你重新定义你的 gem 的功能,就有可能的解决方案。与默认情况下解析一些 XML 并处理数据,但可选择允许您传入预先解析的数据的 gem 不同,如果主要关注处理,但可选择为您解析 XML(如果你有野科切)。
为此,只需将 Nokogiri 排除在 gemspec 依赖项之外即可(您可以将其添加为开发依赖项< /a> 或要求)。
在代码中,确保仅在
begin..end
块中使用rescue LoadError
调用require 'nokogiri'
并将其处理为合适的。I don't think you can do exactly what you're after, but there's apossible solution if you reframe what your gem does. Rather than a gem that by default parses some XML and processes the data, but optionally allows you to pass in the pre-parsed data, what about a gem that is mainly concerned with the processing, but optionally will parse the XML for you (if you have Nokogiri).
To do this just leave Nokogiri out of your gemspec dependencies (you could add it as a development dependency or a requirement).
Inside your code, make sure you only call
require 'nokogiri'
in abegin..end
block with arescue LoadError
and deal with it as appropriate.Gemspec 在构建时会变成静态文件,因此这是行不通的。您可以尝试使用
-f
来绕过依赖项检查。Gemspecs are turned into static files at build time, so that wouldn't work. You could try using
-f
which bypasses dependency checks.