Skip to content

Commit

Permalink
Add Project.reference_for_path
Browse files Browse the repository at this point in the history
It mirrors the functionality of the method with
the same name in CocoaPod's Pod::Project class
(which after this patch will override it). The
differences are 1) CocoaPod's implementation is
constant time but this is linear wrt the number
of objects, 2) CocoaPod's implementation does not
work when you load a previously existing file.
  • Loading branch information
Per Eckerdal committed Oct 17, 2013
1 parent 137c227 commit cda5026
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/xcodeproj/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,24 @@ def files
objects.select { |obj| obj.class == PBXFileReference }
end

# Returns the file reference for the given absolute path.
#
# @param [#to_s] absolute_path
# The absolute path of the file whose reference is needed.
#
# @return [PBXFileReference] The file reference.
# @return [Nil] If no file reference could be found.
#
def reference_for_path(absolute_path)
unless Pathname.new(absolute_path).absolute?
raise ArgumentError, "Paths must be absolute #{absolute_path}"
end

objects.find do |child|
child.isa == 'PBXFileReference' && child.real_path == absolute_path
end
end

# @return [ObjectList<PBXNativeTarget>] A list of all the targets in the
# project.
#
Expand Down
13 changes: 13 additions & 0 deletions spec/project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,19 @@ module ProjectSpecs
@project.files.should.include?(f)
end

it "finds file references by absolute path" do
file_path = 'Classes/Test.h'
@project.reference_for_path(@project.path.dirname + file_path).should.be.nil
file = @project.new_file(file_path)
@project.reference_for_path(@project.path.dirname + file_path).should == file
end

it "does not find references by relative path" do
should.raise ArgumentError do
@project.reference_for_path(@project.path.basename)
end.message.should.match /must be absolute/
end

it "returns the targets" do
target = @project.new_target(:static_library, 'Pods', :ios).product_reference
@project.products.should.include?(target)
Expand Down

0 comments on commit cda5026

Please sign in to comment.