Last active
December 21, 2015 14:59
-
-
Save estum/6323910 to your computer and use it in GitHub Desktop.
Bundler::DSL extension to provide OS-depended gems in Gemfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/bundler/dsl.rb | |
# Bundler::DSL extension to provide OS-depended gems in Gemfile | |
# https://github.com/bundler/bundler/wiki/Platform-as-a-parameter | |
# | |
# Allows to group os-dependent gems in blocks with one or more symbols, strings or regexps as parameters. | |
# OS name is obtained from `RbConfig::CONFIG['host_os']` or `RUBY_PLATFORM` if the first one is not available. | |
# | |
# Place this file in the project's lib folder and require it in your Gemfile: | |
# require File.expand_path('../lib/bundler/dsl', __FILE__) | |
module Bundler | |
class Dsl | |
# ==== Usage: | |
# os :linux do | |
# gem 'rb-inotify' | |
# gem 'libnotify' | |
# end | |
# | |
# # matching with multiple parameters | |
# os :linux, :darwin, :mac do | |
# gem 'unicorn' | |
# end | |
# | |
# # Regexp | |
# os /mingw|mswin/ do | |
# gem 'wdm', "~> 0.1.0" | |
# end | |
def os(*names) | |
yield if os?(*names) | |
end | |
# You can also use if/unless: | |
# ==== Usage: | |
# # single line, negative matching: | |
# gem 'unicorn' unless os? :mingw, :mswin | |
def os_name_match?(*names) | |
names.map! {|name| name.is_a?(Regexp) ? name : name.to_s } | |
!!host_os[Regexp.union(*names)] | |
end | |
alias_method :os?, :os_name_match? | |
def host_os | |
@host_os ||= host_os_or_platform.downcase | |
end | |
private | |
def host_os_or_platform | |
RbConfig::CONFIG['host_os'] | |
rescue => e | |
return RUBY_PLATFORM | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example Gemfile: