Class: XcodeGroupRepresentation

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

Overview

This object handles Xcode groups, folder reference and synchronized folder reference

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, xcode_group, additional_path = []) ⇒ XcodeGroupRepresentation

Returns a new instance of XcodeGroupRepresentation.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ccios/xcode_group_representation.rb', line 28

def initialize(project, xcode_group, additional_path = [])
  if project.nil?
    throw "Unexpected xcode_group when project is nil, we should be in an filesystem context" unless xcode_group.nil?
  else
    throw "Unsupported group type" unless xcode_group.is_a?(Xcodeproj::Project::Object::PBXFileSystemSynchronizedRootGroup) || xcode_group.is_a?(Xcodeproj::Project::Object::PBXGroup)
    if !additional_path.empty? && !xcode_group.is_a?(Xcodeproj::Project::Object::PBXFileSystemSynchronizedRootGroup)
      throw "additional_path can only be specified for a synchronized file system group"
    end
  end
  # This represent the xcode project if present, if nil we should be generating files in an Swift package
  @project = project
  # This represents the deepest group or folder reference in the project
  @xcode_deepest_group = xcode_group
  # This represents the additional filesystem path after `xcode_deepest_group` as an Array of strings. This should be non empty only when deepest group is a synchronized group
  @additional_path = additional_path
end

Class Method Details

.findGroup(path, project) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ccios/xcode_group_representation.rb', line 5

def self.findGroup(path, project)
  intermediates_groups = path.split("/")

  if project.nil?
    return self.new nil, nil, intermediates_groups
  end

  deepest_group = project.main_group
  additional_path = []

  intermediates_groups.each do |group_name|
    if deepest_group.is_a?(Xcodeproj::Project::Object::PBXFileSystemSynchronizedRootGroup)
      additional_path.append(group_name)
    elsif deepest_group.is_a?(Xcodeproj::Project::Object::PBXGroup)
      deepest_group = deepest_group.find_subpath(group_name)
      return nil if deepest_group.nil?
    else
      raise "Unsupported element found with name \"#{group_name}\": #{deepest_group}"
    end
  end
  self.new project, deepest_group, additional_path
end

Instance Method Details

#create_groups_if_needed_for_intermediate_groups(intermediates_groups) ⇒ XcodeGroupRepresentation

Returns the created group or self if the intermediates_groups is empty.

Returns:



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ccios/xcode_group_representation.rb', line 73

def create_groups_if_needed_for_intermediate_groups(intermediates_groups)
  return self if intermediates_groups.empty?

  new_deepest_group = @xcode_deepest_group
  new_additional_path = @additional_path

  intermediates_groups.each do |group_name|
    if new_deepest_group.nil? || new_deepest_group.is_a?(Xcodeproj::Project::Object::PBXFileSystemSynchronizedRootGroup)
      new_additional_path.append(group_name)
    elsif new_deepest_group.is_a?(Xcodeproj::Project::Object::PBXGroup)
      existing_child = new_deepest_group.find_subpath(group_name)
      new_group_path = File.join(new_deepest_group.real_path, group_name)
      new_deepest_group = existing_child || new_deepest_group.pf_new_group(
        associate_path_to_group: !new_deepest_group.path.nil?,
        name: group_name,
        path: new_group_path
      )
    else
      raise "Unsupported element found for \"#{group_name}\": #{new_deepest_group}"
    end
  end
  XcodeGroupRepresentation.new @project, new_deepest_group, new_additional_path
end

#real_pathObject



45
46
47
48
49
50
51
# File 'lib/ccios/xcode_group_representation.rb', line 45

def real_path
  if @xcode_deepest_group.nil?
    @additional_path.join("/")
  else
    Xcodeproj::Project::Object::GroupableHelper.real_path(@xcode_deepest_group) + @additional_path.join("/")
  end
end

#register_file_to_targets(file_path, targets) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ccios/xcode_group_representation.rb', line 54

def register_file_to_targets(file_path, targets)
  if @xcode_deepest_group.nil?
    return
  end
  if @xcode_deepest_group.is_a?(Xcodeproj::Project::Object::PBXGroup)
    file_ref = @xcode_deepest_group.new_reference(file_path)
    targets.each do |target|
      target.add_file_references([file_ref])
    end
  else
    # no file to register, unless exceptions needs to be made
    # TODO: Handle synchronized groups (no new reference, but use exceptions)
    targets.each do |taget|
      puts "Unsupported target mismatch between \"#{file_path}\" and synchronized group #{ @xcode_deepest_group.display_name }" unless taget.file_system_synchronized_groups.index(@xcode_deepest_group) != nil
    end
  end
end