Class: ApiGenHelper::GroupGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/api_gen_helper/group_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_file_to_project_and_targets(project, targets_name, group_path, dir_path, file_group_path, file_name, file_is_resource = false) ⇒ void

This method returns an undefined value.

Adds a provided file to a specific Project and Target

Parameters:

  • project (Xcodeproj::Project)

    The target xcodeproj file

  • targets_name (String)

    Array of targets name

  • group_path (Pathname)

    The Xcode group path for current file

  • dir_path (Pathname)

    The directory path for current file

  • file_group_path (String)

    Directory path

  • file_name (String)

    Current file name

  • file_is_resource (TrueClass or FalseClass) (defaults to: false)

    If true then file is resource



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/api_gen_helper/group_generator.rb', line 96

def add_file_to_project_and_targets(project, targets_name, group_path, dir_path, file_group_path, file_name, file_is_resource = false)
  file_path = dir_path
  file_path = file_path.join(file_group_path) if file_group_path
  file_path = file_path.join(file_name) if file_name

  module_group = self.retrieve_group_or_create_if_needed(group_path, dir_path, file_group_path, project, true)
  xcode_file = module_group.new_file(File.absolute_path(file_path))

  targets_name.each do |target|
    xcode_target = obtain_target(target, project)

    if file_is_resource || self.is_bundle_resource?(file_name)
      xcode_target.add_resources([xcode_file])
    elsif self.is_compile_source?(file_name)
      xcode_target.add_file_references([xcode_file])
    end
  end
end

#added_references_recursivly_for_target(target, group) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/api_gen_helper/group_generator.rb', line 27

def added_references_recursivly_for_target(target, group)
  group.children.each do |child|
      if child.isa == "PBXFileReference"
        if is_bundle_resource?(child.name)
          target.add_resources([child])
        elsif is_compile_source?(child.name)
          target.add_file_references([child])
        end
      else
        added_references_recursivly_for_target(target, child)
      end
  end
end

#build_phases_from_targets(targets_name, project) ⇒ [PBXSourcesBuildPhase]

Find and return target build phases

Parameters:

  • targets_name (String)

    Array of targets

  • project (Xcodeproj::Project)

    The target xcodeproj file

Returns:

  • ([PBXSourcesBuildPhase])


223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/api_gen_helper/group_generator.rb', line 223

def build_phases_from_targets(targets_name, project)
  build_phases = []

  targets_name.each do |target_name|
    xcode_target = obtain_target(target_name, project)
    xcode_target.build_phases.each do |build_phase|
      if build_phase.isa == 'PBXSourcesBuildPhase'
        build_phases.push(build_phase)
      end
    end
  end

  build_phases
end

#clear_group(project, targets_name, group_path) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/api_gen_helper/group_generator.rb', line 168

def clear_group(project, targets_name, group_path)
  module_group = retrieve_group_or_create_if_needed(group_path, nil, nil, project, false)
  return unless module_group

  files_path = files_path_from_group(module_group, project)
  return unless files_path

  files_path.each do |file_path|
    remove_file_by_file_path(file_path, targets_name, project)
  end

  module_group.clear
end

#configure_file_ref_path(file_ref) ⇒ String

Get configure file full path

Parameters:

  • file_ref (PBXFileReference)

    Build file

Returns:

  • (String)


242
243
244
245
246
247
# File 'lib/api_gen_helper/group_generator.rb', line 242

def configure_file_ref_path(file_ref)
  build_file_ref_path = file_ref.hierarchy_path.to_s
  build_file_ref_path[0] = ''

  build_file_ref_path
end

#create_file_reference(group, path) ⇒ Xcodeproj::Project::Object::PBXFileReference

Create file reference for specific group

Parameters:

  • group (Xcodeproj::Project::Object::PBXGroup)
    • group where create file reference

  • path (String)

    path for file

Returns:

  • (Xcodeproj::Project::Object::PBXFileReference)

    file reference



258
259
260
# File 'lib/api_gen_helper/group_generator.rb', line 258

def create_file_reference(group, path)
  file_reference = Xcodeproj::Project::Object::FileReferencesFactory.new_reference(group, path, :group)
end

#files_path_from_group(module_group, _project) ⇒ [String]

Get all files path from group path

Parameters:

  • module_group (PBXGroup)

    The module group

  • project (Xcodeproj::Project)

    The target xcodeproj file

Returns:

  • ([String])


155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/api_gen_helper/group_generator.rb', line 155

def files_path_from_group(module_group, _project)
  files_path = []

  module_group.recursive_children.each do |file_ref|
    if file_ref.isa == 'PBXFileReference'
      file_ref_path = configure_file_ref_path(file_ref)
      files_path.push(file_ref_path)
    end
  end

  files_path
end

#generate_group(project_path, group_path, target_name) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/api_gen_helper/group_generator.rb', line 9

def generate_group(project_path, group_path, target_name)
  project = obtain_project(project_path)

  clear_group(project, [target_name], group_path)

  group = retrieve_group_or_create_if_needed(group_path, nil, nil, project, true)

  prepare_main_group(group, group_path, project)

  project.targets.each do |target|
     if target.name == target_name
      added_references_recursivly_for_target(target, group)
     end
  end

  project.save()
end

#is_bundle_resource?(resource_name) ⇒ Boolean

