Class: Bibliothecary::Parsers::Swift

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

Constant Summary collapse

PLATFORM_NAME =
'swift'

Class Method Summary collapse

Class Method Details

.analyse(folder_path, file_list) ⇒ Object



14
15
16
# File 'lib/bibliothecary/parsers/swift.rb', line 14

def self.analyse(folder_path, file_list)
  [analyse_package_swift(folder_path, file_list)]
end

.analyse_package_swift(folder_path, file_list) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bibliothecary/parsers/swift.rb', line 18

def self.analyse_package_swift(folder_path, file_list)
  path = file_list.find{|path| path.gsub(folder_path, '').gsub(/^\//, '').match(/^Package\.swift$/i) }
  return unless path

  manifest = File.open(path).read

  {
    platform: PLATFORM_NAME,
    path: path,
    dependencies: parse_package_swift(manifest)
  }
rescue
  []
end

.parse(filename, file_contents) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/bibliothecary/parsers/swift.rb', line 6

def self.parse(filename, file_contents)
  if filename.match(/^Package\.swift$/i)
    parse_package_swift(file_contents)
  else
    []
  end
end

.parse_package_swift(manifest) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/bibliothecary/parsers/swift.rb', line 33

def self.parse_package_swift(manifest)
  response = Typhoeus.post("http://46.101.24.4:8080/to-json", body: manifest)
  json = JSON.parse(response.body)
  json["dependencies"].map do |dependency|
    name = dependency['url'].gsub(/^https?:\/\//, '').gsub(/\.git$/,'')
    version = "#{dependency['version']['lowerBound']} - #{dependency['version']['upperBound']}"
    {
      name: name,
      version: version,
      type: 'runtime'
    }
  end
end