Class: Gem::Commands::CacheCommand

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

Instance Method Summary collapse

Constructor Details

#initializeCacheCommand

Returns a new instance of CacheCommand.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/rubygems/commands/cache_command.rb', line 5

def initialize
  super 'cache', 'Cache your current path for fast loading'

  add_option('-p', '--path PATH', 'Path to use instead of the default, separated by `:\'') do |value,options|
    $:.replace value.split(':')
  end

  add_option('-d', '--with-dir DIR', 'Directory that should be included in the cache') do |dir,options|
    $:.unshift dir
  end
  
  add_option('-o', '--output-file FILE', 'The file in which the cache is placed') do |f,options|
    options[:output] = f
  end
end

Instance Method Details

#descriptionObject



21
22
23
# File 'lib/rubygems/commands/cache_command.rb', line 21

def description
  'A RubyGems plugin that takes an image of your gem structure and caches it to allow for a faster require time.'
end

#executeObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rubygems/commands/cache_command.rb', line 29

def execute
  options[:output] ||= File.expand_path("~/.gem/cache")
  puts "Caching gem structure"
  tree = Trie.new; names = []; i = 0
  $:.each do |dir|
    (Dir["#{dir}/*.rb"] + Dir["#{dir}/lib/**/*.rb"]).each do |file|
      names << file
      tree.add file[dir.size+1..-4], i
      i += 1 
    end
  end

  File.open(options[:output]+".list", File::CREAT|File::TRUNC|File::RDWR) do |f|
    f.write Marshal.dump names
  end

  tree.save(options[:output])
end

#usageObject



25
26
27
# File 'lib/rubygems/commands/cache_command.rb', line 25

def usage
  "#{program_name} [-p PATH] [-d DIR]"
end