Class: Pod::Installer::ProjectCache::TargetCacheKey

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoapods/installer/project_cache/target_cache_key.rb

Overview

Uniquely identifies a Target.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sandbox, type, key_hash) ⇒ TargetCacheKey

Initialize a new instance.



31
32
33
34
35
# File 'lib/cocoapods/installer/project_cache/target_cache_key.rb', line 31

def initialize(sandbox, type, key_hash)
  @sandbox = sandbox
  @type = type
  @key_hash = key_hash
end

Instance Attribute Details

#key_hashHash{String => Object} (readonly)



23
24
25
# File 'lib/cocoapods/installer/project_cache/target_cache_key.rb', line 23

def key_hash
  @key_hash
end

#sandboxSandbox (readonly)



13
14
15
# File 'lib/cocoapods/installer/project_cache/target_cache_key.rb', line 13

def sandbox
  @sandbox
end

#typeSymbol (readonly)



18
19
20
# File 'lib/cocoapods/installer/project_cache/target_cache_key.rb', line 18

def type
  @type
end

Class Method Details

.from_aggregate_target(sandbox, target_by_label, aggregate_target) ⇒ TargetCacheKey

Construct a TargetCacheKey instance from an AggregateTarget.



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/cocoapods/installer/project_cache/target_cache_key.rb', line 180

def self.from_aggregate_target(sandbox, target_by_label, aggregate_target)
  build_settings = {}
  aggregate_target.user_build_configurations.keys.each do |configuration|
    build_settings[configuration] = Digest::MD5.hexdigest(aggregate_target.build_settings(configuration).xcconfig.to_s)
  end

  # find resources from upstream dependencies that will be named in the `{name}-resources.sh` script
  resource_dependencies = []
  aggregate_target.pod_targets.each do |name|
    upstream = target_by_label[name]

    # pod target
    resource_dependencies.append(upstream.resource_paths.values.flatten) if upstream.respond_to?(:resource_paths)

    # aggregate target
    resource_dependencies.append(upstream.resource_paths_by_config.values.flatten) if upstream.respond_to?(:resource_paths_by_config)
  end

  contents = {
    'BUILD_SETTINGS_CHECKSUM' => build_settings,
  }
  if aggregate_target.includes_resources? || aggregate_target.includes_on_demand_resources?
    relative_resource_file_paths = aggregate_target.resource_paths_by_config.values.flatten.uniq
    relative_on_demand_resource_file_paths = aggregate_target.on_demand_resources.map do |res|
      res.relative_path_from(sandbox.project_path.dirname).to_s
    end
    contents['FILES'] = (relative_resource_file_paths + relative_on_demand_resource_file_paths).sort_by(&:downcase)
    contents['RESOURCES'] = resource_dependencies.flatten.uniq.sort_by(&:downcase) if resource_dependencies.any?
  end
  TargetCacheKey.new(sandbox, :aggregate, contents)
end

.from_cache_hash(sandbox, key_hash) ⇒ TargetCacheKey

Creates a TargetCacheKey instance from the given hash.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/cocoapods/installer/project_cache/target_cache_key.rb', line 97

def self.from_cache_hash(sandbox, key_hash)
  cache_hash = key_hash.dup
  if files = cache_hash['FILES']
    cache_hash['FILES'] = files.sort_by(&:downcase)
  end
  if specs = cache_hash['SPECS']
    cache_hash['SPECS'] = specs.sort_by(&:downcase)
  end
  if resources = cache_hash['RESOURCES']
    cache_hash['RESOURCES'] = resources.sort_by(&:downcase)
  end
  type = cache_hash['CHECKSUM'] ? :pod_target : :aggregate
  TargetCacheKey.new(sandbox, type, cache_hash)
end

.from_pod_target(sandbox, target_by_label, pod_target, is_local_pod: false, checkout_options: nil) ⇒ TargetCacheKey

