Class: FileTemplateDefinition

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

Instance Method Summary collapse

Constructor Details

#initialize(file_template_definition_hash) ⇒ FileTemplateDefinition

Returns a new instance of FileTemplateDefinition.



6
7
8
9
10
11
# File 'lib/ccios/file_template_definition.rb', line 6

def initialize(file_template_definition_hash)
  @name = file_template_definition_hash["name"]
  @path = file_template_definition_hash["file"]
  @template = file_template_definition_hash["template"]
  @variables = file_template_definition_hash["variables"] || {}
end

Instance Method Details

#generate(parser, project, context, template_definition, config) ⇒ Object



54
55
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
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ccios/file_template_definition.rb', line 54

def generate(parser, project, context, template_definition, config)
  merged_variables = config.variables_for_template_element(template_definition, @name, @variables)

  base_path = merged_variables["base_path"]
  base_group = project[base_path]
  file_path = CodeTemplater.new.render_string(@path, context)

  intermediates_groups = file_path.split("/")[0...-1]
  generated_filename = file_path.split("/")[-1]

  group = base_group
  associate_path_to_group = !base_group.path.nil?

  intermediates_groups.each do |group_name|
    new_group_path = File.join(group.real_path, group_name)
    existing_group = group.groups.find { |g| g.display_name == group_name }
    group = existing_group || group.pf_new_group(
      associate_path_to_group: associate_path_to_group,
      name: group_name,
      path: new_group_path
    )
  end

  target_name = merged_variables["target"]

  targets = []
  if target_name.is_a?(String)
    targets = [parser.target_for(project, target_name)]
  elsif target_name.is_a?(Array)
    targets = target_name.map { |name| parser.target_for(project, name) }
  end

  FileCreator.new.create_file_using_template_path(
    template_definition.template_source_file(@template),
    generated_filename,
    group,
    targets,
    context
  )
end

#validate(parser, project, context, template_definition, config) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
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
# File 'lib/ccios/file_template_definition.rb', line 13

def validate(parser, project, context, template_definition, config)

  merged_variables = config.variables_for_template_element(template_definition, @name, @variables)

  code_templater = CodeTemplater.new
  expected_context_keys = template_definition.provided_context_keys
  pathTags = code_templater.get_unknown_context_keys_for_string(@path)
  pathTags.each do |tag|
    raise "Unknown parameter \"#{tag}\" in path \"#{@path}\"" unless expected_context_keys.include?(tag)
  end

  file_path = code_templater.render_string(@path, context)
  raise "File #{file_path} already exists" if File.exist?(file_path)

  file_creator = FileCreator.new
   = file_creator.get_unknown_template_tags_for(template_definition.template_source_file(@template))
  .each do |tag|
    raise "Unknown parameter \"#{tag}\" in template \"#{@template}\"" unless expected_context_keys.include?(tag)
  end

  base_path = merged_variables["base_path"]
  raise "Missing base_path variable" if base_path.nil?

  base_group = project[base_path]
  raise "Base path \"#{base_path}\" is missing" if base_group.nil?

  target_name = merged_variables["target"]
  if target_name.is_a?(String)
    target = parser.target_for(project, target_name)
    raise "Unable to find target \"#{target_name}\"" if target.nil?
  elsif target_name.is_a?(Array)
    target_name.each do |target_name|
      target = parser.target_for(project, target_name)
      raise "Unable to find target \"#{target_name}\"" if target.nil?
    end
  else
    raise "Invalid target in template #{@name}"
  end

end