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



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
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ccios/file_template_definition.rb', line 68

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 = XcodeGroupRepresentation.findGroup(base_path, project)
  file_path = CodeTemplater.new.render_string(@path, context)

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

  group = base_group.create_groups_if_needed_for_intermediate_groups(intermediates_groups)

  target_name = merged_variables["target"]

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

  FileCreator.new.create_file_using_template_path(
    template_definition.template_source_file(@template),
    generated_filename,
    group,
    targets,
    project,
    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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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?

  target_name = merged_variables["target"]
  if project.nil?
    if target_name.is_a?(String)
      target = target_name
    elsif target_name.nil?
      guessed_name = guess_target_name_from_path(base_path)
      raise "Unable to guess the target from the base path \"#{base_path}\", please specify the target in your config file" if guessed_name.nil?
      target = guessed_name
    elsif target_name.is_a?(Array)
      raise "A template generating files in an filesystem project type cannot specify multiple targets"
    else
      raise "Invalid target in template #{@name}"
    end
  else
    base_group = XcodeGroupRepresentation.findGroup(base_path, project)
    raise "Base path \"#{base_path}\" is missing" if base_group.nil?

    if target_name.is_a?(String) || target_name.nil?
      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

end