Skip to content

Commit

Permalink
Template maintains its project
Browse files Browse the repository at this point in the history
  • Loading branch information
nolanw committed Nov 5, 2011
1 parent 5129dcd commit e08a67a
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 67 deletions.
35 changes: 15 additions & 20 deletions lib/cocoapods/installer.rb
Expand Up @@ -133,31 +133,26 @@ def initialize(podfile)
end

def template
@template ||= ProjectTemplate.new(@podfile.platform)
end

def xcodeproj
unless @xcodeproj
@xcodeproj = Xcode::Project.new(template.xcodeproj_path)
# First we need to resolve dependencies across *all* targets, so that the
# same correct versions of pods are being used for all targets. This
# happens when we call `build_specifications'.
build_specifications.each do |spec|
# Add all source files to the project grouped by pod
group = xcodeproj.add_pod_group(spec.name)
spec.expanded_source_files.each do |path|
group.children.new('path' => path.to_s)
end
return @template if @template
@template = ProjectTemplate.new(@podfile.platform)
# First we need to resolve dependencies across *all* targets, so that the
# same correct versions of pods are being used for all targets. This
# happens when we call `build_specifications'.
build_specifications.each do |spec|
# Add all source files to the project grouped by pod
group = @template.project.add_pod_group(spec.name)
spec.expanded_source_files.each do |path|
group.children.new('path' => path.to_s)
end
# Add a group to hold all the target support files
xcodeproj.main_group.groups.new('name' => 'Targets Support Files')
end
@xcodeproj
# Add a group to hold all the target support files
@template.project.main_group.groups.new('name' => 'Targets Support Files')
@template
end

def targets
@targets ||= @podfile.targets.values.map do |target_definition|
Target.new(@podfile, xcodeproj, target_definition)
Target.new(@podfile, template.project, target_definition)
end
end

Expand All @@ -176,7 +171,7 @@ def install!
end
pbxproj = File.join(root, 'Pods.xcodeproj')
puts " * Writing Xcode project file to `#{pbxproj}'" if config.verbose?
xcodeproj.save_as(pbxproj)
template.project.save_as(pbxproj)

# Post install hooks run last!
targets.each do |target|
Expand Down
8 changes: 4 additions & 4 deletions lib/cocoapods/project_template.rb
Expand Up @@ -4,8 +4,11 @@ module Pod
class ProjectTemplate
def initialize(platform)
@platform = platform
@project = Xcode::Project.new(File.join(path, 'Pods.xcodeproj'))
end

attr_reader :project

# TODO this is a workaround for an issue with MacRuby with compiled files
# that makes the use of __FILE__ impossible.
#
Expand Down Expand Up @@ -34,12 +37,9 @@ def path
end
end

def xcodeproj_path
@xcodeproj_path = File.join(path, 'Pods.xcodeproj')
end

def copy_to(pods_root)
FileUtils.cp_r("#{path}/.", pods_root)
@project.save_as(File.join(pods_root, 'Pods.xcodeproj'))
end
end
end
4 changes: 2 additions & 2 deletions spec/integration_spec.rb
Expand Up @@ -77,7 +77,7 @@ def should_successfully_perform(command)
(root + 'Pods.xcconfig').read.should == installer.targets.first.xcconfig.to_s

project_file = (root + 'Pods.xcodeproj/project.pbxproj').to_s
NSDictionary.dictionaryWithContentsOfFile(project_file).should == installer.xcodeproj.to_hash
NSDictionary.dictionaryWithContentsOfFile(project_file).should == installer.template.project.to_hash

puts "\n[!] Compiling static library..."
Dir.chdir(config.project_pods_root) do
Expand Down Expand Up @@ -144,7 +144,7 @@ def should_successfully_perform(command)
installer.install!

project = Pod::Xcode::Project.new(config.project_pods_root + 'Pods.xcodeproj')
project.source_files.should == installer.xcodeproj.source_files
project.source_files.should == installer.template.project.source_files
end

it "creates a project with multiple targets" do
Expand Down
81 changes: 40 additions & 41 deletions spec/unit/xcode/project_spec.rb
Expand Up @@ -5,11 +5,10 @@

before do
@template = Pod::ProjectTemplate.new(:ios)
@project = Pod::Xcode::Project.new(@template.xcodeproj_path)
end

def find_objects(conditions)
@project.objects_hash.select do |_, object|
@template.project.objects_hash.select do |_, object|
object.objectsForKeys(conditions.keys, notFoundMarker:Object.new) == conditions.values
end
end
Expand All @@ -19,21 +18,21 @@ def find_object(conditions)
end

