Class: TemplateDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/ccios/template_definition.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_definition_hash, template_path) ⇒ TemplateDefinition

Returns a new instance of TemplateDefinition.



26
27
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
# File 'lib/ccios/template_definition.rb', line 26

def initialize(template_definition_hash, template_path)
  @template_definition_hash = template_definition_hash
  @template_path = template_path

  @name = template_definition_hash["name"]
  @description = template_definition_hash["description"] || ""
  @variables = template_definition_hash["variables"] || {}

  @parameters = template_definition_hash["parameters"].map { |hash|
    next ArgumentTemplateParameter.new(hash) unless hash["argument"].nil?
    next FlagTemplateParameter.new(hash) unless hash["flag"].nil?
  }.compact

  @generated_elements = template_definition_hash["generated_elements"].map { |hash|
    next FileTemplateDefinition.new(hash) unless hash["file"].nil?
    next GroupTemplateDefinition.new(hash) unless hash["group"].nil?
    next nil
  }.compact

  if template_definition_hash["code_snippets"].nil?
      @snippets = []
  else
  @snippets = template_definition_hash["code_snippets"].map { |hash|
    SnippetTemplateDefinition.new(hash)
  }
  end

  @template_file_source = template_definition_hash["template_file_source"] || {}
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



13
14
15
# File 'lib/ccios/template_definition.rb', line 13

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/ccios/template_definition.rb', line 13

def name
  @name
end

#parametersObject (readonly)

Returns the value of attribute parameters.



13
14
15
# File 'lib/ccios/template_definition.rb', line 13

def parameters
  @parameters
end

#template_file_sourceObject (readonly)

Returns the value of attribute template_file_source.



13
14
15
# File 'lib/ccios/template_definition.rb', line 13

def template_file_source
  @template_file_source
end

#template_pathObject (readonly)

Returns the value of attribute template_path.



13
14
15
# File 'lib/ccios/template_definition.rb', line 13

def template_path
  @template_path
end

#variablesObject (readonly)

Returns the value of attribute variables.



13
14
15
# File 'lib/ccios/template_definition.rb', line 13

def variables
  @variables
end

Class Method Details

.parse(template_path) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/ccios/template_definition.rb', line 15

def self.parse(template_path)
  template_definition = File.join(template_path, 'template.yml')
  if File.exist?(template_definition)
    template_definition = YAML.load_file(template_definition)
    self.new template_definition, template_path
  else
    puts "Template #{template_path} is invalid: file #{template_definition} not found."
    nil
  end
end

Instance Method Details

#agrument_transformed_options(options) ⇒ Object



109
110
111
112
113
114
# File 'lib/ccios/template_definition.rb', line 109

def agrument_transformed_options(options)
  @parameters.select { |p| p.is_a? ArgumentTemplateParameter }.each do |argument|
    options = argument.update_context(options)
  end
  options
end

#generate(parser, options, config) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ccios/template_definition.rb', line 91

def generate(parser, options, config)

  options = agrument_transformed_options(options)

  merged_variables = config.variables_for_template(self)
  project_path = merged_variables["project"]

  project = parser.project_for project_path

  @generated_elements.each do |element|
    element.generate(parser, project, options, self, config)
  end

  @snippets.each do |snippet|
    snippet.generate(options, self)
  end
end

#provided_context_keysObject



87
88
89
# File 'lib/ccios/template_definition.rb', line 87

def provided_context_keys
  @parameters.flat_map { |p| p.provided_context_keys }.to_set
end

#template_source_file(name) ⇒ Object



82
83
84
85
# File 'lib/ccios/template_definition.rb', line 82

def template_source_file(name)
  raise "Unknown template file #{name}" if @template_file_source[name].nil?
  File.join(@template_path, @template_file_source[name])
end

#validate(parser, options, config) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ccios/template_definition.rb', line 56

def validate(parser, options, config)

  raise "Error: missing name in template" if @name.nil?
  raise "Error: invalid template name" unless @name.is_a? String

  merged_variables = config.variables_for_template(self)
  project_path = merged_variables["project"]

  project = parser.project_for(project_path)
  raise "Error: Unable to find project \"#{project_path}\"" if project.nil?

  @template_file_source.each do |file_template_name, path|
    raise "Missing template source file for \"#{file_template_name}\"" unless File.exist?(self.template_source_file(file_template_name))
  end

  options = agrument_transformed_options(options)

  @generated_elements.each do |generated_element|
    generated_element.validate(parser, project, options, self, config)
  end

  @snippets.each do |snippet|
    snippet.validate(self)
  end
end