Class: SimplyGenius::Atmos::Generator

Inherits:
Object
  • Object
show all
Includes:
GemLogger::LoggerSupport
Defined in:
lib/simplygenius/atmos/generator.rb

Overview

Defined Under Namespace

Classes: ThorGenerator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**opts) ⇒ Generator

Returns a new instance of Generator.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/simplygenius/atmos/generator.rb', line 16

def initialize(**opts)
  if opts.has_key?(:dependencies)
    @dependencies = opts.delete(:dependencies)
  else
    @dependencies = true
  end
  @thor_opts = opts
  @thor_generators = {}
  @resolved_templates = {}
  @visited_templates = []
end

Instance Attribute Details

#visited_templatesObject (readonly)

Returns the value of attribute visited_templates.



14
15
16
# File 'lib/simplygenius/atmos/generator.rb', line 14

def visited_templates
  @visited_templates
end

Instance Method Details

#apply_template(tmpl) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/simplygenius/atmos/generator.rb', line 60

def apply_template(tmpl)
  @thor_generators[tmpl.source] ||= Class.new(ThorGenerator) do
    source_root tmpl.source.directory
  end

  gen = @thor_generators[tmpl.source].new(tmpl, self, **@thor_opts)
  gen.apply
  visited_templates << tmpl

  gen # makes testing easier by giving a handle to thor generator instance
end

#generate(*template_names, context: {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/simplygenius/atmos/generator.rb', line 28

def generate(*template_names, context: {})
  seen = Set.new

  template_names.each do |template_name|

    # clone since we are mutating context and can be called from within a
    # template, walk_deps also clones
    tmpl = SourcePath.find_template(template_name)
    tmpl.clone.context.merge!(context)

    if @dependencies
      deps = tmpl.walk_dependencies.to_a
    else
      deps = [tmpl]
    end

    deps.each do |dep_tmpl|
      seen_tmpl = [dep_tmpl.name, dep_tmpl.scoped_context]
      unless seen.include?(seen_tmpl)
        apply_template(dep_tmpl)
      end
      seen <<  seen_tmpl
    end

  end

  # TODO: return all context so the calling template can see answers to
  # template questions to use in customizing its output (e.g. service
  # needs cluster name and ec2 backed state)
  return visited_templates
end