Class: Guard::PluginUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/guard/plugin_util.rb

Overview

This class contains useful methods to:

  • Fetch all the Guard plugins names;

  • Initialize a plugin, get its location;

  • Return its class name;

  • Add its template to the Guardfile.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ PluginUtil

Initializes a new `Guard::PluginUtil` object.

Parameters:

  • name (String)

    the name of the Guard plugin



39
40
41
# File 'lib/guard/plugin_util.rb', line 39

def initialize(name)
  @name = name.to_s.sub(/^guard-/, '')
end

Instance Attribute Details

#nameObject

Returns the value of attribute name



14
15
16
# File 'lib/guard/plugin_util.rb', line 14

def name
  @name
end

Class Method Details

.plugin_namesArray<String>

Returns a list of Guard plugin Gem names installed locally.

Returns:

  • (Array<String>)

    a list of Guard plugin gem names



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/guard/plugin_util.rb', line 20

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

Instance Method Details

#add_to_guardfileObject

Adds a plugin's template to the Guardfile.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/guard/plugin_util.rb', line 121

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

#initialize_plugin(options) ⇒ Guard::Plugin, Guard::Guard

Initializes a new `Guard::Plugin` with the given `options` hash. This methods handles plugins that inherit from the deprecated `Guard::Guard` class as well as plugins that inherit from `Guard::Plugin`.

Returns:

See Also:



56
57
58
59
60
61
62
# File 'lib/guard/plugin_util.rb', line 56

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

#plugin_class(options = {}) ⇒ Class?

Tries to load the Guard plugin main class. This transforms the supplied plugin name into a class name:

  • `guardname` will become `Guard::Guardname`

  • `dashed-guard-name` will become `Guard::DashedGuardName`

  • `underscore_guard_name` will become `Guard::UnderscoreGuardName`

When no class is found with the strict case sensitive rules, another try is made to locate the class without matching case:

  • `rspec` will find a class `Guard::RSpec`

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • fail_gracefully (Boolean)

    whether error messages should not be printed

Returns:

  • (Class, nil)

    the loaded class



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/guard/plugin_util.rb', line 95

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 => typeError
    if try_require
      ::Guard::UI.error "Could not find class Guard::#{ _constant_name }"
      ::Guard::UI.error typeError.backtrace.join("\n")
    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.backtrace.join("\n")
    end
  end
end

#plugin_locationString

Locates a path to a Guard plugin gem.

Returns:

  • (String)

    the full path to the plugin gem



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/guard/plugin_util.rb', line 68

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