Sha256: ec8e348f5795fe4c2e51e9698a5094d0af0083dc8ddd2b43d0fe8b33df4494d8

Contents?: true

Size: 1.38 KB

Versions: 55

Compression:

Stored size: 1.38 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

      def self.parse_cabal(file_contents)
        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)
        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

55 entries across 55 versions & 1 rubygems

Version Path
bibliothecary-7.3.6 lib/bibliothecary/parsers/hackage.rb
bibliothecary-7.3.5 lib/bibliothecary/parsers/hackage.rb
bibliothecary-7.3.4 lib/bibliothecary/parsers/hackage.rb
bibliothecary-7.3.3 lib/bibliothecary/parsers/hackage.rb
bibliothecary-7.3.2 lib/bibliothecary/parsers/hackage.rb
bibliothecary-7.3.1 lib/bibliothecary/parsers/hackage.rb
bibliothecary-7.3.0 lib/bibliothecary/parsers/hackage.rb
bibliothecary-7.2.1 lib/bibliothecary/parsers/hackage.rb
bibliothecary-7.2.0 lib/bibliothecary/parsers/hackage.rb
bibliothecary-7.1.5 lib/bibliothecary/parsers/hackage.rb
bibliothecary-7.1.4 lib/bibliothecary/parsers/hackage.rb
bibliothecary-7.1.3 lib/bibliothecary/parsers/hackage.rb
bibliothecary-7.1.2 lib/bibliothecary/parsers/hackage.rb
bibliothecary-7.1.1 lib/bibliothecary/parsers/hackage.rb
bibliothecary-7.1.0 lib/bibliothecary/parsers/hackage.rb
bibliothecary-7.0.2 lib/bibliothecary/parsers/hackage.rb
bibliothecary-7.0.1 lib/bibliothecary/parsers/hackage.rb
bibliothecary-7.0.0 lib/bibliothecary/parsers/hackage.rb
bibliothecary-6.12.3 lib/bibliothecary/parsers/hackage.rb
bibliothecary-6.12.2 lib/bibliothecary/parsers/hackage.rb