Class: Pod::CDNSource

Inherits:
Source
  • Object
show all
Defined in:
lib/cocoapods-core/cdn_source.rb

Overview

Subclass of Pod::Source to provide support for CDN-based Specs repositories

Direct Known Subclasses

TrunkSource

Constant Summary collapse

MAX_CDN_NETWORK_THREADS =
(ENV['MAX_CDN_NETWORK_THREADS'] || 50).to_i
MAX_NUMBER_OF_RETRIES =
(ENV['COCOAPODS_CDN_MAX_NUMBER_OF_RETRIES'] || 5).to_i

Constants inherited from Source

Source::DEFAULT_SPECS_BRANCH

Instance Attribute Summary

Attributes inherited from Source

#metadata, #repo

Querying the source collapse

Searching the source collapse

Instance Method Summary collapse

Methods inherited from Source

#<=>, #fuzzy_search, #inspect, #metadata_path, #name, #pod_path, #pods_for_specification_paths, #set, #specification, #to_hash, #to_yaml, #verify_compatibility!

Constructor Details

#initialize(repo) ⇒ CDNSource

Returns a new instance of CDNSource.

Parameters:

  • repo (String)

    The name of the repository



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cocoapods-core/cdn_source.rb', line 14

def initialize(repo)
  @check_existing_files_for_update = false
  # Optimization: we initialize startup_time when the source is first initialized
  # and then test file modification dates against it. Any file that was touched
  # after the source was initialized, is considered fresh enough.
  @startup_time = Time.new

  @executor = Concurrent::ThreadPoolExecutor.new(
    :min_threads => 5,
    :max_threads => MAX_CDN_NETWORK_THREADS,
    :max_queue => 0 # unbounded work queue
  )

  @version_arrays_by_fragment_by_name = {}

  super(repo)
end

Instance Method Details

#all_specsArray<Specification>

Returns all the specifications contained by the source.

Returns:

  • (Array<Specification>)

    all the specifications contained by the source.

Raises:



173
174
175
# File 'lib/cocoapods-core/cdn_source.rb', line 173

def all_specs
  raise Informative, "Can't retrieve all the specs for a CDN-backed source, it will take forever"
end

#deprecated_local_podspecsObject



76
77
78
79
80
81
# File 'lib/cocoapods-core/cdn_source.rb', line 76

def deprecated_local_podspecs
  download_file('deprecated_podspecs.txt')
  local_file('deprecated_podspecs.txt', &:to_a).
    map { |f| Pathname.new(f.chomp) }.
    select { |f| repo.join(f).exist? }
end

#files_definitely_to_updateObject



72
73
74
# File 'lib/cocoapods-core/cdn_source.rb', line 72

def files_definitely_to_update
  Pathname.glob(repo.join('**/*.{txt,yml}')).map { |f| f.relative_path_from(repo).to_s }
end

#git?Boolean

Returns:

  • (Boolean)


264
265
266
# File 'lib/cocoapods-core/cdn_source.rb', line 264

def git?
  false
end

#indexable?Boolean

Returns:

  • (Boolean)


268
269
270
# File 'lib/cocoapods-core/cdn_source.rb', line 268

def indexable?
  false
end

#pod_setsArray<Sets>

Returns the sets of all the Pods.

Returns:

  • (Array<Sets>)

    the sets of all the Pods.

Raises:



179
180
181
# File 'lib/cocoapods-core/cdn_source.rb', line 179

def pod_sets
  raise Informative, "Can't retrieve all the pod sets for a CDN-backed source, it will take forever"
end

#podsArray<String>

Returns the list of the name of all the Pods.

Returns:

  • (Array<String>)

    the list of the name of all the Pods.



94
95
96
97
# File 'lib/cocoapods-core/cdn_source.rb', line 94

def pods
  download_file('all_pods.txt')
  local_file('all_pods.txt', &:to_a).map(&:chomp)
end

#preheat_existing_filesObject



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

def preheat_existing_files
  files_to_update = files_definitely_to_update + deprecated_local_podspecs - ['deprecated_podspecs.txt']
  debug "CDN: #{name} Going to update #{files_to_update.count} files"
  loaders = files_to_update.map do |file|
    Concurrent::Promises.future_on(@executor) do
      download_file(file)
    end
  end

  catching_concurrent_errors do
    Concurrent::Promises.zip(*loaders).wait!
  end
end

#refresh_metadataObject



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cocoapods-core/cdn_source.rb', line 44

def 
  if .nil?
    unless repo.exist?
      debug "CDN: Repo #{name} does not exist!"
      return
    end

    specs_dir.mkpath
    download_file('CocoaPods-version.yml')
  end

  super
end

#search(query) ⇒ Set

TODO:

Rename to #load_set

Note:

This method is optimized for fast lookups by name, i.e. it does not require iterating through #pod_sets

Returns a set for a given dependency. The set is identified by the name of the dependency and takes into account subspecs.

Returns:

  • (Set)

    a set for a given dependency. The set is identified by the name of the dependency and takes into account subspecs.



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/cocoapods-core/cdn_source.rb', line 194

def search(query)
  unless specs_dir
    raise Informative, "Unable to find a source named: `#{name}`"
  end
  if query.is_a?(Dependency)
    query = query.root_name
  end

  fragment = pod_shard_fragment(query)

  ensure_versions_file_loaded(fragment)

  version_arrays_by_name = @version_arrays_by_fragment_by_name[fragment] || {}

  found = version_arrays_by_name[query].nil? ? nil : query

  if found
    set = set(query)
    set if set.specification_name == query
  end
