Class: Pod::Generate::PodfileGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoapods/generate/podfile_generator.rb

Overview

Generates podfiles for pod specifications given a configuration.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ PodfileGenerator

Returns a new instance of PodfileGenerator.



13
14
15
# File 'lib/cocoapods/generate/podfile_generator.rb', line 13

def initialize(configuration)
  @configuration = configuration
end

Instance Attribute Details

#configurationConfiguration (readonly)

Returns the configuration used when generating podfiles.

Returns:

  • (Configuration)

    the configuration used when generating podfiles



11
12
13
# File 'lib/cocoapods/generate/podfile_generator.rb', line 11

def configuration
  @configuration
end

Instance Method Details

#dependency_compilation_kwargs(pod_name) ⇒ Hash

Returns a hash with “compilation”-related dependency options for the ‘pod` DSL method.

Parameters:

  • pod_name (String)

Returns:

  • (Hash)

    a hash with “compilation”-related dependency options for the ‘pod` DSL method



193
194
195
196
197
198
# File 'lib/cocoapods/generate/podfile_generator.rb', line 193

def dependency_compilation_kwargs(pod_name)
  options = {}
  options[:inhibit_warnings] = inhibit_warnings?(pod_name) if inhibit_warnings?(pod_name) != inhibit_all_warnings?
  options[:modular_headers] = modular_headers?(pod_name) if modular_headers?(pod_name) != use_modular_headers?
  options
end

#inhibit_all_warnings?Boolean

Returns whether all warnings should be inhibited.

Returns:

  • (Boolean)

    whether all warnings should be inhibited



164
165
166
167
168
169
# File 'lib/cocoapods/generate/podfile_generator.rb', line 164

def inhibit_all_warnings?
  return false unless configuration.use_podfile?
  target_definition_list.all? do |target_definition|
    target_definition.send(:inhibit_warnings_hash)['all']
  end
end

#installation_optionsObject



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/cocoapods/generate/podfile_generator.rb', line 295

def installation_options
  installation_options = {
    deterministic_uuids: configuration.deterministic_uuids?,
    share_schemes_for_development_pods: configuration.share_schemes_for_development_pods?,
    warn_for_multiple_pod_sources: configuration.warn_for_multiple_pod_sources?
  }

  if Pod::Installer::InstallationOptions.all_options.include?('generate_multiple_pod_projects')
    installation_options[:generate_multiple_pod_projects] = configuration.generate_multiple_pod_projects?
  end

  if Pod::Installer::InstallationOptions.all_options.include?('incremental_installation')
    installation_options[:incremental_installation] = configuration.incremental_installation?
  end

  installation_options
end

#lockfile_versionsHash<String,String>

Returns versions in the lockfile keyed by pod name.

Returns:

  • (Hash<String,String>)

    versions in the lockfile keyed by pod name



239
240
241
242
# File 'lib/cocoapods/generate/podfile_generator.rb', line 239

def lockfile_versions
  return {} unless configuration.use_lockfile_versions?
  @lockfile_versions ||= Hash[configuration.lockfile.pod_names.map { |name| [name, "= #{configuration.lockfile.version(name)}"] }]
end

#pod_args_for_dependency(podfile, dependency) ⇒ Hash<String,Array<Dependency>>

Returns the arguments that should be passed to the Podfile DSL’s ‘pod` method for the given podfile and dependency

Parameters:

  • podfile (Podfile)
  • dependency (Dependency)

Returns:

  • (Hash<String,Array<Dependency>>)

    returns the arguments that should be passed to the Podfile DSL’s ‘pod` method for the given podfile and dependency



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/cocoapods/generate/podfile_generator.rb', line 252

def pod_args_for_dependency(podfile, dependency)
  dependency = podfile_dependencies[dependency.root_name]
               .map { |dep| dep.dup.tap { |d| d.name = dependency.name } }
               .push(dependency)
               .reduce(&:merge)

  options = dependency_compilation_kwargs(dependency.name)
  options[:source] = dependency.podspec_repo if dependency.podspec_repo
  options.update(dependency.external_source) if dependency.external_source
  %i[path podspec].each do |key|
    next unless (path = options[key])
    options[key] = Pathname(path)
                   .expand_path(configuration.podfile.defined_in_file.dirname)
                   .relative_path_from(podfile.defined_in_file.dirname)
                   .to_s
  end
  args = [dependency.name]
  if dependency.external_source.nil?
    requirements = dependency.requirement.as_list
    if (version = lockfile_versions[dependency.name])
      requirements << version
    end
    args.concat requirements.uniq
  end
  args << options unless options.empty?
  args
end

