Class: Pod::Installer::Analyzer::PodVariantSet

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoapods/installer/analyzer/pod_variant_set.rb

Overview

Collects all PodVariant.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(variants) ⇒ PodVariantSet

Initialize a new instance.

Parameters:



16
17
18
# File 'lib/cocoapods/installer/analyzer/pod_variant_set.rb', line 16

def initialize(variants)
  @variants = variants
end

Instance Attribute Details

#variantsArray<PodVariant> (readonly)

Returns the different variants within this set.

Returns:

  • (Array<PodVariant>)

    the different variants within this set.



10
11
12
# File 'lib/cocoapods/installer/analyzer/pod_variant_set.rb', line 10

def variants
  @variants
end

Instance Method Details

#group_by(&block) ⇒ Array<PodVariantSet>

Groups the collection by result of the block.

Parameters:

  • block (Block<Variant, #hash>)

Returns:



38
39
40
# File 'lib/cocoapods/installer/analyzer/pod_variant_set.rb', line 38

def group_by(&block)
  variants.group_by(&block).map { |_, v| PodVariantSet.new(v) }
end

#scope_by_build_typeHash<PodVariant, String>

Returns:



73
74
75
76
77
# File 'lib/cocoapods/installer/analyzer/pod_variant_set.rb', line 73

def scope_by_build_type
  scope_if_necessary(group_by { |v| v.build_type.packaging }.map(&:scope_by_linkage)) do |variant|
    variant.build_type.packaging
  end
end

#scope_by_linkageHash<PodVariant, String>

Returns:



82
83
84
85
86
# File 'lib/cocoapods/installer/analyzer/pod_variant_set.rb', line 82

def scope_by_linkage
  scope_if_necessary(group_by { |v| v.build_type.linkage }.map(&:scope_by_platform)) do |variant|
    variant.build_type.linkage
  end
end

#scope_by_platformHash<PodVariant, String>

Returns:



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/cocoapods/installer/analyzer/pod_variant_set.rb', line 91

def scope_by_platform
  grouped_variants = group_by { |v| v.platform.name }
  if grouped_variants.all? { |set| set.variants.count == 1 }
    # => Platform name
    platform_name_proc = proc { |v| Platform.string_name(v.platform.symbolic_name).tr(' ', '') }
  else
    grouped_variants = group_by(&:platform)
    # => Platform name + SDK version
    platform_name_proc = proc { |v| v.platform.to_s.tr(' ', '') }
  end
  scope_if_necessary(grouped_variants.map(&:scope_by_swift_version), &platform_name_proc)
end

#scope_by_specsHash<PodVariant, String>

Returns:



116
117
118
119
120
121
122
123
124
125
126
127
128
129
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
# File 'lib/cocoapods/installer/analyzer/pod_variant_set.rb', line 116

def scope_by_specs
  root_spec = variants.first.root_spec
  specs = [root_spec]
  specs += if root_spec.default_subspecs.empty?
             root_spec.subspecs.compact
           else
             root_spec.default_subspecs.map do |subspec_name|
               root_spec.subspec_by_name("#{root_spec.name}/#{subspec_name}")
             end
           end
  default_specs = Set.new(specs)
  grouped_variants = group_by(&:specs)
  all_spec_variants = grouped_variants.map { |set| set.variants.first.specs }
  common_specs = all_spec_variants.map(&:to_set).flatten.inject(&:&)
  omit_common_specs = common_specs.any? && common_specs.proper_superset?(default_specs)
  scope_if_necessary(grouped_variants.map(&:scope_by_build_type)) do |variant|
    specs = variant.specs.to_set

    # The current variant contains all default specs
    omit_default_specs = default_specs.any? && default_specs.subset?(specs)
    if omit_default_specs
      specs -= default_specs
    end

    # There are common specs, which are different from the default specs
    if omit_common_specs
      specs -= common_specs
    end

    spec_names = specs.map do |spec|
      spec.root? ? '.root' : spec.name.split('/')[1..-1].join('_')
    end.sort
    if spec_names.empty?
      omit_common_specs ? '.common' : nil
    else
      if omit_common_specs
        spec_names.unshift('.common')
      elsif omit_default_specs
        spec_names.unshift('.default')
      end
      spec_names.reduce('') do |acc, name|
        "#{acc}#{acc.empty? || name[0] == '.' ? '' : '-'}#{name}"
      end
    end
  end
end

#scope_by_swift_versionHash<PodVariant, String>

Returns:



107
108
109
110
111
# File 'lib/cocoapods/installer/analyzer/pod_variant_set.rb', line 107

def scope_by_swift_version
  scope_if_necessary(group_by(&:swift_version).map(&:scope_without_suffix)) do |variant|
    variant.swift_version ? "Swift#{variant.swift_version}" : ''
  end
end

#scope_if_necessary(scoped_variants, &block) ⇒ Hash<PodVariant, String>

Prepends the given scoped Pod::Installer::Analyzer::PodVariants with another scoping label, if there was more than one group of Pod::Installer::Analyzer::PodVariants given.

Parameters:

Returns:



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cocoapods/installer/analyzer/pod_variant_set.rb', line 57

def scope_if_necessary(scoped_variants, &block)
  if scoped_variants.count == 1
    return scoped_variants.first
  end
  Hash[scoped_variants.flat_map do |variants|
    variants.map do |variant, suffix|
      prefix = block.call(variant)
      scope = [prefix, suffix].compact.join('-')
      [variant, !scope.empty? ? scope : nil]
    end
  end]
end

#scope_suffixesHash<PodVariant, String>

Describes what makes each Pod::Installer::Analyzer::PodVariant distinct among the others.

Returns:



24
25
26
27
28
29
30
31
# File 'lib/cocoapods/installer/analyzer/pod_variant_set.rb', line 24

def scope_suffixes
  return { variants.first => nil } if variants.count == 1
  Hash[scope_by_specs.map do |variant, scope|
    require 'digest'
    scope = Digest::MD5.hexdigest(scope)[0..7] if !scope.nil? && scope.length >= 50
    [variant, scope]
  end]
end

#scope_without_suffixHash<PodVariant, String>

Helps to define scope suffixes recursively.

Returns:



169
170
171
# File 'lib/cocoapods/installer/analyzer/pod_variant_set.rb', line 169

def scope_without_suffix
  Hash[variants.map { |v| [v, nil] }]
end