Ruby Bundler。如何使用Gemfile.lock中的工作集锁定Gemfile中的所有版本?

问题描述 投票:3回答:3

我想设置我的Gemfile使用工作Gemfile.lock的所有确切版本。

最简单的方法是什么?

我不想手动做。 bundler是否开箱即用。如果没有,这是否有宝石?

为了澄清,我有这样的Gemfile

source 'https://rubygems.org'

gem 'pg'
gem 'puma'
gem 'rails'

我运行bundle install,我得到一个适合我的Gemfile.lock

GEM
  remote: https://rubygems.org/
  specs:
    pg (0.21.0)
    puma (3.10.0)
    rails (5.0.5)
      actioncable (= 5.0.5)
      actionmailer (= 5.0.5)
      actionpack (= 5.0.5)
      actionview (= 5.0.5)
      activejob (= 5.0.5)
      activemodel (= 5.0.5)
      activerecord (= 5.0.5)
      activesupport (= 5.0.5)
      bundler (>= 1.3.0)
      railties (= 5.0.5)
      sprockets-rails (>= 2.0.0)

现在我想要一个更新我的Gemfile的命令,以便指定版本:

source 'https://rubygems.org'

gem 'pg', '0.21.0'
gem 'puma', '3.10.0'
gem 'rails', '5.0.5' 
ruby bundler
3个回答
2
投票

如果您尝试在Gemfile.lock中为另一个项目重现当前的Gemfile,则可以指定宝石的确切版本。

让我们假设我的Rails应用程序中有以下Gemfile

source 'https://rubygems.org'

gem 'pg', '~> 0.18'
gem 'puma', '~> 3.0'
gem 'rails', '~> 5.0.0', '>= 5.0.0.1' 

它生成以下Gemfile.lock

GEM
  remote: https://rubygems.org/
  specs:
    pg (0.21.0)
    puma (3.10.0)
    rails (5.0.5)
      actioncable (= 5.0.5)
      actionmailer (= 5.0.5)
      actionpack (= 5.0.5)
      actionview (= 5.0.5)
      activejob (= 5.0.5)
      activemodel (= 5.0.5)
      activerecord (= 5.0.5)
      activesupport (= 5.0.5)
      bundler (>= 1.3.0)
      railties (= 5.0.5)
      sprockets-rails (>= 2.0.0)

如果我想在新项目中重现完全相同的情况,我将创建一个具有确切版本的Gemfile:

source 'https://rubygems.org'

gem 'pg', '0.21.0'
gem 'puma', '3.10.0'
gem 'rails', '5.0.5'

等等..

你可以参考bundler documentation

Edit

在你改变了问题的焦点之后,似乎你正试图从Gemfile创建一个Gemfile.lock

你可以看看bundle --deployment。我看到几个SO问题抱怨输出。

因此,如果它不是100%令人满意,您可以使用Bundler::LockfileParser并制作您自己的脚本,例如:

# test.rb
require 'bundler'

lockfile = Bundler::LockfileParser.new(Bundler.read_file('Gemfile.lock'))

specs = lockfile.specs
gems_hash = Hash.new.tap do |h|
  specs.each do |s|
    h[s.name] = {
      spec: s,
      dependencies: s.dependencies.map(&:name)
    }
  end
end

dependencies = gems_hash.keys && gems_hash.values.map { |h| h[:dependencies] }.flatten.uniq.sort

# Remove from the new Gemfile all gems installed as dependencies
dependencies.each { |dep| gems_hash.delete(dep) }

relevant_specs = gems_hash.values.map { |h| h[:spec] }

# I assume that by default you are installing from rubygems
puts "source 'https://rubygems.org'"
puts

relevant_specs.each do |s|
  if s.source.to_s =~ /https:\/\/rubygems.org/
    puts "gem '#{s.name}', '#{s.version}'" # eventually add "plaftform: :#{s.platform}"
  # I consider as only alternative a git source. 
  elsif s.source.is_a?(Bundler::Source::Git)
    uri = s.source.uri
    branch = s.source.branch
    ref = s.source.ref
    puts
    puts "git '#{uri}', branch: '#{branch}', ref: :#{ref} do"
    puts "  gem '#{s.name}'"
    puts "end"
    puts
  end
end
puts

我创建了这个gist以便于阅读。

你可以通过运行来创建你的Gemfile

$ ruby test.rb > Gemfile

1
投票

你总是可以自己写:)

result = ""
File.readlines("Gemfile").each do |line|
  if line.strip.start_with?("gem") && !line.include?(",")
    name = line.match(/gem '(.+)'/)[1]
    output = `bundle info #{name}`
    version = output.match(/#{name} \((.+)\)/)[1]
    result << "  " if line.start_with?("  ")
    result << "gem '#{name}', '#{version}'\n"
  else
    result << line
  end
end
puts result

但是,仅适用于单引号。


1
投票

要查看Gemfile中的gem名称和版本,请参阅 bundle show

在linux上我使用这个快速和脏的代码片段转换为我可以通过Gemfile复制的格式。

bundle show | ruby -ne 'puts $_.gsub(/ \* /,"gem \"").gsub(/ \(/,"\", \"~> ").gsub(/\)/,"\"")'

© www.soinside.com 2019 - 2024. All rights reserved.