Class: Gem::Commands::UnusedCommand

Inherits:
Gem::Command
  • Object
show all
Defined in:
lib/rubygems/commands/unused_command.rb

Instance Method Summary collapse

Constructor Details

#initializeUnusedCommand

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, options|
    options[: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, options|
    options[: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, options|
    options[:excluded_gems] = value
  end

  add_option("-q", "--quiet",
             "Make this script perform its work quietly"
  ) do |value, options|
    options[:quiet] = true
  end
end

Instance Method Details

#argumentsObject



18
19
20
# File 'lib/rubygems/commands/unused_command.rb', line 18

def arguments

end

#descriptionObject



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

#executeObject



55
56
57
58
59
60
61
62
63
64
# File 'lib/rubygems/commands/unused_command.rb', line 55

def execute
  if options[:branches].nil?
    log "no branch names provided. using master, staging, and production as default."
    options[:branches] = %w(master staging production)
  else
    options[:branches] = options[:branches].split
  end
  find_unused_gems
  remove_unused_gems if options[:remove]
end

#find_unused_gemsObject



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_optionObject



106
107
108
109
110
111
112
113
114
# File 'lib/rubygems/commands/unused_command.rb', line 106

def handle_excluded_gems_option
  if options[:excluded_gems].nil?
    options[:excluded_gems] = []
    log "Not excluding any gems."
  else
    options[:excluded_gems] = options[:excluded_gems].split
    log "Excluding the following gems: #{options[:excluded_gems]}"
  end
end

#log(message) ⇒ Object



137
138
139
# File 'lib/rubygems/commands/unused_command.rb', line 137

def log message
  puts message unless options[:quiet]
end

#remove_unused_gemsObject



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)
  uninstall_options = {
    :ignore => true,
    :executables => true
  }
  @unneeded.each do |bad_gem|
    uninstall_options[:install_dir] = bad_gem.base_dir
    uninstall_options[:user_install] = true
    uninstall_options[:version] = bad_gem.version
    uninstall_options[:prerelease] = true if bad_gem.version.prerelease?
    uninstaller = Gem::Uninstaller.new(bad_gem, uninstall_options)
    uninstaller.instance_variable_set(:@user_install, true)
    uninstaller.uninstall_gem(bad_gem)
  end
end

#retrieve_installedObject



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/) && !options[:excluded_gems].include?(installed_spec.name)
  end.uniq { |spec| "#{spec.name} #{spec.version}" }
end

#retrieve_requirementsObject



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 = []
  options[: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

#usageObject



22
23
24
# File 'lib/rubygems/commands/unused_command.rb', line 22

def usage
  "#{program_name}"
end