xcodeproj 0.9.0 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -32,6 +32,18 @@ module Xcodeproj
32
32
 
33
33
  # @!group Helpers
34
34
 
35
+ # Returns the build configuration with the given name.
36
+ #
37
+ # @param [String] name
38
+ # The name of the build configuration.
39
+ #
40
+ # @return [XCBuildConfiguration] The build configuration.
41
+ # @return [Nil] If not build configuration with the given name is found.
42
+ #
43
+ def [](name)
44
+ build_configurations.find { |bc| bc.name == name }
45
+ end
46
+
35
47
  # Returns the build settings of the build configuration with
36
48
  # the given name.
37
49
  #
@@ -41,8 +53,9 @@ module Xcodeproj
41
53
  # @return [Hash {String=>String}] the build settings
42
54
  #
43
55
  def build_settings(build_configuration_name)
44
- config = build_configurations.find { |bc| bc.name == build_configuration_name }
45
- config.build_settings if config
56
+ if config = self[build_configuration_name]
57
+ config.build_settings
58
+ end
46
59
  end
47
60
 
48
61
  #---------------------------------------------------------------------#
@@ -1,3 +1,5 @@
1
+ require 'xcodeproj/project/object/helpers/groupable_helper'
2
+
1
3
  module Xcodeproj
2
4
  class Project
3
5
  module Object
@@ -16,10 +18,17 @@ module Xcodeproj
16
18
  #
17
19
  attribute :path, String
18
20
 
19
- # @return [String] the source tree to which the file is relative.
21
+ # @return [String] the directory to which the path is relative.
20
22
  #
21
- # @note Common values are `SOURCE_ROOT`, `SDKROOT` and
22
- # `BUILT_PRODUCTS_DIR`
23
+ # @note The accepted values are:
24
+ # - `<absolute>` for absolute paths
25
+ # - `<group>` for paths relative to the group
26
+ # - `SOURCE_ROOT` for paths relative to the project
27
+ # - `DEVELOPER_DIR` for paths relative to the developer
28
+ # directory.
29
+ # - `BUILT_PRODUCTS_DIR` for paths relative to the build
30
+ # products directory.
31
+ # - `SDKROOT` for paths relative to the SDK directory.
23
32
  #
24
33
  attribute :source_tree, String, 'SOURCE_ROOT'
25
34
 
@@ -92,7 +101,6 @@ module Xcodeproj
92
101
  #
93
102
  attribute :wraps_lines, String
94
103
 
95
-
96
104
  # @return [String] Apparently whether Xcode should add, if needed, a
97
105
  # new line feed before saving the file.
98
106
  #
@@ -117,19 +125,37 @@ module Xcodeproj
117
125
  # needed.
118
126
  #
119
127
  def display_name
120
- name || File.basename(path)
128
+ name || (File.basename(path) if path)
129
+ end
130
+
131
+ # @return [PBXGroup, PBXProject] the parent of the file.
132
+ #
133
+ def parent
134
+ GroupableHelper.parent(self)
135
+ end
136
+
137
+ # @return [String] A representation of the reference hierarchy.
138
+ #
139
+ def hierarchy_path
140
+ GroupableHelper.hierarchy_path(self)
121
141
  end
122
142
 
123
- # @return [Pathname] the path of the file.
143
+ # Moves the reference to a new parent.
144
+ #
145
+ # @param [PBXGroup] new_parent
146
+ # The new parent.
124
147
  #
125
- def pathname
126
- Pathname.new(path)
148
+ # @return [void]
149
+ #
150
+ def move(new_parent)
151
+ GroupableHelper.move(self, new_parent)
127
152
  end
128
153
 
129
- # @return [PBXGroup] the group that contains the current file reference.
154
+ # @return [Pathname] the absolute path of the file resolving the
155
+ # source tree.
130
156
  #
131
- def group
132
- referrers.select { |r| r.class == PBXGroup }.first
157
+ def real_path
158
+ GroupableHelper.real_path(self)
133
159
  end
134
160
 
135
161
  # @return [Array<PBXBuildFile>] the build files associated with the
@@ -141,10 +167,30 @@ module Xcodeproj
141
167
 
142
168
  # Sets the last known file type according to the extension of the path.
143
169
  #
144
- # @return [String] the computed file type.
170
+ # @return [void]
171
+ #
172
+ def set_last_known_file_type(type = nil)
173
+ if type
174
+ self.last_known_file_type = type
175
+ elsif path
176
+ extension = Pathname(path).extname[1..-1]
177
+ self.last_known_file_type = Constants::FILE_TYPES_BY_EXTENSION[extension]
178
+ end
179
+ end
180
+
181
+ # Sets the explicit file type according to the extension of the path,
182
+ # and clears the last known file type.
183
+ #
184
+ # @return [void]
145
185
  #
