Ruby 捆绑器。如何使用 Gemfile.lock 的工作集锁定 Gemfile 中的所有版本?
Ruby Bundler. How do I lock down all versions in Gemfile with working set from Gemfile.lock?
我想将我的 Gemfile
设置为使用工作 Gemfile.lock
中的所有确切版本。
最简单的方法是什么?
我不想手动操作。 bundler
是否开箱即用。如果没有,是否有一个 gem 用于此?
澄清一下,我有一个 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'
如果您试图在 Gemfile
中为另一个项目重现您当前的 Gemfile.lock
,您可以指定您的 gem 的确切版本。
假设我的 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'
等等..
编辑
在您改变问题的焦点之后,您似乎正试图从 Gemfile.lock
.
创建一个 Gemfile
你可以看看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
你总是可以自己写:)
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
但仅适用于单引号。
要查看 gem Gemfile 格式的名称和版本,请参见
bundle show
在 linux 上,我使用这个快速而肮脏的片段转换为我可以通过 Gemfile 复制的格式。
bundle show | ruby -ne 'puts $_.gsub(/ \* /,"gem \"").gsub(/ \(/,"\", \"~> ").gsub(/\)/,"\"")'
我想将我的 Gemfile
设置为使用工作 Gemfile.lock
中的所有确切版本。
最简单的方法是什么?
我不想手动操作。 bundler
是否开箱即用。如果没有,是否有一个 gem 用于此?
澄清一下,我有一个 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'
如果您试图在 Gemfile
中为另一个项目重现您当前的 Gemfile.lock
,您可以指定您的 gem 的确切版本。
假设我的 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'
等等..
编辑
在您改变问题的焦点之后,您似乎正试图从 Gemfile.lock
.
Gemfile
你可以看看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
你总是可以自己写:)
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
但仅适用于单引号。
要查看 gem Gemfile 格式的名称和版本,请参见
bundle show
在 linux 上,我使用这个快速而肮脏的片段转换为我可以通过 Gemfile 复制的格式。
bundle show | ruby -ne 'puts $_.gsub(/ \* /,"gem \"").gsub(/ \(/,"\", \"~> ").gsub(/\)/,"\"")'