Class: Toys::Utils::Gems

Inherits:
Object
  • Object
show all
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, IncompatibleToysError, InstallFailedError

Constant Summary collapse

DEFAULT_GEMFILE_NAMES =

The gemfile names that are searched by default.

Returns:

  • (Array<String>)
[".gems.rb", "gems.rb", "Gemfile"].freeze

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • on_missing (:confirm, :error, :install) (defaults to: nil)

    What to do if a needed gem is not installed. Possible values:

    • :confirm - prompt the user on whether to install
    • :error - raise an exception
    • :install - just install the gem

    The default is :confirm.

  • on_conflict (:error, :warn, :ignore) (defaults to: nil)

    What to do if bundler has already been run with a different Gemfile. Possible values:

    • :error - raise an exception
    • :ignore - just silently proceed without bundling again
    • :warn - print a warning and proceed without bundling again

    The default is :error.

  • terminal (Toys::Utils::Terminal) (defaults to: nil)

    Terminal to use (optional)

  • input (IO) (defaults to: nil)

    Input IO (optional, defaults to STDIN)

  • output (IO) (defaults to: nil)

    Output IO (optional, defaults to STDOUT)

  • suppress_confirm (Boolean) (defaults to: nil)

    Deprecated. Use on_missing instead.

  • default_confirm (Boolean) (defaults to: nil)

    Deprecated. Use on_missing instead.



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/toys/utils/gems.rb', line 119

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).

Parameters:

  • name (String)

    Name of the gem

  • requirements (String...)

    Version requirements



88
89
90
# File 'lib/toys/utils/gems.rb', line 88

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).

Parameters:

  • name (String)

    Name of the gem

  • requirements (String...)

    Version requirements



147
148
149
150
151
152
153
154
155
# File 'lib/toys/utils/gems.rb', line 147

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.

Parameters:

  • groups (Array<String>) (defaults to: nil)

    The groups to include in setup.

  • gemfile_path (String) (defaults to: nil)

    The path to the Gemfile to use. If nil or not given, the :search_dirs will be searched for a Gemfile.

  • search_dirs (String, Array<String>) (defaults to: nil)

    Directories in which to search for a Gemfile, if gemfile_path is not given. You can provide a single directory or an array of directories.

  • gemfile_names (String, Array<String>) (defaults to: nil)

    File names that are recognized as Gemfiles, when searching because gemfile_path is not given. Defaults to DEFAULT_GEMFILE_NAMES.

  • retries (Integer) (defaults to: nil)

    Number of times to retry bundler operations. Optional.

Raises:



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/toys/utils/gems.rb', line 178

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.2")
      require "bundler"
      setup_bundle(gemfile_path, groups: groups, retries: retries)
    end
  end
end