您不能在 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
...