Module: DepthCharge

Defined in:
lib/depth_charge/version.rb,
lib/depth_charge.rb

Overview

:nodoc:

Defined Under Namespace

Modules: VERSION

Constant Summary collapse

USER_HOME =
File.expand_path('~')

Class Method Summary collapse

Class Method Details

.display(hash, header, show_files = true) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/depth_charge.rb', line 74

def display(hash, header, show_files = true)
  unless hash.empty?
    puts "\n#{header}\n#{'='*header.length}"
    hash.keys.sort.each do |k|
      puts "#{k}"
      puts "  #{hash[k].join("\n  ")}\n" if show_files
    end
  end
end

.find_requirements(root) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/depth_charge.rb', line 36

def find_requirements(root)
  root ||= File.dirname(__FILE__) + "/.."
  requirements = {}

  Dir.chdir(root)
  Dir['**/*.rb'].each do |path|
    next if path =~ /^vendor\/rails/
    File.open( path ) do |f|
      f.grep( /^(require|gem)/ ) do |line|
        required = line.scan(/('|")(.+?)('|")/).flatten[1]
        unless [/File/, /\*/, /\</, /#/, /\.{2}/, /^\//].detect {|pattern| required =~ pattern}
          requirements[required] ||= []
          requirements[required] << path
        end
      end
    end
  end

  installed_gems   = local_gems
  all_gems         = remote_gems
  uninstalled_gems = all_gems - installed_gems

  installed   = {}
  uninstalled = {}
  other       = {}
  requirements.keys.sort.each do |k|
    if installed_gems.include?(k)
      installed[k]   = requirements[k] 
    elsif uninstalled_gems.include?(k)
      uninstalled[k] = requirements[k] 
    else
      other[k] = requirements[k]
    end
  end
  
  return installed, uninstalled, other
end

.gem_list(scope = 'local') ⇒ Object



7
8
9
# File 'lib/depth_charge.rb', line 7

def gem_list(scope = 'local')
  process_gem_list(`gem list --#{scope} --no-verbose`, scope)
end

.local_gemsObject



16
17
18
# File 'lib/depth_charge.rb', line 16

def local_gems
  gem_list
end

.process_gem_list(raw_gem_list, scope = 'remote') ⇒ Object



11
12
13
14
# File 'lib/depth_charge.rb', line 11

def process_gem_list(raw_gem_list, scope = 'remote')
  gems = raw_gem_list.split(/\n/).select {|g| g =~ / \(/}
  gems.map! {|g| g.sub(/\s+.+$/, '')}
end

.remote_gemsObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/depth_charge.rb', line 20

def remote_gems
  gems = []
  if File.exists?(File.join(USER_HOME, '.remote_gemlist'))
    File.open(File.join(USER_HOME, '.remote_gemlist'), 'r') do |file|
      gems = file.readlines.map {|l| l.chomp}
    end
  else
    puts "Creating remote gem list cache (this may take awhile)"
    gems = gem_list('remote')
    File.open(File.join(USER_HOME, '.remote_gemlist'), 'wb+') do |file|
      file.puts gems.join("\n")
    end
  end
  gems
end

.run(path) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/depth_charge.rb', line 84

def run(path)
  installed, uninstalled, other = find_requirements(path)

  display(installed, 'INSTALLED GEMS', false)
  display(uninstalled, 'UNINSTALLED GEMS')
  display(other, 'OTHER REQUIREMENTS')
end