1

我正在尝试composite_primary_keys使用 Bundler 安装 gem 的自定义版本,该版本根据版本进行了一些我们需要的更改2.2.2。我们的 gem 版本是2.2.2.0并且托管在我们的内部 gem 服务器上(这个 4 位版本控制方案仅存在于我们的内部 gem 服务器上)。应用程序要求我们使用 jruby-1.6.7 ( RUBY_VERSION= 1.8.7)、RubyGems 版本 ( gem -v) 1.3.6 和 Bundler 的最新稳定版本 1.2.3。

无论如何,我的 Gemfile 看起来像这样:

source 'http://rubygems.org'
source 'http://gems.internal'

gem 'composite_primary_keys', '2.2.2.0'
gem 'hoe',                    '1.8.3'

执行bundle输出:

$ bundle
Fetching gem metadata from http://rubygems.org/........
Fetching gem metadata from http://gems.internal/...
Installing rake (10.0.3) 
Installing i18n (0.6.1) 
Installing multi_json (1.5.0) 
Installing activesupport (3.2.11) 
Installing builder (3.0.4) 
Installing activemodel (3.2.11) 
Installing arel (3.0.2) 
Installing tzinfo (0.3.35) 
Installing activerecord (3.2.11) 
Installing json_pure (1.7.6) 
Installing rubyforge (2.0.4) 
Installing hoe (1.8.3) 
Installing composite_primary_keys (2.2.2) 
Using bundler (1.2.3) 
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.

如您所见,composite_primary_keys实际安装的是 2.2.2,而不是 2.2.2.0。这是生成的 Gemfile.lock:

GEM
  remote: http://rubygems.org/
  remote: http://gems.internal/
  specs:
    activemodel (3.2.11)
      activesupport (= 3.2.11)
      builder (~> 3.0.0)
    activerecord (3.2.11)
      activemodel (= 3.2.11)
      activesupport (= 3.2.11)
      arel (~> 3.0.2)
      tzinfo (~> 0.3.29)
    activesupport (3.2.11)
      i18n (~> 0.6)
      multi_json (~> 1.0)
    arel (3.0.2)
    builder (3.0.4)
    composite_primary_keys (2.2.2)
      activerecord (>= 2.2.0)
      hoe (>= 1.8.3)
    hoe (1.8.3)
      rake (>= 0.8.3)
      rubyforge (>= 1.0.2)
    i18n (0.6.1)
    json_pure (1.7.6)
    multi_json (1.5.0)
    rake (10.0.3)
    rubyforge (2.0.4)
      json_pure (>= 1.1.7)
    tzinfo (0.3.35)

PLATFORMS
  java

DEPENDENCIES
  composite_primary_keys (= 2.2.2.0)
  hoe (= 1.8.3)

现在,如果我手动卸载composite_primary_keys并安装我的版本:

$ gem uninstall composite_primary_keys
Successfully uninstalled composite_primary_keys-2.2.2
$ gem install composite_primary_keys -v 2.2.2.0
Successfully installed composite_primary_keys-2.2.2.0
1 gem installed
Installing ri documentation for composite_primary_keys-2.2.2.0...
Installing RDoc documentation for composite_primary_keys-2.2.2.0...

然后执行bundle

$ bundle        
Using rake (10.0.3) 
Using i18n (0.6.1) 
Using multi_json (1.5.0) 
Using activesupport (3.2.11) 
Using builder (3.0.4) 
Using activemodel (3.2.11) 
Using arel (3.0.2) 
Using tzinfo (0.3.35) 
Using activerecord (3.2.11) 
Using composite_primary_keys (2.2.2.0) 
Using json_pure (1.7.6) 
Using rubyforge (2.0.4) 
Using hoe (1.8.3) 
Using bundler (1.2.3) 
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.

它保留了我的自定义宝石。此时运行 bundle update 会更新Gemfile.lock

$ bundle update composite_primary_keys
Fetching gem metadata from http://rubygems.org/........
Fetching gem metadata from http://gems.internal/...
Using rake (10.0.3) 
Using i18n (0.6.1) 
Using multi_json (1.5.0) 
Using activesupport (3.2.11) 
Using builder (3.0.4) 
Using activemodel (3.2.11) 
Using arel (3.0.2) 
Using tzinfo (0.3.35) 
Using activerecord (3.2.11) 
Using composite_primary_keys (2.2.2.0) 
Using json_pure (1.7.6) 
Using rubyforge (2.0.4) 
Using hoe (1.8.3) 
Using bundler (1.2.3) 
Your bundle is updated! Use `bundle show [gemname]` to see where a bundled gem is installed.

Gemfile.lock 现在看起来像这样:

GEM
  remote: http://rubygems.org/
  remote: http://gems.internal/
  specs:
    activemodel (3.2.11)
      activesupport (= 3.2.11)
      builder (~> 3.0.0)
    activerecord (3.2.11)
      activemodel (= 3.2.11)
      activesupport (= 3.2.11)
      arel (~> 3.0.2)
      tzinfo (~> 0.3.29)
    activesupport (3.2.11)
      i18n (~> 0.6)
      multi_json (~> 1.0)
    arel (3.0.2)
    builder (3.0.4)
    composite_primary_keys (2.2.2.0)
      activerecord (>= 2.2.0)
    hoe (1.8.3)
      rake (>= 0.8.3)
      rubyforge (>= 1.0.2)
    i18n (0.6.1)
    json_pure (1.7.6)
    multi_json (1.5.0)
    rake (10.0.3)
    rubyforge (2.0.4)
      json_pure (>= 1.1.7)
    tzinfo (0.3.35)

PLATFORMS
  java

DEPENDENCIES
  composite_primary_keys (= 2.2.2.0)
  hoe (= 1.8.3)

我猜

我的怀疑是 Bundler 以某种方式依赖于 RubyGems 如何实现 <=> 运算符...

require 'rubygems'
Gem::Version.new('2.2.2') <=> Gem::Version.new('2.2.2.0') # => 0
Gem::Version.new('2.2.2') == Gem::Version.new('2.2.2.0') # => true

任何人都可以确认吗?这是预期的行为吗?如果版本号丢失一个数字,这是否是避免次要版本号为 0 的一个很好的论据?例如

Gem::Version.new('2') == Gem::Version.new('2.0.0.0.0.0.0.0.0') # => true
4

1 回答 1

0

尽管这并不能直接回答您的问题。过去我遇到过类似的问题,最终使自定义 gem 成为另一个 gem 的扩展。即修补“my-awesome-gem”中的功能,您可以将其称为“my-awesome-gem-custom”或类似名称。相信我,你要走的路充满了依赖噩梦。:)

一个注意事项是要注意扩展 gem 所需的自定义 Gem 命名的差异,因为它们略有不同。“要求 /x/y/z”与“要求 x_y_z”。这样做应该允许您根据需要使用 Ruby 的开放类来分层或覆盖功能。

于 2013-01-30T00:33:52.873 回答