class Xcodeproj::Project::Object::PBXNativeTarget

Represents a target handled by Xcode.

Public Instance Methods

add_file_references(file_references, compiler_flags = {}) { |build_file| ... } click to toggle source

Adds source files to the target.

@param [Array<PBXFileReference>] file_references

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

@param [String] compiler_flags

the compiler flags for the source files.

@yield_param [PBXBuildFile] each created build file.

@return [Array<PBXBuildFile>] the created build files.

# File lib/xcodeproj/project/object/native_target.rb, line 526
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) click to toggle source

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

@param {String => [Array<PBXFileReference>]} on_demand_resource_tag_files

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

@return [void]

# File lib/xcodeproj/project/object/native_target.rb, line 574
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) click to toggle source

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

@param [Array<PBXFileReference>] resource_file_references

the files references of the resources to the target.

@return [void]

# File lib/xcodeproj/project/object/native_target.rb, line 558
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
excluded_keys_for_serialization_when_empty() click to toggle source

@return [Array<String>] array of keys to exclude from serialization when the value is empty

# File lib/xcodeproj/project/object/native_target.rb, line 711
def excluded_keys_for_serialization_when_empty
  %w(packageProductDependencies fileSystemSynchronizedGroups)
end
extension_target_type?() click to toggle source

@return [Boolean] Whether the target is an extension.

# File lib/xcodeproj/project/object/native_target.rb, line 493
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_phase() click to toggle source

Finds or creates the frameworks build phase of the target.

@note A target should have only one frameworks build phase.

@return [PBXFrameworksBuildPhase] the frameworks build phase.

# File lib/xcodeproj/project/object/native_target.rb, line 636
def frameworks_build_phase
  find_or_create_build_phase_by_class(PBXFrameworksBuildPhase)
end
headers_build_phase() click to toggle source

Finds or creates the headers build phase of the target.

@note A target should have only one headers build phase.

@return [PBXHeadersBuildPhase] the headers build phase.

# File lib/xcodeproj/project/object/native_target.rb, line 616
def headers_build_phase
  find_or_create_build_phase_by_class(PBXHeadersBuildPhase)
end
launchable_target_type?() click to toggle source

@return [Boolean] Whether the target is launchable.

# File lib/xcodeproj/project/object/native_target.rb, line 504
def launchable_target_type?
  case symbol_type
  when :application, :command_line_tool
    true
  else
    false
  end
end
remove_on_demand_resources(on_demand_resource_tag_files) click to toggle source

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

@param {String => [Array<PBXFileReference>]} on_demand_resource_tag_files

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

@return [void]

# File lib/xcodeproj/project/object/native_target.rb, line 598
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_phase() click to toggle source

Finds or creates the resources build phase of the target.

@note A target should have only one resources build phase.

@return [PBXResourcesBuildPhase] the resources build phase.

# File lib/xcodeproj/project/object/native_target.rb, line 646
def resources_build_phase
  find_or_create_build_phase_by_class(PBXResourcesBuildPhase)
end
sort(_options = nil) click to toggle source

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

Build phases are not sorted as they order is relevant.

# File lib/xcodeproj/project/object/native_target.rb, line 680
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_phase() click to toggle source

Finds or creates the source build phase of the target.

@note A target should have only one source build phase.

@return [PBXSourcesBuildPhase] the source build phase.

# File lib/xcodeproj/project/object/native_target.rb, line 626
def source_build_phase
  find_or_create_build_phase_by_class(PBXSourcesBuildPhase)
end
symbol_type() click to toggle source

@return [Symbol] The type of the target expressed as a symbol.

# File lib/xcodeproj/project/object/native_target.rb, line 476
def symbol_type
  Constants::PRODUCT_TYPE_UTI.key(product_type)
end
test_target_type?() click to toggle source

@return [Boolean] Whether the target is a test target.

# File lib/xcodeproj/project/object/native_target.rb, line 482
def test_target_type?
  case symbol_type
  when :octest_bundle, :unit_test_bundle, :ui_test_bundle
    true
  else
    false
  end
end
to_ascii_plist() click to toggle source
# File lib/xcodeproj/project/object/native_target.rb, line 700
def to_ascii_plist
  plist = super
  excluded_keys_for_serialization_when_empty.each do |key|
    if !plist.value[key].nil? && plist.value[key].empty?
      plist.value.delete(key)
    end
  end
  plist
end
to_hash_as(method = :to_hash) click to toggle source
# File lib/xcodeproj/project/object/native_target.rb, line 690
def to_hash_as(method = :to_hash)
  hash_as = super
  excluded_keys_for_serialization_when_empty.each do |key|
    if !hash_as[key].nil? && hash_as[key].empty?
      hash_as.delete(key)
    end
  end
  hash_as
end

Private Instance Methods

find_or_create_build_phase_by_class(phase_class) click to toggle source

Find or create a build phase by a given class

@param [Class] phase_class the class of the build phase to find or create.

@return [AbstractBuildPhase] the build phase whose class match the given phase_class.

# File lib/xcodeproj/project/object/native_target.rb, line 661
def find_or_create_build_phase_by_class(phase_class)
  @phases ||= {}
  unless phase_class < AbstractBuildPhase
    raise ArgumentError, "#{phase_class} must be a subclass of #{AbstractBuildPhase.class}"
  end
  @phases[phase_class] ||= build_phases.find { |bp| bp.class == phase_class } ||
    project.new(phase_class).tap { |bp| build_phases << bp }
end