Class: Toys::Utils::Gems
- Inherits:
-
Object
- Object
- Toys::Utils::Gems
- Defined in:
- lib/toys/utils/gems.rb
Overview
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, IncompatibleGemSourceError, 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) ⇒ :activated, ...
Activate the given gem.
Instance Method Summary collapse
-
#activate(name, *requirements) ⇒ :activated, ...
Activate the given gem.
-
#bundle(groups: nil, gemfile_path: nil, search_dirs: nil, gemfile_names: nil, retries: nil) ⇒ :setup, ...
Search for an appropriate Gemfile, and set up the bundle.
-
#initialize(on_missing: nil, on_conflict: nil, default_confirm: nil, terminal: nil, input: nil, output: nil) ⇒ Gems
constructor
Create a new gem activator.
-
#with(**overrides) ⇒ Toys::Utils::Gems
Return a gem activator with the same settings as this one, except for the provided overrides.
Constructor Details
#initialize(on_missing: nil, on_conflict: nil, default_confirm: nil, terminal: nil, input: nil, output: nil) ⇒ Gems
Create a new gem activator.
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/toys/utils/gems.rb', line 177 def initialize(on_missing: nil, on_conflict: nil, default_confirm: nil, terminal: nil, input: nil, output: nil) require "rubygems" unless [nil, :confirm, :install, :error].include?(on_missing) raise ::ArgumentError, "Illegal value for on_missing: #{on_missing.inspect}" end unless [nil, :error, :warn, :ignore].include?(on_conflict) raise ::ArgumentError, "Illegal value for on_conflict: #{on_conflict.inspect}" end unless [nil, true, false].include?(default_confirm) raise ::ArgumentError, "Illegal value for default_confirm: #{default_confirm.inspect}" end @on_missing = on_missing || :confirm @on_conflict = on_conflict || :error default_confirm = true if default_confirm.nil? @default_confirm = default_confirm # The terminal passed in is remembered separately from the one this # object may later derive from the input and output, so that #with # copies the former. Copying a derived terminal would silently defeat # an input or output override. @param_terminal = terminal @terminal = terminal @input = input || $stdin @output = output || $stdout end |
Class Method Details
.activate(name, *requirements) ⇒ :activated, ...
Activate the given gem. If it is not present, attempt to install it (or inform the user to update the bundle).
148 149 150 |
# File 'lib/toys/utils/gems.rb', line 148 def self.activate(name, *requirements) new.activate(name, *requirements) end |
Instance Method Details
#activate(name, *requirements) ⇒ :activated, ...
Activate the given gem. If it is not present, attempt to install it (or inform the user to update the bundle).
242 243 244 245 246 247 248 |
# File 'lib/toys/utils/gems.rb', line 242 def activate(name, *requirements) Gems.synchronize do gem(name, *requirements) ? :activated : false rescue ::Gem::LoadError => e handle_activation_error(e, name, requirements) end end |
#bundle(groups: nil, gemfile_path: nil, search_dirs: nil, gemfile_names: nil, retries: nil) ⇒ :setup, ...
Search for an appropriate Gemfile, and set up the bundle.
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/toys/utils/gems.rb', line 276 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 setup_bundle(gemfile_path, Array(groups), retries) end end |
#with(**overrides) ⇒ Toys::Utils::Gems
Return a gem activator with the same settings as this one, except for the provided overrides. See the constructor for argument docs. If no non-nil overrides are provided, self is returned.
215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/toys/utils/gems.rb', line 215 def with(**overrides) overrides = overrides.compact return self if overrides.empty? current_settings = { on_missing: @on_missing, on_conflict: @on_conflict, default_confirm: @default_confirm, terminal: @param_terminal, input: @input, output: @output, } Gems.new(**current_settings, **overrides) end |