Class: Xcodeproj::Project::Object::PBXNativeTarget

Inherits:
AbstractTarget show all
Defined in:
lib/xcodeproj/project/object/native_target.rb

Overview

Represents a target handled by Xcode.

Attributes collapse

Attributes inherited from AbstractTarget

#build_configuration_list, #comments, #name, #product_name

Attributes inherited from AbstractObject

#isa, #project, #uuid

Attributes collapse

Helpers collapse

AbstractObject Hooks collapse

Methods inherited from AbstractTarget

#add_build_configuration, #add_dependency, #add_system_framework, #add_system_library, #add_system_library_tbd, #build_configurations, #build_settings, #common_resolved_build_setting, #copy_files_build_phases, #dependencies, #dependency_for_target, #deployment_target, #deployment_target=, #frameworks_build_phases, #new_copy_files_build_phase, #new_shell_script_build_phase, #platform_name, #pretty_print, #resolved_build_setting, #sdk, #sdk_version, #shell_script_build_phases

Methods inherited from AbstractObject

#<=>, #==, #ascii_plist_annotation, #display_name, #inspect, isa, #nested_object_for_hash, #pretty_print, #remove_from_project, #sort_recursively, #to_hash

Instance Attribute Details

#product_install_pathString

Returns the install path of the product.

Returns:

  • (String)

    the install path of the product.


446
# File 'lib/xcodeproj/project/object/native_target.rb', line 446

attribute :product_install_path, String

#product_referencePBXFileReference

Returns the reference to the product file.

Returns:


437
# File 'lib/xcodeproj/project/object/native_target.rb', line 437

has_one :product_reference, PBXFileReference

#product_typeString

Returns the build product type identifier.

Returns:

  • (String)

    the build product type identifier.


433
# File 'lib/xcodeproj/project/object/native_target.rb', line 433

attribute :product_type, String

Instance Method Details

#add_file_references(file_references, compiler_flags = {}) ⇒ Array<PBXBuildFile>

Adds source files to the target.

Parameters:

  • file_references (Array<PBXFileReference>)

    the files references of the source files that should be added to the target.

  • compiler_flags (String) (defaults to: {})

    the compiler flags for the source files.

Returns:


514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
# File 'lib/xcodeproj/project/object/native_target.rb', line 514

def add_file_references(file_references, compiler_flags = {})
  file_references.map do |file|
    extension = File.extname(file.path).downcase
    header_extensions = Constants::HEADER_FILES_EXTENSIONS
    is_header_phase = header_extensions.include?(extension)
    phase = is_header_phase ? headers_build_phase : source_build_phase

    unless build_file = phase.build_file(file)
      build_file = project.new(PBXBuildFile)
      build_file.file_ref = file
      phase.files << build_file
    end

    if compiler_flags && !compiler_flags.empty? && !is_header_phase
      (build_file.settings ||= {}).merge!('COMPILER_FLAGS' => compiler_flags) do |_, old, new|
        [old, new].compact.join(' ')
      end
    end

    yield build_file if block_given?

    build_file
  end
end

#add_on_demand_resources(on_demand_resource_tag_files) ⇒ void

This method returns an undefined value.

Adds on demand resources to the resources build phase of the target.

Parameters:

  • on_demand_resource_tag_files (String => [Array<PBXFileReference>])

    the files references of the on demand resources to add to the target keyed by the tag.


562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
# File 'lib/xcodeproj/project/object/native_target.rb', line 562

def add_on_demand_resources(on_demand_resource_tag_files)
  on_demand_resource_tag_files.each do |tag, file_refs|
    file_refs.each do |file_ref|
      if resources_build_phase.include?(file_ref)
        existing_build_file = resources_build_phase.build_file(file_ref)
        existing_build_file.settings ||= {}
        existing_build_file.settings['ASSET_TAGS'] ||= []
        existing_build_file.settings['ASSET_TAGS'] << tag
        existing_build_file.settings['ASSET_TAGS'].uniq!
        next
      end
      build_file = resources_build_phase.add_file_reference(file_ref, true)
      build_file.settings = (build_file.settings ||= {}).merge('ASSET_TAGS' => [tag])
    end
  end
end

#add_resources(resource_file_references) ⇒ void

This method returns an undefined value.

Adds resource files to the resources build phase of the target.

Parameters:

  • resource_file_references (Array<PBXFileReference>)

    the files references of the resources to the target.


546
547
548
549
550
551
552
553
# File 'lib/xcodeproj/project/object/native_target.rb', line 546

def add_resources(resource_file_references)
  resource_file_references.each do |file|
    next if resources_build_phase.include?(file)
    build_file = project.new(PBXBuildFile)
    build_file.file_ref = file
    resources_build_phase.files << build_file
  end
end

#build_phasesObjectList<AbstractBuildPhase>

Note:

Apparently only PBXCopyFilesBuildPhase and PBXShellScriptBuildPhase can appear multiple times in a target.

Returns the build phases of the target.

Returns:


455
# File 'lib/xcodeproj/project/object/native_target.rb', line 455

has_many :build_phases, AbstractBuildPhase

#build_rulesPBXBuildRule

Returns the build rules of this target.

Returns:


429
# File 'lib/xcodeproj/project/object/native_target.rb', line 429

has_many :build_rules, PBXBuildRule

#extension_target_type?Boolean

