Class: Bundler::Settings

Inherits:
Object
  • Object
show all
Defined in:
lib/bundler/mirror.rb,
lib/bundler/settings.rb

Defined Under Namespace

Classes: Mirror, MirrorConfig, Mirrors, TCPSocketProbe

Constant Summary

BOOL_KEYS =
%w(
  allow_offline_install
  auto_install
  cache_all
  cache_all_platforms
  disable_checksum_validation
  disable_exec_load
  disable_local_branch_check
  disable_shared_gems
  disable_version_check
  force_ruby_platform
  frozen
  gem.coc
  gem.mit
  ignore_messages
  major_deprecations
  no_install
  no_prune
  only_update_to_newer_versions
  plugins
  silence_root_warning
).freeze
NUMBER_KEYS =
%w(
  redirect
  retry
  ssl_verify_mode
  timeout
).freeze
DEFAULT_CONFIG =
{
  :redirect => 5,
  :retry => 3,
  :timeout => 10,
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root = nil) ⇒ Settings



47
48
49
50
51
52
53
# File 'lib/bundler/settings.rb', line 47

def initialize(root = nil)
  @root            = root
  @local_config    = load_config(local_config_file)
  @global_config   = load_config(global_config_file)
  @cli_flags_given = false
  @temporary       = {}
end

Instance Attribute Details

#cli_flags_givenObject

Returns the value of attribute cli_flags_given



45
46
47
# File 'lib/bundler/settings.rb', line 45

def cli_flags_given
  @cli_flags_given
end

Instance Method Details

#[](name) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/bundler/settings.rb', line 55

def [](name)
  key = key_for(name)
  value = @temporary.fetch(name) do
          @local_config.fetch(key) do
          ENV.fetch(key) do
          @global_config.fetch(key) do
          DEFAULT_CONFIG.fetch(name) do
            nil
          end end end end end

  converted_value(value, name)
end

#[]=(key, value) ⇒ Object Also known as: set_local



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/bundler/settings.rb', line 68

def []=(key, value)
  if cli_flags_given
    command = if value.nil?
      "bundle config --delete #{key}"
    else
      "bundle config #{key} #{Array(value).join(":")}"
    end

    Bundler::SharedHelpers.major_deprecation \
      "flags passed to commands " \
      "will no longer be automatically remembered. Instead please set flags " \
      "you want remembered between commands using `bundle config " \
      "<setting name> <setting value>`, i.e. `#{command}`"
  end
  local_config_file || raise(GemfileNotFound, "Could not locate Gemfile")
  set_key(key, value, @local_config, local_config_file)
end

#allObject



106
107
108
109
110
111
112
113
114
# File 'lib/bundler/settings.rb', line 106

def all
  env_keys = ENV.keys.select {|k| k =~ /BUNDLE_.*/ }

  keys = @global_config.keys | @local_config.keys | env_keys

  keys.map do |key|
    key.sub(/^BUNDLE_/, "").gsub(/__/, ".").downcase
  end
end

#allow_sudo?Boolean



199
200
201
# File 'lib/bundler/settings.rb', line 199

def allow_sudo?
  !@local_config.key?(key_for(:path))
end

#app_cache_pathObject



207
208
209
210
211
212
213
# File 'lib/bundler/settings.rb', line 207

def app_cache_path
  @app_cache_path ||= begin
    path = self[:cache_path] || "vendor/cache"
    raise InvalidOption, "Cache path must be relative to the bundle path" if path.start_with?("/")
    path
  end
end

#credentials_for(uri) ⇒ Object



129
130
131
# File 'lib/bundler/settings.rb', line 129

def credentials_for(uri)
  self[uri.to_s] || self[uri.host]
end

#delete(key) ⇒ Object



98
99
100
# File 'lib/bundler/settings.rb', line 98

def delete(key)
  @local_config.delete(key_for(key))
end

#gem_mirrorsObject



133
134
135
136
137
138
# File 'lib/bundler/settings.rb', line 133

def gem_mirrors
  all.inject(Mirrors.new) do |mirrors, k|
    mirrors.parse(k, self[k]) if k =~ /^mirror\./
    mirrors
  end
end

#ignore_config?Boolean



203
204
205
# File 'lib/bundler/settings.rb', line 203

def ignore_config?
  ENV["BUNDLE_IGNORE_CONFIG"]
end

#local_overridesObject



116
117
118
119
120
121
122
# File 'lib/bundler/settings.rb', line 116

def local_overrides
  repos = {}
  all.each do |k|
    repos[$'] = self[k] if k =~ /^local\./
  end
  repos
end

#locations(key) ⇒ Object



140
141
142
143
144
145
146
147
148
# File 'lib/bundler/settings.rb', line 140

def locations(key)
  key = key_for(key)
  locations = {}
  locations[:local]  = @local_config[key] if @local_config.key?(key)
  locations[:env]    = ENV[key] if ENV[key]
  locations[:global] = @global_config[key] if @global_config.key?(key)
  locations[:default] = DEFAULT_CONFIG[key] if DEFAULT_CONFIG.key?(key)
  locations
end

#mirror_for(uri) ⇒ Object



124
125
126
127
# File 'lib/bundler/settings.rb', line 124

def mirror_for(uri)
  uri = URI(uri.to_s) unless uri.is_a?(URI)
  gem_mirrors.for(uri.to_s).uri
end

#pathObject

@local_config should be prioritized over ENV



187
188
189
190
191
192
193
194
195
196
197
# File 'lib/bundler/settings.rb', line 187

def path
  key  = key_for(:path)
  path = ENV[key] || @global_config[key]
  return path if path && !@local_config.key?(key)

  if path = self[:path]
    "#{path}/#{Bundler.ruby_scope}"
  else
    Bundler.rubygems.gem_dir
  end
end

#pretty_values_for(exposed_key) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/bundler/settings.rb', line 150

def pretty_values_for(exposed_key)
  key = key_for(exposed_key)

  locations = []
  if @local_config.key?(key)
    locations << "Set for your local app (#{local_config_file}): #{converted_value(@local_config[key], exposed_key).inspect}"
  end

  if value = ENV[key]
    locations << "Set via #{key}: #{converted_value(value, exposed_key).inspect}"
  end

  if @global_config.key?(key)
    locations << "Set for the current user (#{global_config_file}): #{converted_value(@global_config[key], exposed_key).inspect}"
  end

  return ["You have not configured a value for `#{exposed_key}`"] if locations.empty?
  locations
end

#set_global(key, value) ⇒ Object



102
103
104
# File 'lib/bundler/settings.rb', line 102

def set_global(key, value)
  set_key(key, value, @global_config, global_config_file)
end

#temporary(update) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/bundler/settings.rb', line 87

def temporary(update)
  existing = Hash[update.map {|k, _| [k, @temporary[k]] }]
  @temporary.update(update)
  return unless block_given?
  begin
    yield
  ensure
    existing.each {|k, v| v.nil? ? @temporary.delete(k) : @temporary[k] = v }
  end
end

#withObject



182
183
184
# File 'lib/bundler/settings.rb', line 182

def with
  get_array(:with)
end

#with=(array) ⇒ Object



174
175
176
# File 'lib/bundler/settings.rb', line 174

def with=(array)
  set_array(:with, array)
end

#withoutObject



178
179
180
# File 'lib/bundler/settings.rb', line 178

def without
  get_array(:without)
end

#without=(array) ⇒ Object



170
171
172
# File 'lib/bundler/settings.rb', line 170

def without=(array)
  set_array(:without, array)
end