class Bibliothecary::Parsers::Hackage

Public Class Methods

mapping() click to toggle source
# File lib/bibliothecary/parsers/hackage.rb, line 9
def self.mapping
  {
    match_extension(".cabal") => {
      kind: "manifest",
      parser: :parse_cabal,
    },
    match_extension("cabal.config") => {
      kind: "lockfile",
      parser: :parse_cabal_config,
    },
  }
end
parse_cabal(file_contents, options: {}) click to toggle source
# File lib/bibliothecary/parsers/hackage.rb, line 26
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
parse_cabal_config(file_contents, options: {}) click to toggle source
# File lib/bibliothecary/parsers/hackage.rb, line 39
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