Skip to content

Commit

Permalink
Merge pull request #187 from mrackwitz/minor_api_improvements
Browse files Browse the repository at this point in the history
Minor API Improvements
  • Loading branch information
kylef committed Oct 26, 2014
2 parents 55288fa + 8bc507d commit 5e06ee0
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 4 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@
[Xcodeproj#178](https://github.com/CocoaPods/Xcodeproj/pull/178)


###### Minor Enhancements

* `PBXCopyFilesBuildPhase`: Add a convenience method `symbol_dst_subfolder_spec`
to set the destination subfolder specification by a symbol.
[Marius Rackwitz](https://github.com/mrackwitz)
[Xcodeproj#187](https://github.com/CocoaPods/Xcodeproj/pull/187)

* `PBXNativeTarget`: Return newly created build files by `add_file_references`
and yield each one to allow direct modification of its settigs.
[Marius Rackwitz](https://github.com/mrackwitz)
[Xcodeproj#187](https://github.com/CocoaPods/Xcodeproj/pull/187)


###### Bug Fixes

* `PBXNativeTarget`: Fixed the creation of target dependencies, which refer
Expand Down
25 changes: 25 additions & 0 deletions lib/xcodeproj/project/object/build_phase.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,31 @@ class PBXCopyFilesBuildPhase < AbstractBuildPhase
# copied to.
#
attribute :dst_subfolder_spec, String, Constants::COPY_FILES_BUILD_PHASE_DESTINATIONS[:resources]

# Alias method for #dst_subfolder_spec=, which accepts symbol values
# instead of numeric string values.
#
# @param [Symbol] value
# one of `COPY_FILES_BUILD_PHASE_DESTINATIONS.keys`
#
# @raise [StandardError] if value is not a valid known key
#
def symbol_dst_subfolder_spec=(value)
numeric_value = Constants::COPY_FILES_BUILD_PHASE_DESTINATIONS[value]
raise "[Xcodeproj] Value checking error: got `#{value.inspect}` for" \
' attribute: dst_subfolder_spec' if numeric_value.nil?
self.dst_subfolder_spec = numeric_value
end

# Alias method for #dst_subfolder_spec, which returns symbol values
# instead of numeric string values.
#
# @return [Symbol]
#
def symbol_dst_subfolder_spec
key = Constants::COPY_FILES_BUILD_PHASE_DESTINATIONS.find { |_, num| num == dst_subfolder_spec }
key ? key.first : nil
end
end

#-----------------------------------------------------------------------#
Expand Down
10 changes: 8 additions & 2 deletions lib/xcodeproj/project/object/native_target.rb
Original file line number Diff line number Diff line change
Expand Up @@ -400,10 +400,12 @@ def symbol_type
# @param [Hash{String=>String}] compiler_flags
# the compiler flags for the source files.
#
# @return [void]
# @yield_param [PBXBuildFile] each created build file.
#
# @return [Array<PBXBuildFile>] the created build files.
#
def add_file_references(file_references, compiler_flags = {})
file_references.each do |file|
file_references.map do |file|
build_file = project.new(PBXBuildFile)
build_file.file_ref = file

Expand All @@ -417,6 +419,10 @@ def add_file_references(file_references, compiler_flags = {})
end
source_build_phase.files << build_file
end

yield build_file if block_given?

build_file
end
end

Expand Down
22 changes: 22 additions & 0 deletions spec/project/object/build_phase_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,28 @@ module ProjectSpecs
it 'defaults the dstSubfolderSpec to the resources folder' do
@build_phase.dst_subfolder_spec.should == '7'
end

describe '#symbol_dst_subfolder_spec' do
it 'returns the matching value' do
@build_phase.symbol_dst_subfolder_spec.should == :resources
end

it 'returns nil if the key is unknown' do
@build_phase.dst_subfolder_spec = '42'
@build_phase.symbol_dst_subfolder_spec.should.be.nil
end
end

describe '#symbol_dst_subfolder_spec=' do
it 'accepts valid values' do
@build_phase.symbol_dst_subfolder_spec = :frameworks
@build_phase.symbol_dst_subfolder_spec.should == :frameworks
end

it 'raises if an invalid value is set by #symbol_dst_subfolder_spec=' do
lambda { @build_phase.symbol_dst_subfolder_spec = :watch_faces }.should.raise?(StandardError)
end
end
end

describe PBXShellScriptBuildPhase do
Expand Down
20 changes: 18 additions & 2 deletions spec/project/object/native_target_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ module ProjectSpecs
end
end

it 'adds a list of sources file to the target to the source build phase' do
it 'adds a list of source files to the target to the source build phase' do
ref = @project.main_group.new_file('Class.m')
@target.add_file_references([ref], '-fobjc-arc')
build_files = @target.source_build_phase.files
Expand All @@ -491,7 +491,7 @@ module ProjectSpecs
build_files.first.settings.should == { 'COMPILER_FLAGS' => '-fobjc-arc' }
end

it 'adds a list of headers file to the target header build phases' do
it 'adds a list of header files to the target header build phases' do
ref = @project.main_group.new_file('Class.h')
@target.add_file_references([ref], '-fobjc-arc')
build_files = @target.headers_build_phase.files
Expand All @@ -500,6 +500,22 @@ module ProjectSpecs
build_files.first.settings.should.be.nil
end

it 'returns a list of header files to the target header build phases' do
ref = @project.main_group.new_file('Class.h')
new_build_files = @target.add_file_references([ref], '-fobjc-arc')
build_files = @target.headers_build_phase.files
new_build_files.should == build_files
end

it 'yields a list of header files to the target header build phases' do
ref = @project.main_group.new_file('Class.h')
build_files = @target.add_file_references([ref], '-fobjc-arc') do |build_file|
build_file.should.be.an.instance_of?(PBXBuildFile)
build_file.settings = { 'ATTRIBUTES' => ['Public'] }
end
build_files.first.settings.should == { 'ATTRIBUTES' => ['Public'] }
end

it 'adds a list of resources to the resources build phase' do
ref = @project.main_group.new_file('Image.png')
@target.add_resources([ref])
Expand Down

0 comments on commit 5e06ee0

Please sign in to comment.