-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
102 lines (87 loc) · 2.58 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
require 'pathname'
require 'yaml'
require 'bundler/setup'
require 'stickler'
require 'rubygems/mirror'
require 'rubygems/format'
$top = Pathname.new(__FILE__).dirname.expand_path
$data = $top.join('data')
$local = $data.join('local')
$mirror = $data.join('mirror')
$mirrorrc = $data.join('mirrorrc.yml')
$upstream = 'http://production.cf.rubygems.org/'
$mirror_config = [{'from' => $upstream, 'to' => $mirror.to_s}]
class Gem::Mirror
def update_gems
gems_to_fetch.each do |g|
@pool.job do
@fetcher.fetch(from('gems', g), to('gems', g))
begin
write_specification_for_gem(g, to('gems'), to('specifications'))
rescue => e
puts e
end
yield if block_given?
end
end
@pool.run_til_done
end
def delete_gems
gems_to_delete.each do |g|
@pool.job do
begin
remove_specification_for_gem(g, to('gems'), to('specifications'))
rescue => e
puts e
end
File.delete(to('gems', g))
yield if block_given?
end
end
@pool.run_til_done
end
private
def remove_specification_for_gem(gem_file, gem_dir, spec_dir)
gem_path = File.join(gem_dir, gem_file)
spec = specification_from_gem_file(gem_path)
spec_path = File.join(spec_dir, spec.spec_name)
rm_rf spec_path
end
def write_specification_for_gem(gem_file, gem_dir, spec_dir)
gem_path = File.join(gem_dir, gem_file)
spec = specification_from_gem_file(gem_path)
spec_path = File.join(spec_dir, spec.spec_name)
File.open(spec_path, 'w+') {|file| file << spec.to_ruby}
end
def specification_from_gem_file(path)
Gem::Format.from_file_by_path(path).spec
end
end
task :create_paths do
[$local, $mirror].each do |type|
['gems', 'specifications'].each do |subdir|
mkdir_p type.join(subdir)
end
end
end
task :configure_mirror => :create_paths do
File.open($mirrorrc, 'w+') do |file|
file << $mirror_config.to_yaml
end unless $mirrorrc.exist?
end
desc 'update local mirror(s) from remote'
task :mirror => :configure_mirror do
configs = YAML.load_file($mirrorrc)
configs.each do |config|
mirror = Gem::Mirror.new(config['from'], File.expand_path(config['to']), 15)
puts "Fetching: #{mirror.from(Gem::Mirror::SPECS_FILE_Z)}"
mirror.update_specs
puts "Total gems: #{mirror.gems.size}"
num_to_fetch = mirror.gems_to_fetch.size
puts "Fetching #{num_to_fetch} gems"
mirror.update_gems #{ print '.' }
num_to_delete = mirror.gems_to_delete.size
puts "Deleting #{num_to_delete} gems"
mirror.delete_gems #{ print '.' }
end
end