How to add Mac-specific gems to bundle on Mac but not on Linux?

后端 未结 5 1614
感动是毒
感动是毒 2020-12-15 04:04

I\'m developing a Rails app on a Mac, and I\'m new to testing, so I just added these gems to my Gemfile:

group :test, :development do
  gem          


        
相关标签:
5条回答
  • 2020-12-15 04:34

    @zed_0xff: found a similar approach in an older rspec-core commit:

    if RUBY_PLATFORM =~ /darwin/
      gem 'foo'
    end
    
    0 讨论(0)
  • 2020-12-15 04:34

    According to the Bundler docs, you need to use the platforms directive:

    #Gemfile
    platforms :mswin do
      gem "x"
    end
    
    gem "weakling",   :platforms => :jruby
    gem "ruby-debug", :platforms => :mri_18
    gem "nokogiri",   :platforms => [:mri_18, :jruby]
    

    There are a number of Gemfile platforms:

    ruby C Ruby (MRI) or Rubinius, but NOT Windows

    ruby_18 ruby AND version 1.8

    ruby_19 ruby AND version 1.9

    ruby_20 ruby AND version 2.0

    mri Same as ruby, but not Rubinius

    mri_18 mri AND version 1.8

    mri_19 mri AND version 1.9

    mri_20 mri AND version 2.0 rbx Same as ruby, but only Rubinius (not MRI

    jruby JRuby

    mswin Windows

    mingw Windows 'mingw32' platform (aka RubyInstaller)

    mingw_18 mingw AND version 1.8

    mingw_19 mingw AND version 1.9 mingw_20 mingw AND version 2.0

    0 讨论(0)
  • 2020-12-15 04:39

    Gemfile actually is a regular ruby file, so you can use something like

    case RUBY_PLATFORM
    when /darwin/
      gem 'foo'
    when /win32/
      gem 'bar'
    end
    
    0 讨论(0)
  • 2020-12-15 04:40

    You can use :install_if method which accepts arbitrary lambda.

    Following example comes directly from Gemfile's man pages:

    install_if -> { RUBY_PLATFORM =~ /darwin/ } do
      gem "pasteboard"
    end
    

    It is much better than control flow constructs (e.g. if) as it maintains dependencies correctly and keeps Gemfile.lock uniform on all machines.

    0 讨论(0)
  • 2020-12-15 04:41

    The Bundler wiki has a method that adds all gems to Gemfile.lock regardless of platform. It sets require => false depending on the system so you don't need to be able to actually run the gems:

    gem 'rb-inotify', :require => RUBY_PLATFORM.include?('linux') && 'rb-inotify'
    

    And they provide helper methods to make this clean:

    def os_is(re)
      RbConfig::CONFIG['host_os'] =~ re
    end
    
    gem 'rb-fsevent', "~> 0.9.3", platforms: :ruby, install_if: os_is(/darwin/)
    gem 'rb-inotify', "~> 0.8.8", platforms: :ruby, install_if: os_is(/linux/)
    gem 'wdm',        "~> 0.1.0", platforms: [:mswin, :mingw. :x64_mingw], install_if: os_is(/mingw|mswin/i)
    

    On my Windows 7 x64 system running Ubuntu 12.04 in a Vagrant VM, this worked fine, but the :platforms setting was required - the Linux bundler choked on the 'win32console' gem:

    Console.c:1:21: fatal error: windows.h: No such file or directory
    
    0 讨论(0)
提交回复
热议问题