Class: Stibium::Bundled::Bundle

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

Overview

Describe a bundle.

Defined Under Namespace

Classes: Config, Directory

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, env: ENV.to_h, ruby_config: {}) ⇒ Bundle

Returns a new instance of Bundle.

Parameters:

  • path (String, Pathname)
  • env (Hash{String => String}) (defaults to: ENV.to_h)
  • ruby_config (Hash{Symbol => Object}) (defaults to: {})

Raises:

  • (Errno::ENOENT)
  • (ArgumentError)

    when given path is not a directory.



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/stibium/bundled/bundle.rb', line 36

def initialize(path, env: ENV.to_h, ruby_config: {})
  self.tap do
    (@path = Pathname.new(path).realpath.freeze).tap do |base_path|
      raise ArgumentError, 'path is not a directory' unless base_path.directory?
    end

    (@config = Config.new(self.path, env: env).freeze).tap do |config|
      Pathname.new(config.fetch('BUNDLE_PATH')).expand_path.tap do |directory_path|
        @directory = Directory.new(directory_path, ruby_config: ruby_config)
      end
    end
  end.freeze
end

Instance Attribute Details

#configConfig (readonly)

Returns:



25
26
27
# File 'lib/stibium/bundled/bundle.rb', line 25

def config
  @config
end

#directoryDirectory (readonly)

Returns:



28
29
30
# File 'lib/stibium/bundled/bundle.rb', line 28

def directory
  @directory
end

#pathPathname (readonly)

Returns:

  • (Pathname)


22
23
24
# File 'lib/stibium/bundled/bundle.rb', line 22

def path
  @path
end

Instance Method Details

#bundled?Boolean

Denote bundle seems installed by bundler.

Returns:

  • (Boolean)


83
84
85
# File 'lib/stibium/bundled/bundle.rb', line 83

def bundled?
  locked? or standalone?
end

#bundler_setupPathname? (protected)

Standalone setup file.

bundle install --standalone[=<list>] makes a bundle that can work without depending on Rubygems or Bundler at runtime. A space separated list of groups to install has to be specified. Bundler creates a directory named bundle and installs the bundle there. It also generates a bundle/bundler/setup.rb file to replace Bundler's own setup in the manner required.



170
171
172
173
174
175
176
# File 'lib/stibium/bundled/bundle.rb', line 170

def bundler_setup
  Pathname.new(config.fetch('BUNDLE_PATH')).yield_self do |bundle_path|
    (bundle_path.absolute? ? bundle_path : path.join(bundle_path)).join('bundler/setup.rb').yield_self do |file|
      (file.file? and file.readable?) ? file : nil # rubocop:disable Style/TernaryParentheses
    end
  end
end

#gemfilePathname?

Get path to gemfile.

Returns:

  • (Pathname, nil)

See Also:



92
93
94
# File 'lib/stibium/bundled/bundle.rb', line 92

def gemfile
  gemfiles&.fetch(0, nil)
end

#gemfilesArray<Pathname>?

Note:

Files are returned in pairs, gemfile and its lockfile. As a result a missing file provides empty result.

Get gemfile files (gemfile + lockfile) or nothing.

Returns:

  • (Array<Pathname>, nil)


101
102
103
104
105
# File 'lib/stibium/bundled/bundle.rb', line 101

def gemfiles
  [%w[gems.rb gems.locked], %w[Gemfile Gemfile.lock]].map do |m|
    m.map { |fname| path.join(fname) }.keep_if(&:file?)
  end.reject(&:empty?).reject { |r| r.size < 2 }.first
end

#installed?Boolean

Denote install seems to be happened (since specifications are present).

Returns:

  • (Boolean)


76
77
78
# File 'lib/stibium/bundled/bundle.rb', line 76

def installed?
  !directory.specifications.empty?
end

#locked?Boolean

Denote lockfile (gems.locked or Gemfile.lock) is present.

Returns:

  • (Boolean)

See Also:



60
61
62
# File 'lib/stibium/bundled/bundle.rb', line 60

def locked?
  !!gemfiles&.fetch(1, nil)
end

#setup(guards: [:locked, :installed]) ⇒ self (protected)

Load standalone setup if present, else fallback to bundler/setup.

Load Bundler's setup (bundler/setup) when all guards are true, as a result, default behavior, is to load bundler/setup only when locked and installed.



149
150
151
152
153
154
155
# File 'lib/stibium/bundled/bundle.rb', line 149

def setup(guards: [:locked, :installed])
  self.standalone! do
    guards.map { |s| self.public_send('%s?' % s.to_s.gsub(/\?$/, '')) }.tap do |results|
      require 'bundler/setup' if results.uniq == [true]
    end
  end.yield_self { self }
end

#specificationsArray<Gem::Specification>

Get specifications.

Returns:

  • (Array<Gem::Specification>)

See Also:



69
70
71
# File 'lib/stibium/bundled/bundle.rb', line 69

def specifications
  directory.specifications.map { |file| instance_eval(file.read, file.to_path) }
end

#standalone!(&fallback) ⇒ self (protected)

Load standalone setup if present.

Returns:

  • (self)

Raises:

  • (Errno::ENOENT)


123
124
125
126
127
128
129
130
# File 'lib/stibium/bundled/bundle.rb', line 123

def standalone!(&fallback)
  self.tap do
    # noinspection RubyResolve
    bundler_setup.tap { |fp| require(fp.realpath) unless fp.nil? }
  rescue Errno::ENOENT => e
    fallback ? fallback.call(self) : raise(e)
  end
end

#standalone?Boolean

Denote bundle seems to be installed as a standalone.

Returns:

  • (Boolean)

See Also:



112
113
114
# File 'lib/stibium/bundled/bundle.rb', line 112

def standalone?
  !!bundler_setup
end

#to_pathString

Returns:

  • (String)


51
52
53
# File 'lib/stibium/bundled/bundle.rb', line 51

def to_path
  path.to_path
end