146
- def update_last_known_file_type
147
- self.last_known_file_type = Constants::FILE_TYPES_BY_EXTENSION[pathname.extname[1..-1]]
186
+ def set_explicit_file_type(type = nil)
187
+ self.last_known_file_type = nil
188
+ if type
189
+ self.explicit_file_type = type
190
+ elsif path
191
+ extension = Pathname(path).extname[1..-1]
192
+ self.explicit_file_type = Constants::FILE_TYPES_BY_EXTENSION[extension]
193
+ end
148
194
  end
149
195
 
150
196
  # Checks whether the reference is a proxy.
@@ -1,3 +1,6 @@
1
+ require 'xcodeproj/project/object/helpers/groupable_helper'
2
+ require 'xcodeproj/project/object/helpers/file_references_factory'
3
+
1
4
  module Xcodeproj
2
5
  class Project
3
6
  module Object
@@ -14,9 +17,17 @@ module Xcodeproj
14
17
  #
15
18
  has_many :children, [PBXGroup, PBXFileReference, PBXReferenceProxy]
16
19
 
17
- # @return [String] the source tree to which this group is relative.
20
+ # @return [String] the directory to which the path is relative.
18
21
  #
19
- # @note Usually is group <group>.
22
+ # @note The accepted values are:
23
+ # - `<absolute>` for absolute paths
24
+ # - `<group>` for paths relative to the group
25
+ # - `SOURCE_ROOT` for paths relative to the project
26
+ # - `DEVELOPER_DIR` for paths relative to the developer
27
+ # directory.
28
+ # - `BUILT_PRODUCTS_DIR` for paths relative to the build
29
+ # products directory.
30
+ # - `SDKROOT` for paths relative to the SDK directory.
20
31
  #
21
32
  attribute :source_tree, String, '<group>'
22
33
 
@@ -86,6 +97,36 @@ module Xcodeproj
86
97
  end
87
98
  end
88
99
 
100
+ # @return [PBXGroup, PBXProject] The parent of the group.
101
+ #
102
+ def parent
103
+ GroupableHelper.parent(self)
104
+ end
105
+
106
+ # @return [String] A representation of the group hierarchy.
107
+ #
108
+ def hierarchy_path
109
+ GroupableHelper.hierarchy_path(self)
110
+ end
111
+
112
+ # Moves the group to a new parent.
113
+ #
114
+ # @param [PBXGroup] new_parent
115
+ # The new parent.
116
+ #
117
+ # @return [void]
118
+ #
119
+ def move(new_parent)
120
+ GroupableHelper.move(self, new_parent)
121
+ end
122
+
123
+ # @return [Pathname] the absolute path of the group resolving the
124
+ # source tree.
125
+ #
126
+ def real_path
127
+ GroupableHelper.real_path(self)
128
+ end
129
+
89
130
  # @return [Array<PBXFileReference>] the files references in the group
90
131
  # children.
91
132
  #
@@ -99,6 +140,13 @@ module Xcodeproj
99
140
  children.select { |obj| obj.class == PBXGroup }
100
141
  end
101
142
 
143
+ # @return [Array<XCVersionGroup>] the version groups in the group
144
+ # children.
145
+ #
146
+ def version_groups
147
+ children.select { |obj| obj.class == XCVersionGroup }
148
+ end
149
+
102
150
  # @return [Array<PBXGroup,PBXFileReference,PBXReferenceProxy>] the
103
151
  # recursive children of the group.
104
152
  #
@@ -111,198 +159,74 @@ module Xcodeproj
111
159
  result
112
160
  end
113
161
 
114
- # @return [Array<XCVersionGroup>] the version groups in the group
115
- # children.
162
+ # @return [Bool] Whether the group is empty.
116
163
  #
117
- def version_groups
118
- children.select { |obj| obj.class == XCVersionGroup }
164
+ def empty?
165
+ children.count.zero?
119
166
  end
120
167
 
121
- # Creates a new file reference with the given path and adds it to the
122
- # group or to an optional subpath.
123
- #
124
- # @note The subpath is created if needed, similar to the UNIX command
125
- # `mkdir -p`
126
- #
127
- # @note To closely match the Xcode behaviour the name attribute of
128
- # the file reference is set only if the path of the file is not
129
- # equal to the path of the group.
168
+ # Creates a new reference with the given path and adds it to the
169
+ # group. The reference is configured according to the extension
170
+ # of the path.
130
171
  #
131
172
  # @param [#to_s] path
