Skip to content
Newer
Older
100644 177 lines (140 sloc) 4.84 KB
d19c577 @jeremy Locked environment checks for Gemfile changes
jeremy authored
1 require "digest/sha1"
8835be6 Print an error if the Gemfile changed after the lock
Yehuda Katz authored
2
c0f8641 Persist spec groups to the lockfile
Carl Lerche authored
3 module Bundler
4 class Runtime < Environment
f7bfdde Actually disable system gems in .bundle/environment.rb
Carlhuda authored
5 include SharedHelpers
6
4d84906 Regenerate .bundle/environment.rb during bundle check
Carl Lerche authored
7 def setup(*groups)
e993e6f Persist spec groups to the lockfile
Carl Lerche authored
8 # Has to happen first
2d05242 Handle rubygems executable wrappers better
Carl Lerche authored
9 clean_load_path
10
5c8d9c8 Set things up to start working on the platform DSL
Carl Lerche authored
11 specs = groups.any? ? @definition.specs_for(groups) : requested_specs
2d05242 Handle rubygems executable wrappers better
Carl Lerche authored
12
2dabdc3 Fix a bug where `bundle exec bundle *` and shelling out to bundle fro…
Carlhuda authored
13 setup_environment
6d36909 Cripple rubygems with a smarter #gem
Carl Lerche authored
14 cripple_rubygems(specs)
e993e6f Persist spec groups to the lockfile
Carl Lerche authored
15
16 # Activate the specs
6d36909 Cripple rubygems with a smarter #gem
Carl Lerche authored
17 specs.each do |spec|
76c89b1 Unify the Installer's local index and the runtime index
Carl Lerche authored
18 unless spec.loaded_from
c48f0ff Bring back a spec describing the cache being updated when the bundle …
Carl Lerche authored
19 raise GemNotFound, "#{spec.full_name} is missing. Run `bundle` to get it."
76c89b1 Unify the Installer's local index and the runtime index
Carl Lerche authored
20 end
21
fe29acb Raise an activation error if a rubygem is already activated that conf…
Carlhuda authored
22 if activated_spec = Gem.loaded_specs[spec.name] and activated_spec.version != spec.version
23 e = Gem::LoadError.new "You have already activated #{activated_spec.name} #{activated_spec.version}, " \
24 "but your Gemfile requires #{spec.name} #{spec.version}. Consider using bundle exec."
25 e.name = spec.name
26 e.version_requirement = Gem::Requirement.new(spec.version.to_s)
27 raise e
28 end
29
e993e6f Persist spec groups to the lockfile
Carl Lerche authored
30 Gem.loaded_specs[spec.name] = spec
9ef8a2e @dweinand Preserve require_paths order from gemspec
dweinand authored
31 load_paths = spec.load_paths.reject {|path| $LOAD_PATH.include?(path)}
32 $LOAD_PATH.unshift(*load_paths)
e993e6f Persist spec groups to the lockfile
Carl Lerche authored
33 end
e9c300a Remove the call to lock from Runtime#initialize
Carlhuda authored
34
35 lock
36
e993e6f Persist spec groups to the lockfile
Carl Lerche authored
37 self
38 end
39
4549f0a Don't swallow require failures
Carlhuda authored
40 REGEXPS = [
41 /^no such file to load -- (.+)$/i,
42 /^Missing \w+ (?:file\s*)?([^\s]+.rb)$/i,
43 /^Missing API definition file in (.+)$/i,
44 /^cannot load such file -- (.+)$/i,
45 ]
46
e993e6f Persist spec groups to the lockfile
Carl Lerche authored
47 def require(*groups)
4d8a76f First pass at not raising if a require by the gem's name does not exist.
Carl Lerche authored
48 groups.map! { |g| g.to_sym }
ca6cc3b @jeremy Merge branch 'master' of git://github.com/carlhuda/bundler into carlhuda
jeremy authored
49 groups = [:default] if groups.empty?
4d8a76f First pass at not raising if a require by the gem's name does not exist.
Carl Lerche authored
50
2d3df08 Rewrite the autorequire code since we do not need to handle generated…
Carl Lerche authored
51 @definition.dependencies.each do |dep|
52 # Skip the dependency if it is not in any of the requested
53 # groups
40e170d @fnichol If the dependency's list of platforms does not contain the current pl…
fnichol authored
54 next unless ((dep.groups & groups).any? && dep.current_platform?)
2d3df08 Rewrite the autorequire code since we do not need to handle generated…
Carl Lerche authored
55
5da215b @wycats 1.9.2 fix
wycats authored
56 required_file = nil
4549f0a Don't swallow require failures
Carlhuda authored
57
2d3df08 Rewrite the autorequire code since we do not need to handle generated…
Carl Lerche authored
58 begin
59 # Loop through all the specified autorequires for the
60 # dependency. If there are none, use the dependency's name
61 # as the autorequire.
62 Array(dep.autorequire || dep.name).each do |file|
5da215b @wycats 1.9.2 fix
wycats authored
63 required_file = file
2d3df08 Rewrite the autorequire code since we do not need to handle generated…
Carl Lerche authored
64 Kernel.require file
4d8a76f First pass at not raising if a require by the gem's name does not exist.
Carl Lerche authored
65 end
4549f0a Don't swallow require failures
Carlhuda authored
66 rescue LoadError => e
67 REGEXPS.find { |r| r =~ e.message }
5da215b @wycats 1.9.2 fix
wycats authored
68 raise if dep.autorequire || $1 != required_file
c0f8641 Persist spec groups to the lockfile
Carl Lerche authored
69 end
70 end
e993e6f Persist spec groups to the lockfile
Carl Lerche authored
71 end
72
73 def dependencies_for(*groups)
74 if groups.empty?
75 dependencies
76 else
77 dependencies.select { |d| (groups & d.groups).any? }
c0f8641 Persist spec groups to the lockfile
Carl Lerche authored
78 end
e993e6f Persist spec groups to the lockfile
Carl Lerche authored
79 end
c0f8641 Persist spec groups to the lockfile
Carl Lerche authored
80
e993e6f Persist spec groups to the lockfile
Carl Lerche authored
81 alias gems specs
82
d707bc5 Rename bundle pack to bundle cache
Carl Lerche authored
83 def cache
0891a38 @spastorino Revert "Don't try to create dirs if they already exists"
spastorino authored
84 FileUtils.mkdir_p(cache_path)
e993e6f Persist spec groups to the lockfile
Carl Lerche authored
85
8f2d9b6 @indirect Update cache messages
indirect authored
86 Bundler.ui.info "Updating .gem files in vendor/cache"
e993e6f Persist spec groups to the lockfile
Carl Lerche authored
87 specs.each do |spec|
4808933 Improve bundling bundler logic
Carlhuda authored
88 next if spec.name == 'bundler'
6bc81d0 Move spec caching heaving lifing into Source
Carl Lerche authored
89 spec.source.cache(spec) if spec.source.respond_to?(:cache)
c0f8641 Persist spec groups to the lockfile
Carl Lerche authored
90 end
9a196d1 Don't run any git commands when in runtime mode (aka Bundler.setup)
Carlhuda authored
91 prune_cache unless Bundler.settings[:no_prune]
e993e6f Persist spec groups to the lockfile
Carl Lerche authored
92 end
2af6b57 @indirect Cache command now prunes stale .gem files from vendor/cache
indirect authored
93
94 def prune_cache
0891a38 @spastorino Revert "Don't try to create dirs if they already exists"
spastorino authored
95 FileUtils.mkdir_p(cache_path)
2af6b57 @indirect Cache command now prunes stale .gem files from vendor/cache
indirect authored
96
5665eef Don't delete cached gems for dependencies that are pinned to certain …
Carl Lerche authored
97 resolve = @definition.resolve
bf36714 Don't claim to be deleting gems from vendor/cache when nothing actual…
Carl Lerche authored
98 cached = Dir["#{cache_path}/*.gem"]
5665eef Don't delete cached gems for dependencies that are pinned to certain …
Carl Lerche authored
99
bf36714 Don't claim to be deleting gems from vendor/cache when nothing actual…
Carl Lerche authored
100 cached = cached.delete_if do |path|
101 spec = Gem::Format.from_file_by_path(path).spec
102
103 resolve.any? do |s|
104 s.name == spec.name && s.version == spec.version
105 end
106 end
107
108 if cached.any?
109 Bundler.ui.info "Removing outdated .gem files from vendor/cache"
110
111 cached.each do |path|
112 Bundler.ui.info " * #{File.basename(path)}"
113 File.delete(path)
d0cc02b @indirect actually fix the cache pruning code
indirect authored
114 end
2af6b57 @indirect Cache command now prunes stale .gem files from vendor/cache
indirect authored
115 end
116 end
117
7eb3b4d @hone it's clean not clear
hone authored
118 def clean
ac34b2e @hone bundle clean removes stale bins too
hone authored
119 gem_bins = Dir["#{Gem.dir}/bin/*"]
120 stale_gem_bins = gem_bins - specs.collect do |spec|
121 spec.executables.collect do |executable|
122 "#{Gem.dir}/#{spec.bindir}/#{executable}"
123 end
124 end.flatten
125
126 stale_gem_bins.each do |bin|
127 FileUtils.rm(bin)
128 end
129
1d5dd00 @hone bundle clean, remove unused standalone gems
hone authored
130 gem_dirs = Dir["#{Gem.dir}/gems/*"]
131 stale_gem_dirs = gem_dirs - specs.collect {|spec| spec.full_gem_path }
ac34b2e @hone bundle clean removes stale bins too
hone authored
132
1d5dd00 @hone bundle clean, remove unused standalone gems
hone authored
133 stale_gem_dirs.collect do |gem_dir|
134 full_name = Pathname.new(gem_dir).basename.to_s
135
136 FileUtils.rm_rf(gem_dir)
137 FileUtils.rm("#{Gem.dir}/specifications/#{full_name}.gemspec")
138
139 parts = full_name.split('-')
140 name = parts[0..-2].join('-')
141 version = parts.last
142 "#{name} (#{version})"
143 end
144 end
145
2af6b57 @indirect Cache command now prunes stale .gem files from vendor/cache
indirect authored
146 private
147
148 def cache_path
149 root.join("vendor/cache")
150 end
151
2dabdc3 Fix a bug where `bundle exec bundle *` and shelling out to bundle fro…
Carlhuda authored
152 def setup_environment
153 begin
154 ENV["BUNDLE_BIN_PATH"] = Gem.bin_path("bundler", "bundle", VERSION)
155 rescue Gem::GemNotFoundException
156 ENV["BUNDLE_BIN_PATH"] = File.expand_path("../../../bin/bundle", __FILE__)
157 end
158
159 # Set PATH
160 paths = (ENV["PATH"] || "").split(File::PATH_SEPARATOR)
161 paths.unshift "#{Bundler.bundle_path}/bin"
162 ENV["PATH"] = paths.uniq.join(File::PATH_SEPARATOR)
163
164 # Set BUNDLE_GEMFILE
165 ENV["BUNDLE_GEMFILE"] = default_gemfile.to_s
166
167 # Set RUBYOPT
168 rubyopt = [ENV["RUBYOPT"]].compact
169 if rubyopt.empty? || rubyopt.first !~ /-rbundler\/setup/
170 rubyopt.unshift "-rbundler/setup"
171 rubyopt.unshift "-I#{File.expand_path('../..', __FILE__)}"
172 ENV["RUBYOPT"] = rubyopt.join(' ')
173 end
174 end
c0f8641 Persist spec groups to the lockfile
Carl Lerche authored
175 end
ca6cc3b @jeremy Merge branch 'master' of git://github.com/carlhuda/bundler into carlhuda
jeremy authored
176 end
Something went wrong with that request. Please try again.