Class: Guard::Guardfile::Generator
- Inherits:
-
Object
- Object
- Guard::Guardfile::Generator
- Defined in:
- lib/guard/guardfile/generator.rb
Overview
This class is responsible for generating the Guardfile and adding Guard’ plugins’ templates into it.
Defined Under Namespace
Classes: Error, NoSuchPlugin
Constant Summary collapse
- INFO_TEMPLATE_ADDED =
"%s template added to Guardfile, feel free to edit it"
- GUARDFILE_TEMPLATE =
The Guardfile template for ‘guard init`
File.( "../../../guard/templates/Guardfile", __FILE__ )
Instance Method Summary collapse
-
#create_guardfile ⇒ Object
Creates the initial Guardfile template when it does not already exist.
-
#initialize_all_templates ⇒ Object
Adds the templates of all installed Guard implementations to an existing Guardfile.
-
#initialize_template(plugin_name) ⇒ Object
Adds the Guardfile template of a Guard plugin to an existing Guardfile.
Instance Method Details
#create_guardfile ⇒ Object
Creates the initial Guardfile template when it does not already exist.
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/guard/guardfile/generator.rb', line 65 def create_guardfile path = Pathname.new("Guardfile"). 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_templates ⇒ Object
Adds the templates of all installed Guard implementations to an existing Guardfile.
117 118 119 |
# File 'lib/guard/guardfile/generator.rb', line 117 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.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/guard/guardfile/generator.rb', line 83 def initialize_template(plugin_name) guardfile = Pathname.new("Guardfile") plugin_util = PluginUtil.new(plugin_name) # TODO: change to "valid?" method plugin_class = plugin_util.plugin_class(fail_gracefully: true) if plugin_class begin plugin_util.add_to_guardfile rescue Errno::ENOENT => error # TODO: refactor template = plugin_class.template(plugin_util.plugin_location) _ui(:error, "Found class #{plugin_class} but loading it's template"\ "failed: '#{template}'") _ui(:error, "Error is: #{error}") return end return end template_code = (HOME_TEMPLATES + plugin_name).read guardfile.binwrite(format("\n%s\n", template_code), open_args: ["a"]) _ui(:info, format(INFO_TEMPLATE_ADDED, plugin_name)) rescue Errno::ENOENT fail NoSuchPlugin, plugin_name.downcase end |