How do I add conditional rubygem requirements to a gem specification?

后端 未结 7 799
伪装坚强ぢ
伪装坚强ぢ 2021-01-06 03:00

Is it possible to add a gem dependency only if the person is using a certain version of ruby?

Background: I\'m working on a fork of a project that u

7条回答
  •  旧巷少年郎
    2021-01-06 03:35

    I did this exact thing for a project. The trick is to add the script as an extension, which will then get executed at install time on the user's machine.

    Here are code snippets and links to our github:

    First, when in the gemspec (we're actually using a Rakefile to generate it, but the result ends up the same) source

    # This file *needs* to be named this, there are other files 
    #  for other extension processors
    s.extensions << 'ext/mkrf_conf.rb'
    

    And then the relevant lines in that mkrf_conf.rb source

    require 'rubygems/dependency_installer.rb' 
    inst = Gem::DependencyInstaller.new
    inst.install "test-unit" if RUBY_VERSION > "1.9"
    

提交回复
热议问题