Returns Whether the target is an extension.

Returns:

  • (Boolean)

    Whether the target is an extension.


481
482
483
484
485
486
487
488
# File 'lib/xcodeproj/project/object/native_target.rb', line 481

def extension_target_type?
  case symbol_type
  when :app_extension, :watch_extension, :watch2_extension, :tv_extension, :messages_extension
    true
  else
    false
  end
end

#frameworks_build_phasePBXFrameworksBuildPhase

Note:

A target should have only one frameworks build phase.

Finds or creates the frameworks build phase of the target.

Returns:


624
625
626
# File 'lib/xcodeproj/project/object/native_target.rb', line 624

def frameworks_build_phase
  find_or_create_build_phase_by_class(PBXFrameworksBuildPhase)
end

#headers_build_phasePBXHeadersBuildPhase

Note:

A target should have only one headers build phase.

Finds or creates the headers build phase of the target.

Returns:


604
605
606
# File 'lib/xcodeproj/project/object/native_target.rb', line 604

def headers_build_phase
  find_or_create_build_phase_by_class(PBXHeadersBuildPhase)
end

#launchable_target_type?Boolean

Returns Whether the target is launchable.

Returns:

  • (Boolean)

    Whether the target is launchable.


492
493
494
495
496
497
498
499
# File 'lib/xcodeproj/project/object/native_target.rb', line 492

def launchable_target_type?
  case symbol_type
  when :application, :command_line_tool
    true
  else
    false
  end
end

#package_product_dependenciesObjectList<XCSwiftPackageProductDependency>

Returns the Swift package products necessary to build this target.

Returns:


442
# File 'lib/xcodeproj/project/object/native_target.rb', line 442

has_many :package_product_dependencies, XCSwiftPackageProductDependency

#remove_on_demand_resources(on_demand_resource_tag_files) ⇒ void

This method returns an undefined value.

Remove on demand resources from the resources build phase of the target.

Parameters:

  • on_demand_resource_tag_files (String => [Array<PBXFileReference>])

    the files references of the on demand resources to add to the target keyed by the tag.


586
587
588
589
590
591
592
593
594
595
596
# File 'lib/xcodeproj/project/object/native_target.rb', line 586

def remove_on_demand_resources(on_demand_resource_tag_files)
  on_demand_resource_tag_files.each do |tag, file_refs|
    file_refs.each do |file_ref|
      build_file = resources_build_phase.build_file(file_ref)
      next if build_file.nil?
      asset_tags = build_file.settings['ASSET_TAGS']
      asset_tags.delete(tag)
      resources_build_phase.remove_file_reference(file_ref) if asset_tags.empty?
    end
  end
end

#resources_build_phasePBXResourcesBuildPhase

Note:

A target should have only one resources build phase.

Finds or creates the resources build phase of the target.

Returns:


634
635
636
# File 'lib/xcodeproj/project/object/native_target.rb', line 634

def resources_build_phase
  find_or_create_build_phase_by_class(PBXResourcesBuildPhase)
end

#sort(_options = nil) ⇒ Object

Sorts the to many attributes of the object according to the display name.

Build phases are not sorted as they order is relevant.


668
669
670
671
672
673
674
675
676
# File 'lib/xcodeproj/project/object/native_target.rb', line 668

def sort(_options = nil)
  attributes_to_sort = to_many_attributes.reject { |attr| attr.name == :build_phases }
  attributes_to_sort.each do |attrb|
    list = attrb.get_value(self)
    list.sort! do |x, y|
      x.display_name <=> y.display_name
    end
  end
end

#source_build_phasePBXSourcesBuildPhase

Note:

A target should have only one source build phase.

Finds or creates the source build phase of the target.

Returns:


614
615
616
# File 'lib/xcodeproj/project/object/native_target.rb', line 614

def source_build_phase
  find_or_create_build_phase_by_class(PBXSourcesBuildPhase)
end

#symbol_typeSymbol

Returns The type of the target expressed as a symbol.

Returns:

  • (Symbol)

    The type of the target expressed as a symbol.


464
465
466
# File 'lib/xcodeproj/project/object/native_target.rb', line 464

def symbol_type
  Constants::PRODUCT_TYPE_UTI.key(product_type)
end

#test_target_type?Boolean

Returns Whether the target is a test target.

Returns:

  • (Boolean)

    Whether the target is a test target.


470
471
472
473
474
475
476
477
# File 'lib/xcodeproj/project/object/native_target.rb', line 470

def test_target_type?
  case symbol_type
  when :octest_bundle, :unit_test_bundle, :ui_test_bundle
    true
  else
    false
  end
end

#to_ascii_plistObject


686
687
688
689
690
691
692
# File 'lib/xcodeproj/project/object/native_target.rb', line 686

def to_ascii_plist
  plist = super
  if !plist.value['packageProductDependencies'].nil? && plist.value['packageProductDependencies'].empty?
    plist.value.delete('packageProductDependencies')
  end
  plist
end

#to_hash_as(method = :to_hash) ⇒ Object


678
679
680
681
682
683
684
# File 'lib/xcodeproj/project/object/native_target.rb', line 678

def to_hash_as(method = :to_hash)
  hash_as = super
  if !hash_as['packageProductDependencies'].nil? && hash_as['packageProductDependencies'].empty?
    hash_as.delete('packageProductDependencies')
  end
  hash_as
end