Sha256: 37d9b9d9726f17316460443612ea379336bf9383ff60fe61f45742d018f153e8

Contents?: true

Size: 1.76 KB

Versions: 2

Compression:

Stored size: 1.76 KB

Contents

require "json"
require "deb_control"

module Bibliothecary
  module Parsers
    class Hackage
      include Bibliothecary::Analyser

      def self.mapping
        {
          match_extension(".cabal") => {
            kind: "manifest",
            parser: :parse_cabal,
          },
          match_extension("cabal.config") => {
            kind: "lockfile",
            parser: :parse_cabal_config,
          },
        }
      end

      add_multi_parser(Bibliothecary::MultiParsers::CycloneDX)
      add_multi_parser(Bibliothecary::MultiParsers::DependenciesCSV)
      add_multi_parser(Bibliothecary::MultiParsers::Spdx)

      def self.parse_cabal(file_contents, options: {}) # rubocop:disable Lint/UnusedMethodArgument
        headers = {
          "Content-Type" => "text/plain;charset=utf-8",
        }

        response = Typhoeus.post("#{Bibliothecary.configuration.cabal_parser_host}/parse", headers: headers, body: file_contents)

        raise Bibliothecary::RemoteParsingError.new("Http Error #{response.response_code} when contacting: #{Bibliothecary.configuration.cabal_parser_host}/parse", response.response_code) unless response.success?
        JSON
          .parse(response.body, symbolize_names: true)
          .map { |dep_kvs| Dependency.new(**dep_kvs) }
      end

      def self.parse_cabal_config(file_contents, options: {}) # rubocop:disable Lint/UnusedMethodArgument
        manifest = DebControl::ControlFileBase.parse(file_contents)
        deps = manifest.first["constraints"].delete("\n").split(",").map(&:strip)
        deps.map do |dependency|
          dep = dependency.delete("==").split(" ")
          Dependency.new(
            name: dep[0],
            requirement: dep[1] || "*",
            type: "runtime",
          )
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bibliothecary-10.1.0 lib/bibliothecary/parsers/hackage.rb
bibliothecary-10.0.0 lib/bibliothecary/parsers/hackage.rb