Sha256: f8422875b475a5c399b857a14c8f7709521b9849d7c7a60ea589d78b736e440c

Contents?: true

Size: 1.7 KB

Versions: 5

Compression:

Stored size: 1.7 KB

Contents

require 'gemnasium/parser'

module Bibliothecary
  module Parsers
    class Rubygems
      include Bibliothecary::Analyser
      extend Bibliothecary::MultiParsers::BundlerLikeManifest

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

      def self.mapping
        {
          match_filenames("Gemfile", "gems.rb") => {
            kind: 'manifest',
            parser: :parse_gemfile,
            related_to: [ 'manifest', 'lockfile' ]
          },
          match_extension(".gemspec") => {
            kind: 'manifest',
            parser: :parse_gemspec,
            related_to: [ 'manifest', 'lockfile' ]
          },
          match_filenames("Gemfile.lock", "gems.locked") => {
            kind: 'lockfile',
            parser: :parse_gemfile_lock,
            related_to: [ 'manifest', 'lockfile' ]
          }
        }
      end

      add_multi_parser(Bibliothecary::MultiParsers::CycloneDX)

      def self.parse_gemfile_lock(file_contents, options: {})
        file_contents.lines(chomp: true).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_gemfile(file_contents, options: {})
        manifest = Gemnasium::Parser.send(:gemfile, file_contents)
        parse_ruby_manifest(manifest)
      end

      def self.parse_gemspec(file_contents, options: {})
        manifest = Gemnasium::Parser.send(:gemspec, file_contents)
        parse_ruby_manifest(manifest)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
bibliothecary-8.2.1 lib/bibliothecary/parsers/rubygems.rb
bibliothecary-8.2.0 lib/bibliothecary/parsers/rubygems.rb
bibliothecary-8.1.1 lib/bibliothecary/parsers/rubygems.rb
bibliothecary-8.1.0 lib/bibliothecary/parsers/rubygems.rb
bibliothecary-8.0.0 lib/bibliothecary/parsers/rubygems.rb