Constructs a TargetCacheKey instance from a PodTarget.



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
164
165
166
167
168
169
# File 'lib/cocoapods/installer/project_cache/target_cache_key.rb', line 130

def self.from_pod_target(sandbox, target_by_label, pod_target, is_local_pod: false, checkout_options: nil)
  build_settings = {}
  build_settings[pod_target.label.to_s] = Hash[pod_target.build_settings.map do |k, v|
    [k, Digest::MD5.hexdigest(v.xcconfig.to_s)]
  end]
  pod_target.test_spec_build_settings_by_config.each do |name, settings_by_config|
    build_settings[name] = Hash[settings_by_config.map { |k, v| [k, Digest::MD5.hexdigest(v.xcconfig.to_s)] }]
  end
  pod_target.app_spec_build_settings_by_config.each do |name, settings_by_config|
    build_settings[name] = Hash[settings_by_config.map { |k, v| [k, Digest::MD5.hexdigest(v.xcconfig.to_s)] }]
  end

  # find resources from upstream dependencies that will be named in the `{name}-resources.sh` script
  resource_dependencies = []
  pod_target.dependencies.each do |name|
    next unless target_by_label[name]

    upstream = target_by_label[name]

    # pod target
    resource_dependencies.append(upstream.resource_paths.values.flatten) if upstream.respond_to?(:resource_paths)

    # aggregate target
    resource_dependencies.append(upstream.resource_paths_by_config.values.flatten) if upstream.respond_to?(:resource_paths_by_config)
  end

  contents = {
    'CHECKSUM' => pod_target.root_spec.checksum,
    'SPECS' => pod_target.specs.map(&:to_s).sort_by(&:downcase),
    'BUILD_SETTINGS_CHECKSUM' => build_settings,
    'PROJECT_NAME' => pod_target.project_name,
  }
  if is_local_pod
    relative_file_paths = pod_target.all_files.map { |f| f.relative_path_from(sandbox.root).to_s }
    contents['FILES'] = relative_file_paths.sort_by(&:downcase)
  end
  contents['CHECKOUT_OPTIONS'] = checkout_options if checkout_options
  contents['RESOURCES'] = resource_dependencies.flatten.uniq.sort_by(&:downcase) if resource_dependencies.any?
  TargetCacheKey.new(sandbox, :pod_target, contents)
end

Instance Method Details

#key_difference(other) ⇒ Symbol

Equality function used to compare TargetCacheKey objects to each other.



45
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
# File 'lib/cocoapods/installer/project_cache/target_cache_key.rb', line 45

def key_difference(other)
  if other.type != type
    :project
  else
    case type
    when :pod_target
      return :project if (other.key_hash.keys - key_hash.keys).any?
      return :project if other.key_hash['CHECKSUM'] != key_hash['CHECKSUM']
      return :project if other.key_hash['SPECS'] != key_hash['SPECS']
      return :project if other.key_hash['PROJECT_NAME'] != key_hash['PROJECT_NAME']
    end

    this_files = key_hash['FILES']
    other_files = other.key_hash['FILES']
    return :project if this_files != other_files

    this_resources = key_hash['RESOURCES']
    other_resources = other.key_hash['RESOURCES']
    return :project if this_resources != other_resources

    this_build_settings = key_hash['BUILD_SETTINGS_CHECKSUM']
    other_build_settings = other.key_hash['BUILD_SETTINGS_CHECKSUM']
    return :project if this_build_settings != other_build_settings

    this_checkout_options = key_hash['CHECKOUT_OPTIONS']
    other_checkout_options = other.key_hash['CHECKOUT_OPTIONS']
    return :project if this_checkout_options != other_checkout_options

    :none
  end
end

#project_nameString



84
85
86
# File 'lib/cocoapods/installer/project_cache/target_cache_key.rb', line 84

def project_name
  key_hash['PROJECT_NAME']
end

#to_hObject



77
78
79
# File 'lib/cocoapods/installer/project_cache/target_cache_key.rb', line 77

def to_h
  key_hash
end