Sha256: 5655f31b8ee0e9347c42d3e3e08e006f78ae4dd402206cb2067b6cf2002cb492

Contents?: true

Size: 1.68 KB

Versions: 6

Compression:

Stored size: 1.68 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)
      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(" ")
          {
            name: dep[0],
            requirement: dep[1] || "*",
            type: "runtime",
          }
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
bibliothecary-9.1.0 lib/bibliothecary/parsers/hackage.rb
bibliothecary-9.0.0 lib/bibliothecary/parsers/hackage.rb
bibliothecary-8.8.1 lib/bibliothecary/parsers/hackage.rb
bibliothecary-8.7.7 lib/bibliothecary/parsers/hackage.rb
bibliothecary-8.7.6 lib/bibliothecary/parsers/hackage.rb
bibliothecary-8.7.5 lib/bibliothecary/parsers/hackage.rb