Sha256: 16752463cad02a3153293f9470f7dc94641bcacf154dfeca3dac9a7c06aa17dd

Contents?: true

Size: 1.15 KB

Versions: 5

Compression:

Stored size: 1.15 KB

Contents

require 'deb_control'

module Bibliothecary
  module Parsers
    class CRAN
      include Bibliothecary::Analyser

      REQUIRE_REGEXP = /([a-zA-Z0-9\-_\.]+)\s?\(?([><=\s\d\.,]+)?\)?/

      def self.mapping
        {
          match_filename("DESCRIPTION", case_insensitive: true) => {
            kind: 'manifest',
            parser: :parse_description
          }
        }
      end

      add_multi_parser(Bibliothecary::MultiParsers::CycloneDX)

      def self.parse_description(file_contents, options: {})
        manifest = DebControl::ControlFileBase.parse(file_contents)
        parse_section(manifest, 'Depends') +
        parse_section(manifest, 'Imports') +
        parse_section(manifest, 'Suggests') +
        parse_section(manifest, 'Enhances')
      end

      def self.parse_section(manifest, name)
        return [] unless manifest.first[name]
        deps = manifest.first[name].delete("\n").split(',').map(&:strip)
        deps.map do |dependency|
          dep = dependency.match(REQUIRE_REGEXP)
          {
            name: dep[1],
            requirement: dep[2] || '*',
            type: name.downcase
          }
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

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