This repository has been archived by the owner on Apr 14, 2021. It is now read-only.
/
repository.rb
252 lines (210 loc) · 7.35 KB
/
repository.rb
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
module Bundler
class InvalidRepository < StandardError ; end
class Repository
attr_reader :path
def initialize(path, bindir)
FileUtils.mkdir_p(path)
@path = Pathname.new(path)
@bindir = Pathname.new(bindir)
@cache = GemDirectorySource.new(:location => @path.join("cache"))
end
def install(dependencies, sources, options = {})
# TODO: clean this up
sources.each do |s|
s.repository = self
s.local = options[:cached]
end
source_requirements = {}
options[:no_bundle].each do |name|
source_requirements[name] = SystemGemSource.instance
end
# Check to see whether the existing cache meets all the requirements
begin
valid = Resolver.resolve(dependencies, [source_index], source_requirements)
rescue Bundler::GemNotFound
end
sources = only_local(sources) if options[:cached]
# Check the remote sources if the existing cache does not meet the requirements
# or the user passed --update
if options[:update] || !valid
Bundler.logger.info "Calculating dependencies..."
bundle = Resolver.resolve(dependencies, [@cache] + sources, source_requirements)
download(bundle, options)
do_install(bundle, options)
valid = bundle
end
generate_bins(valid, options)
cleanup(valid, options)
configure(valid, options)
end
def cache(*gemfiles)
FileUtils.mkdir_p(@path.join("cache"))
gemfiles.each do |gemfile|
Bundler.logger.info "Caching: #{File.basename(gemfile)}"
FileUtils.cp(gemfile, @path.join("cache"))
end
end
def prune(dependencies, sources)
sources.each do |s|
s.repository = self
s.local = true
end
sources = only_local(sources)
bundle = Resolver.resolve(dependencies, [@cache] + sources)
@cache.gems.each do |name, spec|
unless bundle.any? { |s| s.name == spec.name && s.version == spec.version }
Bundler.logger.info "Pruning #{spec.name} (#{spec.version}) from the cache"
FileUtils.rm @path.join("cache", "#{spec.full_name}.gem")
end
end
end
def gems
source_index.gems.values
end
def outdated_gems
source_index.outdated.sort
end
def source_index
index = Gem::SourceIndex.from_gems_in(@path.join("specifications"))
index.each { |n, spec| spec.loaded_from = @path.join("specifications", "#{spec.full_name}.gemspec") }
index
end
def download_path_for(type)
@repos[type].download_path_for
end
private
def only_local(sources)
sources.select { |s| s.can_be_local? }
end
def download(bundle, options)
bundle.sort_by {|s| s.full_name.downcase }.each do |spec|
next if options[:no_bundle].include?(spec.name)
spec.source.download(spec)
end
end
def do_install(bundle, options)
bundle.each do |spec|
next if options[:no_bundle].include?(spec.name)
spec.loaded_from = @path.join("specifications", "#{spec.full_name}.gemspec")
# Do nothing if the gem is already expanded
next if @path.join("gems", spec.full_name).directory?
case spec.source
when GemSource, GemDirectorySource, SystemGemSource
expand_gemfile(spec, options)
else
expand_vendored_gem(spec, options)
end
end
end
def generate_bins(bundle, options)
bundle.each do |spec|
next if options[:no_bundle].include?(spec.name)
# HAX -- Generate the bin
bin_dir = @bindir
path = @path
installer = Gem::Installer.allocate
installer.instance_eval do
@spec = spec
@bin_dir = bin_dir
@gem_dir = path.join("gems", "#{spec.full_name}")
@gem_home = path
@wrappers = true
@format_executable = false
@env_shebang = false
end
installer.generate_bin
end
end
def expand_gemfile(spec, options)
Bundler.logger.info "Installing #{spec.name} (#{spec.version})"
gemfile = @path.join("cache", "#{spec.full_name}.gem").to_s
if build_args = options[:build_options] && options[:build_options][spec.name]
Gem::Command.build_args = build_args.map {|k,v| "--with-#{k}=#{v}"}
end
installer = Gem::Installer.new(gemfile, options.merge(
:install_dir => @path,
:ignore_dependencies => true,
:env_shebang => true,
:wrappers => true,
:bin_dir => @bindir
))
installer.install
ensure
Gem::Command.build_args = []
end
def expand_vendored_gem(spec, options)
add_spec(spec)
FileUtils.mkdir_p(@path.join("gems"))
File.symlink(spec.location, @path.join("gems", spec.full_name))
end
def add_spec(spec)
destination = path.join('specifications')
destination.mkdir unless destination.exist?
File.open(destination.join("#{spec.full_name}.gemspec"), 'w') do |f|
f.puts spec.to_ruby
end
end
def cleanup(valid, options)
to_delete = gems
to_delete.delete_if do |spec|
valid.any? { |other| spec.name == other.name && spec.version == other.version }
end
valid_executables = valid.map { |s| s.executables }.flatten.compact
to_delete.each do |spec|
Bundler.logger.info "Deleting gem: #{spec.name} (#{spec.version})"
FileUtils.rm_rf(@path.join("specifications", "#{spec.full_name}.gemspec"))
FileUtils.rm_rf(@path.join("gems", spec.full_name))
# Cleanup the bin directory
spec.executables.each do |bin|
next if valid_executables.include?(bin)
Bundler.logger.info "Deleting bin file: #{bin}"
FileUtils.rm_rf(@bindir.join(bin))
end
end
end
def expand(options)
each_repo do |repo|
repo.expand(options)
end
end
def configure(specs, options)
generate_environment(specs, options)
end
def generate_environment(specs, options)
FileUtils.mkdir_p(path)
load_paths = load_paths_for_specs(specs, options)
bindir = @bindir.relative_path_from(path).to_s
filename = options[:manifest].relative_path_from(path).to_s
File.open(path.join("environment.rb"), "w") do |file|
template = File.read(File.join(File.dirname(__FILE__), "templates", "environment.erb"))
erb = ERB.new(template, nil, '-')
file.puts erb.result(binding)
end
end
def load_paths_for_specs(specs, options)
load_paths = []
specs.each do |spec|
next if options[:no_bundle].include?(spec.name)
gem_path = Pathname.new(spec.full_gem_path)
load_paths << load_path_for(gem_path, spec.bindir) if spec.bindir
spec.require_paths.each do |path|
load_paths << load_path_for(gem_path, path)
end
end
load_paths
end
def load_path_for(gem_path, path)
gem_path.join(path).relative_path_from(@path).to_s
end
def spec_file_for(spec)
spec.loaded_from.relative_path_from(@path).to_s
end
def require_code(file, dep)
constraint = case
when dep.only then %{ if #{dep.only.inspect}.include?(env)}
when dep.except then %{ unless #{dep.except.inspect}.include?(env)}
end
"require #{file.inspect}#{constraint}"
end
end
end