api_gen_helper 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f43f43e1bcdf8094ab84ae9847a3543c699bf352
4
- data.tar.gz: 40fe845387ca8dda29daa773bf2c35d6f282f89b
3
+ metadata.gz: a1a829f9a8b59c8fb8cb99d686832bdd4a4a08e4
4
+ data.tar.gz: 6ddd5ac2534b5e1d4b42cd99e7637e09586371e5
5
5
  SHA512:
6
- metadata.gz: dee19b7cb9f47e890c0b9a32c57d7f529ca541f17ec6fdcdb5144f1ef6d40a7c7816c6eb07ae2d91c888b3a58ba4caabab58370a8ed8b8d8a7c3d347a8030fce
7
- data.tar.gz: 55b6d00d438cdd754b50d5e7f5ffb608f1d1aa44b8b123dfe322bb8b1ea995b3d0ea63cb82000d2a8ab287b49d24b9bc61c8857e09536de1271b3ef4e0b40fee
6
+ metadata.gz: 49313fb202e9897de18e908f4988f0e5d7eddb453e9199afbd56a7af9fbd7d0ad925c74c2439ac12cb2ca68fd8988896c3ea659ad383c45beb02bd218afda263
7
+ data.tar.gz: 122670ef0c3744d146d069e3664c958eb61abf2191f80c8054d5e6343af5c2e44b70fdcf5159c124ae06b2611b9cbe523d554bd92cdc27c4d20b8c050818883d
@@ -8,45 +8,30 @@ module ApiGenHelper
8
8
 
9
9
  def generate_group(project_path, group_path, target_name)
10
10
  project = obtain_project(project_path)
11
- target_name = target_name
12
- group_path = group_path
13
- group_name = URI(group_path).path.split('/').last
14
-
15
- group = project[group_path]
16
- if !group.nil?
17
- remove_build_file_recursivly(group)
18
- group.clear
19
- group.remove_from_project
20
- end
21
11
 
22
- main_group = create_group_from_directory(project, target_name, group_name, group_path)
12
+ clear_group(project, [target_name], group_path)
23
13
 
24
- prepare_main_group(main_group, group_path)
14
+ group = retrieve_group_or_create_if_needed(group_path, group_path, nil, project, true)
25
15
 
26
- project.targets.each do |proj_target|
27
- if proj_target.name == target_name
28
- added_references_recursivly_for_target(proj_target, main_group)
29
- end
30
- end
31
- project.save()
32
- end
16
+ prepare_main_group(group, group_path)
33
17
 
34
- def remove_build_file_recursivly(group)
35
- group.children.each do |child|
36
- if child.isa == "PBXFileReference"
37
- child.build_files.each do |build_file|
38
- build_file.remove_from_project
39
- end
40
- else
41
- remove_build_file_recursivly(child)
42
- end
18
+ project.targets.each do |target|
19
+ if target.name == target_name
20
+ added_references_recursivly_for_target(target, group)
21
+ end
43
22
  end
23
+
24
+ project.save()
44
25
  end
45
26
 
46
27
  def added_references_recursivly_for_target(target, group)
47
28
  group.children.each do |child|
48
29
  if child.isa == "PBXFileReference"
49
- target.add_file_references([child])
30
+ if is_bundle_resource?(child.name)
31
+ target.add_resources([child])
32
+ elsif is_compile_source?(child.name)
33
+ target.add_file_references([child])
34
+ end
50
35
  else
51
36
  added_references_recursivly_for_target(target, child)
52
37
  end
@@ -60,8 +45,8 @@ module ApiGenHelper
60
45
  Dir.entries(directory).each do |file_name|
61
46
  next if file_name =~ /^\./
62
47
  if File.file? File.join(directory, file_name)
63
- xcode_file_reference = create_file_reference(group, file_name)
64
- group<< xcode_file_reference
48
+ file_path = File.join(directory, file_name)
49
+ xcode_file_reference = group.new_reference(file_path)
65
50
  end
66
51
 
67
52
  if File.directory? File.join(directory, file_name)
@@ -72,17 +57,6 @@ module ApiGenHelper
72
57
  end
