Ruby-on-rails 如何在 .gemspec 文件中将本地 gem 的依赖项添加到 rails 插件/引擎
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13678957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add dependency of a local gem to a rails plugin/engine, in .gemspec file
提问by QingHua CHEN
I had tried in this way:
我曾经尝试过这种方式:
s.add_dependency 'gem', :path => '../gem'
like add gem in the gemfile,but it doesn't work, and will cause this error:
就像在里面添加 gemgemfile,但它不起作用,并且会导致这个错误:
/Users/chenqh/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/requirement.rb:81:in `parse': Illformed requirement
采纳答案by My God
It's likely not possible to add local dependencies because other users will not be able to access the gem as it is local dependent and hence of no use after publish. Instead of that, Add remote dependency in your own plugin's gemspec.
可能无法添加本地依赖项,因为其他用户将无法访问 gem,因为它是本地依赖项,因此在发布后没有用。取而代之的是,在您自己的插件的 gemspec 中添加远程依赖项。
Steps -
脚步 -
1) Open gemspec file of your own plugin in vendor/plugins/my_plugin/and add before the keyword end:
1)在vendor/plugins/my_plugin/中打开自己插件的gemspec文件,在关键字end前添加:
s.add_dependency('will_paginate', '~> 3.0.pre2')
(In this example I have used will_paginate required dependency of my_plugin)
(在这个例子中,我使用了 my_plugin 的 will_paginate 所需的依赖项)
2) Now go in your rails app and edit Gemfile, add:
2)现在进入你的rails应用程序并编辑Gemfile,添加:
gem 'my_plugin', path: 'vendor/plugins/my_plugin'
3) Now in rails app root do:
3) 现在在 rails app root 中执行:
bundle install
And dependency of my_plugin (will_paginate in this case) is installed.
并且安装了 my_plugin(在本例中为 will_paginate)的依赖项。
回答by Ashitaka
While developing 2 gems, gem1 and gem2, requiring that gem1 locally depends on gem2 is quite handy.
在开发2个gemgem1和gem2时,要求gem1本地依赖gem2是很方便的。
You can't do this in your gemspec, however, you can do so in your gem's Gemfile!
你不能在你的gemspec,但是,你可以在你的 gem 中这样做Gemfile!
# Gemfile
source "https://rubygems.org"
gem 'gem2', :path => '../gem2'
# Specify your gem's dependencies in gem1.gemspec
gemspec
And then in your gemspec, require your gem like you would normally if that gem was already published:
然后在您的 gemspec 中,如果该 gem 已经发布,则像通常一样需要您的 gem:
# gem1.gemspec
spec.add_runtime_dependency "gem2"
Just make sure you don't accidentally push that Gemfilechange!
只要确保您不会意外地推动该Gemfile更改!
回答by Ian
Sometimes you want to embed one gem into another gem, nevermind why. You can reference one gempec from another to completely encapsulate a local gem.
有时您想将一颗宝石嵌入到另一颗宝石中,不管为什么。您可以从另一个引用一个 gempec 以完全封装本地 gem。
require "rubygems"
embedded_gemspec = Gem::Specification::load("path/to/internal.gemspec")
Gem::Specification.new do |spec|
spec.name = "gem_that_contains_another_gem"
# spec.whatever, = whole bunch of other info
# our files + other gem's files
spec.files = ['file1.rb', 'file2.rb'] + embedded_gemspec.files
# our dependencies
spec.add_dependency 'nokogiri'
# other gem's dependencies
embedded_gemspec.runtime_dependencies.each { |d| spec.add_dependency d }
end
EDIT: this appears to only work locally. If you try to install this gemspec from, say, a git repo, it won't know where to get embedded_gemspec(even though embedded_gemspec's dependencies come in just fine).
编辑:这似乎只适用于本地。如果您尝试从 git 存储库安装此 gemspec,它将不知道从哪里获取embedded_gemspec(即使embedded_gemspec的依赖项很好)。

