33

I've a ruby gem that has different dependencies for each OS. I have to explicitly write all of them down:

On Mac OS X:

gem install livereload

on Linux:

gem install rb-inotify livereload

on Windows:

gem install eventmachine-win32 win32-changenotify win32-event livereload

Can I tweak a gemspec a bit so installation instructions would look like plain gem install livereload for every OS?

1

1 Answer 1

21

The proper way to do this is outlined here. Since the gemspec is evaluated at package time, you need to do it in a native extension. Don't worry, it's not that scary since its still just Ruby code (not compiling C or anything).

We are currently using this approach for some client tools for OpenShift (source). Then in your gemspec/Rakefile, instead of adding dependencies, you would add an extension. Note that the file needs to be named ext/mkrf_conf.rb for this to work.

4
  • Your source-link does not work. Also: would it not be possible to simply add all gems as dependencies in the Gemspec, and require them based on platform? (require 'gemname' if RbConfig::CONFIG['target_os'] =~ /freebsd/i)
    – Automatico
    Mar 4, 2014 at 19:53
  • 2
    @Cort3z you are missing one important point. The solution that you describe works, but it solves the problem of not using the gem dependency for certain OS after it is installed. The original problem is not installing the dependency at all.
    – i4niac
    Mar 30, 2014 at 12:42
  • Here's my example. I have a set of tools developed in-house that combine a number os shell and ruby scripts that we use for iOS, Android developemtn and other tasks. It's all wrapped into a Ruby gem. For iOS we have dependencies like xcodeproj, cocoapods, calabash-cucumber. These gems won't install on any OS other than OSX, simply because they need Xcode command line tools, iOS simulator and other things. But I still want to be able to install the gem on Linux to use Android scripts. I will not use any iOS tools on Linux, but it is also important that those are not installed.
    – i4niac
    Mar 30, 2014 at 12:46
  • I could, of course, have 2 versions of the gem built, one for OSX, and one for the rest. But I like more the idea of having on gem on the gem server.
    – i4niac
    Mar 30, 2014 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.