Class: Bundler::Runtime

Inherits:
Environment show all
Includes:
SharedHelpers
Defined in:
lib/bundler/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 Attribute Summary

Attributes included from SharedHelpers

#gem_loaded

Attributes inherited from Environment

#root

Instance Method Summary collapse

Methods included from SharedHelpers

#default_gemfile, #default_lockfile, #in_bundle?

Methods inherited from Environment

#current_dependencies, #dependencies, #index, #initialize, #inspect, #lock, #requested_specs, #specs, #update

Constructor Details

This class inherits a constructor from Bundler::Environment

Instance Method Details

#cacheObject



83
84
85
86
87
88
89
90
91
92
# File 'lib/bundler/runtime.rb', line 83

def cache
  FileUtils.mkdir_p(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

#dependencies_for(*groups) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/bundler/runtime.rb', line 73

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

#prune_cacheObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/bundler/runtime.rb', line 94

def prune_cache
  FileUtils.mkdir_p(cache_path)

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

  cached = cached.delete_if do |path|
    spec = Gem::Format.from_file_by_path(path).spec

    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



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

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
        Kernel.require file
      end
    rescue LoadError => e
      REGEXPS.find { |r| r =~ e.message }
      raise if dep.autorequire || $1 != required_file
    end
  end
end

#setup(*groups) ⇒ Object



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

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

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

  setup_environment
  cripple_rubygems(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 = Gem.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
      e.version_requirement = Gem::Requirement.new(spec.version.to_s)
      raise e
    end

    Gem.loaded_specs[spec.name] = spec
    load_paths = spec.load_paths.reject {|path| $LOAD_PATH.include?(path)}
    $LOAD_PATH.unshift(*load_paths)
  end

  lock

  self
end