Permalink
Browse files

Merge pull request #16 from CocoaPods/CP-ticket-291

[NativeTarget] #add_source_file -> #add_source_files.
  • Loading branch information...
2 parents 633560a + 728a3c7 commit f0d455bcd839e8fdada2e117681ac93b296659b1 @fabiopelosin fabiopelosin committed Jun 25, 2012
Showing with 57 additions and 21 deletions.
  1. +1 −1 Rakefile
  2. +48 −16 lib/xcodeproj/project/object/native_target.rb
  3. +8 −4 spec/project_spec.rb
View
@@ -94,7 +94,7 @@ namespace :gem do
desc "Install a gem version of the current code"
task :install => :build do
require File.expand_path('../lib/xcodeproj', __FILE__)
- sh "sudo gem install xcodeproj-#{Xcodeproj::VERSION}.gem"
+ sh "gem install xcodeproj-#{Xcodeproj::VERSION}.gem"
end
end
@@ -102,25 +102,57 @@ def shell_script_build_phases
build_phases.list_by_class(PBXShellScriptBuildPhase)
end
- # Finds an existing file reference or creates a new one.
- def add_source_file(path, copy_header_phase = nil, compiler_flags = nil)
- file = @project.files.find { |file| file.path == path.to_s } || @project.files.new('path' => path.to_s)
- build_file = file.build_files.new
- if path.extname == '.h'
- build_file.settings = { 'ATTRIBUTES' => ["Public"] }
- # Working around a bug in Xcode 4.2 betas, remove this once the Xcode bug is fixed:
- # https://github.com/alloy/cocoapods/issues/13
- #phase = copy_header_phase || headers_build_phases.first
- phase = copy_header_phase || copy_files_build_phases.first
- phase.build_files << build_file
- else
- build_file.settings = { 'COMPILER_FLAGS' => compiler_flags } if compiler_flags
- source_build_phases.first.build_files << build_file
+ # Adds source files to the target.
+ #
+ # @note
+ # It finds an existing file reference or creates a new one.
+ #
+ # @param source_file_descriptions [Array<SourceFileDescription>] The
+ # description of the source files to add.
+ #
+ # @return [Array<PBXFileReference>]
+ #
+ def add_source_files(source_file_descriptions)
+ # Cache the files for performance.
+ files = @project.files.to_a
+ new_files = []
+ source_file_descriptions.each do |source_file_description|
+ path = source_file_description.path
+ copy_header_phase = source_file_description.copy_header_phase
+ compiler_flags = source_file_description.compiler_flags
+
+ file = (files + new_files).find { |file| file.path == path.to_s } || @project.files.new('path' => path.to_s)
+ build_file = file.build_files.new
+ if path.extname == '.h'
+ build_file.settings = { 'ATTRIBUTES' => ["Public"] }
+ # Working around a bug in Xcode 4.2 betas, remove this once the Xcode bug is fixed:
+ # https://github.com/alloy/cocoapods/issues/13
+ #phase = copy_header_phase || headers_build_phases.first
+ phase = copy_header_phase || copy_files_build_phases.first
+ phase.build_files << build_file
+ else
+ build_file.settings = { 'COMPILER_FLAGS' => compiler_flags } if compiler_flags && !compiler_flags.empty?
+ source_build_phases.first.build_files << build_file
+ end
+ new_files << file
end
- file
+ new_files
end
- end
+ # Struct representing the description needed to add a source file to
+ # the target.
+ #
+ # @!attribute path
+ # @return [Pathname] The path of the file.
+ #
+ # @!attribute compiler_flags
+ # @return [String] Any compiler flag.
+ #
+ # @!attribute copy_header_phase
+ # @return [PBXCopyFilesBuildPhase].
+ #
+ SourceFileDescription = Struct.new(:path, :compiler_flags, :copy_header_phase)
+ end
end
end
end
View
@@ -84,7 +84,8 @@ module ProjectSpecs
it "adds an `m' or `c' file to the `sources build' phase list" do
%w{ m mm c cpp }.each do |ext|
path = Pathname.new("path/to/file.#{ext}")
- file = @target.add_source_file(path)
+ desc = Xcodeproj::Project::PBXNativeTarget::SourceFileDescription.new(path, nil, nil)
+ file = @target.add_source_files([desc]).first
# ensure that it was added to all objects
file = @project.objects[file.uuid]
@@ -100,7 +101,8 @@ module ProjectSpecs
build_file_uuids = []
%w{ m mm c cpp }.each do |ext|
path = Pathname.new("path/to/file.#{ext}")
- file = @project.targets.first.add_source_file(path, nil, '-fno-obj-arc')
+ desc = Xcodeproj::Project::PBXNativeTarget::SourceFileDescription.new(path, '-fno-obj-arc', nil)
+ file = @project.targets.first.add_source_files([desc]).first
find_object({
'isa' => 'PBXBuildFile',
'fileRef' => file.uuid,
@@ -113,7 +115,8 @@ module ProjectSpecs
#it "adds a `h' file as a build file and adds it to the `headers build' phase list" do
it "adds a `h' file as a build file and adds it to the `copy header files' build phase list" do
path = Pathname.new("path/to/file.h")
- file = @target.add_source_file(path)
+ desc = Xcodeproj::Project::PBXNativeTarget::SourceFileDescription.new(path, nil, nil)
+ file = @target.add_source_files([desc]).first
# ensure that it was added to all objects
file = @project.objects[file.uuid]
@@ -135,7 +138,8 @@ module ProjectSpecs
it "returns all source files" do
group = @project.groups.new('name' => 'SomeGroup')
files = [Pathname.new('/some/file.h'), Pathname.new('/some/file.m')]
- files.each { |file| group << @target.add_source_file(file) }
+ descriptions = files.map { |p| Xcodeproj::Project::PBXNativeTarget::SourceFileDescription.new(p, nil, nil) }
+ @target.add_source_files(descriptions).each { |new_file| group << new_file }
group.source_files.map(&:pathname).sort.should == files.sort
end
end

0 comments on commit f0d455b

Please sign in to comment.