end

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

Note:

full text search requires to load the specification for each pod, and therefore not supported.

Returns The list of the sets that contain the search term.

Parameters:

  • query (String)

    the search term. Can be a regular expression.

  • full_text_search (Bool) (defaults to: false)

    performed using Algolia

Returns:

  • (Array<Set>)

    The list of the sets that contain the search term.



227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/cocoapods-core/cdn_source.rb', line 227

def search_by_name(query, full_text_search = false)
  if full_text_search
    require 'algoliasearch'
    begin
      algolia_result = algolia_search_index.search(query, :attributesToRetrieve => 'name')
      names = algolia_result['hits'].map { |r| r['name'] }
      names.map { |n| set(n) }.reject { |s| s.versions.compact.empty? }
    rescue Algolia::AlgoliaError => e
      raise Informative, "CDN: #{name} - Cannot perform full-text search because Algolia returned an error: #{e}"
    end
  else
    super(query)
  end
end

#specification_path(name, version) ⇒ Pathname

Returns the path of the specification with the given name and version.

Parameters:

  • name (String)

    the name of the Pod.

  • version (Version, String)

    the version for the specification.

Returns:

  • (Pathname)

    The path of the specification.

Raises:

  • (ArgumentError)


156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/cocoapods-core/cdn_source.rb', line 156

def specification_path(name, version)
  raise ArgumentError, 'No name' unless name
  raise ArgumentError, 'No version' unless version
  unless versions(name).include?(Version.new(version))
    raise StandardError, "Unable to find the specification #{name} " \
      "(#{version}) in the #{self.name} source."
  end

  podspec_version_path_relative = Pathname.new(version.to_s).join("#{name}.podspec.json")
  relative_podspec = relative_pod_path(name).join(podspec_version_path_relative).to_s
  download_file(relative_podspec)
  pod_path(name).join(podspec_version_path_relative)
end

#specs_dirPathname

Returns The directory where the specs are stored.

Returns:

  • (Pathname)

    The directory where the specs are stored.



85
86
87
# File 'lib/cocoapods-core/cdn_source.rb', line 85

def specs_dir
  @specs_dir ||= repo + 'Specs'
end

#typeString

Returns The type of the source.

Returns:

  • (String)

    The type of the source.



40
41
42
# File 'lib/cocoapods-core/cdn_source.rb', line 40

def type
  'CDN'
end

#update(_show_output) ⇒ Array<String>

Check update dates for all existing files. Does not download non-existing specs, since CDN-backed repo is updated live.

Parameters:

  • show_output (Bool)

Returns:

  • (Array<String>)

    Always returns empty array, as it cannot know everything that actually changed.



250
251
252
253
254
255
256
257
258
# File 'lib/cocoapods-core/cdn_source.rb', line 250

def update(_show_output)
  @check_existing_files_for_update = true
  begin
    preheat_existing_files
  ensure
    @check_existing_files_for_update = false
  end
  []
end

#updateable?Boolean

Returns:

  • (Boolean)


260
261
262
# File 'lib/cocoapods-core/cdn_source.rb', line 260

def updateable?
  true
end

#urlString

Returns The URL of the source.

Returns:

  • (String)

    The URL of the source.



34
35
36
# File 'lib/cocoapods-core/cdn_source.rb', line 34

def url
  @url ||= File.read(repo.join('.url')).chomp.chomp('/') + '/'
end

#versions(name) ⇒ Array<Version>

Returns all the available versions for the Pod, sorted from highest to lowest.

Parameters:

  • name (String)

    the name of the Pod.

Returns:

  • (Array<Version>)

    all the available versions for the Pod, sorted from highest to lowest.

Raises:

  • (ArgumentError)


105
106
107
108
109
110
111
112
113
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
143
144
# File 'lib/cocoapods-core/cdn_source.rb', line 105

def versions(name)
  return nil unless specs_dir
  raise ArgumentError, 'No name' unless name

  fragment = pod_shard_fragment(name)

  ensure_versions_file_loaded(fragment)

  return @versions_by_name[name] unless @versions_by_name[name].nil?

  pod_path_actual = pod_path(name)
  pod_path_relative = relative_pod_path(name)

  return nil if @version_arrays_by_fragment_by_name[fragment][name].nil?

  loaders = []
  @versions_by_name[name] ||= @version_arrays_by_fragment_by_name[fragment][name].map do |version|
    # Optimization: ensure all the podspec files at least exist. The correct one will get refreshed
    # in #specification_path regardless.
    podspec_version_path_relative = Pathname.new(version).join("#{name}.podspec.json")
    unless pod_path_actual.join(podspec_version_path_relative).exist?
      loaders << Concurrent::Promises.future_on(@executor) do
        download_file(pod_path_relative.join(podspec_version_path_relative).to_s)
      end
    end
    begin
      Version.new(version) if version[0, 1] != '.'
    rescue ArgumentError
      raise Informative, 'An unexpected version directory ' \
      "`#{version}` was encountered for the " \
      "`#{pod_path_actual}` Pod in the `#{name}` repository."
    end
  end.compact.sort.reverse

  catching_concurrent_errors do
    Concurrent::Promises.zip(*loaders).wait!
  end

  @versions_by_name[name]
end