Class: Xfabricator::Fabricator

Inherits:
Object
  • Object
show all
Defined in:
lib/xfabricator/fabricator.rb

Instance Method Summary collapse

Constructor Details

#initialize(config, search_path) ⇒ Fabricator

Returns a new instance of Fabricator.



11
12
13
14
# File 'lib/xfabricator/fabricator.rb', line 11

def initialize(config, search_path)
  @config = config
  @search_path = search_path
end

Instance Method Details

#fabricate(template, variables, target_name) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/xfabricator/fabricator.rb', line 16

def fabricate(template, variables, target_name)
  group = find_current_group

  unless group
    raise ArgumentError, "couldn't find current group"
  end

  fabricate_in_group(template, variables, target_name, group)
end

#fabricate_file(output_name, template_file_path, variables, output_dir) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/xfabricator/fabricator.rb', line 74

def fabricate_file(output_name, template_file_path, variables, output_dir)
  FileUtils.mkdir_p output_dir

  template = XCodeFileTemplate.new @config.project, variables
  template.template_file = template_file_path
  rendered_content = template.render

  output_file = "#{output_dir}/#{output_name}"
  File.open(output_file, 'w') { |file| file.write(rendered_content) }
  output_file
end

#fabricate_in_group(template, variables, target_name, group) ⇒ Object



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
67
68
69
70
71
72
# File 'lib/xfabricator/fabricator.rb', line 26

def fabricate_in_group(template, variables, target_name, group)
  unless group
    raise ArgumentError, "group not specified"
  end

  unless target_name
    raise ArgumentError, "target_name not specified"
  end

  project = @config.project
  target = project.targets.find { |target| target.name == target_name }

  unless target
    raise ArgumentError, "Couldn't find target with name: #{target_name}"
  end

  template.template_files.each do |template_file|
    template_file_path = "#{@config.template_location}/#{template_file.template_file}"

    unless Pathname.new(template_file_path).exist?
      template_file_path = template_file.template_file
    end

    if Pathname.new(template_file_path).exist?
      name_template = Mustache.new
      output_name = name_template.render template_file.output_name_template, variables

      current_group = group

      if template_file.sub_path.length > 0
        groups = group.xfab_create_subpath(template_file.sub_path)
        current_group = groups.last
      end

      output_file = fabricate_file(output_name, template_file_path, variables, current_group.real_path)

      ref = current_group.new_reference output_file
      target.add_file_references [ref]
    else
      raise RuntimeError, "template file was not found: #{template_file_path}"
    end

  end

  project.save

end

#find_current_groupObject



86
87
88
89
90
91
92
93
94
95
# File 'lib/xfabricator/fabricator.rb', line 86

def find_current_group
  project = @config.project
  main_group = project.main_group

  if main_group.real_path == @search_path.to_s
    return main_group
  end

  find_group_for_directory(main_group, @search_path, project)
end

#find_group_for_directory(current_group, directory, project) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/xfabricator/fabricator.rb', line 97

def find_group_for_directory(current_group, directory, project)
  group_found = current_group.groups.find { |ref|
    full_path = "#{project.path.parent.to_s}#{ref.hierarchy_path}"
    full_path == directory.to_s
  }

  unless group_found
    current_group.groups.each do |group|
      full_path = "#{project.path.parent.to_s}#{group.hierarchy_path}"
      return find_group_for_directory(group, directory, project) if directory.to_s.start_with? full_path
    end
  end

  group_found
end