Class: Vendor::XCode::Project
- Inherits:
-
Object
- Object
- Vendor::XCode::Project
- Defined in:
- lib/vendor/xcode/project.rb
Constant Summary collapse
- BUILD_SETTING_TYPES =
Build settings and the format that they should be stored in
{ "OTHER_LDFLAGS" => :array }
Instance Attribute Summary collapse
-
#archive_version ⇒ Object
readonly
Returns the value of attribute archive_version.
-
#dirty ⇒ Object
Returns the value of attribute dirty.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#object_version ⇒ Object
readonly
Returns the value of attribute object_version.
-
#objects ⇒ Object
readonly
Returns the value of attribute objects.
-
#project_folder ⇒ Object
readonly
Returns the value of attribute project_folder.
-
#root_object ⇒ Object
readonly
Returns the value of attribute root_object.
Instance Method Summary collapse
- #add_build_setting(name, value, options = {}) ⇒ Object
- #add_file(options) ⇒ Object
- #add_file_to_target(file, target) ⇒ Object
- #add_framework(framework, options = {}) ⇒ Object
- #backup ⇒ Object
- #create_group(path) ⇒ Object
- #dirty? ⇒ Boolean
- #find_group(path) ⇒ Object
- #find_object(id) ⇒ Object
- #find_target(t) ⇒ Object
-
#initialize(project_folder) ⇒ Project
constructor
A new instance of Project.
- #reload ⇒ Object
- #remove_file_from_target(file, target) ⇒ Object
- #remove_group(path) ⇒ Object
- #save ⇒ Object
- #to_ascii_plist ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(project_folder) ⇒ Project
Returns a new instance of Project.
23 24 25 26 27 28 29 30 |
# File 'lib/vendor/xcode/project.rb', line 23 def initialize(project_folder) @project_folder = project_folder @pbxproject = ::File.join(project_folder, "project.pbxproj") @name = File.basename(project_folder).split(".").first @dirty = false reload end |
Instance Attribute Details
#archive_version ⇒ Object (readonly)
Returns the value of attribute archive_version.
16 17 18 |
# File 'lib/vendor/xcode/project.rb', line 16 def archive_version @archive_version end |
#dirty ⇒ Object
Returns the value of attribute dirty.
21 22 23 |
# File 'lib/vendor/xcode/project.rb', line 21 def dirty @dirty end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
14 15 16 |
# File 'lib/vendor/xcode/project.rb', line 14 def name @name end |
#object_version ⇒ Object (readonly)
Returns the value of attribute object_version.
15 16 17 |
# File 'lib/vendor/xcode/project.rb', line 15 def object_version @object_version end |
#objects ⇒ Object (readonly)
Returns the value of attribute objects.
17 18 19 |
# File 'lib/vendor/xcode/project.rb', line 17 def objects @objects end |
#project_folder ⇒ Object (readonly)
Returns the value of attribute project_folder.
19 20 21 |
# File 'lib/vendor/xcode/project.rb', line 19 def project_folder @project_folder end |
#root_object ⇒ Object (readonly)
Returns the value of attribute root_object.
18 19 20 |
# File 'lib/vendor/xcode/project.rb', line 18 def root_object @root_object end |
Instance Method Details
#add_build_setting(name, value, options = {}) ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/vendor/xcode/project.rb', line 199 def add_build_setting(name, value, = {}) ().each do |target| target.build_configuration_list.build_configurations.each do |config| build_settings = config.build_settings debug_key = "#{target.name}/#{config.name}".inspect # If the build setting already has the key if build_settings.has_key?(name) # Is this setting known to have multiple values? if (setting_type = BUILD_SETTING_TYPES[name]) # If its an array if setting_type == :array # Is it already an array, if so, check to see if # it already exists, and if it doesn't add it. if build_settings[name].kind_of?(Array) unless build_settings[name].include? Vendor.ui.debug(" Added build setting (#{name.inspect} = #{value.inspect}) to #{debug_key}") build_settings[name] << value end else Vendor.ui.debug(" Added build setting (#{name.inspect} = #{value.inspect}) to #{debug_key}") # Don't add it if the single build value is already there unless build_settings[name].strip == value.strip build_settings[name] = [ build_settings[name], value ] end end end else # If its an unknown type, then we should just throw a warning # because we should'nt just change stuff willy, nilly. Vendor.ui.warn(" Build setting #{name.inspect} wanted to change to #{value.inspect}, but it was already #{build_settings[name].inspect} in #{debug_key}") end else Vendor.ui.debug(" Added build setting (#{name.inspect} = #{value.inspect}) to #{debug_key}") build_settings[name] = value end end end @dirty = true end |
#add_file(options) ⇒ Object
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/vendor/xcode/project.rb', line 255 def add_file() , :path, :file, :source_tree # Ensure file exists if we'nre not using the sdk root source tree. # The SDKROOT source tree has a virtual file on the system, so # File.exist checks will always return false. unless [:source_tree] == :sdkroot raise StandardError.new("Could not find file `#{[:file]}`") unless File.exists?([:file]) end # Find targets targets = () # Create the group group = create_group([:path]) # File type type = Vendor::XCode::Proxy::PBXFileReference.file_type_from_extension(File.extname([:file])) # The file name name = File.basename([:file]) attributes = { 'lastKnownFileType' => type, 'sourceTree' => "<#{[:source_tree].to_s}>" } # Handle the different source tree types if [:source_tree] == :group # Ensure the path exists on the filesystem path = File.join(@project_folder, "..", [:path]) FileUtils.mkdir_p path # Copy the file FileUtils.cp_r [:file], File.join(path, name) # Set the path of the file attributes['path'] = name elsif [:source_tree] == :absolute # Set the path and the name of the file attributes['name'] = name attributes['path'] = [:file] elsif [:source_tree] == :sdkroot # Set the path and the name of the framework attributes['name'] = name attributes['path'] = [:file] attributes['sourceTree'] = "SDKROOT" else # Could not handle that option raise StandardError.new("Invalid :source_tree option `#{[:source_tree].to_s}`") end # Add the file to XCode file = Vendor::XCode::Proxy::PBXFileReference.new(:project => self, :id => Vendor::XCode::Proxy::Base.generate_id, :attributes => attributes) # Set the parent file.parent = group # Add the file id to the groups children group.attributes['children'] << file.id # Add the file to targets targets.each do |t| add_file_to_target file, t end # Mark as dirty @dirty = true # Add the file to the internal index @objects_by_id[file.id] = file end |
#add_file_to_target(file, target) ⇒ Object
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
# File 'lib/vendor/xcode/project.rb', line 356 def add_file_to_target(file, target) build_phase = build_phase_for_file(file.last_known_file_type, target) if build_phase Vendor.ui.debug "Adding #{file.attributes} to #{target.name} (build_phase = #{build_phase.class.name})" # Add the file to XCode build_file = Vendor::XCode::Proxy::PBXBuildFile.new(:project => self, :id => Vendor::XCode::Proxy::Base.generate_id, :attributes => { 'fileRef' => file.id }) # Set the parent build_file.parent = build_phase # Add the file to the internal index @objects_by_id[build_file.id] = build_file # Add the file to the build phase build_phase.attributes['files'] << build_file.id end end |
#add_framework(framework, options = {}) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/vendor/xcode/project.rb', line 165 def add_framework(framework, = {}) # Find targets targets = () Vendor.ui.debug %{Adding #{framework} to targets "#{targets.map(&:name)}"} path = if framework.match(/\.dylib/) "usr/lib/#{framework}" else "System/Library/Frameworks/#{framework}" end targets.each do |t| # Does the framework already exist? build_phase = build_phase_for_file("wrapper.framework", t) if build_phase # Does a framework already exist? existing_framework = build_phase.files.map(&:file_ref).find do |file| # Some files have names, some done. Framework references # have names... if file.respond_to?(:name) file.name == framework end end # If an existing framework was found, don't add it again unless existing_framework add_file :targets => t, :file => path, :name => framework, :path => "Frameworks", :source_tree => :sdkroot end end end end |
#backup ⇒ Object
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
# File 'lib/vendor/xcode/project.rb', line 402 def backup dir = File.dirname(@project_folder) backup_name = "#{@name}.vendorbackup" backups = Dir[File.join(dir, "#{backup_name}*")].sort unless backups.empty? if backups.last.match(/([\d]+)$/) backup_name += ".#{$1.to_i + 1}" else backup_name += ".1" end end backup_to = File.join(dir, backup_name) Vendor.ui.warn "Backup created #{backup_to}" FileUtils.cp_r @project_folder, backup_to end |
#create_group(path) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/vendor/xcode/project.rb', line 84 def create_group(path) current = root_object.main_group path.split("/").each do |name| group = current.children.find { |x| (x.respond_to?(:name) ? x.name : x.path) == name } unless group group = Vendor::XCode::Proxy::PBXGroup.new(:project => self, :id => Vendor::XCode::Proxy::Base.generate_id, :attributes => { 'path' => name, 'sourceTree' => '<group>', 'children' => [] }) # Set the parent group.parent = current @objects_by_id[group.id] = group # This is hacky current.attributes['children'] << group.id end current = group end # Mark as dirty @dirty = true current end |
#dirty? ⇒ Boolean
421 422 423 |
# File 'lib/vendor/xcode/project.rb', line 421 def dirty? @dirty end |
#find_group(path) ⇒ Object
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/vendor/xcode/project.rb', line 73 def find_group(path) current = root_object.main_group path.split("/").each do |name| current = current.children.find { |x| (x.respond_to?(:name) ? x.name : x.path) == name } return nil unless current end current end |
#find_object(id) ⇒ Object
60 61 62 |
# File 'lib/vendor/xcode/project.rb', line 60 def find_object(id) @objects_by_id[id] end |
#find_target(t) ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/vendor/xcode/project.rb', line 64 def find_target(t) # Are we already a target? if t.kind_of?(Vendor::XCode::Proxy::Base) t else root_object.targets.find { |x| x.name == t } end end |
#reload ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/vendor/xcode/project.rb', line 32 def reload # We switch between our custom PList converter and the JSON format # because the custom implementation isn't very reliable. We use it mainly # so the gem can run on systems that don't have plutil installed (like our # CI server). The plutil app is far more reliable. if RUBY_PLATFORM !=~ /darwin/ || ENV['PARSER'] == 'custom' contents = File.readlines(@pbxproject).join("\n") parsed = Vendor::Plist.parse_ascii(contents) else parsed = JSON.parse(`plutil -convert json -o - "#{@pbxproject}"`) end @object_version = parsed['objectVersion'].to_i @archive_version = parsed['archiveVersion'].to_i @objects_by_id = {} @objects = parsed['objects'].map do |id, attributes| klass = Vendor::XCode::Proxy.const_get(attributes['isa']) @objects_by_id[id] = klass.new(:project => self, :id => id, :attributes => attributes) end @objects.each { |object| object.send(:after_initialize) } @root_object = @objects_by_id[parsed['rootObject']] end |
#remove_file_from_target(file, target) ⇒ Object
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'lib/vendor/xcode/project.rb', line 338 def remove_file_from_target(file, target) # Search through all the build phases for references to the file build_files = [] target.build_phases.each do |phase| build_files << phase.files.find_all do |build_file| build_file.attributes['fileRef'] == file.id end end # Remove the build files from the references build_files.flatten.each do |build_file| build_file.parent.attributes['files'].delete build_file.id @objects_by_id.delete build_file.id end end |
#remove_group(path) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/vendor/xcode/project.rb', line 113 def remove_group(path) group = find_group(path) # If we have the group if group ids_to_remove = [] # Remove the children from the file system group.children.each do |child| if child.group? # Is it a group? remove_group child.full_path # Recursivley remove the child group elsif child.file? # Or a file file = File.(File.join(@project_folder, "..", child.full_path)) # Remove the file from the filesystem if File.exist?(file) FileUtils.rm_rf file end else Vendor.ui.error "Couldn't remove object: #{child}" end # Remove from the targets (if we can) root_object.targets.each { |t| remove_file_from_target(child, t) } # Remove the file from the parent child.parent.attributes['children'].delete child.id # Add the id to the list of stuff to remove. If we do this # during the loop, bad things happen - not sure why. ids_to_remove << child.id end # Remove the group from the parent group.parent.attributes['children'].delete group.id # Add to the list of stuff to remove ids_to_remove << group.id ids_to_remove.each do |id| @objects_by_id.delete id end # Mark as dirty @dirty = true else false end end |
#save ⇒ Object
392 393 394 395 396 397 398 399 400 |
# File 'lib/vendor/xcode/project.rb', line 392 def save backup open(@pbxproject, 'w+') do |f| f << to_ascii_plist end if valid? # Not dirty anymore @dirty = false end |
#to_ascii_plist ⇒ Object
382 383 384 385 386 387 388 389 390 |
# File 'lib/vendor/xcode/project.rb', line 382 def to_ascii_plist plist = { :archiveVersion => archive_version, :classes => {}, :objectVersion => object_version, :objects => @objects_by_id, :rootObject => @root_object.id }.to_ascii_plist "// !$*UTF8*$!\n" << plist end |
#valid? ⇒ Boolean
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 |
# File 'lib/vendor/xcode/project.rb', line 425 def valid? begin # Try and parse the plist again. If it parses, then we've # got valid syntax, if it fails, it will raise a parse error. We # know we've done something bad at this point. Vendor::Plist.parse_ascii(to_ascii_plist) true rescue Vendor::Plist::AsciiParser::ParseError => e Vendor.ui.error "There was an error converting the XCode project back to a Plist" Vendor.ui.error e.inspect false end end |