73
58
  end
74
59
 
75
- # Create group for specific target and project
76
- # @param project [Xcodeproj::Project] The target xcodeproj file
77
- # @param target [Xcodeproj::Project::Object::PBXNativeTarget] The target in xcodeproj file
78
- # @param name [String] String name for group
79
- # @param path [String] path for group
80
- #
81
- # @return [Xcodeproj::Project::Object::PBXGroup]
82
- def create_group_from_directory(project, target, name, path)
83
- group = project[target].new_group(name, path, :group)
84
- end
85
-
86
60
  # Open xcodoproj file
87
61
  # @param project_name [String] String path for xcodeproj file
88
62
  #
@@ -91,6 +65,188 @@ module ApiGenHelper
91
65
  Xcodeproj::Project.open(project_name)
92
66
  end
93
67
 
68
+ # Returns an AbstractTarget class for a given name
69
+ # @param target_name [String] The name of the target
70
+ # @param project [Xcodeproj::Project] The target xcodeproj file
71
+ #
72
+ # @return [Xcodeproj::AbstractTarget]
73
+ def obtain_target(target_name, project)
74
+ project.targets.each do |target|
75
+ return target if target.name == target_name
76
+ end
77
+
78
+ error_description = "Cannot find a target with name #{target_name} in Xcode project"
79
+ raise StandardError, error_description
80
+ end
81
+
82
+
83
+ # Adds a provided file to a specific Project and Target
84
+ # @param project [Xcodeproj::Project] The target xcodeproj file
85
+ # @param targets_name [String] Array of targets name
86
+ # @param group_path [Pathname] The Xcode group path for current file
87
+ # @param dir_path [Pathname] The directory path for current file
88
+ # @param file_group_path [String] Directory path
89
+ # @param file_name [String] Current file name
90
+ # @param file_is_resource [TrueClass or FalseClass] If true then file is resource
91
+ #
92
+ # @return [void]
93
+ def add_file_to_project_and_targets(project, targets_name, group_path, dir_path, file_group_path, file_name, file_is_resource = false)
94
+ file_path = dir_path
95
+ file_path = file_path.join(file_group_path) if file_group_path
96
+ file_path = file_path.join(file_name) if file_name
97
+
98
+ module_group = self.retrieve_group_or_create_if_needed(group_path, dir_path, file_group_path, project, true)
99
+ xcode_file = module_group.new_file(File.absolute_path(file_path))
100
+
101
+ targets_name.each do |target|
102
+ xcode_target = obtain_target(target, project)
103
+
104
+ if file_is_resource || self.is_bundle_resource?(file_name)
105
+ xcode_target.add_resources([xcode_file])
106
+ elsif self.is_compile_source?(file_name)
107
+ xcode_target.add_file_references([xcode_file])
108
+ end
109
+ end
110
+ end
111
+
112
+
113
+ # Finds or creates a group in a xcodeproj file with a given path
114
+ # @param group_path [Pathname] The Xcode group path for module
115
+ # @param dir_path [Pathname] The directory path for module
116
+ # @param file_group_path [String] Directory path
117
+ # @param project [Xcodeproj::Project] The working Xcode project file
118
+ # @param create_group_if_not_exists [TrueClass or FalseClass] If true nonexistent group will be created
119
+ #
120
+ # @return [PBXGroup]
121
+ def retrieve_group_or_create_if_needed(group_path, dir_path, file_group_path, project, create_group_if_not_exists)
122
+ group_names = path_names_from_path(group_path)
123
+ group_components_count = group_names.count
124
+ group_names += path_names_from_path(file_group_path) if file_group_path
125
+
126
+ final_group = project
127
+
128
+ group_names.each_with_index do |group_name, index|
129
+ next_group = final_group[group_name]
130
+
131
+ unless next_group
132
+ return nil unless create_group_if_not_exists
133
+
134
+ if group_path != dir_path && index == group_components_count-1
135
+ next_group = final_group.new_group(group_name, dir_path, :project)
136
+ else
137
+ next_group = final_group.new_group(group_name, group_name)
138
+ end
139
+ end
140
+
141
+ final_group = next_group
142
+ end
143
+
144
+ final_group
145
+ end
146
+
147
+ # Get all files path from group path
148
+ # @param module_group [PBXGroup] The module group
149
+ # @param project [Xcodeproj::Project] The target xcodeproj file
150
+ #
151
+ # @return [[String]]
152
+ def files_path_from_group(module_group, _project)
153
+ files_path = []
154
+
155
+ module_group.recursive_children.each do |file_ref|
156
+ if file_ref.isa == 'PBXFileReference'
157
+ file_ref_path = configure_file_ref_path(file_ref)
158
+ files_path.push(file_ref_path)
159
+ end
160
+ end
161
+
162
+ files_path
163
+ end
164
+
165
+ def clear_group(project, targets_name, group_path)
166
+ module_group = retrieve_group_or_create_if_needed(group_path, nil, nil, project, false)
167
+ return unless module_group
168
+
169
+ files_path = files_path_from_group(module_group, project)
170
+ return unless files_path
171
+
172
+ files_path.each do |file_path|
173
+ remove_file_by_file_path(file_path, targets_name, project)
174
+ end
175
+
176
+ module_group.clear
177
+ end
178
+
179
+ # Remove build file from target build phase
180
+ # @param file_path [String] The path of the file
181
+ # @param targets_name [String] Array of targets
182
+ # @param project [Xcodeproj::Project] The target xcodeproj file
183
+ #
184
+ # @return [Void]
185
+ def remove_file_by_file_path(file_path, targets_name, project)
186
+ file_names = path_names_from_path(file_path)
187
+
188
+ build_phases = nil
189
+
190
+ if is_compile_source?(file_names.last)
191
+ build_phases = build_phases_from_targets(targets_name, project)
192
+ elsif is_bundle_resource?(file_names.last)
193
+ build_phases = resources_build_phase_from_targets(targets_name, project)
194
+ end
195
+
196
+ remove_file_from_build_phases(file_path, build_phases)
197
+ end
198
+
199
+ def remove_file_from_build_phases(file_path, build_phases)
200
+ return if build_phases.nil?
201
+
202
+ build_phases.each do |build_phase|
203
+ build_phase.files.each do |build_file|
204
+ next if build_file.nil? || build_file.file_ref.nil?
205
+
206
+ build_file_path = configure_file_ref_path(build_file.file_ref)
207
+
208
+ if build_file_path == file_path
209
+ build_phase.remove_build_file(build_file)
210
+ end
211
+ end
212
+ end
213
+ end
214
+
215
+ # Find and return target build phases
216
+ # @param targets_name [String] Array of targets
217
+ # @param project [Xcodeproj::Project] The target xcodeproj file
218
+ #
219
+ # @return [[PBXSourcesBuildPhase]]
220
+ def build_phases_from_targets(targets_name, project)
221
+ build_phases = []
222
+
223
+ targets_name.each do |target_name|
224
+ xcode_target = obtain_target(target_name, project)
225
+ xcode_target.build_phases.each do |build_phase|
226
+ if build_phase.isa == 'PBXSourcesBuildPhase'
227
+ build_phases.push(build_phase)
228
+ end
229
+ end
230
+ end
231
+
232
+ build_phases
233
+ end
234
+
235
+ # Get configure file full path
236
+ # @param file_ref [PBXFileReference] Build file
237
+ #
238
+ # @return [String]
239
+ def configure_file_ref_path(file_ref)
240
+ build_file_ref_path = file_ref.hierarchy_path.to_s
241
+ build_file_ref_path[0] = ''
242
+
243
+ build_file_ref_path
244
+ end
245
+
246
+ def path_names_from_path(path)
247
+ path.to_s.split('/')
248
+ end
249
+
94
250
  #Create file reference for specific group
95
251
  # @param group [Xcodeproj::Project::Object::PBXGroup] - group where create file reference
96
252
  # @param path [String] path for file
@@ -1,3 +1,3 @@
1
1
  module ApiGenHelper
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api_gen_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nikolay Aksenov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-07-28 00:00:00.000000000 Z
11
+ date: 2017-07-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: xcodeproj