Class: Stibium::Bundled::Bundle
- Inherits:
-
Object
- Object
- Stibium::Bundled::Bundle
- Defined in:
- lib/stibium/bundled/bundle.rb
Overview
Describe a bundle.
Defined Under Namespace
Instance Attribute Summary collapse
- #config ⇒ Config readonly
- #directory ⇒ Directory readonly
- #path ⇒ Pathname readonly
Instance Method Summary collapse
-
#bundled? ⇒ Boolean
Denote bundle seems installed by bundler.
-
#bundler_setup ⇒ Pathname?
protected
Standalone setup file.
-
#gemfile ⇒ Pathname?
Get path to gemfile.
-
#gemfiles ⇒ Array<Pathname>?
Get gemfile files (gemfile + lockfile) or nothing.
-
#initialize(path, env: ENV.to_h, ruby_config: {}) ⇒ Bundle
constructor
A new instance of Bundle.
-
#installed? ⇒ Boolean
Denote install seems to be happened (since specifications are present).
-
#locked? ⇒ Boolean
Denote lockfile (
gems.lockedorGemfile.lock) is present. -
#setup(guards: [:locked, :installed]) ⇒ self
protected
Load standalone setup if present, else fallback to
bundler/setup. -
#specifications ⇒ Array<Gem::Specification>
Get specifications.
-
#standalone!(&fallback) ⇒ self
protected
Load standalone setup if present.
-
#standalone? ⇒ Boolean
Denote bundle seems to be installed as a standalone.
- #to_path ⇒ String
Constructor Details
#initialize(path, env: ENV.to_h, ruby_config: {}) ⇒ Bundle
Returns a new instance of Bundle.
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'))..tap do |directory_path| @directory = Directory.new(directory_path, ruby_config: ruby_config) end end end.freeze end |
Instance Attribute Details
#config ⇒ Config (readonly)
25 26 27 |
# File 'lib/stibium/bundled/bundle.rb', line 25 def config @config end |
#directory ⇒ Directory (readonly)
28 29 30 |
# File 'lib/stibium/bundled/bundle.rb', line 28 def directory @directory end |
#path ⇒ Pathname (readonly)
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.
83 84 85 |
# File 'lib/stibium/bundled/bundle.rb', line 83 def bundled? locked? or standalone? end |
#bundler_setup ⇒ Pathname? (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 |
#gemfile ⇒ Pathname?
Get path to gemfile.
92 93 94 |
# File 'lib/stibium/bundled/bundle.rb', line 92 def gemfile gemfiles&.fetch(0, nil) end |
#gemfiles ⇒ Array<Pathname>?
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.
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).
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.
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 |
#specifications ⇒ Array<Gem::Specification>
Get specifications.
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.
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.
112 113 114 |
# File 'lib/stibium/bundled/bundle.rb', line 112 def standalone? !!bundler_setup end |
#to_path ⇒ String
51 52 53 |
# File 'lib/stibium/bundled/bundle.rb', line 51 def to_path path.to_path end |