Sha256: 5cdb3419a68674b724497faec41b1e3bd06cedb4c90f3e38547e18b50bacc3ed

Contents?: true

Size: 1.47 KB

Versions: 5

Compression:

Stored size: 1.47 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)

      def self.parse_cabal(file_contents, options: {})
        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: {})
        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

5 entries across 5 versions & 1 rubygems

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