在gemfile中使用“:platform =>”吗?

时间:2022-11-03 02:13:10

I have a Rails app that I'm developing on Windows and deploying to Linux. I suspect I'll just switch entirely over to Linux in the future. Anyway, on Linux I need 'execjs' and 'therubyracer' but I don't need those in Win7. So I put these lines in my gemfile:

我有一个Rails应用程序,我正在Windows上开发并部署到Linux。我想以后我将完全切换到Linux。无论如何,在Linux上我需要'execjs'和'therubyracer',但在Win7中我不需要这些。所以我把这几行写在gemfile里:

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

Ran a bundle install on the Linux VM and the app started up fine. But on Windows I get:

在Linux VM上运行捆绑安装,应用程序启动良好。但在Windows上,我得到:

Uncaught exception: Could not find execjs-1.2.11 in any of the sources

未捕获的异常:在任何来源中都找不到execjs-1.2.11

Now, from what I read (here under PLATFORMS) it tells me that "If a gem should only be used in a particular platform or set of platforms, you can specify them" and the sample is this:

现在,根据我所读到的(在平台下面),它告诉我,“如果一个gem只应该在特定的平台或平台集中使用,那么您可以指定它们”,示例如下:

gem "weakling",   :platforms => :jruby 

And it says "ruby C Ruby (MRI) or Rubinius, but NOT Windows". So to me that says that bundler should be ignoring the execjs line on Windows. However on Windows when I ran bundle install I saw this:

上面写着“ruby C ruby (MRI)或Rubinius,但不是Windows”。所以对我来说,bundler应该忽略Windows上的execjs行。然而,当我在Windows上运行bundle install时,我发现:

Installing execjs (1.2.11)

So that says to me I'm missing something about the docs or bundler is ignoring the platforms command. Am I doing something wrong?

这说明我忽略了文档或者bundler忽略了platform命令。我做错什么了吗?

PS>bundle -v
Bundler version 1.0.21

7 个解决方案

#1


18  

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

:platform =>:ruby确实将gems排除在Windows上。

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

然而,它不能在cygwin环境中工作。在cygwin,它认为平台是: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.

您还会注意到ruby -e 'put RUBY_PLATFORM'输出i386-cygwin,而不是像Windows ruby那样输出i386-mingw32或i386-mswin。

Were you working in a cygwin environment?

你是在cygwin环境工作吗?

#2


10  

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

向Gemfile中添加这样的代码,根据操作系统平台排除/包含gems

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

#3


3  

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

Bundler平台概念与RUBY_PLATFORM匹配或RubyGems行为的一般理解不同。

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

您可以在这里找到关于如何使用Bundler的平台的完整文档:

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

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

您可能不需要therubyraceron Windows(它实际上不工作),但是您可能需要execjs以便对资产管道进行修改或其他详细的工作

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.

更新:可能会安装execjs gem,因为另一个依赖项(不受平台限制)依赖于要安装它。

#4


2  

Rails 5:

Rails 5:

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

#5


1  

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...

我不确定:平台切换,因为我从未使用过它。但是,我认为适合您的方案的另一个选择是将这两个gem的声明封装在Gemfile中的“组”块中。如……

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

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

这样,这些gem只会在您的生产环境中使用,而不会在开发中使用。

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.

注意,我相信bundler仍将在开发中安装它们(与依赖项检查有关),但是它们实际上不会被加载,因此不应该引起问题。

#6


0  

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

That works pretty well for me.

这对我来说很有效。

#7


0  

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

@ffoeg的解决方案对我有效,并处理所有windows环境,而只是使用RUBY_PLATFORM=~ /win32/没有工作:

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...

我同意拥有不同的gemfiles并不理想,但是因为我正在使用独角兽来为我在Heroku上的Jekyll博客服务,所以我需要gem独角兽——这依赖kgio,有几个消息来源已经证实,几乎不可能安装在windows上……

#1


18  

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

:platform =>:ruby确实将gems排除在Windows上。

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

然而,它不能在cygwin环境中工作。在cygwin,它认为平台是: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.

您还会注意到ruby -e 'put RUBY_PLATFORM'输出i386-cygwin,而不是像Windows ruby那样输出i386-mingw32或i386-mswin。

Were you working in a cygwin environment?

你是在cygwin环境工作吗?

#2


10  

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

向Gemfile中添加这样的代码,根据操作系统平台排除/包含gems

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

#3


3  

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

Bundler平台概念与RUBY_PLATFORM匹配或RubyGems行为的一般理解不同。

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

您可以在这里找到关于如何使用Bundler的平台的完整文档:

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

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

您可能不需要therubyraceron Windows(它实际上不工作),但是您可能需要execjs以便对资产管道进行修改或其他详细的工作

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.

更新:可能会安装execjs gem,因为另一个依赖项(不受平台限制)依赖于要安装它。

#4


2  

Rails 5:

Rails 5:

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

#5


1  

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...

我不确定:平台切换,因为我从未使用过它。但是,我认为适合您的方案的另一个选择是将这两个gem的声明封装在Gemfile中的“组”块中。如……

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

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

这样,这些gem只会在您的生产环境中使用,而不会在开发中使用。

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.

注意,我相信bundler仍将在开发中安装它们(与依赖项检查有关),但是它们实际上不会被加载,因此不应该引起问题。

#6


0  

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

That works pretty well for me.

这对我来说很有效。

#7


0  

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

@ffoeg的解决方案对我有效,并处理所有windows环境,而只是使用RUBY_PLATFORM=~ /win32/没有工作:

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...

我同意拥有不同的gemfiles并不理想,但是因为我正在使用独角兽来为我在Heroku上的Jekyll博客服务,所以我需要gem独角兽——这依赖kgio,有几个消息来源已经证实,几乎不可能安装在windows上……