Class: Pod::Source::Manager

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

Master repo collapse

Instance Attribute Summary collapse

Master repo collapse

Instance Method Summary collapse

Constructor Details

#initialize(repos_dir) ⇒ Manager



9
10
11
# File 'lib/cocoapods-core/source/manager.rb', line 9

def initialize(repos_dir)
  @repos_dir = Pathname(repos_dir).expand_path
end

Instance Attribute Details

#repos_dirPathname (readonly)



7
8
9
# File 'lib/cocoapods-core/source/manager.rb', line 7

def repos_dir
  @repos_dir
end

#search_index_pathPathname



275
276
277
# File 'lib/cocoapods-core/source/manager.rb', line 275

def search_index_path
  @search_index_path
end

#updated_search_indexHash{String => Hash{String => Array<String>}}

Returns the search data. If a saved search data exists, retrieves it from file and returns it. Else, creates the search data from scratch, saves it to file system, and returns it. Search data is grouped by source repos. For each source, it contains a hash where keys are words and values are the pod names containing corresponding word.

For each source, list of unique words are generated from the following spec information.

- version
- summary
- description
- authors


186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/cocoapods-core/source/manager.rb', line 186

def updated_search_index
  index = stored_search_index || {}
  all.each do |source|
    source_name = source.name
    unless index[source_name]
      CoreUI.print "Creating search index for spec repo '#{source_name}'.."
      index[source_name] = aggregate.generate_search_index_for_source(source)
      CoreUI.puts ' Done!'
    end
  end
  save_search_index(index)
  index
end

Instance Method Details

#aggregateSource::Aggregate



23
24
25
# File 'lib/cocoapods-core/source/manager.rb', line 23

def aggregate
  aggregate_with_repos(source_repos)
end

#aggregate_for_dependency(dependency) ⇒ Source::Aggregate



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

def aggregate_for_dependency(dependency)
  if dependency.podspec_repo
    source = source_with_url(dependency.podspec_repo)
    raise StandardError, '[Bug] Failed to find known source with the URL ' \
      "#{dependency.podspec_repo.inspect}" if source.nil?

    aggregate_with_repos([source.repo])
  else
    aggregate
  end
end

#allArray<Source>



60
61
62
# File 'lib/cocoapods-core/source/manager.rb', line 60

def all
  aggregate.sources
end

#masterArray<Source>



66
67
68
# File 'lib/cocoapods-core/source/manager.rb', line 66

def master
  sources(['master']).select { |s| s.repo.directory? }
end

#master_repo_dirPathname



74
75
76
# File 'lib/cocoapods-core/source/manager.rb', line 74

def master_repo_dir
  source_dir('master')
end

#master_repo_functional?Bool

Note:

Note this is used to automatically setup the master repo if needed.

Returns Checks if the master repo is usable.



83
84
85
86
# File 'lib/cocoapods-core/source/manager.rb', line 83

def master_repo_functional?
  return false unless master_repo = master.first
  master_repo..compatible?(CORE_VERSION)
end

#save_search_index(index) ⇒ Object

Stores given search data in the file system.



261
262
263
264
265
266
267
# File 'lib/cocoapods-core/source/manager.rb', line 261

def save_search_index(index)
  require 'json'
  @updated_search_index = index
  search_index_path.open('w') do |io|
    io.write(@updated_search_index.to_json)
  end
end

#search(dependency) ⇒ Set?

Search the appropriate sources to match the set for the given dependency.

Raises:

  • If no source can be found that includes the dependency.



96
97
98
# File 'lib/cocoapods-core/source/manager.rb', line 96

def search(dependency)
  aggregate_for_dependency(dependency).search(dependency)
end

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

Search all the sources with the given search term.

Raises:

  • If no source including the set can be found.



114
115
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
# File 'lib/cocoapods-core/source/manager.rb', line 114

def search_by_name(query, full_text_search = false)
  query_word_regexps = query.split.map { |word| /#{word}/i }
  if full_text_search
    query_word_results_hash = {}
    updated_search_index.each_value do |word_spec_hash|
      word_spec_hash.each_pair do |word, spec_names|
        query_word_regexps.each do |query_word_regexp|
          set = (query_word_results_hash[query_word_regexp] ||= Set.new)
          set.merge(spec_names) if word =~ query_word_regexp
        end
      end
    end
    found_set_names = query_word_results_hash.values.reduce(:&)
    found_set_names ||= []
    sets = found_set_names.map do |name|
      aggregate.representative_set(name)
    end
    # Remove nil values because representative_set return nil if no pod is found in any of the sources.
    sets.compact!
  else
    sets = aggregate.search_by_name(query, false)
  end
  if sets.empty?
    extra = ', author, summary, or description' if full_text_search
    raise Informative, "Unable to find a pod with name#{extra} " \
      "matching `#{query}`"
  end
  sorted_sets(sets, query_word_regexps)
end

#sorted_sets(sets, query_word_regexps) ⇒ Array<Set>

Returns given set array by sorting it in-place.



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/cocoapods-core/source/manager.rb', line 154

def sorted_sets(sets, query_word_regexps)
  sets.sort_by! do |set|
    pre_match_length = nil
    found_query_index = nil
    found_query_count = 0
    query_word_regexps.each_with_index do |q, idx|
      if (m = set.name.match(/#{q}/i))
        pre_match_length ||= m.pre_match.length
        found_query_index ||= idx
        found_query_count += 1
      end
    end
    pre_match_length ||= 1000
    found_query_index ||= 1000
    [-found_query_count, pre_match_length, found_query_index, set.name.downcase]
  end
  sets
end

#source_reposArray<Pathname>



15
16
17
18
# File 'lib/cocoapods-core/source/manager.rb', line 15

def source_repos
  return [] unless repos_dir.exist?
  repos_dir.children.select(&:directory?).sort_by { |d| d.basename.to_s.downcase }
end

#sources(names) ⇒ Array<Source>



52
53
54
55
# File 'lib/cocoapods-core/source/manager.rb', line 52

def sources(names)
  dirs = names.map { |name| source_dir(name) }
  dirs.map { |repo| source_from_path(repo) }
end

#stored_search_indexObject

Returns the search data stored in the file system. If existing data in the file system is not valid, returns nil.



247
248
249
250
251
252
253
254
255
# File 'lib/cocoapods-core/source/manager.rb', line 247

def stored_search_index
  @updated_search_index ||= begin
    if search_index_path.exist?
      require 'json'
      index = JSON.parse(search_index_path.read)
      index if index.is_a?(Hash) # TODO: should we also check if hash has correct hierarchy?
    end
  end
end

#update_search_index_if_needed(changed_spec_paths) ⇒ Object

Updates the stored search index if there are changes in spec repos while updating them. Update is performed incrementally. Only the changed pods’ search data is re-generated and updated.



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/cocoapods-core/source/manager.rb', line 205

def update_search_index_if_needed(changed_spec_paths)
  search_index = stored_search_index
  return unless search_index
  changed_spec_paths.each_pair do |source, spec_paths|
    index_for_source = search_index[source.name]
    next unless index_for_source && !spec_paths.empty?
    updated_pods = source.pods_for_specification_paths(spec_paths)

    new_index = aggregate.generate_search_index_for_changes_in_source(source, spec_paths)
    # First traverse search_index and update existing words
    # Remove traversed words from new_index after adding to search_index,
    # so that only non existing words will remain in new_index after enumeration completes.
    index_for_source.each_pair do |word, _|
      if new_index[word]
        index_for_source[word] |= new_index[word]
        new_index.delete(word)
      else
        index_for_source[word] -= updated_pods
      end
    end

    # Now add non existing words remained in new_index to search_index
    index_for_source.merge!(new_index)
  end
  save_search_index(search_index)
end

#update_search_index_if_needed_in_background(changed_spec_paths) ⇒ Object

Updates search index for changed pods in background



236
237
238
239
240
241
242
# File 'lib/cocoapods-core/source/manager.rb', line 236

def update_search_index_if_needed_in_background(changed_spec_paths)
  Process.fork do
    Process.daemon
    update_search_index_if_needed(changed_spec_paths)
    exit
  end
end