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

Instance Method Summary collapse

Constructor Details

#initialize(repos_dir) ⇒ Aggregate

Returns a new instance of Aggregate.

Parameters:

  • repos_dir (Pathname)

    @see repos_dir.



14
15
16
# File 'lib/cocoapods-core/source/aggregate.rb', line 14

def initialize(repos_dir)
  @repos_dir = repos_dir
end

Instance Attribute Details

#repos_dirPathname (readonly)

Returns the directory were the repositories are stored.

Returns:

  • (Pathname)

    the directory were the repositories are stored.



10
11
12
# File 'lib/cocoapods-core/source/aggregate.rb', line 10

def repos_dir
  @repos_dir
end

Instance Method Details

#allArray<Source>

Returns all the sources.

Returns:

  • (Array<Source>)

    all the sources.



20
21
22
# File 'lib/cocoapods-core/source/aggregate.rb', line 20

def all
  @sources ||= dirs.map { |repo| Source.new(repo) }.sort_by(&:name)
end

#all_podsArray<String>

Returns the names of all the pods available.

Returns:

  • (Array<String>)

    the names of all the pods available.



26
27
28
# File 'lib/cocoapods-core/source/aggregate.rb', line 26

def all_pods
  all.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 prevent slowness caches the values before processing them.

Returns the sets for all the pods available.

Returns:

  • (Array<Set>)

    the sets for all the pods available.



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

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

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

#dirsArray<Pathname>

Note:

If the repos dir doesn’t exits this will return an empty array.

Returns the directories where the sources are stored.

Returns:

  • (Array<Pathname>)

    the directories where the sources are stored.

Raises:

  • If the repos dir doesn’t exits.



57
58
59
60
61
62
63
# File 'lib/cocoapods-core/source/aggregate.rb', line 57

def dirs
  if repos_dir.exist?
    repos_dir.children.select(&:directory?)
  else
    []
  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.



147
148
149
150
151
152
153
# File 'lib/cocoapods-core/source/aggregate.rb', line 147

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

#represenative_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.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/cocoapods-core/source/aggregate.rb', line 74

def represenative_set(name)
  representative_source = nil
  highest_version = nil
  all.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:



103
104
105
106
# File 'lib/cocoapods-core/source/aggregate.rb', line 103

def search(dependency)
  sources = all.select { |s| !s.search(dependency).nil? }
  Specification::Set.new(dependency.root_name, sources) unless sources.empty?
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:



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/cocoapods-core/source/aggregate.rb', line 116

def search_by_name(query, full_text_search = false)
  pods_by_source = {}
  result = []
  all.each { |s| pods_by_source[s] = s.search_by_name(query, full_text_search).map(&:name) }
  root_spec_names = pods_by_source.values.flatten.uniq
  root_spec_names.each do |pod|
    sources = []
    pods_by_source.each{ |source, pods| sources << source if pods.include?(pod) }
    result << Specification::Set.new(pod, 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

#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.



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/cocoapods-core/source/aggregate.rb', line 167

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']
    needs_update = !has_data || Version.new(set_data['version']) < set.required_version
    if needs_update
      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