Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rubygems optional dependencies

Tags:

ruby

rubygems

How do I add optional dependencies to rubygems, as it does not support it?

Use cases:

  • I have a library that does either depend on Backports, Extlib, Facets or ActiveSupport. In the past I just did not add any of this dependencies, as people could choose (read: it would play nicely with whatever they were using), but people complained a lot about the library not working out of the box, or they would believe it depended on ActiveSupport (which in some cases is a no go for some people). So at the moment it depends on Backports, as it is the most minimal. Though that means people will install Backports if they install my library, even though they might not use it at Runtime.
  • I want to depend on different gems depending on the environment (i.e. Johnson on MRI/Rubinius, TheRubyRhino on JRuby, Lyndon on MacRuby).

One approach I though about is highjacking extconf.rb. I could do system checks there. However, this feels wrong. Moreover, if this is the only way, how do I go about it? How do I trigger installing additional gems from there? system 'gem install ...'?

like image 960
Konstantin Haase Avatar asked Jun 07 '10 21:06

Konstantin Haase


2 Answers

Have you tried giving informative error messages for unmet dependencies to clear up those misconceptions? With zombie-chaser, I don't make gosu a dependency, but if people try to use the graphical version of it without gosu, I inform them that they need to install gosu.

like image 173
Andrew Grimm Avatar answered Oct 17 '22 13:10

Andrew Grimm


I know of a couple gems that take the "hijacking extconf.rb" approach:

  • perftools.rb has a dependency if it's on 1.9, but does not require that dependency on 1.8.
  • ruby-debug-ide depends on ruby-debug-base on 1.8, ruby-debug-base19 on 1.9.

Both use Gem::DependencyInstaller#install to do the installation, which is probably preferable to system 'gem install ...'.

See also this rubygems feature request: specify dependencies per ruby version.

like image 21
John Avatar answered Oct 17 '22 13:10

John