#podfile_dependenciesHash<String,Array<Dependency>>

Returns dependencies in the podfile grouped by root name.

Returns:

  • (Hash<String,Array<Dependency>>)

    dependencies in the podfile grouped by root name



231
232
233
234
# File 'lib/cocoapods/generate/podfile_generator.rb', line 231

def podfile_dependencies
  return {} unless configuration.use_podfile?
  @podfile_dependencies ||= configuration.podfile.dependencies.group_by(&:root_name).tap { |h| h.default = [] }
end

#podfile_for_spec(spec) ⇒ Podfile

Returns a podfile suitable for installing the given spec.

Parameters:

  • spec (Specification)

Returns:

  • (Podfile)

    a podfile suitable for installing the given spec



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
# File 'lib/cocoapods/generate/podfile_generator.rb', line 30

def podfile_for_spec(spec)
  generator = self
  dir = configuration.gen_dir_for_pod(spec.name)

  Pod::Podfile.new do
    project "#{spec.name}.xcodeproj"
    workspace "#{spec.name}.xcworkspace"

    plugin 'cocoapods-generate'

    install! 'cocoapods', generator.installation_options

    generator.podfile_plugins.each do |name, options|
      plugin(*[name, options].compact)
    end

    use_frameworks!(generator.configuration.use_frameworks?)

    if (supported_swift_versions = generator.supported_swift_versions)
      supports_swift_versions(supported_swift_versions)
    end

    # Explicitly set sources
    generator.configuration.sources.each do |source_url|
      source(source_url)
    end

    self.defined_in_file = dir.join('CocoaPods.podfile.yaml')

    test_specs = spec.recursive_subspecs.select(&:test_specification?)
    app_specs = if spec.respond_to?(:app_specification?)
                  spec.recursive_subspecs.select(&:app_specification?)
                else
                  []
                end

    # Stick all of the transitive dependencies in an abstract target.
    # This allows us to force CocoaPods to use the versions / sources / external sources
    # that we want.
    # By using an abstract target,
    abstract_target 'Transitive Dependencies' do
      pods_for_transitive_dependencies = [spec.name]
                                         .concat(test_specs.map(&:name))
                                         .concat(test_specs.flat_map { |ts| ts.dependencies.flat_map(&:name) })
                                         .concat(app_specs.map(&:name))
                                         .concat(app_specs.flat_map { |as| as.dependencies.flat_map(&:name) })

      dependencies = generator
                     .transitive_dependencies_by_pod
                     .values_at(*pods_for_transitive_dependencies)
                     .compact
                     .flatten(1)
                     .uniq
                     .sort_by(&:name)
                     .reject { |d| d.root_name == spec.root.name }

      dependencies.each do |dependency|
        pod_args = generator.pod_args_for_dependency(self, dependency)
        pod(*pod_args)
      end
    end

    # Add platform-specific concrete targets that inherit the
    # `pod` declaration for the local pod.
    spec.available_platforms.map(&:string_name).sort.each do |platform_name|
      target "App-#{platform_name}" do
        current_target_definition.swift_version = generator.swift_version if generator.swift_version
      end
    end

    # this block has to come _before_ inhibit_all_warnings! / use_modular_headers!,
    # and the local `pod` declaration
    current_target_definition.instance_exec do
      transitive_dependencies = children.find { |c| c.name == 'Transitive Dependencies' }

      %w[use_modular_headers inhibit_warnings].each do |key|
        value = transitive_dependencies.send(:internal_hash).delete(key)
        next if value.blank?
        set_hash_value(key, value)
      end
    end

    inhibit_all_warnings! if generator.inhibit_all_warnings?
    use_modular_headers! if generator.use_modular_headers?

    # This is the pod declaration for the local pod,
    # it will be inherited by the concrete target definitions below
    pod_options = generator.dependency_compilation_kwargs(spec.name)
    pod_options[:path] = spec.defined_in_file.relative_path_from(dir).to_s
    { testspecs: test_specs, appspecs: app_specs }.each do |key, specs|
      pod_options[key] = specs.map { |s| s.name.sub(%r{^#{Regexp.escape spec.root.name}/}, '') }.sort unless specs.empty?
    end

    pod spec.name, **pod_options

    # Implement local-sources option to set up dependencies to podspecs in the local filesystem.
    next if generator.configuration.local_sources.empty?
    generator.transitive_local_dependencies(spec, generator.configuration.local_sources).each do |dependency, podspec_file|
      pod_options = generator.dependency_compilation_kwargs(dependency.name)
      pod_options[:path] = if podspec_file[0] == '/' # absolute path
                             podspec_file
                           else
                             '../../' + podspec_file
                           end
      pod dependency.name, **pod_options
    end
  end
end

#podfile_pluginsObject



313
314
315
316
317
# File 'lib/cocoapods/generate/podfile_generator.rb', line 313

def podfile_plugins
  configuration.podfile_plugins.merge('cocoapods-disable-podfile-validations' => { 'no_abstract_only_pods' => true }) do |_key, old_value, new_value|
    old_value.merge(new_value)
  end
end

#podfiles_by_specHash<Specification, Podfile>

Returns a hash of specifications to generated podfiles.

Returns:

  • (Hash<Specification, Podfile>)

    a hash of specifications to generated podfiles



20
21
22
23
24
# File 'lib/cocoapods/generate/podfile_generator.rb', line 20

def podfiles_by_spec
  Hash[configuration.podspecs.map do |spec|
    [spec, podfile_for_spec(spec)]
  end]
end

#supported_swift_versionsObject



284
285
286
287
288
289
290
291
292
293
# File 'lib/cocoapods/generate/podfile_generator.rb', line 284

def supported_swift_versions
  return unless configuration.use_podfile?
  return if target_definition_list.empty?
  return unless target_definition_list.first.respond_to?(:swift_version_requirements)
  target_definition_list.reduce(nil) do |supported_swift_versions, target_definition|
    target_swift_versions = target_definition.swift_version_requirements
    next supported_swift_versions unless target_swift_versions
    Array(target_swift_versions) | Array(supported_swift_versions)
  end
end

#swift_versionObject



280
281
282
# File 'lib/cocoapods/generate/podfile_generator.rb', line 280

def swift_version
  @swift_version ||= target_definition_list.map(&:swift_version).compact.max
end

#transitive_dependencies_by_podHash<String,Array<Dependency>>

Returns the transitive dependency objects dependency upon by each pod.

Returns:

  • (Hash<String,Array<Dependency>>)

    the transitive dependency objects dependency upon by each pod



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/cocoapods/generate/podfile_generator.rb', line 203

def transitive_dependencies_by_pod
  return {} unless configuration.use_lockfile?
  @transitive_dependencies_by_pod ||= begin
    lda = ::Pod::Installer::Analyzer::LockingDependencyAnalyzer
    dependency_graph = Molinillo::DependencyGraph.new
    configuration.lockfile.dependencies.each do |dependency|
      dependency_graph.add_vertex(dependency.name, dependency, true)
    end
    add_to_dependency_graph = if lda.method(:add_to_dependency_graph).parameters.size == 4 # CocoaPods < 1.6.0
                                ->(pod) { lda.add_to_dependency_graph(pod, [], dependency_graph, []) }
                              else
                                ->(pod) { lda.add_to_dependency_graph(pod, [], dependency_graph, [], Set.new) }
                              end
    configuration.lockfile.internal_data['PODS'].each(&add_to_dependency_graph)

    transitive_dependencies_by_pod = Hash.new { |hash, key| hash[key] = [] }
    dependency_graph.each do |v|
      transitive_dependencies_by_pod[v.name].concat v.recursive_successors.map(&:payload) << v.payload
    end

    transitive_dependencies_by_pod.each_value(&:uniq!)
    transitive_dependencies_by_pod
  end
end

#transitive_local_dependencies(spec, paths) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/cocoapods/generate/podfile_generator.rb', line 139

def transitive_local_dependencies(spec, paths)
  dependencies = spec.dependencies
  return_list = []
  dependencies.each do |dependency|
    found_podspec_file = nil
    name = dependency.name.split('/')[0]
    paths.each do |path|
      podspec_file = path + '/' + name + '.podspec'
      next unless File.file?(podspec_file)
      found_podspec_file = podspec_file
      break
    end
    next unless found_podspec_file
    return_list << [dependency, found_podspec_file]
    dep_spec = Pod::Specification.from_file(found_podspec_file)
    dep_spec.dependencies.each do |d_dep|
      dependencies << d_dep unless dependencies.include? d_dep
    end
  end
  return_list
end

#use_modular_headers?Boolean

Returns whether all pods should use modular headers.

Returns:

  • (Boolean)

    whether all pods should use modular headers



174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/cocoapods/generate/podfile_generator.rb', line 174

def use_modular_headers?
  if configuration.use_podfile? && configuration.use_modular_headers?
    raise Informative, 'Conflicting `use_modular_headers` option. Cannot specify both `--use-modular-headers` and `--use-podfile`.'
  end

  if configuration.use_podfile?
    target_definition_list.all? do |target_definition|
      target_definition.use_modular_headers_hash['all']
    end
  else
    configuration.use_modular_headers?
  end
end