Class: Toys::Utils::Gems
- Inherits:
-
Object
- Object
- Toys::Utils::Gems
- Defined in:
- toys-core/lib/toys/utils/gems.rb
Overview
Defined in the toys-core gem
A helper class that activates and installs gems and sets up bundler.
This class is not loaded by default. Before using it directly, you should
require "toys/utils/gems"
Defined Under Namespace
Classes: ActivationFailedError, AlreadyBundledError, BundleNotInstalledError, BundlerFailedError, GemfileNotFoundError, GemfileUpdateNeededError, IncompatibleToysError, InstallFailedError
Constant Summary collapse
- DEFAULT_GEMFILE_NAMES =
The gemfile names that are searched by default.
[".gems.rb", "gems.rb", "Gemfile"].freeze
Class Method Summary collapse
-
.activate(name, *requirements) ⇒ void
Activate the given gem.
Instance Method Summary collapse
-
#activate(name, *requirements) ⇒ void
Activate the given gem.
-
#bundle(groups: nil, gemfile_path: nil, search_dirs: nil, gemfile_names: nil, retries: nil) ⇒ void
Search for an appropriate Gemfile, and set up the bundle.
-
#initialize(on_missing: nil, on_conflict: nil, terminal: nil, input: nil, output: nil, suppress_confirm: nil, default_confirm: nil) ⇒ Gems
constructor
Create a new gem activator.
Constructor Details
#initialize(on_missing: nil, on_conflict: nil, terminal: nil, input: nil, output: nil, suppress_confirm: nil, default_confirm: nil) ⇒ Gems
Create a new gem activator.
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'toys-core/lib/toys/utils/gems.rb', line 137 def initialize(on_missing: nil, on_conflict: nil, terminal: nil, input: nil, output: nil, suppress_confirm: nil, default_confirm: nil) @default_confirm = default_confirm || default_confirm.nil? ? true : false @on_missing = on_missing || if suppress_confirm @default_confirm ? :install : :error else :confirm end @on_conflict = on_conflict || :error @terminal = terminal @input = input || $stdin @output = output || $stdout end |
Class Method Details
.activate(name, *requirements) ⇒ void
This method returns an undefined value.
Activate the given gem. If it is not present, attempt to install it (or inform the user to update the bundle).
106 107 108 |
# File 'toys-core/lib/toys/utils/gems.rb', line 106 def self.activate(name, *requirements) new.activate(name, *requirements) end |
Instance Method Details
#activate(name, *requirements) ⇒ void
This method returns an undefined value.
Activate the given gem. If it is not present, attempt to install it (or inform the user to update the bundle).
165 166 167 168 169 170 171 172 173 |
# File 'toys-core/lib/toys/utils/gems.rb', line 165 def activate(name, *requirements) Gems.synchronize do begin gem(name, *requirements) rescue ::Gem::LoadError => e handle_activation_error(e, name, requirements) end end end |
#bundle(groups: nil, gemfile_path: nil, search_dirs: nil, gemfile_names: nil, retries: nil) ⇒ void
This method returns an undefined value.
Search for an appropriate Gemfile, and set up the bundle.
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'toys-core/lib/toys/utils/gems.rb', line 196 def bundle(groups: nil, gemfile_path: nil, search_dirs: nil, gemfile_names: nil, retries: nil) Array(search_dirs).each do |dir| break if gemfile_path gemfile_path = Gems.find_gemfile(dir, gemfile_names: gemfile_names) end raise GemfileNotFoundError, "Gemfile not found" unless gemfile_path gemfile_path = ::File.absolute_path(gemfile_path) Gems.synchronize do if configure_gemfile(gemfile_path) activate("bundler", "~> 2.1") require "bundler" lockfile_path = find_lockfile_path(gemfile_path) setup_bundle(gemfile_path, lockfile_path, groups: groups, retries: retries) end end end |