it "returns an instance initialized from the iOS static library template" do
template_file = (@template.xcodeproj_path + '/project.pbxproj').to_s
@project.to_hash.should == NSDictionary.dictionaryWithContentsOfFile(template_file)
template_file = File.join(@template.path, 'Pods.xcodeproj', 'project.pbxproj')
@template.project.to_hash.should == NSDictionary.dictionaryWithContentsOfFile(template_file)
end

before do
@target = @project.targets.new_static_library('Pods')
@target = @template.project.targets.new_static_library('Pods')
end

it "returns the objects hash" do
@project.objects_hash.should == @project.to_hash['objects']
@template.project.objects_hash.should == @template.project.to_hash['objects']
end

describe "PBXObject" do
before do
@object = Pod::Xcode::Project::PBXObject.new(@project, nil, 'name' => 'AnObject')
@object = Pod::Xcode::Project::PBXObject.new(@template.project, nil, 'name' => 'AnObject')
end

it "merges the class name into the attributes" do
Expand All @@ -54,29 +53,29 @@ def find_object(conditions)
end

it "adds the object to the objects hash" do
@project.objects_hash[@object.uuid].should == @object.attributes
@template.project.objects_hash[@object.uuid].should == @object.attributes
end
end

describe "a PBXFileReference" do
before do
@file = @project.files.new('path' => 'some/file.m')
@file = @template.project.files.new('path' => 'some/file.m')
end

it "is automatically added to the main group" do
@file.group.should == @project.main_group
@file.group.should == @template.project.main_group
end

it "is removed from the original group when added to another group" do
@project.pods.children << @file
@file.group.should == @project.pods
@project.main_group.children.should.not.include @file
@template.project.pods.children << @file
@file.group.should == @template.project.pods
@template.project.main_group.children.should.not.include @file
end
end

describe "a new PBXBuildPhase" do
before do
@phase = @project.objects.add(Pod::Xcode::Project::PBXBuildPhase)
@phase = @template.project.objects.add(Pod::Xcode::Project::PBXBuildPhase)
end

it "has an empty list of files" do
Expand All @@ -94,7 +93,7 @@ def find_object(conditions)

describe "a new PBXCopyFilesBuildPhase" do
before do
@phase = @project.objects.add(Pod::Xcode::Project::PBXCopyFilesBuildPhase, 'dstPath' => 'some/path')
@phase = @template.project.objects.add(Pod::Xcode::Project::PBXCopyFilesBuildPhase, 'dstPath' => 'some/path')
end

it "is a PBXBuildPhase" do
Expand All @@ -112,7 +111,7 @@ def find_object(conditions)

describe "a new PBXSourcesBuildPhase" do
before do
@phase = @project.objects.add(Pod::Xcode::Project::PBXSourcesBuildPhase)
@phase = @template.project.objects.add(Pod::Xcode::Project::PBXSourcesBuildPhase)
end

it "is a PBXBuildPhase" do
Expand All @@ -122,7 +121,7 @@ def find_object(conditions)

describe "a new PBXFrameworksBuildPhase" do
before do
@phase = @project.objects.add(Pod::Xcode::Project::PBXFrameworksBuildPhase)
@phase = @template.project.objects.add(Pod::Xcode::Project::PBXFrameworksBuildPhase)
end

it "is a PBXBuildPhase" do
Expand All @@ -132,23 +131,23 @@ def find_object(conditions)

describe "a new XCBuildConfiguration" do
before do
@configuration = @project.objects.add(Pod::Xcode::Project::XCBuildConfiguration)
@configuration = @template.project.objects.add(Pod::Xcode::Project::XCBuildConfiguration)
end

it "returns the xcconfig that this configuration is based on (baseConfigurationReference)" do
xcconfig = @project.objects.new
xcconfig = @template.project.objects.new
@configuration.baseConfiguration = xcconfig
@configuration.baseConfigurationReference.should == xcconfig.uuid
end
end

describe "a new XCConfigurationList" do
before do
@list = @project.objects.add(Pod::Xcode::Project::XCConfigurationList)
@list = @template.project.objects.add(Pod::Xcode::Project::XCConfigurationList)
end

it "returns the configurations" do
configuration = @project.objects.add(Pod::Xcode::Project::XCBuildConfiguration)
configuration = @template.project.objects.add(Pod::Xcode::Project::XCBuildConfiguration)
@list.buildConfigurations.to_a.should == []
@list.buildConfigurations = [configuration]
@list.buildConfigurationReferences.should == [configuration.uuid]
Expand Down Expand Up @@ -221,45 +220,45 @@ def find_object(conditions)
end

it "returns the objects as PBXObject instances" do
@project.objects.each do |object|
@project.objects_hash[object.uuid].should == object.attributes
@template.project.objects.each do |object|
@template.project.objects_hash[object.uuid].should == object.attributes
end
end

