Class: Gem::Commands::OrphanedCommand

Inherits:
Gem::Command
  • Object
show all
Defined in:
lib/gem-orphaned/orphaned_command.rb,
lib/gem-orphaned/version.rb

Constant Summary collapse

PreferredGemsFile =
File.expand_path('~/.preferred_gems')
DefaultPreferredGems =
%w{
  bundler
  gem-orphaned
  rubygems-update
}
VERSION =
'0.4'

Instance Method Summary collapse

Constructor Details

#initializeOrphanedCommand

Returns a new instance of OrphanedCommand.



12
13
14
# File 'lib/gem-orphaned/orphaned_command.rb', line 12

def initialize
  super 'orphaned', 'Show orphaned gems'
end

Instance Method Details

#check_preferred_gemsObject



42
43
44
45
46
47
48
# File 'lib/gem-orphaned/orphaned_command.rb', line 42

def check_preferred_gems
  @preferred_gems.each do |name|
    unless @gems[name]
      puts "#{name}: in preferred list but not installed"
    end
  end
end

#default_gemsObject



56
57
58
59
60
# File 'lib/gem-orphaned/orphaned_command.rb', line 56

def default_gems
  @gems.select do |name, specs|
    specs.find(&:default_gem?)
  end.keys
end

#executeObject



16
17
18
19
20
21
# File 'lib/gem-orphaned/orphaned_command.rb', line 16

def execute
  read_gems
  read_preferred_gems
  check_preferred_gems
  show_orphaned_gems
end

#orphaned_gemsObject



62
63
64
65
66
67
# File 'lib/gem-orphaned/orphaned_command.rb', line 62

def orphaned_gems
  names = @gems.keys - @preferred_gems
  @gems.values_at(*names).map do |specs|
    specs.select { |s| s.dependent_gems.empty? }
  end.flatten
end

#read_gemsObject



23
24
25
26
27
28
29
# File 'lib/gem-orphaned/orphaned_command.rb', line 23

def read_gems
  @gems = {}
  Gem::Specification.each do |spec|
    @gems[spec.name] ||= []
    @gems[spec.name] << spec
  end
end

#read_preferred_gemsObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/gem-orphaned/orphaned_command.rb', line 31

def read_preferred_gems
  @preferred_gems = DefaultPreferredGems
  @preferred_gems += default_gems
  if File.exist?(PreferredGemsFile)
    data = File.read(PreferredGemsFile)
    lines = data.split("\n").map { |s| s.sub(/#.*/, '').strip }.reject(&:empty?)
    @preferred_gems += lines
  end
  @preferred_gems.uniq!
end

#show_orphaned_gemsObject



50
51
52
53
54
# File 'lib/gem-orphaned/orphaned_command.rb', line 50

def show_orphaned_gems
  orphaned_gems.each do |spec|
    puts "#{spec.name} (#{spec.version}): orphaned"
  end
end