4

我想阻止更新我的 windows ( rmagick) 上的 gem,所以它坚持2.12.0 mswin32. 不过,我的同事需要在他的 Darwin 安装上安装 gem ......

所以,我试图在Gemfile

if RUBY_PLATFORM =~ /darwin/i
  gem 'rmagick', '~> 2.12.0'
else
  gem 'rmagick', '=2.12.0.mswin32'
end

bundle install投诉。

正确处理此问题的正确方法是什么?

4

2 回答 2

1

您应该使用Bundler 提供的平台选项:

如果 gem 应该只用于特定平台或平台集,您可以指定它们。平台本质上与组相同,只是您不需要使用 --without install-time 标志来排除其他平台的 gem 组。

因此,在您的特定情况下,它看起来像这样:

gem 'rmagick', '~> 2.12.0', :platforms => :ruby
gem 'rmagick', '=2.12.0.mswin32', :platforms => :mswin
于 2013-02-12T13:20:23.210 回答
1

您不能在 gemspec 上使用条件,因为 gemspec 被序列化为 YAML,它不包含可执行代码。

Gemfile我在本地 Rails 项目(不是 gem)中遇到了相关问题。

目前,Gemfile 包含:

group :test do
...
# on Mac os X
  gem 'rb-fsevent' if RUBY_PLATFORM.include?("x86_64-darwin")
  gem 'ruby_gntp' if RUBY_PLATFORM.include?("x86_64-darwin")

# on Linux
  gem 'rb-inotify' unless RUBY_PLATFORM.include?("x86_64-darwin")
  gem 'libnotify' unless RUBY_PLATFORM.include?("x86_64-darwin")
end

这适用于在 Mac 和 Linux 系统上进行开发(尽管它很丑)。

但是,我们停止签入,Gemfile.lock因为每次使用不同平台的开发人员签入代码时它都会发生变化。

因此,多平台 Gemfile 的解决方案也应该解决Gemfile.lock.

其他解决方案是为每个目标操作系统构建多个.gemspec 文件,并更改每个平台的平台和依赖项:

gemspec = Gem::Specification.new do |s|
  s.platform = Gem::Platform::RUBY
end

# here build the normal gem

# Now for linux:
gemspec.platform = "linux"
gemspec.add_dependency ...

# build the newer gemspec
...
于 2013-02-12T10:12:45.520 回答