class Xfabricator::Fabricator

Public Class Methods

new(config, search_path) click to toggle source
# File lib/xfabricator/fabricator.rb, line 11
def initialize(config, search_path)
  @config = config
  @search_path = search_path
end

Public Instance Methods

fabricate(template, variables, target_name) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_group() click to toggle source
# 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) click to toggle source
# 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