Class: Pod::Swift::PackageDescription::Target

Inherits:
Pod::Swift::PackageDescriptionBaseObject show all
Defined in:
lib/cocoapods-spm/swift/package/target.rb

Instance Attribute Summary

Attributes inherited from Pod::Swift::PackageDescriptionBaseObject

#parent, #raw

Instance Method Summary collapse

Methods inherited from Pod::Swift::PackageDescriptionBaseObject

#[], #dup_with_attrs, from_file, from_s, #initialize, #inspect, #name, #pkg_name, #root

Constructor Details

This class inherits a constructor from Pod::Swift::PackageDescriptionBaseObject

Instance Method Details

#binary?Boolean

Returns:

  • (Boolean)


17
18
19
20
21
# File 'lib/cocoapods-spm/swift/package/target.rb', line 17

def binary?
  return @binary unless @binary.nil?

  @binary = type == "binary"
end

#binary_basenameObject



136
137
138
139
140
141
142
143
144
145
146
# File 'lib/cocoapods-spm/swift/package/target.rb', line 136

def binary_basename
  return nil unless binary?

  @binary_basename ||= begin
    xcframework_dir ||= (root.artifacts_dir / name).glob("*.xcframework")[0]
    xcframework_dir ||= root.src_dir / raw["path"] if raw.key?("path")
    paths = xcframework_dir.glob("*/*.{a,framework}")
    UI.warn "Cannot detect binary_basename for #{name}" if paths.empty?
    paths[0].basename.to_s unless paths.empty?
  end
end

#built_framework_pathObject



125
126
127
# File 'lib/cocoapods-spm/swift/package/target.rb', line 125

def built_framework_path
  "${BUILT_PRODUCTS_DIR}/PackageFrameworks/#{framework_name}.framework"
end

#clang_modulemap_argObject



63
64
65
66
67
# File 'lib/cocoapods-spm/swift/package/target.rb', line 63

def clang_modulemap_arg
  return nil unless use_generated_modulemap?

  "-fmodule-map-file=\"${GENERATED_MODULEMAP_DIR}/#{name}.modulemap\""
end

#dynamic?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/cocoapods-spm/swift/package/target.rb', line 23

def dynamic?
  @dynamic
end

#framework_nameObject



27
28
29
# File 'lib/cocoapods-spm/swift/package/target.rb', line 27

def framework_name
  @product_name || name
end

#header_search_path_argObject



38
39
40
41
42
43
# File 'lib/cocoapods-spm/swift/package/target.rb', line 38

def header_search_path_arg
  return nil if public_headers_path.nil?

  path = public_headers_path.to_s.sub(root.checkouts_dir.to_s, "${SOURCE_PACKAGES_CHECKOUTS_DIR}")
  "\"#{path}\""
end

#implicit_public_headersObject



51
52
53
54
# File 'lib/cocoapods-spm/swift/package/target.rb', line 51

def implicit_public_headers
  path = sources_path / "include"
  path unless path.glob("**/*.h*").empty?
end

#implicit_resourcesObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/cocoapods-spm/swift/package/target.rb', line 75

def implicit_resources
  target_sources_path = raw["path"] || "Sources/#{name}"
  target_sources_path = root.src_dir / target_sources_path

  # Refer to the following link for the implicit resources
  # https://developer.apple.com/documentation/xcode/bundling-resources-with-a-swift-package#Add-resource-files
  patterns = [
    "*.xcassets",
    "*.xib",
    "*.storyboard",
    "*.xcdatamodeld",
    "*.lproj",
  ]
  return [] if patterns.all? { |p| target_sources_path.glob(p).empty? }

  [Resources.new({}, parent: self)]
end

#linker_flagsObject



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/cocoapods-spm/swift/package/target.rb', line 93

def linker_flags
  return ["-framework \"#{framework_name}\""] if dynamic?
  return ["-l\"#{name}.o\""] unless binary?

  case binary_basename
  when /(\S+)\.framework/ then ["-framework \"#{$1}\""]
  when /lib(\S+)\.(a|dylib)/ then ["-l\"#{$1}\""]
  when /(\S+\.(a|dylib))/ then ["\"${PODS_CONFIGURATION_BUILD_DIR}/#{$1}\""]
  else []
  end
end

#macro?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/cocoapods-spm/swift/package/target.rb', line 13

def macro?
  type == "macro"
end

#public_headers_pathObject



45
46
47
48
49
# File 'lib/cocoapods-spm/swift/package/target.rb', line 45

def public_headers_path
  res = sources_path / raw["publicHeadersPath"] if raw.key?("publicHeadersPath")
  res = implicit_public_headers if res.nil?
  res
end

#resolve_dependencies(pkg_desc_cache) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/cocoapods-spm/swift/package/target.rb', line 105

def resolve_dependencies(pkg_desc_cache)
  raw.fetch("dependencies", []).flat_map do |hash|
    type = ["byName", "target", "product"].find { |k| hash.key?(k) }
    if type.nil?
      raise Informative, "Unexpected dependency type. Must be either `byName`, `target`, or `product`."
    end

    name = hash[type][0]
    pkg_name = hash.key?("product") ? hash["product"][1] : self.pkg_name
    pkg_desc = pkg_desc_cache[pkg_name]
    find_by_target = -> { pkg_desc.targets.select { |t| t.name == name } }
    find_by_product = -> { pkg_desc.targets_of_product(name) }
    next find_by_target.call if hash.key?("target")
    next find_by_product.call if hash.key?("product")

    # byName, could be either a target or a product
    next find_by_target.call || find_by_product.call
  end
end

#resourcesObject



69
70
71
72
73
# File 'lib/cocoapods-spm/swift/package/target.rb', line 69

def resources
  res = raw.fetch("resources", []).flat_map { |h| Resources.new(h, parent: self) }
  res = implicit_resources if res.empty?
  res
end

#sources_pathObject



31
32
33
34
35
36
# File 'lib/cocoapods-spm/swift/package/target.rb', line 31

def sources_path
  @sources_path ||= begin
    path = raw["path"] || "Sources/#{name}"
    root.src_dir / path
  end
end

#typeObject



9
10
11
# File 'lib/cocoapods-spm/swift/package/target.rb', line 9

def type
  raw["type"]
end

#use_default_xcode_linking?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/cocoapods-spm/swift/package/target.rb', line 148

def use_default_xcode_linking?
  root.use_default_xcode_linking?
end

#use_generated_modulemap?Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
# File 'lib/cocoapods-spm/swift/package/target.rb', line 56

def use_generated_modulemap?
  return false if public_headers_path.nil?

  # If there exists module.modulemap, it'll be auto picked up during compilation
  true if public_headers_path.glob("module.modulemap").empty?
end

#xcframeworkObject



129
130
131
132
133
134
# File 'lib/cocoapods-spm/swift/package/target.rb', line 129

def xcframework
  @xcframework ||= begin
    path = (root.artifacts_dir / name).glob("*.xcframework")[0]
    Xcode::XCFramework.new(name, path.realpath) unless path.nil?
  end
end