it "adds any type of new PBXObject to the objects hash" do
object = @project.objects.add(Pod::Xcode::Project::PBXObject, 'name' => 'An Object')
object = @template.project.objects.add(Pod::Xcode::Project::PBXObject, 'name' => 'An Object')
object.name.should == 'An Object'
@project.objects_hash[object.uuid].should == object.attributes
@template.project.objects_hash[object.uuid].should == object.attributes
end

it "adds a new PBXObject, of the configured type, to the objects hash" do
group = @project.groups.new('name' => 'A new group')
group = @template.project.groups.new('name' => 'A new group')
group.isa.should == 'PBXGroup'
group.name.should == 'A new group'
@project.objects_hash[group.uuid].should == group.attributes
@template.project.objects_hash[group.uuid].should == group.attributes
end

it "adds a new PBXFileReference to the objects hash" do
file = @project.files.new('path' => '/some/file.m')
file = @template.project.files.new('path' => '/some/file.m')
file.isa.should == 'PBXFileReference'
file.name.should == 'file.m'
file.path.should == '/some/file.m'
file.sourceTree.should == 'SOURCE_ROOT'
@project.objects_hash[file.uuid].should == file.attributes
@template.project.objects_hash[file.uuid].should == file.attributes
end

it "adds a new PBXBuildFile to the objects hash when a new PBXFileReference is created" do
file = @project.files.new('name' => '/some/source/file.h')
file = @template.project.files.new('name' => '/some/source/file.h')
build_file = file.buildFiles.new
build_file.file = file
build_file.fileRef.should == file.uuid
build_file.isa.should == 'PBXBuildFile'
@project.objects_hash[build_file.uuid].should == build_file.attributes
@template.project.objects_hash[build_file.uuid].should == build_file.attributes
end

it "adds a group to the `Pods' group" do
group = @project.add_pod_group('JSONKit')
@project.pods.childReferences.should.include group.uuid
group = @template.project.add_pod_group('JSONKit')
@template.project.pods.childReferences.should.include group.uuid
find_object({
'isa' => 'PBXGroup',
'name' => 'JSONKit',
Expand All @@ -273,7 +272,7 @@ def find_object(conditions)
path = Pathname.new("path/to/file.#{ext}")
file = @target.add_source_file(path)
# ensure that it was added to all objects
file = @project.objects[file.uuid]
file = @template.project.objects[file.uuid]

phase = @target.buildPhases.find { |phase| phase.is_a?(Pod::Xcode::Project::PBXSourcesBuildPhase) }
phase.files.map { |buildFile| buildFile.file }.should.include file
Expand All @@ -287,7 +286,7 @@ def find_object(conditions)
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')
file = @template.project.targets.first.add_source_file(path, nil, '-fno-obj-arc')
find_object({
'isa' => 'PBXBuildFile',
'fileRef' => file.uuid,
Expand All @@ -297,13 +296,13 @@ def find_object(conditions)
end

it "creates a copy build header phase which will copy headers to a specified path" do
phase = @project.targets.first.copy_files_build_phases.new_pod_dir("SomePod", "Path/To/Source")
phase = @template.project.targets.first.copy_files_build_phases.new_pod_dir("SomePod", "Path/To/Source")
find_object({
'isa' => 'PBXCopyFilesBuildPhase',
'dstPath' => '$(PUBLIC_HEADERS_FOLDER_PATH)/Path/To/Source',
'name' => 'Copy SomePod Public Headers'
}).should.not == nil
@project.targets.first.buildPhases.should.include phase
@template.project.targets.first.buildPhases.should.include phase
end

# TODO add test for the optional copy_header_phase
Expand All @@ -312,7 +311,7 @@ def find_object(conditions)
path = Pathname.new("path/to/file.h")
file = @target.add_source_file(path)
# ensure that it was added to all objects
file = @project.objects[file.uuid]
file = @template.project.objects[file.uuid]

phase = @target.buildPhases.find { |phase| phase.is_a?(Pod::Xcode::Project::PBXSourcesBuildPhase) }
phase.files.map { |buildFile| buildFile.file }.should.not.include file
Expand All @@ -323,13 +322,13 @@ def find_object(conditions)

it "saves the template with the adjusted project" do
@template.copy_to(temporary_directory)
@project.save_as(temporary_directory + 'Pods.xcodeproj')
@template.project.save_as(temporary_directory + 'Pods.xcodeproj')
project_file = (temporary_directory + 'Pods.xcodeproj/project.pbxproj')
NSDictionary.dictionaryWithContentsOfFile(project_file.to_s).should == @project.to_hash
NSDictionary.dictionaryWithContentsOfFile(project_file.to_s).should == @template.project.to_hash
end

it "returns all source files" do
group = @project.groups.new('name' => 'SomeGroup')
group = @template.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) }
group.source_files.map(&:pathname).sort.should == files.sort
Expand Down

0 comments on commit e08a67a

Please sign in to comment.