Skip to content

Commit

Permalink
Merge pull request #101 from pereckerdal/reference_for_path
Browse files Browse the repository at this point in the history
PBXNativeTarget.add_dependency support for subproject targets
  • Loading branch information
alloy committed Oct 17, 2013
2 parents 137c227 + b3ead03 commit c2e05a6
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 2 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
## Master

###### Enhancements

* [PBXNativeTarget] Add support for subproject targets in `#add_dependency`
[Per Eckerdal](https://github.com/pereckerdal)
[#101](https://github.com/CocoaPods/Xcodeproj/pull/101)
* [Project] Add `#reference_for_path` for retrieving a file reference for a
given absolute path.
[Per Eckerdal](https://github.com/pereckerdal)
[#101](https://github.com/CocoaPods/Xcodeproj/pull/101)


## 0.13.1

###### Bug Fixes
Expand Down
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
12 changes: 10 additions & 2 deletions lib/xcodeproj/project/object/native_target.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,22 @@ def shell_script_build_phases
#
# @param [AbstractTarget] target
# the target which should be added to the dependencies list of
# the receiver.
# the receiver. The target may be a target of this target's
# project or of a subproject of this project. Note that the
# subproject must already be added to this target's project.
#
# @return [void]
#
def add_dependency(target)
unless dependencies.map(&:target).include?(target)
container_proxy = project.new(Xcodeproj::Project::PBXContainerItemProxy)
container_proxy.container_portal = project.root_object.uuid
if target.project == project
container_proxy.container_portal = project.root_object.uuid
else
subproject_reference = project.reference_for_path(target.project.path)
raise ArgumentError, "add_dependency got target that belongs to a project is not this project and is not a subproject of this project" unless subproject_reference
container_proxy.container_portal = subproject_reference.uuid
end
container_proxy.proxy_type = '1'
container_proxy.remote_global_id_string = target.uuid
container_proxy.remote_info = target.name
Expand Down
22 changes: 22 additions & 0 deletions spec/project/object/native_target_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,28 @@ module ProjectSpecs
container_proxy.remote_info.should == dependency_target.name
end

it "adds a dependency on a target in a subproject" do
subproject = Xcodeproj::Project.new('/other_project_dir/OtherProject.xcodeproj')
dependency_target = subproject.new_target(:static_library, 'Pods-SMCalloutView', :ios)
subproject_file_reference = @project.main_group.new_file(subproject.path)
@sut.add_dependency(dependency_target)

@sut.dependencies.count.should == 1
target_dependency = @sut.dependencies.first
target_dependency.target.should == dependency_target

target_dependency.target_proxy.container_portal.should == subproject_file_reference.uuid
end

it "doesn't add a dependency on a target in an unknown project" do
unknown_project = Xcodeproj::Project.new('/other_project_dir/OtherProject.xcodeproj')
dependency_target = unknown_project.new_target(:static_library, 'Pods-SMCalloutView', :ios)

should.raise ArgumentError do
@sut.add_dependency(dependency_target)
end.message.should.match /not this project/
end

it "doesn't duplicate dependencies" do
dependency_target = @project.new_target(:static_library, 'Pods-SMCalloutView', :ios)
@sut.add_dependency(dependency_target)
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 c2e05a6

Please sign in to comment.