Class: Bundler::Runtime

Inherits:
Environment
  • Object
show all
Includes:
SharedHelpers
Defined in:
lib/bundler-1.0/runtime.rb,
lib/bundler-1.2/runtime.rb

Constant Summary collapse

REGEXPS =
[
  /^no such file to load -- (.+)$/i,
  /^Missing \w+ (?:file\s*)?([^\s]+.rb)$/i,
  /^Missing API definition file in (.+)$/i,
  /^cannot load such file -- (.+)$/i,
]

Instance Method Summary collapse

Instance Method Details

#cacheObject



94
95
96
97
98
99
100
101
102
103
# File 'lib/bundler-1.0/runtime.rb', line 94

def cache
  FileUtils.mkdir_p(cache_path) unless File.exists?(cache_path)

  Bundler.ui.info "Updating .gem files in vendor/cache"
  specs.each do |spec|
    next if spec.name == 'bundler'
    spec.source.cache(spec) if spec.source.respond_to?(:cache)
  end
  prune_cache unless Bundler.settings[:no_prune]
end

#cleanObject



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
# File 'lib/bundler-1.2/runtime.rb', line 124

def clean
  gem_bins             = Dir["#{Gem.dir}/bin/*"]
  git_dirs             = Dir["#{Gem.dir}/bundler/gems/*"]
  git_cache_dirs       = Dir["#{Gem.dir}/cache/bundler/git/*"]
  gem_dirs             = Dir["#{Gem.dir}/gems/*"]
  gem_files            = Dir["#{Gem.dir}/cache/*.gem"]
  gemspec_files        = Dir["#{Gem.dir}/specifications/*.gemspec"]
  spec_gem_paths       = []
  # need to keep git sources around
  spec_git_paths       = @definition.sources.select {|s| s.is_a?(Bundler::Source::Git) }.map {|s| s.path.to_s }
  spec_git_cache_dirs  = []
  spec_gem_executables = []
  spec_cache_paths     = []
  spec_gemspec_paths   = []
  specs.each do |spec|
    spec_gem_paths << spec.full_gem_path
    # need to check here in case gems are nested like for the rails git repo
    md = %r{(.+bundler/gems/.+-[a-f0-9]{7,12})}.match(spec.full_gem_path)
    spec_git_paths << md[1] if md
    spec_gem_executables << spec.executables.collect do |executable|
      "#{Bundler.rubygems.gem_bindir}/#{executable}"
    end
    spec_cache_paths << spec.cache_file
    spec_gemspec_paths << spec.spec_file
    spec_git_cache_dirs << spec.source.cache_path.to_s if spec.source.is_a?(Bundler::Source::Git)
  end
  spec_gem_paths.uniq!
  spec_gem_executables.flatten!

  stale_gem_bins       = gem_bins - spec_gem_executables
  stale_git_dirs       = git_dirs - spec_git_paths
  stale_git_cache_dirs = git_cache_dirs - spec_git_cache_dirs
  stale_gem_dirs       = gem_dirs - spec_gem_paths
  stale_gem_files      = gem_files - spec_cache_paths
  stale_gemspec_files  = gemspec_files - spec_gemspec_paths

  stale_gem_bins.each {|bin| FileUtils.rm(bin) }
  output = stale_gem_dirs.collect do |gem_dir|
    full_name = Pathname.new(gem_dir).basename.to_s

    FileUtils.rm_rf(gem_dir)

    parts   = full_name.split('-')
    name    = parts[0..-2].join('-')
    version = parts.last
    output  = "#{name} (#{version})"

    Bundler.ui.info "Removing #{output}"

    output
  end + stale_git_dirs.collect do |gem_dir|
    full_name = Pathname.new(gem_dir).basename.to_s

    FileUtils.rm_rf(gem_dir)

    parts    = full_name.split('-')
    name     = parts[0..-2].join('-')
    revision = parts[-1]
    output   = "#{name} (#{revision})"

    Bundler.ui.info "Removing #{output}"

    output
  end

  stale_gem_files.each {|file| FileUtils.rm(file) if File.exists?(file) }
  stale_gemspec_files.each {|file| FileUtils.rm(file) if File.exists?(file) }
  stale_git_cache_dirs.each {|dir| FileUtils.rm_rf(dir) if File.exists?(dir) }

  output
end

#dependencies_for(*groups) ⇒ Object

!!!



