Commit c14cdd66 authored by Eloy Duran's avatar Eloy Duran

Untested refactoring

parent 3a8a62fc
...@@ -8,5 +8,9 @@ module Pod ...@@ -8,5 +8,9 @@ module Pod
autoload :Spec, 'cocoa_pods/specification' autoload :Spec, 'cocoa_pods/specification'
autoload :Specification, 'cocoa_pods/specification' autoload :Specification, 'cocoa_pods/specification'
autoload :Version, 'cocoa_pods/version' autoload :Version, 'cocoa_pods/version'
autoload :XcodeProject, 'cocoa_pods/xcode_project'
module Xcode
autoload :Config, 'cocoa_pods/xcode/config'
autoload :Project, 'cocoa_pods/xcode/project'
end
end end
...@@ -120,8 +120,16 @@ module Pod ...@@ -120,8 +120,16 @@ module Pod
def create_static_library_project! def create_static_library_project!
puts "==> Creating static library project" puts "==> Creating static library project"
xcconfigs = []
project = XcodeProject.static_library xcconfig = Xcode::Config.new({
# in a workspace this is where the static library headers should be found
'USER_HEADER_SEARCH_PATHS' => '$(BUILT_PRODUCTS_DIR)',
# search the user headers
'ALWAYS_SEARCH_USER_PATHS' => 'YES',
})
project = Xcode::Project.static_library
resolved_dependent_specification_sets.each do |set| resolved_dependent_specification_sets.each do |set|
# In case the set is only part of other pods we don't need to build # In case the set is only part of other pods we don't need to build
# the pod itself. # the pod itself.
...@@ -138,35 +146,15 @@ module Pod ...@@ -138,35 +146,15 @@ module Pod
end end
end end
if xcconfig = spec.read(:xcconfig) if xcconfig_hash = spec.read(:xcconfig)
xcconfigs << xcconfig xcconfig << xcconfig_hash
end end
end end
project.create_in(config.project_pods_root) project.create_in(config.project_pods_root)
# TODO the static library needs an extra xcconfig which sets the values from issue #1. # TODO the static library needs an extra xcconfig which sets the values from issue #1.
# Or we could edit the project.pbxproj file, but that seems like more work... # Or we could edit the project.pbxproj file, but that seems like more work...
xcconfig.create_in(config.project_pods_root)
merged_xcconfig = {
# in a workspace this is where the static library headers should be found
'USER_HEADER_SEARCH_PATHS' => '$(BUILT_PRODUCTS_DIR)',
# search the user headers
'ALWAYS_SEARCH_USER_PATHS' => 'YES',
}
xcconfigs.each do |xcconfig|
xcconfig.each do |key, value|
if existing_value = merged_xcconfig[key]
merged_xcconfig[key] = "#{existing_value} #{value}"
else
merged_xcconfig[key] = value
end
end
end
(config.project_pods_root + 'Pods.xcconfig').open('w') do |file|
merged_xcconfig.each do |key, value|
file.puts "#{key} = #{value}"
end
end
end end
include Config::Mixin include Config::Mixin
......
module Pod
module Xcode
class Config
def initialize(xcconfig_hash = {})
@attributes = {}
merge!(xcconfig_hash)
end
def merge!(xcconfig_hash)
xcconfig_hash.each do |key, value|
if existing_value = @attributes[key]
@attributes[key] = "#{existing_value} #{value}"
else
@attributes[key] = value
end
end
end
alias_method :<<, :merge!
def create_in(pods_root)
(pods_root + 'Pods.xcconfig').open('w') do |file|
@attributes.each do |key, value|
file.puts "#{key} = #{value}"
end
end
end
end
end
end
framework 'Foundation'
module Pod
module Xcode
class Project
TEMPLATES_DIR = Pathname.new(File.expand_path('../../../xcode-project-templates', __FILE__))
# TODO see if we really need different templates for iOS and OS X
def self.static_library
new TEMPLATES_DIR + 'cocoa-touch-static-library'
end
def initialize(template_dir)
@template_dir = template_dir
file = template_dir + template_file
@template = NSMutableDictionary.dictionaryWithContentsOfFile(file.to_s)
end
def template_file
'Pods.xcodeproj/project.pbxproj'
end
def add_source_file(file)
file_ref_uuid = add_file_reference(file, 'SOURCE_ROOT')
add_file_to_group(file_ref_uuid, 'Pods')
if file.extname == '.h'
build_file_uuid = add_build_file(file_ref_uuid, "settings" => { "ATTRIBUTES" => ["Public"] })
add_file_to_list('PBXHeadersBuildPhase', build_file_uuid)
else
build_file_uuid = add_build_file(file_ref_uuid)
add_file_to_list('PBXSourcesBuildPhase', build_file_uuid)
end
end
def add_framework(path)
file_ref_uuid = add_file_reference(path, 'SDKROOT')
add_file_to_group(file_ref_uuid, 'Frameworks')
build_file_uuid = add_build_file(file_ref_uuid)
add_file_to_list('PBXFrameworksBuildPhase', build_file_uuid)
end
def add_header_search_path(path)
objects_by_isa('XCBuildConfiguration').each do |_, object|
(object['buildSettings']['HEADER_SEARCH_PATHS'] ||= []) << path
end
end
def to_hash
@template
end
def create_in(pods_root)
@template_dir.children.each do |child|
FileUtils.cp_r(child, pods_root + child.relative_path_from(@template_dir))
end
pbxproj = pods_root + template_file
@template.writeToFile(pbxproj.to_s, atomically:true)
end
private
def add_object(object)
uuid = generate_uuid
objects[uuid] = object
uuid
end
def add_file_reference(path, source_tree)
add_object({
"name" => path.basename.to_s,
"isa" => "PBXFileReference",
"sourceTree" => source_tree,
"path" => path.to_s,
})
end
def add_build_file(file_ref_uuid, extra = {})
add_object(extra.merge({
"isa" => "PBXBuildFile",
"fileRef" => file_ref_uuid
}))
end
def add_file_to_list(isa, build_file_uuid)
object_uuid, object = objects_by_isa(isa).first
object['files'] << build_file_uuid
end
def add_file_to_group(file_ref_uuid, name)
object_uuid, object = objects.find do |_, object|
object['isa'] == 'PBXGroup' && object['name'] == name
end
object['children'] << file_ref_uuid
end
def objects
@template['objects']
end
def objects_by_isa(isa)
objects.select { |_, object| object['isa'] == isa }
end
def generate_uuid
_uuid = CFUUIDCreate(nil)
uuid = CFUUIDCreateString(nil, _uuid)
CFRelease(_uuid)
CFMakeCollectable(uuid)
# Xcode's version is actually shorter, not worrying about collisions too much right now.
uuid.gsub('-', '')[0..23]
end
public
def pretty_print
puts `ruby -r pp -e 'pp(#{@template.inspect})'`
end
end
end
end
framework 'Foundation'
module Pod
class XcodeProject
TEMPLATES_DIR = Pathname.new(File.expand_path('../../../xcode-project-templates', __FILE__))
# TODO see if we really need different templates for iOS and OS X
def self.static_library
new TEMPLATES_DIR + 'cocoa-touch-static-library'
end
def initialize(template_dir)
@template_dir = template_dir
file = template_dir + template_file
@template = NSMutableDictionary.dictionaryWithContentsOfFile(file.to_s)
end
def template_file
'Pods.xcodeproj/project.pbxproj'
end
def add_source_file(file)
file_ref_uuid = add_file_reference(file, 'SOURCE_ROOT')
add_file_to_group(file_ref_uuid, 'Pods')
if file.extname == '.h'
build_file_uuid = add_build_file(file_ref_uuid, "settings" => { "ATTRIBUTES" => ["Public"] })
add_file_to_list('PBXHeadersBuildPhase', build_file_uuid)
else
build_file_uuid = add_build_file(file_ref_uuid)
add_file_to_list('PBXSourcesBuildPhase', build_file_uuid)
end
end
def add_framework(path)
file_ref_uuid = add_file_reference(path, 'SDKROOT')
add_file_to_group(file_ref_uuid, 'Frameworks')
build_file_uuid = add_build_file(file_ref_uuid)
add_file_to_list('PBXFrameworksBuildPhase', build_file_uuid)
end
def add_header_search_path(path)
objects_by_isa('XCBuildConfiguration').each do |_, object|
(object['buildSettings']['HEADER_SEARCH_PATHS'] ||= []) << path
end
end
def to_hash
@template
end
def create_in(pods_root)
@template_dir.children.each do |child|
FileUtils.cp_r(child, pods_root + child.relative_path_from(@template_dir))
end
pbxproj = pods_root + template_file
@template.writeToFile(pbxproj.to_s, atomically:true)
end
private
def add_object(object)
uuid = generate_uuid
objects[uuid] = object
uuid
end
def add_file_reference(path, source_tree)
add_object({
"name" => path.basename.to_s,
"isa" => "PBXFileReference",
"sourceTree" => source_tree,
"path" => path.to_s,
})
end
def add_build_file(file_ref_uuid, extra = {})
add_object(extra.merge({
"isa" => "PBXBuildFile",
"fileRef" => file_ref_uuid
}))
end
def add_file_to_list(isa, build_file_uuid)
object_uuid, object = objects_by_isa(isa).first
object['files'] << build_file_uuid
end
def add_file_to_group(file_ref_uuid, name)
object_uuid, object = objects.find do |_, object|
object['isa'] == 'PBXGroup' && object['name'] == name
end
object['children'] << file_ref_uuid
end
def objects
@template['objects']
end
def objects_by_isa(isa)
objects.select { |_, object| object['isa'] == isa }
end
def generate_uuid
_uuid = CFUUIDCreate(nil)
uuid = CFUUIDCreateString(nil, _uuid)
CFRelease(_uuid)
CFMakeCollectable(uuid)
# Xcode's version is actually shorter, not worrying about collisions too much right now.
uuid.gsub('-', '')[0..23]
end
public
def pretty_print
puts `ruby -r pp -e 'pp(#{@template.inspect})'`
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment