Class: Rodbot::Generator

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

Overview

Generators for new bots, deployments and such

All files inside the templates_path are honoured provided they match the GLOB. Files with the extension “.gerb” are parsed like ERB files, however, GERB tags must use square brackets.

ERB:  <%= 'foobar' %>
GERB: [%= 'foobar' %]

It’s therefore possible to generate ERB files such as index.erb.gerb.

Helpers available in GERB templates have to be defined in Helpers.

Defined Under Namespace

Classes: Helpers

Constant Summary collapse

GLOB =

Glob to filter relevant template files

"**/{*,.ruby-version*,.gitignore,.keep}"
TAG_COLORS =

Colors used by info to color part of the output

{
  create: :green
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(templates_path) ⇒ Generator

Returns a new instance of Generator.

Parameters:

  • templates_path (Pathname)

    root path of the templates to generate from



33
34
35
36
37
# File 'lib/rodbot/generator.rb', line 33

def initialize(templates_path)
  @templates_path = templates_path
  @helpers_binding = Helpers.new.instance_eval('binding')
  @pastel = Pastel.new
end

Instance Method Details

#displayObject

Print the interpolated template to STDOUT



40
41
42
43
44
45
# File 'lib/rodbot/generator.rb', line 40

def display
  puts
  each_template_path do |template_path, target_path, content|
    puts "### #{target_path} ###", (content || template_path.read), nil
  end
end

#write(directory) ⇒ Object

Write the interpolated template to directory

Parameters:

  • directory (Pathname)

    where to write the files to



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rodbot/generator.rb', line 50

def write(directory)
  fail(Rodbot::GeneratorError, "cowardly refusing to write to existing #{directory}") if directory.exist?
  each_template_path do |template_path, target_path, content|
    absolute_target_path = directory.join(target_path)
    absolute_target_path.dirname.mkpath
    puts tag(:create, target_path)
    if content
      absolute_target_path.write(content)
    else
      FileUtils.copy(template_path, absolute_target_path)
    end
  end
end