Last active
December 14, 2015 07:19
-
-
Save mmichaa/5050281 to your computer and use it in GitHub Desktop.
Paste this code snippet into your Gemfile. It let's you easily define gems for certain OSes.
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
=begin | |
Paste this code snippet into your Gemfile. It let's you easily define gems for certain OSes using gem_for_darwin, gem_for_linux or gem_for_windows as gem-statement, e. g. | |
# notifications for file changes | |
gem_for_osx 'rb-fsevent', :platforms => :ruby | |
gem_for_linux 'rb-inotify', :platforms => :ruby | |
gem_for_windows 'rb-fchange', :platforms => [:mswin, :mingw] | |
# console color for windows | |
gem_for_windows 'win32console', :platforms => [:mswin, :mingw] | |
# To use growl ui-notifications | |
gem_for_osx 'ruby_gntp', :platforms => :ruby | |
=end | |
# ----- Helper Methods ----- | |
def darwin_only(require_as) | |
RbConfig::CONFIG['host_os'] =~ /darwin/ && require_as | |
end | |
alias :osx_only :darwin_only | |
def linux_only(require_as) | |
RbConfig::CONFIG['host_os'] =~ /linux/ && require_as | |
end | |
def windows_only(require_as) | |
RbConfig::CONFIG['host_os'] =~ /mingw|mswin/i && require_as | |
end | |
alias :win_only :windows_only | |
# OS must be one of: :darwin | :osx, :linux, :windows | :win | |
# Last elemt in args should be the options-hash: | |
# :require_as => Optional parameter, giving the name for the require statement | |
def gem_for_os(os, *args) | |
if args.last.is_a?(Hash) | |
opts = args.last | |
else | |
opts = {} | |
args << opts | |
end | |
opts[:require] = send("#{os}_only", opts.delete(:require_as) || args.first) | |
# --- | |
gem(*args) | |
end | |
def gem_for_darwin(*args) | |
gem_for_os(:darwin, *args) | |
end | |
alias :gem_for_osx :gem_for_darwin | |
def gem_for_linux(*args) | |
gem_for_os(:linux, *args) | |
end | |
def gem_for_windows(*args) | |
gem_for_os(:windows, *args) | |
end | |
alias :gem_for_win :gem_for_windows | |
# ----- End ----- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment