Class: Guard::Guardfile::Generator

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

Overview

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

See Also:

Constant Summary

GUARDFILE_TEMPLATE =

The Guardfile template for `guard init`

File.expand_path('../../../guard/templates/Guardfile', __FILE__)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Generator

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

Options Hash (options):

  • :abort_on_existence (Boolean)

    Whether to abort or not when a Guardfile already exists



33
34
35
# File 'lib/guard/guardfile/generator.rb', line 33

def initialize(options = {})
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options



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

def options
  @options
end

Instance Method Details

#create_guardfileObject

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

See Also:



42
43
44
45
46
47
48
49
50
# 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_templatesObject

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

See Also:



87
88
89
# 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) ⇒ Object

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

See Also:



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# 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