Sha256: 9fb08d638456e142c5a54d3376da14eabefaf306cb41bce2fe73946dbec71783

Contents?: true

Size: 1.38 KB

Versions: 2

Compression:

Stored size: 1.38 KB

Contents

require 'gemnasium/parser'

module Bibliothecary
  module Parsers
    class Rubygems
      include Bibliothecary::Analyser

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

      def self.parse(filename, file_contents)
        if filename.match(/^Gemfile$|^gems\.rb$/)
          manifest = Gemnasium::Parser.send(:gemfile, file_contents)
          parse_manifest(manifest)
        elsif filename.match(/[A-Za-z0-9_-]+\.gemspec$/)
          manifest = Gemnasium::Parser.send(:gemspec, file_contents)
          parse_manifest(manifest)
        elsif filename.match(/^Gemfile\.lock$|^gems\.locked$/)
          parse_gemfile_lock(file_contents)
        else
          []
        end
      end

      def self.parse_gemfile_lock(manifest)
        manifest.split("\n").map do |line|
          match = line.match(NAME_VERSION_4)
          next unless match
          name = match[1]
          version = match[2].gsub(/\(|\)/,'')
          {
            name: name,
            requirement: version,
            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
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

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