Module: Pod::Specification::JSONSupport

Included in:
Pod::Specification
Defined in:
lib/cocoapods-core/specification/json.rb

Instance Method Summary collapse

Instance Method Details

#to_hashHash

Returns the hash representation of the specification including subspecs.

Returns:

  • (Hash)

    the hash representation of the specification including subspecs.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cocoapods-core/specification/json.rb', line 23

def to_hash
  hash = attributes_hash.dup
  if root? || available_platforms != parent.available_platforms
    platforms = Hash[available_platforms.map { |p| [p.name.to_s, p.deployment_target && p.deployment_target.to_s] }]
    hash['platforms'] = platforms
  end
  specs_by_type = subspecs.group_by(&:spec_type)
  all_appspecs = specs_by_type[:app] || []
  all_testspecs = specs_by_type[:test] || []
  all_subspecs = specs_by_type[:library] || []

  hash.delete('testspecs')
  hash['testspecs'] = all_testspecs.map(&:to_hash) unless all_testspecs.empty?
  hash.delete('appspecs')
  hash['appspecs'] = all_appspecs.map(&:to_hash) unless all_appspecs.empty?
  hash.delete('subspecs')
  hash['subspecs'] = all_subspecs.map(&:to_hash) unless all_subspecs.empty?

  # Since CocoaPods 1.7 version the DSL has changed to be pluralized. When we serialize a podspec to JSON with
  # 1.7, ensure that we also include the singular version in the hash to maintain backwards compatibility with
  # < 1.7 versions. We also delete this key and re-add it to ensure it gets added at the end.
  hash.delete('swift_version')
  hash['swift_version'] = swift_version.to_s unless swift_version.nil?

  hash
end

#to_json(*a) ⇒ String

Returns the json representation of the specification.

Returns:

  • (String)

    the json representation of the specification.



6
7
8
9
# File 'lib/cocoapods-core/specification/json.rb', line 6

def to_json(*a)
  require 'json'
  JSON.dump(to_hash, *a) << "\n"
end

#to_pretty_json(*a) ⇒ String

Returns the pretty json representation of the specification.

Returns:

  • (String)

    the pretty json representation of the specification.



13
14
15
16
# File 'lib/cocoapods-core/specification/json.rb', line 13

def to_pretty_json(*a)
  require 'json'
  JSON.pretty_generate(to_hash, *a) << "\n"
end