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

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.



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/toys/utils/gems.rb', line 113

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



82
83
84
# File 'lib/toys/utils/gems.rb', line 82

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



141
142
143
144
145
146
147
148
149
# File 'lib/toys/utils/gems.rb', line 141

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, search_dirs: nil) ⇒ void

This method returns an undefined value.

Set up the bundle.

Parameters:

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

    The groups to include in setup

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

    Directories to search for a Gemfile



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/toys/utils/gems.rb', line 158

def bundle(groups: nil,
           search_dirs: nil)
  Gems.synchronize do
    gemfile_path = find_gemfile(Array(search_dirs))
    if configure_gemfile(gemfile_path)
      activate("bundler", "~> 2.1")
      require "bundler"
      setup_bundle(gemfile_path, groups || [])
    end
  end
end