Sha256: 1fc52ab619a7da25ab9e89d3fa534957eb900ddb1c12d2e16923180f1e70dcf6

Contents?: true

Size: 1.76 KB

Versions: 25

Compression:

Stored size: 1.76 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)
      add_multi_parser(Bibliothecary::MultiParsers::DependenciesCSV)

      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

25 entries across 25 versions & 1 rubygems

Version Path
bibliothecary-8.6.0 lib/bibliothecary/parsers/rubygems.rb
bibliothecary-8.5.1 lib/bibliothecary/parsers/rubygems.rb
bibliothecary-8.5.0 lib/bibliothecary/parsers/rubygems.rb
bibliothecary-8.4.6 lib/bibliothecary/parsers/rubygems.rb
bibliothecary-8.4.5 lib/bibliothecary/parsers/rubygems.rb
bibliothecary-8.4.4 lib/bibliothecary/parsers/rubygems.rb
bibliothecary-8.4.3 lib/bibliothecary/parsers/rubygems.rb
bibliothecary-8.4.2 lib/bibliothecary/parsers/rubygems.rb
bibliothecary-8.4.1 lib/bibliothecary/parsers/rubygems.rb
bibliothecary-8.4.0 lib/bibliothecary/parsers/rubygems.rb
bibliothecary-8.3.9 lib/bibliothecary/parsers/rubygems.rb
bibliothecary-8.3.8 lib/bibliothecary/parsers/rubygems.rb
bibliothecary-8.3.7 lib/bibliothecary/parsers/rubygems.rb
bibliothecary-8.3.6 lib/bibliothecary/parsers/rubygems.rb
bibliothecary-8.3.5 lib/bibliothecary/parsers/rubygems.rb
bibliothecary-8.3.4 lib/bibliothecary/parsers/rubygems.rb
bibliothecary-8.3.3 lib/bibliothecary/parsers/rubygems.rb
bibliothecary-8.3.2 lib/bibliothecary/parsers/rubygems.rb
bibliothecary-8.3.1 lib/bibliothecary/parsers/rubygems.rb
bibliothecary-8.3.0 lib/bibliothecary/parsers/rubygems.rb