我想打扫一个小的系统房。基本上,

(Gem.all_system_gems - Bundler.referenced_gems(array_of_gemfile_locks)).each do |gem, ver|
  `gem uninstall #{gem} -v #{ver}
end

有没有这样的rubygems/bundler方法?或者有任何已知的/有效的方法来实现同样的目标?
谢谢,

最佳答案

注意:可能会造成严重的脑损伤。
I put up a version here解释每个功能。

# gem_cleaner.rb

require 'bundler'

`touch Gemfile` unless File.exists?("Gemfile")

dot_lockfiles = [ "/path/to/gemfile1.lock", "/path/to/gemfile2.lock"
  # ..and so on...
]

lockfile_parser = ->(path) do
  Bundler::LockfileParser.new(File.read(path))
end

lockfile_specs = ->(lockfile) { lockfile.specs.map(&:to_s) }

de_parenthesize = ->(string) { string.gsub(/\,|\(|\)/, "") }

uninstaller = ->(string) do
  `gem uninstall #{string.split(" ").map(&de_parenthesize).join(" -v ")}`
end

splitter = ->(string) do
  temp = string.split(" ").map(&de_parenthesize)
  gem_name = temp.shift
  temp.map {|x| "#{gem_name} (#{x})"}
end

# Remove #lazy and #to_a if on Ruby version < 2.0
gems_to_be_kept    = dot_lockfiles.lazy.map(&lockfile_parser).map(&lockfile_specs).to_a.uniq
all_installed_gems = `gem list`.split("\n").map(&splitter).flatten
gems_to_be_uninstalled = all_installed_gems - gems_to_be_kept
gems_to_be_uninstalled.map(&uninstaller)

为什么我要这样写这个片段?前几天我碰巧看到这个:http://www.confreaks.com/videos/2382-rmw2013-functional-principles-for-oo-development

关于ruby - 卸载不在Gemfile.lock文件的指定列表中的所有gem,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16476266/

10-12 00:28
查看更多