Class: Stibium::Bundled::Bundle::Config

Inherits:
Hash
  • Object
show all
Defined in:
lib/stibium/bundled/bundle/config.rb

Overview

Note:

Executing bundle with the BUNDLE_IGNORE_CONFIG environment set will cause it to ignore all configuration.

Describe bundler configuration settings.

Bundler loads configuration settings in this order:

  1. Local config (.bundle/config or $BUNDLE_APP_CONFIG/config)
  2. Environment variables (ENV)
  3. Global config (~/.bundle/config)
  4. Bundler default config - PARTIAL support is implemented in Config.defaults

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(basedir, env: ENV.to_h) ⇒ Config



33
34
35
36
37
38
39
40
# File 'lib/stibium/bundled/bundle/config.rb', line 33

def initialize(basedir, env: ENV.to_h)
  super().tap do
    @basedir = Pathname.new(basedir).freeze
    @env = self.class.__send__(:env, source: env).freeze

    self.class.__send__(:call, self.resolve_file, env: env).each { |k, v| self[k.freeze] = v.freeze }
  end.freeze
end

Instance Attribute Details

#basedirPathname (readonly)



46
47
48
# File 'lib/stibium/bundled/bundle/config.rb', line 46

def basedir
  @basedir
end

#envHash{String => Object} (readonly)



43
44
45
# File 'lib/stibium/bundled/bundle/config.rb', line 43

def env
  @env
end

Class Method Details

.call(file, env: ENV.to_h) ⇒ Hash{String => Object} (protected)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Load config.



96
97
98
99
100
101
102
103
104
# File 'lib/stibium/bundled/bundle/config.rb', line 96

def call(file, env: ENV.to_h)
  # @formatter:off
  self.defaults                            # 4. Bundler default config
      .merge(self.global_config(env: env)) # 3. Global config
      .merge(self.env(source: env))        # 2. Environmental variables
      .merge(self.read(file, env: env))    # 1. Local config
      .sort.map { |k, v| [k.freeze, v.freeze] }.to_h.freeze
  # @formatter:on
end

.defaultsObject

Default config values (as seen from an empty environment).

rm -rfv ~/.bundle/config .bundle/config
env -i $(which bundle) install --standalone
cat .bundle/config


78
79
80
81
82
83
# File 'lib/stibium/bundled/bundle/config.rb', line 78

def defaults
  {
    'BUNDLE_APP_CONFIG' => '.bundle',
    'BUNDLE_PATH' => 'bundle',
  }
end

.env(source: ENV.to_h) ⇒ Hash{String => Object} (protected)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get bundler related environment variables (/^BUNDLE_.+/)



130
131
132
133
134
135
# File 'lib/stibium/bundled/bundle/config.rb', line 130

def env(source: ENV.to_h)
  source.dup.keep_if { |k, _| /^BUNDLE_.+/ =~ k }
        .transform_keys(&:freeze)
        .transform_values { |v| (v.is_a?(String) ? YAML.safe_load(v) : v).freeze }
        .sort.to_h
end

.global_config(env: ENV.to_h) ⇒ Hash{String => Object} (protected)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get global config.



113
114
115
116
117
118
119
120
121
# File 'lib/stibium/bundled/bundle/config.rb', line 113

def global_config(env: ENV.to_h)
  env['HOME'].yield_self do |home_path|
    return {} if home_path.nil?

    Pathname.new(home_path).expand_path.join('.bundle/config').yield_self do |file|
      self.read(file, env: env)
    end
  end
end

.read(file, env: ENV.to_h) ⇒ Hash{String => Object} (protected)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Read given config file.



145
146
147
148
149
150
151
152
# File 'lib/stibium/bundled/bundle/config.rb', line 145

def read(file, env: ENV.to_h)
  return {} if env(source: env).key?('BUNDLE_IGNORE_CONFIG')

  return {} unless file.file? and file.readable?

  (file.read.yield_self { |content| YAML.safe_load(content) })
    .transform_values { |v| v.is_a?(String) ? YAML.safe_load(v) : v }
end

Instance Method Details

#resolve_filePathname (protected)

Resolve path to local config (depending on BUNDLE_APP_CONFIG value).



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/stibium/bundled/bundle/config.rb', line 53

def resolve_file
  begin
    'BUNDLE_APP_CONFIG'.yield_self do |k|
      self.env.fetch(k) { self.class.defaults.fetch(k) }
    end
  end.yield_self do |s|
    Pathname.new(s)
  end.yield_self do |path|
    (path.absolute? ? path : basedir.join(path)).join('config')
  end
end