Class: Gem::MiniMirror::Runner

Inherits:
Object
  • Object
show all
Includes:
Finder
Defined in:
lib/rubygems/mini_mirror/runner.rb

Constant Summary collapse

DEFAULT_GEMS_DIR =
File.join(Gem.user_home, '.gem', 'mirror')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Finder

#add_to_deps, #add_to_specs, #fetch_deps, #find_all_specs, #is_in_deps?, #is_in_specs?, #with_sources

Constructor Details

#initialize(options = {}) ⇒ Runner

Returns a new instance of Runner.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rubygems/mini_mirror/runner.rb', line 10

def initialize(options = {})
  @resource_handler = ResourceHandler.instance
  @dependencies = []
  @specs_list = Hash.new{|h,k| h[k] = Hash.new{ |h1,k1| h1[k1] = Hash.new}}
  @specs = []
  @dependencies_list = Hash.new{|h,k| h[k] = Hash.new}
  @to_fetch = {}
  @resources = []
  @resources_signatures = {}
  @pool = Gem::MiniMirror::Pool.new(options[:pool_size] || Gem::MiniMirror::POOL_SIZE)
  @gems_dir = File.expand_path(options[:gems_dir] || DEFAULT_GEMS_DIR)
  @fetcher = Gem::MiniMirror::Fetcher.new
  super
end

Instance Attribute Details

#resource_handlerObject (readonly)

Returns the value of attribute resource_handler.



7
8
9
# File 'lib/rubygems/mini_mirror/runner.rb', line 7

def resource_handler
  @resource_handler
end

#specsObject (readonly)

Returns the value of attribute specs.



7
8
9
# File 'lib/rubygems/mini_mirror/runner.rb', line 7

def specs
  @specs
end

Class Method Details

.run(file, options = {}) ⇒ Object



89
90
91
92
93
# File 'lib/rubygems/mini_mirror/runner.rb', line 89

def self.run(file, options = {})
  runner = new(options)
  runner.load_resource! :path => file, :type => 'ruby'
  runner.update
end

Instance Method Details

#delete_gemsObject



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

def delete_gems
  gems_to_delete.each do |g|
    @pool.job do
      File.delete(to('gems',g))
      yield
    end
  end
  @pool.run_til_done
end

#existing_gemsObject



43
44
45
# File 'lib/rubygems/mini_mirror/runner.rb', line 43

def existing_gems
  Dir[to('gems','*.gem')].entries.map{|f| File.basename(f)}
end

#from(*args) ⇒ Object



35
36
37
# File 'lib/rubygems/mini_mirror/runner.rb', line 35

def from(*args)
  File.join(*args)
end

#gemsObject



52
53
54
# File 'lib/rubygems/mini_mirror/runner.rb', line 52

def gems
  @gems ||= gems_with_sources.keys
end

#gems_to_deleteObject



60
61
62
# File 'lib/rubygems/mini_mirror/runner.rb', line 60

def gems_to_delete
  existing_gems - gems
end

#gems_to_fetchObject



56
57
58
# File 'lib/rubygems/mini_mirror/runner.rb', line 56

def gems_to_fetch
  gems - existing_gems
end

#gems_with_sourcesObject



47
48
49
50
# File 'lib/rubygems/mini_mirror/runner.rb', line 47

def gems_with_sources
  load_all!
  @gems_with_sources ||= Hash[*specs.map{|s,src| [s.full_name + '.gem', src] }.flatten]
end

#load_resource!(options) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/rubygems/mini_mirror/runner.rb', line 25

def load_resource!(options)
  klass = @resource_handler.find(options)
  resource = klass.new(self, options)
  unless @resources_signatures[resource.tag]
    resource.load!
    @resources << resource
    @resources_signatures[resource.tag] = true
  end
end

#to(*args) ⇒ Object



39
40
41
# File 'lib/rubygems/mini_mirror/runner.rb', line 39

def to(*args)
  File.join(@gems_dir,*args)
end

#updateObject



84
85
86
87
# File 'lib/rubygems/mini_mirror/runner.rb', line 84

def update
  update_gems
  delete_gems
end

#update_gemsObject



64
65
66
67
68
69
70
71
72
# File 'lib/rubygems/mini_mirror/runner.rb', line 64

def update_gems
  gems_to_fetch.each do |g|
    @pool.job do
      @fetcher.fetch(from(gems_with_sources[g],'gems',g),to('gems',g))
      yield
    end
  end
  @pool.run_til_done
end