Class: Pod::Source::Aggregate

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoapods-core/source/aggregate.rb

Overview

The Aggregate manages a directory of sources repositories.

Instance Attribute Summary collapse

Search collapse

Search Index collapse

Private helpers collapse

Instance Method Summary collapse

Constructor Details

#initialize(repos_dirs) ⇒ Aggregate

Returns a new instance of Aggregate

Parameters:

  • repos_dirs (Array<Pathname>)

    @see directories


12
13
14
# File 'lib/cocoapods-core/source/aggregate.rb', line 12

def initialize(repos_dirs)
  @directories = Array(repos_dirs)
end

Instance Attribute Details

#directoriesArray<Pathname> (readonly)

Returns The ordered list of source directories.

Returns:

  • (Array<Pathname>)

    The ordered list of source directories.


8
9
10
# File 'lib/cocoapods-core/source/aggregate.rb', line 8

def directories
  @directories
end

Instance Method Details

#all_podsArray<String>

Returns the names of all the pods available.

Returns:

  • (Array<String>)

    the names of all the pods available.


24
25
26
# File 'lib/cocoapods-core/source/aggregate.rb', line 24

def all_pods
  sources.map(&:pods).flatten.uniq
end

#all_setsArray<Set>

Note:

Implementation detail: The sources don't cache their values because they might change in response to an update. Therefore this method to preserve performance caches the values before processing them.

Returns The sets for all the pods available.

Returns:

  • (Array<Set>)

    The sets for all the pods available.


35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cocoapods-core/source/aggregate.rb', line 35

def all_sets
  pods_by_source = {}
  sources.each do |source|
    pods_by_source[source] = source.pods
  end
  pods = pods_by_source.values.flatten.uniq

  pods.map do |pod|
    pod_sources = sources.select { |s| pods_by_source[s].include?(pod) }
    pod_sources = pod_sources.compact
    Specification::Set.new(pod, pod_sources)
  end
end

#generate_search_indexHash{String=>Hash}

Generates from scratch the search data for all the sources of the aggregate. This operation can take a considerable amount of time (seconds) as it needs to evaluate the most representative podspec for each Pod.

Returns:

  • (Hash{String=>Hash})

    The search data of every set grouped by name.


140
141
142
143
144
145
146
# File 'lib/cocoapods-core/source/aggregate.rb', line 140

def generate_search_index
  result = {}
  all_sets.each do |set|
    result[set.name] = search_data_from_set(set)
  end
  result
end

#representative_set(name) ⇒ Set

Returns a set configured with the source which contains the highest version in the aggregate.

Parameters:

  • name (String)

    The name of the Pod.

Returns:

  • (Set)

    The most representative set for the Pod with the given name.


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/cocoapods-core/source/aggregate.rb', line 58

def representative_set(name)
  representative_source = nil
  highest_version = nil
  sources.each do |source|
    source_versions = source.versions(name)
    if source_versions
      source_version = source_versions.first
      if highest_version.nil? || (highest_version < source_version)
        highest_version = source_version
        representative_source = source
      end
    end
  end
  Specification::Set.new(name, representative_source)
end

#search(dependency) ⇒ Set?

Returns a set for a given dependency including all the Pod::Source that contain the Pod. If no sources containing the Pod where found it returns nil.

Returns:

  • (Set, nil)

    a set for a given dependency including all the Pod::Source that contain the Pod. If no sources containing the Pod where found it returns nil.

Raises:

  • If no source including the set can be found.

See Also:


87
88
89
90
91
92
# File 'lib/cocoapods-core/source/aggregate.rb', line 87

def search(dependency)
  found_sources = sources.select { |s| s.search(dependency) }
  unless found_sources.empty?
    Specification::Set.new(dependency.root_name, found_sources)
  end
end

#search_by_name(query, full_text_search = false) ⇒ Array<Set>

TODO:

Clients should raise not this method.

Returns the sets that contain the search term.

Returns:

  • (Array<Set>)

    the sets that contain the search term.

Raises:

  • If no source including the set can be found.

See Also:


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/cocoapods-core/source/aggregate.rb', line 102

def search_by_name(query, full_text_search = false)
  pods_by_source = {}
  result = []
  sources.each do |s|
    source_pods = s.search_by_name(query, full_text_search)
    pods_by_source[s] = source_pods.map(&:name)
  end

  root_spec_names = pods_by_source.values.flatten.uniq
  root_spec_names.each do |pod|
    result_sources = sources.select do |source|
      pods_by_source[source].include?(pod)
    end

    result << Specification::Set.new(pod, result_sources)
  end

  if result.empty?
    extra = ', author, summary, or description' if full_text_search
    raise Informative, 'Unable to find a pod with name' \
      "#{extra} matching `#{query}'"
  end
  result
end

#search_data_from_set(set) ⇒ Hash{String=>String} (private)

Note:

If the specification can't load an empty hash is returned and a warning is printed.

Note:

For compatibility with non Ruby clients a strings are used instead of symbols for the keys.

Returns the search related information from the most representative specification of the set following keys:

  • version
  • summary
  • description
  • authors

Parameters:

  • set (Set)

    The set for which the information is needed.

Returns:

  • (Hash{String=>String})

    A hash with the search information.


209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/cocoapods-core/source/aggregate.rb', line 209

def search_data_from_set(set)
  result = {}
  spec = set.specification
  result['version'] = spec.version.to_s
  result['summary'] = spec.summary
  result['description'] = spec.description
  result['authors'] = spec.authors.keys.sort * ', '
  result
rescue
  CoreUI.warn "Skipping `#{set.name}` because the podspec contains " \
    'errors.'
  result
end

#sourcesArray<Source>

Returns The ordered list of the sources.

Returns:

  • (Array<Source>)

    The ordered list of the sources.


18
19
20
# File 'lib/cocoapods-core/source/aggregate.rb', line 18

def sources
  @sources ||= directories.map { |repo| Source.new(repo) }
end

#update_search_index(search_data) ⇒ Hash{String=>Hash}

Note:

This procedure is considerably faster as it only needs to load the most representative spec of the new or updated Pods.

Updates inline the given search data with the information stored in all the sources. The update skips the Pods for which the version of the search data is the same of the highest version known to the aggregate. This can lead to updates in podspecs being skipped until a new version is released.

Returns:

  • (Hash{String=>Hash})

    The search data of every set grouped by name.


160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/cocoapods-core/source/aggregate.rb', line 160

def update_search_index(search_data)
  enumerated_names = []
  all_sets.each do |set|
    enumerated_names << set.name
    set_data = search_data[set.name]
    has_data = set_data && set_data['version']
    if has_data
      stored_version = Version.new(set_data['version'])
      if stored_version < set.highest_version
        search_data[set.name] = search_data_from_set(set)
      end
    else
      search_data[set.name] = search_data_from_set(set)
    end
  end

  stored_names = search_data.keys
  delted_names = stored_names - enumerated_names
  delted_names.each do |name|
    search_data.delete(name)
  end

  search_data
end