Sha256: 05ff3c36888b420bc97149bd41d8b216235b7bfe8c85255d873a785b5c89f705

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 KB

Contents

require "json"
require 'deb_control'

module Bibliothecary
  module Parsers
    class Hackage
      include Bibliothecary::Analyser

      def self.mapping
        {
          /.*\.cabal$/ => {
            kind: 'manifest',
            parser: :parse_cabal
          },
          /^cabal\.config$|.*\/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)

        if response.response_code == 200 then
          JSON.parse(response.body, symbolize_names: true)
        else
          []
        end
      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

2 entries across 2 versions & 1 rubygems

Version Path
bibliothecary-6.3.1 lib/bibliothecary/parsers/hackage.rb
bibliothecary-6.3.0 lib/bibliothecary/parsers/hackage.rb