132
- # the file system path of the file.
133
- #
134
- # @param [String] sub_group_path
135
- # an optional subgroup path indicating the groups separated by
136
- # a `/`.
137
- #
138
- # @return [PBXFileReference] the new file reference.
139
- #
140
- def new_file(path, sub_group_path = nil)
141
- extname = File.extname(path)
142
- case
143
- when extname == '.framework' then new_framework(path, sub_group_path)
144
- when extname == '.xcdatamodeld' then new_xcdatamodeld(path, sub_group_path)
145
- else new_file_reference(path, sub_group_path)
146
- end
147
- end
148
-
149
- # Creates a new file reference with the given path and adds it to the
150
- # group or to an optional subpath.
151
- #
152
- # @note @see #new_file
173
+ # The, preferably absolute, path of the reference.
153
174
  #
154
- # @param @see #new_file
175
+ # @param [Symbol] source_tree
176
+ # The source tree key to use to configure the path (@see
177
+ # GroupableHelper::SOURCE_TREES_BY_KEY).
155
178
  #
156
- # @return [PBXFileReference] the new file reference.
179
+ # @return [PBXFileReference, XCVersionGroup] The new reference.
157
180
  #
158
- def new_file_reference(path, sub_group_path = nil)
159
- ref = project.new(PBXFileReference)
160
- ref.path = path.to_s
161
- ref.update_last_known_file_type
162
-
163
- parent_group = find_subpath(sub_group_path, true)
164
- parent_group.children << ref
165
- set_file_refenrece_name_if_needed(ref, parent_group)
166
- ref
181
+ def new_reference(path, source_tree = :group)
182
+ FileReferencesFactory.new_reference(self, path, source_tree)
167
183
  end
184
+ alias :new_file :new_reference
168
185
 
169
- # Sets the name of a reference if needed, to match Xcode behaviour.
170
- #
171
- # @param [PBXFileReference, XCVersionGroup] ref
172
- # the reference which needs the name optionally set.
173
- #
174
- # @return [void]
175
- #
176
- def set_file_refenrece_name_if_needed(ref, parent_group)
177
- same_path_of_group = (parent_group.path == Pathname(ref.path).dirname.to_s)
178
- same_path_project = (Pathname(ref.path).dirname.to_s == '.' && parent_group.path.nil?)
179
- unless same_path_of_group || same_path_project
180
- ref.name = Pathname(ref.path).basename.to_s
181
- end
182
- end
183
-
184
- # Creates a new file reference to a framework bundle.
185
- #
186
- # @note @see #new_file
186
+ # Creates a file reference to a static library and adds it to the
187
+ # group.
187
188
  #
188
- # @param @see #new_file
189
+ # @param [#to_s] product_name
190
+ # The name of the static library.
189
191
  #
190
- # @return [PBXFileReference] the new file reference.
192
+ # @return [PBXFileReference] The new file reference.
191
193
  #
192
- def new_framework(path, sub_group_path = nil)
193
- ref = new_file_reference(path, sub_group_path = nil)
194
- ref.include_in_index = nil
195
- ref
194
+ def new_static_library(product_name)
195
+ FileReferencesFactory.new_static_library(self, product_name)
196
196
  end
197
197
 
198
- # Creates a new version group reference to an xcdatamodeled adding the
199
- # xcdatamodel files included in the wrapper as chidren file references.
200
- #
201
- # @note To match Xcode behaviour the last xcdatamodel according to its
202
- # path is set as the current version.
203
- #
204
- # @note @see #new_file
198
+ # Creates a file reference to a new bundle.
205
199
  #
206
- # @param @see #new_file
200
+ # @param [#to_s] product_name
201
+ # The name of the bundle.
207
202
  #
208
- # @return [XCVersionGroup] the new reference.
203
+ # @return [PBXFileReference] The new file reference.
209
204
  #
210
- def new_xcdatamodeld(path, sub_group_path = nil)
211
- path = Pathname.new(path)
212
- ref = project.new(XCVersionGroup)
213
- ref.path = path.to_s
214
- ref.source_tree = '<group>'
215
- ref.version_group_type = 'wrapper.xcdatamodel'
216
-
217
- last_child_ref = nil
218
- if path.exist?
219
- path.children.each do |child_path|
220
- if File.extname(child_path) == '.xcdatamodel'
221
- child_ref = ref.new_file_reference(child_path)
222
- child_ref.source_tree = '<group>'
223
- last_child_ref = child_ref
224
- end
225
- end
226
- ref.current_version = last_child_ref
227
- end
228
-
229
- parent_group = find_subpath(sub_group_path, true)
230
- parent_group.children << ref
231
- set_file_refenrece_name_if_needed(ref, parent_group)
232
- ref
205
+ def new_bundle(product_name)
206
+ FileReferencesFactory.new_bundle(self, product_name)
233
207
  end
