class Guard::PluginUtil


* Add its template to the Guardfile.
* Return its class name;
* Initialize a plugin, get its location;
* Fetch all the Guard plugins names;
This class contains useful methods to:

def self.plugin_names

Returns:
  • (Array) - a list of Guard plugin gem names
def self.plugin_names
  valid = Gem::Specification.find_all.select do |gem|
    _gem_valid?(gem)
  end
  valid.map { |x| x.name.sub(/^guard-/, "") }.uniq
end

def _constant_name

Other tags:
    Example: Returns the most probable name for a plugin -
def _constant_name
  @_constant_name ||= name.gsub(%r{/(.?)}) { "::#{ $1.upcase }" }.
                      gsub(/(?:^|[_-])(.)/) { $1.upcase }
end

def _full_gem_path(name)

def _full_gem_path(name)
  Gem::Specification.find_by_name(name).full_gem_path
end

def _gem_valid?(gem)

def _gem_valid?(gem)
  return false if gem.name == "guard-compat"
  return true if gem.name =~ /^guard-/
  full_path = gem.full_gem_path
  file = File.join(full_path, "lib", "guard", "#{gem.name}.rb")
  File.exist?(file)
end

def _plugin_constant

Other tags:
    Example: Returns the constant for a plugin -
def _plugin_constant
  @_plugin_constant ||= Guard.constants.detect do |c|
    c.to_s.casecmp(_constant_name.downcase).zero?
  end
end

def add_to_guardfile


Adds a plugin's template to the Guardfile.
def add_to_guardfile
  klass = plugin_class # call here to avoid failing later
  require_relative "guardfile/evaluator"
  # TODO: move this to Generator?
  options = Guard.state.session.evaluator_options
  evaluator = Guardfile::Evaluator.new(options)
  begin
    evaluator.evaluate
  rescue Guard::Guardfile::Evaluator::NoPluginsError
  end
  if evaluator.guardfile_include?(name)
    UI.info "Guardfile already includes #{ name } guard"
  else
    content = File.read("Guardfile")
    File.open("Guardfile", "wb") do |f|
      f.puts(content)
      f.puts("")
      f.puts(klass.template(plugin_location))
    end
    UI.info INFO_ADDED_GUARD_TO_GUARDFILE % name
  end
end

def initialize(name)

Parameters:
  • name (String) -- the name of the Guard plugin
def initialize(name)
  @name = name.to_s.sub(/^guard-/, "")
end

def initialize_plugin(options)

Returns:
  • (Guard::Guard) - the initialized plugin. This return type is
  • (Guard::Plugin) - the initialized plugin

Other tags:
    See: https://github.com/guard/guard/wiki/Upgrading-to-Guard-2.0 - How to
    See: Guard::Plugin -
def initialize_plugin(options)
  klass = plugin_class
  fail "Could not load class: #{_constant_name.inspect}" unless klass
  if klass.ancestors.include?(Guard)
    klass.new(options.delete(:watchers), options)
  else
    begin
      klass.new(**options)
    rescue ArgumentError => e
      fail "Failed to call #{klass}.new(options): #{e}"
    end
  end
end

def plugin_class(options = {})

Returns:
  • (Class, nil) - the loaded class

Options Hash: (**options)
  • fail_gracefully (Boolean) -- whether error messages should
def plugin_class(options = {})
  options = { fail_gracefully: false }.merge(options)
  const = _plugin_constant
  fail TypeError, "no constant: #{_constant_name}" unless const
  @plugin_class ||= Guard.const_get(const)
rescue TypeError
  begin
    require "guard/#{ name.downcase }"
    const = _plugin_constant
    @plugin_class ||= Guard.const_get(const)
  rescue TypeError => error
    UI.error "Could not find class Guard::#{ _constant_name }"
    UI.error error.backtrace.join("\n")
    # TODO: return value or move exception higher
  rescue LoadError => error
    unless options[:fail_gracefully]
      msg = format(ERROR_NO_GUARD_OR_CLASS, name.downcase, _constant_name)
      UI.error(msg)
      UI.error("Error is: #{error}")
      UI.error(error.backtrace.join("\n"))
      # TODO: return value or move exception higher
    end
  end
end

def plugin_location

Returns:
  • (String) - the full path to the plugin gem
def plugin_location
  @plugin_location ||= _full_gem_path("guard-#{name}")
rescue Gem::LoadError
  UI.error "Could not find 'guard-#{ name }' gem path."
end