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:

  • CLI

Defined Under Namespace

Classes: NoSuchPlugin

Instance Method Summary collapse

Instance Method Details

#create_guardfileObject

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

See Also:

  • CLI#init


58
59
60
61
62
63
64
65
66
67
# File 'lib/guard/guardfile/generator.rb', line 58

def create_guardfile
  path = Pathname.new("Guardfile").expand_path
  if path.exist?
    UI.error("Guardfile already exists at #{path}")
    abort
  end

  UI.info("Writing new Guardfile to #{path}")
  FileUtils.cp(GUARDFILE_TEMPLATE, path.to_s)
end

#initialize_all_templatesObject

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

See Also:

  • CLI#init


101
102
103
# File 'lib/guard/guardfile/generator.rb', line 101

def initialize_all_templates
  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:

  • CLI#init


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/guard/guardfile/generator.rb', line 73

def initialize_template(plugin_name)
  guardfile = Pathname.new("Guardfile")
  plugin_util = PluginUtil.new(plugin_name)

  if plugin_util.valid?
    begin
      plugin_util.add_to_guardfile
    rescue Errno::ENOENT => e
      UI.error("Found class #{plugin_util.plugin_class} but loading its template failed.")
      UI.error("Error is: #{e}")
      return
    end
    return
  end

  template_code = (HOME_TEMPLATES + plugin_name).read
  guardfile.binwrite(format("\n%<template>s\n", template: template_code), open_args: ["a"])

  UI.info(format(INFO_TEMPLATE_ADDED, plugin_name))
rescue Errno::ENOENT
  fail NoSuchPlugin, plugin_name.downcase
end