Module: Bundler::SharedHelpers

Extended by:
SharedHelpers
Included in:
Runtime, SharedHelpers
Defined in:
lib/bundler/shared_helpers.rb

Instance Method Summary collapse

Instance Method Details

#chdir(dir, &blk) ⇒ Object



53
54
55
56
57
# File 'lib/bundler/shared_helpers.rb', line 53

def chdir(dir, &blk)
  Bundler.rubygems.ext_lock.synchronize do
    Dir.chdir dir, &blk
  end
end

#const_get_safely(constant_name, namespace) ⇒ Object



125
126
127
128
129
130
# File 'lib/bundler/shared_helpers.rb', line 125

def const_get_safely(constant_name, namespace)
  const_in_namespace = namespace.constants.include?(constant_name.to_s) ||
    namespace.constants.include?(constant_name.to_sym)
  return nil unless const_in_namespace
  namespace.const_get(constant_name)
end

#default_bundle_dirObject



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/bundler/shared_helpers.rb', line 37

def default_bundle_dir
  bundle_dir = find_directory(".bundle")
  return nil unless bundle_dir

  bundle_dir = Pathname.new(bundle_dir)

  global_bundle_dir = Bundler.user_home.join(".bundle")
  return nil if bundle_dir == global_bundle_dir

  bundle_dir
end

#default_gemfileObject

Raises:



22
23
24
25
26
# File 'lib/bundler/shared_helpers.rb', line 22

def default_gemfile
  gemfile = find_gemfile
  raise GemfileNotFound, "Could not locate Gemfile" unless gemfile
  Pathname.new(gemfile).untaint
end

#default_lockfileObject



28
29
30
31
32
33
34
35
# File 'lib/bundler/shared_helpers.rb', line 28

def default_lockfile
  gemfile = default_gemfile

  case gemfile.basename.to_s
  when "gems.rb" then Pathname.new(gemfile.sub(/.rb$/, ".locked"))
  else Pathname.new("#{gemfile}.lock")
  end.untaint
end

#ensure_same_dependencies(spec, old_deps, new_deps) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/bundler/shared_helpers.rb', line 155

def ensure_same_dependencies(spec, old_deps, new_deps)
  new_deps = new_deps.reject {|d| d.type == :development }
  old_deps = old_deps.reject {|d| d.type == :development }

  without_type = proc {|d| Gem::Dependency.new(d.name, d.requirements_list.sort) }
  new_deps.map!(&without_type)
  old_deps.map!(&without_type)

  extra_deps = new_deps - old_deps
  return if extra_deps.empty?

  Bundler.ui.debug "#{spec.full_name} from #{spec.remote} has either corrupted API or lockfile dependencies" \
    " (was expecting #{old_deps.map(&:to_s)}, but the real spec has #{new_deps.map(&:to_s)})"
  raise APIResponseMismatchError,
    "Downloading #{spec.full_name} revealed dependencies not in the API or the lockfile (#{extra_deps.join(", ")})." \
    "\nEither installing with `--full-index` or running `bundle update #{spec.name}` should fix the problem."
end

#filesystem_access(path, action = :write) { ... } ⇒ Object

Rescues permissions errors raised by file system operations (ie. Errno:EACCESS, Errno::EAGAIN) and raises more friendly errors instead.

Examples:

filesystem_access("vendor/cache", :write) do
  FileUtils.mkdir_p("vendor/cache")
end

Parameters:

  • path (String)

    the path that the action will be attempted to

  • action (Symbol, #to_s) (defaults to: :write)

    the type of operation that will be performed. For example: :write, :read, :exec

Yields:

  • path

Raises:

See Also:

  • Bundler::SharedHelpers.{Bundler{Bundler::PermissionError}


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/bundler/shared_helpers.rb', line 105

def filesystem_access(path, action = :write, &block)
  # Use block.call instead of yield because of a bug in Ruby 2.2.2
  # See https://github.com/bundler/bundler/issues/5341 for details
  block.call(path.dup.untaint)
rescue Errno::EACCES
  raise PermissionError.new(path, action)
rescue Errno::EAGAIN
  raise TemporaryResourceError.new(path, action)
rescue Errno::EPROTO
  raise VirtualProtocolError.new
rescue Errno::ENOSPC
  raise NoSpaceOnDeviceError.new(path, action)
rescue *[const_get_safely(:ENOTSUP, Errno)].compact
  raise OperationNotSupportedError.new(path, action)
rescue Errno::EEXIST, Errno::ENOENT
  raise
rescue SystemCallError => e
  raise GenericSystemCallError.new(e, "There was an error accessing `#{path}`.")
end

#in_bundle?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/bundler/shared_helpers.rb', line 49

def in_bundle?
  find_gemfile
end

#major_deprecation(message) ⇒ Object



132
133
134
135
136
137
# File 'lib/bundler/shared_helpers.rb', line 132

def major_deprecation(message)
  return unless prints_major_deprecations?
  @major_deprecation_ui ||= Bundler::UI::Shell.new("no-color" => true)
  ui = Bundler.ui.is_a?(@major_deprecation_ui.class) ? Bundler.ui : @major_deprecation_ui
  ui.warn("[DEPRECATED FOR #{Bundler::VERSION.split(".").first.to_i + 1}.0] #{message}")
end


139
140
141
142
143
144
145
146
# File 'lib/bundler/shared_helpers.rb', line 139

def print_major_deprecations!
  deprecate_gemfile(find_gemfile) if find_gemfile == find_file("Gemfile")
  if RUBY_VERSION < "2"
    major_deprecation("Bundler will only support ruby >= 2.0, you are running #{RUBY_VERSION}")
  end
  return if Bundler.rubygems.provides?(">= 2")
  major_deprecation("Bundler will only support rubygems >= 2.0, you are running #{Bundler.rubygems.version}")
end

#pwdObject



59
60
61
62
63
# File 'lib/bundler/shared_helpers.rb', line 59

def pwd
  Bundler.rubygems.ext_lock.synchronize do
    Pathname.pwd
  end
end

#set_bundle_environmentObject



78
79
80
81
82
83
# File 'lib/bundler/shared_helpers.rb', line 78

def set_bundle_environment
  set_bundle_variables
  set_path
  set_rubyopt
  set_rubylib
end

#trap(signal, override = false, &block) ⇒ Object



148
149
150
151
152
153
# File 'lib/bundler/shared_helpers.rb', line 148

def trap(signal, override = false, &block)
  prior = Signal.trap(signal) do
    block.call
    prior.call unless override
  end
end

#with_clean_git_env(&block) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/bundler/shared_helpers.rb', line 65

def with_clean_git_env(&block)
  keys    = %w(GIT_DIR GIT_WORK_TREE)
  old_env = keys.inject({}) do |h, k|
    h.update(k => ENV[k])
  end

  keys.each {|key| ENV.delete(key) }

  block.call
ensure
  keys.each {|key| ENV[key] = old_env[key] }
end