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
  if Gem::Version.create(Gem::VERSION) >= Gem::Version.create('1.8.0')
    Gem::Specification.find_all.select do |x|
      if x.name =~ /^guard-/
        true
      elsif x.name != 'guard'
        guard_plugin_path = File.join(x.full_gem_path, "lib/guard/#{ x.name }.rb")
        File.exists?( guard_plugin_path )
      end
    end
  else
    Gem.source_index.find_name(/^guard-/)
  end.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(/\/(.?)/) { "::#{ $1.upcase }" }.gsub(/(?:^|[_-])(.)/) { $1.upcase }
end

def _plugin_constant

Other tags:
    Example: Returns the constant for a plugin -
def _plugin_constant
  @_plugin_constant ||= ::Guard.constants.find { |c| c.to_s.downcase == _constant_name.downcase }
end

def add_to_guardfile


Adds a plugin's template to the Guardfile.
def add_to_guardfile
  if ::Guard.evaluator.guardfile_include?(name)
    ::Guard::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(plugin_class.template(plugin_location))
    end
    ::Guard::UI.info "#{ name } guard added to Guardfile, feel free to edit it"
  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 upgrade for Guard 2.0
    See: Guard::Plugin -
def initialize_plugin(options)
  if plugin_class.superclass.to_s == 'Guard::Guard'
    plugin_class.new(options.delete(:watchers), options)
  else
    plugin_class.new(options)
  end
end

def plugin_class(options = {})

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

Options Hash: (**options)
  • fail_gracefully (Boolean) -- whether error messages should not be printed
def plugin_class(options = {})
  options = { fail_gracefully: false }.merge(options)
  try_require = false
  begin
    require "guard/#{ name.downcase }" if try_require
    @plugin_class ||= ::Guard.const_get(_plugin_constant)
  rescue TypeError
    if try_require
      ::Guard::UI.error "Could not find class Guard::#{ _constant_name }"
    else
      try_require = true
      retry
    end
  rescue LoadError => loadError
    unless options[:fail_gracefully]
      ::Guard::UI.error "Could not load 'guard/#{ name.downcase }' or find class Guard::#{ _constant_name }"
      ::Guard::UI.error loadError.to_s
    end
  end
end

def plugin_location

Returns:
  • (String) - the full path to the plugin gem
def plugin_location
  @plugin_location ||= begin
    if Gem::Version.create(Gem::VERSION) >= Gem::Version.create('1.8.0')
      Gem::Specification.find_by_name("guard-#{ name }").full_gem_path
    else
      Gem.source_index.find_name("guard-#{ name }").last.full_gem_path
    end
  end
rescue
  ::Guard::UI.error "Could not find 'guard-#{ name }' gem path."
end