does using “:platforms =>” in your gemfile work?

帅比萌擦擦* 提交于 2019-11-29 03:32:49

:platforms => :ruby does indeed exclude gems from being installed on Windows.

However, it does not work in a cygwin environment. In cygwin, it considers the platform to be :mri.

You'll also notice that ruby -e 'puts RUBY_PLATFORM' outputs i386-cygwin, not i386-mingw32 or i386-mswin like it would on Windows ruby.

Were you working in a cygwin environment?

Add code to the Gemfile like this that excludes/includes gems depending on the OS platform

if RUBY_PLATFORM=~ /win32/ 
   gem "windows-only-gem"
else
   gem "os-agnostic-gem"
end
Luis Lavena

Bundler concept of platform differs from normal understanding of RUBY_PLATFORM matching or RubyGems behaviors.

You can find the entire documentation about how to use platforms for Bundler here:

http://bundler.io/v1.14/man/gemfile.5.html

You might not need therubyraceron Windows (it actually doesn't work), but you might need execjs so CoffeeScript or other details of Asset Pipeline work properly

In your case, I will do:

gem "execjs"
gem "therubyracer", :platforms => :ruby

UPDATE: execjs gem might be installed because another dependency (not limited by platforms) is depending on it to be installed.

Rails 5:

if Gem.win_platform?
  # Install gem for Windows
else
  # Install another gem
end

I'm not sure about the :platform switch as I've never used it. However, an alternative that I think will work for your scenario would be to wrap your declarations for those two gems in a 'group' block in your Gemfile. Such as...

group :production do
  gem 'therubyracer'
  gem 'execjs'
end

This way, those gems will only be used in your production environment, not in development.

Note that I believe bundler will still install them in development (something to do with dependency checking), but they won't actually get loaded and therefore shouldn't cause problems.

gem 'win32-security', '~> 0.3.1' if (RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/)

That works pretty well for me.

A variation of @ffoeg's answer worked for me, and handles all windows environments, whereas just using RUBY_PLATFORM=~ /win32/ didn't work:

if RUBY_PLATFORM =~ /mswin|mingw|cygwin/i

  gem 'windows-only'

else

  gem 'non-windows'    

end

I agree that it's not ideal to have different gemfiles, however since I'm using unicorn to serve my Jekyll blog on Heroku, so I need gem unicorn - and this relies on kgio which several sources have confirmed is virtually impossible to install on windows...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!