Parent

Files

Guard::Guardfile::Generator

This class is responsible for generating the Guardfile and adding Guard’ plugins’ templates into it.

@see Guard::CLI

Constants

GUARDFILE_TEMPLATE

The Guardfile template for `guard init`

HOME_TEMPLATES

home isn’t defined. Set to the root of the drive. Trust that there won’t be user defined templates there

Attributes

options[R]

Public Class Methods

new(options = {}) click to toggle source

Initialize a new `Guard::Guardfile::Generator` object.

@param [Hash] options The options for creating a Guardfile @option options [Boolean] :abort_on_existence Whether to abort or not

when a Guardfile already exists
# File lib/guard/guardfile/generator.rb, line 33
def initialize(options = {})
  @options = options
end

Public Instance Methods

create_guardfile() click to toggle source

Creates the initial Guardfile template when it does not already exist.

@see Guard::CLI#init

# File lib/guard/guardfile/generator.rb, line 42
def create_guardfile
  if !File.exist?('Guardfile')
    ::Guard::UI.info "Writing new Guardfile to #{ Dir.pwd }/Guardfile"
    FileUtils.cp(GUARDFILE_TEMPLATE, 'Guardfile')
  elsif options[:abort_on_existence]
    ::Guard::UI.error "Guardfile already exists at #{ Dir.pwd }/Guardfile"
    abort
  end
end
initialize_all_templates() click to toggle source

Adds the templates of all installed Guard implementations to an existing Guardfile.

@see Guard::CLI#init

# File lib/guard/guardfile/generator.rb, line 87
def initialize_all_templates
  ::Guard::PluginUtil.plugin_names.each { |g| initialize_template(g) }
end
initialize_template(plugin_name) click to toggle source

Adds the Guardfile template of a Guard plugin to an existing Guardfile.

@see Guard::CLI#init

@param [String] plugin_name the name of the Guard plugin or template to

initialize
# File lib/guard/guardfile/generator.rb, line 59
def initialize_template(plugin_name)
  plugin_util = ::Guard::PluginUtil.new(plugin_name)
  if plugin_util.plugin_class(fail_gracefully: true)
    plugin_util.add_to_guardfile

    @options[:guardfile] = File.read('Guardfile') if File.exists?('Guardfile')

  elsif File.exist?(File.join(HOME_TEMPLATES, plugin_name))
    content = File.read('Guardfile')

    File.open('Guardfile', 'wb') do |f|
      f.puts(content)
      f.puts('')
      f.puts(File.read(File.join(HOME_TEMPLATES, plugin_name)))
    end

    ::Guard::UI.info "#{ plugin_name } template added to Guardfile, feel free to edit it"
  else
    const_name = plugin_name.downcase.gsub('-', '')
    UI.error "Could not load 'guard/#{ plugin_name.downcase }' or '~/.guard/templates/#{ plugin_name.downcase }' or find class Guard::#{ const_name.capitalize }"
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.