234
208
 
235
- # Creates a new group with the given name and adds it to the children
236
- # of the group.
209
+ # Creates a file reference to a new bundle and adds it to the group.
237
210
  #
238
- # @note @see new_file
211
+ # @note @see new_reference
239
212
  #
240
213
  # @param [#to_s] name
241
214
  # the name of the new group.
242
215
  #
243
- # @param [String] sub_group_path @see new_file
244
- #
245
216
  # @return [PBXGroup] the new group.
246
217
  #
247
- def new_group(name, sub_group_path = nil)
218
+ def new_group(name, path = nil, source_tree = :group)
248
219
  group = project.new(PBXGroup)
220
+ children << group
249
221
  group.name = name
250
-
251
- target = find_subpath(sub_group_path, true)
252
- target.children << group
222
+ if path
223
+ GroupableHelper.set_path_with_source_tree(group, path, source_tree)
224
+ else
225
+ group.source_tree = '<group>'
226
+ end
253
227
  group
254
228
  end
255
229
 
256
- # Creates a file reference to a static library and adds it to the
257
- # children of the group.
258
- #
259
- # @note @see new_file
260
- #
261
- # @param [#to_s] product_name
262
- # the name of the new static library.
263
- #
264
- # @param [String] sub_group_path @see new_file
265
- #
266
- # @return [PBXFileReference] the new file reference.
267
- #
268
- def new_static_library(product_name, sub_group_path = nil)
269
- file = new_file("lib#{product_name}.a", sub_group_path)
270
- file.include_in_index = '0'
271
- file.source_tree = 'BUILT_PRODUCTS_DIR'
272
- file.explicit_file_type = file.last_known_file_type
273
- file.last_known_file_type = nil
274
- file
275
- end
276
-
277
- # Creates a file reference to a new bundle.
278
- #
279
- # @param [#to_s] product_name
280
- # the name of the bundle.
281
- #
282
- # @return [PBXFileReference] the new file reference.
283
- #
284
- def new_bundle(product_name)
285
- file = new_file("#{product_name}.bundle")
286
- file.explicit_file_type = 'wrapper.cfbundle'
287
- file.include_in_index = '0'
288
- file.source_tree = 'BUILT_PRODUCTS_DIR'
289
- file.last_known_file_type = nil
290
- file
291
- end
292
-
293
- # Creates a new group to represent a `xcdatamodel` file.
294
- #
295
- # @return [XCVersionGroup] The new group.
296
- #
297
- def new_xcdatamodel_group(xcdatamodel_path)
298
- g = @project.new(XCVersionGroup)
299
- g.path = xcdatamodel_path
300
- g.version_group_type = 'wrapper.xcdatamodel'
301
- file = g.new_file(xcdatamodel_path.sub(/xcdatamodeld$/, 'xcdatamodel'))
302
- g.current_version = file
303
- g
304
- end
305
-
306
230
  # Traverses the children groups and finds the group with the given
307
231
  # path, if exists.
308
232
  #
@@ -314,12 +238,10 @@ module Xcodeproj
314
238
 
315
239
  # Removes children files and groups under this group.
316
240
  #
241
+ # @TODO: remove from project should suffice.
242
+ #
317
243
  def remove_children_recursively
318
- groups.each do |g|
319
- g.remove_children_recursively
320
- g.remove_from_project
321
- end
322
- files.each { |f| f.remove_from_project }
244
+ children.objects.each(&:remove_from_project)
323
245
  end
324
246
 
325
247
  # Traverses the children groups and finds the children with the given
@@ -379,7 +301,7 @@ module Xcodeproj
379
301
  #
380
302
  # @return [void]
381
303
  #
382
- def sort_by_type!
304
+ def sort_by_type
383
305
  children.sort! do |x, y|
384
306
  if x.is_a?(PBXGroup) && y.is_a?(PBXFileReference)
385
307
  -1
@@ -390,6 +312,15 @@ module Xcodeproj
390
312
  end
391
313
  end
392
314
  end
315
+
316
+ # Sorts the group by type recursively.
317
+ #
318
+ # @return [void]
319
+ #
320
+ def recursively_sort_by_type
321
+ groups.each { |group| group.recursively_sort_by_type }
322
+ sort_by_type
323
+ end
393
324
  end
394
325
 
395
326
  #-----------------------------------------------------------------------#
@@ -403,6 +334,7 @@ module Xcodeproj
403
334
  # @return [String] the file type guessed by Xcode.
404
335
  #
405
336
  attribute :last_known_file_type, String
337
+
406
338
  end
407
339
 
408
340
  #-----------------------------------------------------------------------#