Returns:

  • (Boolean)


266
267
268
# File 'lib/api_gen_helper/group_generator.rb', line 266

def is_bundle_resource?(resource_name)
  File.extname(resource_name) == '.xib' || File.extname(resource_name) == '.storyboard'
end

#is_compile_source?(file_name) ⇒ Boolean

Returns:

  • (Boolean)


262
263
264
# File 'lib/api_gen_helper/group_generator.rb', line 262

def is_compile_source?(file_name)
  File.extname(file_name) == '.m' || File.extname(file_name) == '.swift' || File.extname(file_name) == '.mm'
end

#obtain_project(project_name) ⇒ Xcodeproj::Project::Object::PBXProject

Open xcodoproj file

Parameters:

  • project_name (String)

    String path for xcodeproj file

Returns:

  • (Xcodeproj::Project::Object::PBXProject)


67
68
69
# File 'lib/api_gen_helper/group_generator.rb', line 67

def obtain_project(project_name)
  Xcodeproj::Project.open(project_name)
end

#obtain_target(target_name, project) ⇒ Xcodeproj::AbstractTarget

Returns an AbstractTarget class for a given name

Parameters:

  • target_name (String)

    The name of the target

  • project (Xcodeproj::Project)

    The target xcodeproj file

Returns:

  • (Xcodeproj::AbstractTarget)

Raises:

  • (StandardError)


76
77
78
79
80
81
82
83
# File 'lib/api_gen_helper/group_generator.rb', line 76

def obtain_target(target_name, project)
  project.targets.each do |target|
    return target if target.name == target_name
  end

  error_description = "Cannot find a target with name #{target_name} in Xcode project"
  raise StandardError, error_description
end

#path_names_from_path(path) ⇒ Object



249
250
251
# File 'lib/api_gen_helper/group_generator.rb', line 249

def path_names_from_path(path)
  path.to_s.split('/')
end

#prepare_main_group(group, directory, project) ⇒ Object

Recursively fills the group with files and groups

Parameters:

  • group (PBXGroup)

    group to fill out

  • directory (String)

    path directory when search files and folders



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/api_gen_helper/group_generator.rb', line 44

def prepare_main_group(group, directory, project)

  Dir.entries(directory).each do |file_name|
    next if file_name =~ /^\./

    if File.file? File.join(directory, file_name)
      file_path = File.join(directory, file_name)
      xcode_file_reference = group.new_file(File.absolute_path(file_path))
    end

    if File.directory? File.join(directory, file_name)
       sub_dir = File.join(directory, file_name)
       new_group = retrieve_group_or_create_if_needed(sub_dir, nil, nil, project, true)
       prepare_main_group(new_group, sub_dir, project)
    end

  end
end

#remove_file_by_file_path(file_path, targets_name, project) ⇒ Void

Remove build file from target build phase

Parameters:

  • file_path (String)

    The path of the file

  • targets_name (String)

    Array of targets

  • project (Xcodeproj::Project)

    The target xcodeproj file

Returns:

  • (Void)


188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/api_gen_helper/group_generator.rb', line 188

def remove_file_by_file_path(file_path, targets_name, project)
  file_names = path_names_from_path(file_path)

  build_phases = nil

  if is_compile_source?(file_names.last)
    build_phases = build_phases_from_targets(targets_name, project)
  elsif is_bundle_resource?(file_names.last)
    build_phases = resources_build_phase_from_targets(targets_name, project)
  end

  remove_file_from_build_phases(file_path, build_phases)
end

#remove_file_from_build_phases(file_path, build_phases) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/api_gen_helper/group_generator.rb', line 202

def remove_file_from_build_phases(file_path, build_phases)
      return if build_phases.nil?

      build_phases.each do |build_phase|
        build_phase.files.each do |build_file|
          next if build_file.nil? || build_file.file_ref.nil?

          build_file_path = configure_file_ref_path(build_file.file_ref)

          if build_file_path == file_path
            build_phase.remove_build_file(build_file)
          end
        end
      end
end

#retrieve_group_or_create_if_needed(group_path, dir_path, file_group_path, project, create_group_if_not_exists) ⇒ PBXGroup

Finds or creates a group in a xcodeproj file with a given path

Parameters:

  • group_path (Pathname)

    The Xcode group path for module

  • dir_path (Pathname)

    The directory path for module

  • file_group_path (String)

    Directory path

  • project (Xcodeproj::Project)

    The working Xcode project file

  • create_group_if_not_exists (TrueClass or FalseClass)

    If true nonexistent group will be created

Returns:

  • (PBXGroup)


124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/api_gen_helper/group_generator.rb', line 124

def retrieve_group_or_create_if_needed(group_path, dir_path, file_group_path, project, create_group_if_not_exists)
  group_names = path_names_from_path(group_path)
  group_components_count = group_names.count
  group_names += path_names_from_path(file_group_path) if file_group_path

  final_group = project

  group_names.each_with_index do |group_name, index|
    next_group = final_group[group_name]

    unless next_group
      return nil unless create_group_if_not_exists

      if group_path != dir_path && index == group_components_count-1
        next_group = final_group.new_group(group_name, dir_path, :project)
      else
        next_group = final_group.new_group(group_name, group_name)
      end
    end

    final_group = next_group
  end

  final_group
end