84
85
86
87
88
89
90
# File 'lib/bundler-1.0/runtime.rb', line 84

def dependencies_for(*groups)
  if groups.empty?
    dependencies
  else
    dependencies.select { |d| (groups & d.groups).any? }
  end
end

#prune_cacheObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/bundler-1.0/runtime.rb', line 105

def prune_cache
  FileUtils.mkdir_p(cache_path) unless File.exists?(cache_path)

  resolve = @definition.resolve
  cached  = Dir["#{cache_path}/*.gem"]

  cached = cached.delete_if do |path|
    spec = Bundler.rubygems.spec_from_gem path

    resolve.any? do |s|
      s.name == spec.name && s.version == spec.version && !s.source.is_a?(Bundler::Source::Git)
    end
  end

  if cached.any?
    Bundler.ui.info "Removing outdated .gem files from vendor/cache"

    cached.each do |path|
      Bundler.ui.info "  * #{File.basename(path)}"
      File.delete(path)
    end
  end
end

#require(*groups) ⇒ Object

!!! HERE BE THE PATCH



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
# File 'lib/bundler-1.0/runtime.rb', line 54

def require(*groups)
  groups.map! { |g| g.to_sym }
  groups = [:default] if groups.empty?

  @definition.dependencies.each do |dep|
    # Skip the dependency if it is not in any of the requested
    # groups
    next unless ((dep.groups & groups).any? && dep.current_platform?)

    required_file = nil

    begin
      # Loop through all the specified autorequires for the
      # dependency. If there are none, use the dependency's name
      # as the autorequire.
      Array(dep.autorequire || dep.name).each do |file|
        required_file = file
        total = Benchmark.realtime {
          Kernel.require file
        }
        puts "Bundler.require #{file}\t#{total}"
      end
    rescue LoadError => e
      REGEXPS.find { |r| r =~ e.message }
      raise if dep.autorequire || $1 != required_file
    end
  end
end

#setup(*groups) ⇒ Object



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
# File 'lib/bundler-1.0/runtime.rb', line 9

def setup(*groups)
  # Has to happen first
  clean_load_path

  specs = groups.any? ? @definition.specs_for(groups) : requested_specs

  setup_environment
  Bundler.rubygems.replace_entrypoints(specs)

  # Activate the specs
  specs.each do |spec|
    unless spec.loaded_from
      raise GemNotFound, "#{spec.full_name} is missing. Run `bundle` to get it."
    end

    if activated_spec = Bundler.rubygems.loaded_specs(spec.name) and activated_spec.version != spec.version
      e = Gem::LoadError.new "You have already activated #{activated_spec.name} #{activated_spec.version}, " \
                             "but your Gemfile requires #{spec.name} #{spec.version}. Consider using bundle exec."
      e.name = spec.name
      if e.respond_to?(:requirement=)
        e.requirement = Gem::Requirement.new(spec.version.to_s)
      else
        e.version_requirement = Gem::Requirement.new(spec.version.to_s)
      end
      raise e
    end

    Bundler.rubygems.mark_loaded(spec)
    load_paths = spec.load_paths.reject {|path| $LOAD_PATH.include?(path)}
    $LOAD_PATH.unshift(*load_paths)
  end

  lock

  self
end

#setup_environmentObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/bundler-1.0/runtime.rb', line 135

def setup_environment
  begin
    ENV["BUNDLE_BIN_PATH"] = Bundler.rubygems.bin_path("bundler", "bundle", VERSION)
  rescue Gem::GemNotFoundException
    ENV["BUNDLE_BIN_PATH"] = File.expand_path("../../../bin/bundle", __FILE__)
  end

  # Set PATH
  paths = (ENV["PATH"] || "").split(File::PATH_SEPARATOR)
  paths.unshift "#{Bundler.bundle_path}/bin"
  ENV["PATH"] = paths.uniq.join(File::PATH_SEPARATOR)

  # Set BUNDLE_GEMFILE
  ENV["BUNDLE_GEMFILE"] = default_gemfile.to_s

  # Set RUBYOPT
  rubyopt = [ENV["RUBYOPT"]].compact
  if rubyopt.empty? || rubyopt.first !~ /-rbundler\/setup/
    rubyopt.unshift "-rbundler/setup"
    rubyopt.unshift "-I#{File.expand_path('../..', __FILE__)}"
    ENV["RUBYOPT"] = rubyopt.join(' ')
  end
end