module Guard

def initialize_template(guard_name = nil)

Parameters:
  • guard_name (String) -- the name of the Guard or template to initialize

Other tags:
    See: Guard::Guard.init -
def initialize_template(guard_name = nil)
  if !File.exist?('Guardfile')
    ::Guard::UI.info "Writing new Guardfile to #{ Dir.pwd }/Guardfile"
    FileUtils.cp(GUARDFILE_TEMPLATE, 'Guardfile')
  elsif guard_name.nil?
    ::Guard::UI.error "Guardfile already exists at #{ Dir.pwd }/Guardfile"
    exit 1
  end
  if guard_name
    guard_class = ::Guard.get_guard_class(guard_name, true)
    if guard_class
      guard_class.init(guard_name)
    elsif File.exist?(File.join(HOME_TEMPLATES, guard_name))
      content  = File.read('Guardfile')
      template = File.read(File.join(HOME_TEMPLATES, guard_name))
      File.open('Guardfile', 'wb') do |f|
        f.puts(content)
        f.puts("")
        f.puts(template)
      end
      ::Guard::UI.info "#{ guard_name } template added to Guardfile, feel free to edit it"
    else
      const_name  = guard_name.downcase.gsub('-', '')
      UI.error "Could not load 'guard/#{ guard_name.downcase }' or '~/.guard/templates/#{ guard_name.downcase }' or find class Guard::#{ const_name.capitalize }"
    end
  end
end