ruby - Uninstall all gems which are not in a specified list of Gemfile.lock files -
i'd clean little system-house. essentially,
(gem.all_system_gems - bundler.referenced_gems(array_of_gemfile_locks)).each |gem, ver| `gem uninstall #{gem} -v #{ver} end
any such rubygems/bundler methods? or known/efficient way of accomplishing same?
thanks, ben
caution: severe brain-damage possible.
i put version here explaining each function.
# gem_cleaner.rb require 'bundler' `touch gemfile` unless file.exists?("gemfile") dot_lockfiles = [ "/path/to/gemfile1.lock", "/path/to/gemfile2.lock" # ..and on... ] lockfile_parser = ->(path) bundler::lockfileparser.new(file.read(path)) end lockfile_specs = ->(lockfile) { lockfile.specs.map(&:to_s) } de_parenthesize = ->(string) { string.gsub(/\,|\(|\)/, "") } uninstaller = ->(string) `gem uninstall #{string.split(" ").map(&de_parenthesize).join(" -v ")}` end splitter = ->(string) temp = string.split(" ").map(&de_parenthesize) gem_name = temp.shift temp.map {|x| "#{gem_name} (#{x})"} end # remove #lazy , #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)
why did write snippet way? happened see other day: http://www.confreaks.com/videos/2382-rmw2013-functional-principles-for-oo-development
Comments
Post a Comment