Class: XcodeArchiveCache::BuildGraph::NativeTargetFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/build_graph/native_target_finder.rb

Instance Method Summary collapse

Constructor Details

#initialize(projects) ⇒ NativeTargetFinder



7
8
9
# File 'lib/build_graph/native_target_finder.rb', line 7

def initialize(projects)
  @all_targets = extract_targets(projects)
end

Instance Method Details

#extract_targets(projects) ⇒ Array<Xcodeproj::Project::Object::PBXNativeTarget>



15
16
17
18
19
20
21
22
23
24
# File 'lib/build_graph/native_target_finder.rb', line 15

def extract_targets(projects)
  projects
    .map {|project| unnest(project)}
    .flatten
    .sort_by(&:path)
    .inject([]) {|unique, current| unique.last && unique.last.path == current.path ? unique : unique + [current]}
    .map(&:native_targets)
    .flatten
    .select {|target| !target.test_target_type?}
end

#find_for_dependency(dependency) ⇒ Xcodeproj::Project::Object::PBXNativeTarget



36
37
38
39
40
# File 'lib/build_graph/native_target_finder.rb', line 36

def find_for_dependency(dependency)
  # targets from embedded projects are proxied
  target = dependency.target ? dependency.target : dependency.target_proxy.proxied_object
  target.is_a?(Xcodeproj::Project::Object::PBXNativeTarget) ? target : nil
end

#find_for_file(file) ⇒ Xcodeproj::Project::Object::PBXNativeTarget



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/build_graph/native_target_finder.rb', line 46

def find_for_file(file)
  if file.file_ref.is_a?(Xcodeproj::Project::Object::PBXReferenceProxy)
    product_reference_uuid = file.file_ref.remote_ref.remote_global_id_string
    target = find_with_product_ref_uuid(product_reference_uuid)
    if target == nil
      project = file.file_ref.remote_ref.container_portal_object
      target = find_in_project(project, product_reference_uuid)

      # allow all targets from this project
      # to be linked to that exact project
      #
      # otherwise, injection will operate on different Xcodeproj::Project objects
      # resulting to only the last target being actually removed
      #
      @all_targets += extract_targets([project])
    end

    if target == nil
      raise Informative, "Target for #{file.file_ref.path} not found"
    end

    target
  elsif file.file_ref.is_a?(Xcodeproj::Project::Object::PBXFileReference)
    # products of sibling project targets are added as PBXFileReferences
    targets = find_with_product_path(file.file_ref.path)
    if targets.length > 1
      raise Informative, "Found more than one target with product #{File.basename(file.file_ref.path)} in:\n#{targets.map(&:project)}"
    end

    targets.first
  end
end

#find_for_product_name(product_name) ⇒ Object



81
82
83
84
# File 'lib/build_graph/native_target_finder.rb', line 81

def find_for_product_name(product_name)
  all_targets.select {|native_target| native_target.name == product_name || native_target.product_reference.display_name == product_name}
      .first
end

#set_platform_name_filter(platform_name) ⇒ Object



28
29
30
# File 'lib/build_graph/native_target_finder.rb', line 28

def set_platform_name_filter(platform_name)
  @platform_name = platform_name
end