Sha256: d5251eaf3eba36e3b3389ef7f2bbc7ead2b163033dc949437b921c2cd0cc8d6e

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

require 'set'

class Gem::Commands::OrphanedCommand < Gem::Command

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

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

  def execute
    read_gems
    read_preferred_gems
    check_preferred_gems
    show_orphaned_gems
  end

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

  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

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

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

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

  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

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gem-orphaned-0.4 lib/gem-orphaned/orphaned_command.rb