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?

NVI
  • 14,907
  • 16
  • 65
  • 104
  • It seems to be a good idea to check how [listen](https://github.com/guard/listen) gem handles its platform-specific dependencies: [rb-inotify](https://github.com/nex3/rb-inotify) and [rb-fsevent](https://github.com/thibaudgg/rb-fsevent). – skalee Sep 24 '14 at 17:58

1 Answers1

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.

Fotios
  • 3,643
  • 1
  • 28
  • 30
  • 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 04 '14 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 '14 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 '14 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 '14 at 12:51