Sha256: ff4ab40e34b4bda2a35111f8ea6247e7402c1a5650e4561a099c8911511860e3

Contents?: true

Size: 1.77 KB

Versions: 2

Compression:

Stored size: 1.77 KB

Contents

require 'gemnasium/parser'
require 'yaml'

module Bibliothecary
  module Parsers
    class CocoaPods
      include Bibliothecary::Analyser

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

      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

      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

      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

      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
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bibliothecary-2.0.1 lib/bibliothecary/parsers/cocoapods.rb
bibliothecary-2.0.0 lib/bibliothecary/parsers/cocoapods.rb