Skip to content

Instantly share code, notes, and snippets.

@estum
Last active December 21, 2015 14:59
Show Gist options
  • Save estum/6323910 to your computer and use it in GitHub Desktop.
Save estum/6323910 to your computer and use it in GitHub Desktop.
Bundler::DSL extension to provide OS-depended gems in Gemfile
# 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
@estum
Copy link
Author

estum commented Aug 23, 2013

Example Gemfile:

require File.expand_path('../lib/bundler/dsl', __FILE__)
source 'https://rubygems.org'

gem 'rails', '3.2.14'
gem 'sqlite3'

# ... 

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

# single line in group, negative matching:
group :production do
  gem 'unicorn' unless os? :mingw, :mswin
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment