Class: Gem::Commands::UnusedCommand
- Inherits:
-
Gem::Command
- Object
- Gem::Command
- Gem::Commands::UnusedCommand
- Defined in:
- lib/rubygems/commands/unused_command.rb
Instance Method Summary collapse
- #arguments ⇒ Object
- #description ⇒ Object
- #execute ⇒ Object
- #find_unused_gems ⇒ Object
- #handle_excluded_gems_option ⇒ Object
-
#initialize ⇒ UnusedCommand
constructor
A new instance of UnusedCommand.
- #log(message) ⇒ Object
- #remove_unused_gems ⇒ Object
- #retrieve_installed ⇒ Object
- #retrieve_requirements ⇒ Object
- #usage ⇒ Object
Constructor Details
#initialize ⇒ UnusedCommand
Returns a new instance of UnusedCommand.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/rubygems/commands/unused_command.rb', line 26 def initialize super "unused", description add_option("-r", "--remove", "Remove all gems found by #{program_name}") do |value, | [:remove] = true end add_option("", "--branches BRANCH", " Set branch names to check for currently used gems. Space separated, surround with a string (\"master staging production\")" ) do |value, | [:branches] = value end add_option("", "--exclude GEM", " Set gem names to exclude from check. These gems should really be installed in the global gemset. Surround with a string for multiples (\"gem_a gem_b gem_c\")" ) do |value, | [:excluded_gems] = value end add_option("-q", "--quiet", "Make this script perform its work quietly" ) do |value, | [:quiet] = true end end |
Instance Method Details
#arguments ⇒ Object
18 19 20 |
# File 'lib/rubygems/commands/unused_command.rb', line 18 def arguments end |
#description ⇒ Object
14 15 16 |
# File 'lib/rubygems/commands/unused_command.rb', line 14 def description "Show and remove unused gems and old versions of gems" end |
#execute ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/rubygems/commands/unused_command.rb', line 55 def execute if [:branches].nil? log "no branch names provided. using master, staging, and production as default." [:branches] = %w(master staging production) else [:branches] = [:branches].split end find_unused_gems remove_unused_gems if [:remove] end |
#find_unused_gems ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/rubygems/commands/unused_command.rb', line 66 def find_unused_gems retrieve_requirements retrieve_installed @unneeded = @installed.delete_if do |installed_spec| # remove any required installed gems @requirements.any? do |spec| spec.name == installed_spec.name && spec.version == installed_spec.version end end log "====REMOVE THESE GEMS====" @unneeded.each do |bad_gem| log "#{bad_gem.name} #{bad_gem.version}" end end |
#handle_excluded_gems_option ⇒ Object
106 107 108 109 110 111 112 113 114 |
# File 'lib/rubygems/commands/unused_command.rb', line 106 def handle_excluded_gems_option if [:excluded_gems].nil? [:excluded_gems] = [] log "Not excluding any gems." else [:excluded_gems] = [:excluded_gems].split log "Excluding the following gems: #{[:excluded_gems]}" end end |
#log(message) ⇒ Object
137 138 139 |
# File 'lib/rubygems/commands/unused_command.rb', line 137 def log puts unless [:quiet] end |
#remove_unused_gems ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/rubygems/commands/unused_command.rb', line 116 def remove_unused_gems log "I'm removing your unused gems....mwuahahahha!!!" # Gem::Uninstaller.new(gem_name, options).uninstall # -I (options[:ignore]): ignore depedencies # -x (options[:executables]): don't prompt for removal of app executables # -i (options[:install_dir]): install dir, directory to uninstall gem from (WIN) = { :ignore => true, :executables => true } @unneeded.each do |bad_gem| [:install_dir] = bad_gem.base_dir [:user_install] = true [:version] = bad_gem.version [:prerelease] = true if bad_gem.version.prerelease? uninstaller = Gem::Uninstaller.new(bad_gem, ) uninstaller.instance_variable_set(:@user_install, true) uninstaller.uninstall_gem(bad_gem) end end |
#retrieve_installed ⇒ Object
99 100 101 102 103 104 |
# File 'lib/rubygems/commands/unused_command.rb', line 99 def retrieve_installed handle_excluded_gems_option @installed = Gem::Specification.find_all do |installed_spec| !installed_spec.gem_dir.match(/@global/) && ![:excluded_gems].include?(installed_spec.name) end.uniq { |spec| "#{spec.name} #{spec.version}" } end |
#retrieve_requirements ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/rubygems/commands/unused_command.rb', line 84 def retrieve_requirements @requirements = [] [:branches].each do |branch| `git co #{branch}` bundle = ::Bundler::LockfileParser.new(File.read("Gemfile.lock")) @requirements.concat bundle.specs end @requirements.uniq! { |spec| "#{spec.name} #{spec.version}" } log "These are the requirements of your lockfiles:" @requirements.each do |spec| log "#{spec.name} #{spec.version}" end # TODO: somewhere we need to skip/omit those excluded gems end |
#usage ⇒ Object
22 23 24 |
# File 'lib/rubygems/commands/unused_command.rb', line 22 def usage "#{program_name}" end |