Class: Bibliothecary::Parsers::CocoaPods

Inherits:
Object
  • Object
show all
Defined in:
lib/bibliothecary/parsers/cocoapods.rb

Constant Summary collapse

NAME_VERSION =
'(?! )(.*?)(?: \(([^-]*)(?:-(.*))?\))?'.freeze
NAME_VERSION_4 =
/^ {4}#{NAME_VERSION}$/
PLATFORM_NAME =
'CocoaPods'

Class Method Summary collapse

Class Method Details

.analyse(folder_path, file_list) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/bibliothecary/parsers/cocoapods.rb', line 31

def self.analyse(folder_path, file_list)
  [
    analyse_podfile(folder_path, file_list),
    analyse_podspec(folder_path, file_list),
    analyse_podfile_lock(folder_path, file_list),
    analyse_podspec_json(folder_path, file_list)
  ]
end

.analyse_podfile(folder_path, file_list) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/bibliothecary/parsers/cocoapods.rb', line 40

def self.analyse_podfile(folder_path, file_list)
  path = file_list.find{|path| path.gsub(folder_path, '').gsub(/^\//, '').match(/^Podfile$/) }
  return unless path

  manifest = Gemnasium::Parser.send(:podfile, File.open(path).read)

  {
    platform: PLATFORM_NAME,
    path: path,
    dependencies: parse_manifest(manifest)
  }
end

.analyse_podfile_lock(folder_path, file_list) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/bibliothecary/parsers/cocoapods.rb', line 79

def self.analyse_podfile_lock(folder_path, file_list)
  path = file_list.find{|path| path.gsub(folder_path, '').gsub(/^\//, '').match(/^Podfile\.lock$/) }
  return unless path

  manifest = YAML.load File.open(path).read

  {
    platform: PLATFORM_NAME,
    path: path,
    dependencies: parse_podfile_lock(manifest)
  }
end

.analyse_podspec(folder_path, file_list) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/bibliothecary/parsers/cocoapods.rb', line 53

def self.analyse_podspec(folder_path, file_list)
  path = file_list.find{|path| path.gsub(folder_path, '').gsub(/^\//, '').match(/^[A-Za-z0-9_-]+\.podspec$/) }
  return unless path

  manifest = Gemnasium::Parser.send(:podspec, File.open(path).read)

  {
    platform: PLATFORM_NAME,
    path: path,
    dependencies: parse_manifest(manifest)
  }
end

.analyse_podspec_json(folder_path, file_list) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/bibliothecary/parsers/cocoapods.rb', line 66

def self.analyse_podspec_json(folder_path, file_list)
  path = file_list.find{|path| path.gsub(folder_path, '').gsub(/^\//, '').match(/^[A-Za-z0-9_-]+\.podspec.json$/) }
  return unless path

  manifest = JSON.parse File.open(path).read

  {
    platform: PLATFORM_NAME,
    path: path,
    dependencies: parse_json_manifest(manifest)
  }
end

.parse(filename, file_contents) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/bibliothecary/parsers/cocoapods.rb', line 12

def self.parse(filename, file_contents)

  if filename.match(/^Podfile$/)
    manifest = Gemnasium::Parser.send(:podfile, file_contents)
    parse_manifest(manifest)
  elsif filename.match(/^[A-Za-z0-9_-]+\.podspec$/)
    manifest = Gemnasium::Parser.send(:podspec, file_contents)
    parse_manifest(manifest)
  elsif filename.match(/^Podfile\.lock$/)
    manifest = YAML.load file_contents
    parse_podfile_lock(manifest)
  elsif filename.match(/^[A-Za-z0-9_-]+\.podspec.json$/)
    json = JSON.parse(file_contents)
    parse_json_manifest(json)
  else
    []
  end
end

.parse_json_manifest(manifest) ⇒ Object



114
115
116
117
118
119
120
121
122
# File 'lib/bibliothecary/parsers/cocoapods.rb', line 114

def self.parse_json_manifest(manifest)
  manifest['dependencies'].inject([]) do |deps, dep|
    deps.push({
      name: dep[0],
      requirement: dep[1],
      type: 'runtime'
    })
  end.uniq
end

.parse_manifest(manifest) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/bibliothecary/parsers/cocoapods.rb', line 104

def self.parse_manifest(manifest)
  manifest.dependencies.inject([]) do |deps, dep|
    deps.push({
      name: dep.name,
      requirement: dep.requirement.to_s,
      type: dep.type
    })
  end.uniq
end

.parse_podfile_lock(manifest) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/bibliothecary/parsers/cocoapods.rb', line 92

def self.parse_podfile_lock(manifest)
  manifest['PODS'].map do |row|
    pod = row.is_a?(String) ? row : row.keys.first
    match = pod.match(/(.+?)\s\((.+?)\)/i)
    {
      name: match[1].split('/').first,
      requirement: match[2],
      type: 'runtime'
    }
  end.compact
end