Sha256: 52ec90b53eeb9a002c4706e8da581184f8adc6c2bcb2b3cffeb0d0f012bddf5c

Contents?: true

Size: 1.87 KB

Versions: 17

Compression:

Stored size: 1.87 KB

Contents

require 'json'

module Bibliothecary
  module Parsers
    class Hex
      PLATFORM_NAME = 'hex'

      def self.parse(filename, file_contents)
        if filename.match(/^mix\.exs$/)
          parse_mix(file_contents)
        elsif filename.match(/^mix\.lock$/)
          parse_mix_lock(file_contents)
        else
          []
        end
      end

      def self.analyse(folder_path, file_list)
        [analyse_mix(folder_path, file_list),
        analyse_mix_lock(folder_path, file_list)]
      end

      def self.analyse_mix(folder_path, file_list)
        path = file_list.find{|path| path.gsub(folder_path, '').gsub(/^\//, '').match(/^mix\.exs$/) }
        return unless path

        manifest = File.open(path).read

        {
          platform: PLATFORM_NAME,
          path: path,
          dependencies: parse_mix(manifest)
        }
      end

      def self.analyse_mix_lock(folder_path, file_list)
        path = file_list.find{|path| path.gsub(folder_path, '').gsub(/^\//, '').match(/^mix\.lock$/) }
        return unless path

        manifest = File.open(path).read

        {
          platform: PLATFORM_NAME,
          path: path,
          dependencies: parse_mix_lock(manifest)
        }
      end

      def self.parse_mix(manifest)
        response = Typhoeus.post("https://mix-deps-json.herokuapp.com/", body: manifest)
        json = JSON.parse response.body

        json.map do |name, version|
          {
            name: name,
            version: version,
            type: "runtime"
          }
        end
      end

      def self.parse_mix_lock(manifest)
        response = Typhoeus.post("https://mix-deps-json.herokuapp.com/lock", body: manifest)
        json = JSON.parse response.body

        json.map do |name, info|
          {
            name: name,
            version: info['version'],
            type: "runtime"
          }
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
bibliothecary-1.2.1 lib/bibliothecary/parsers/hex.rb
bibliothecary-1.2.0 lib/bibliothecary/parsers/hex.rb
bibliothecary-1.1.0 lib/bibliothecary/parsers/hex.rb
bibliothecary-1.0.0 lib/bibliothecary/parsers/hex.rb
bibliothecary-0.16.1 lib/bibliothecary/parsers/hex.rb
bibliothecary-0.16.0 lib/bibliothecary/parsers/hex.rb
bibliothecary-0.15.2 lib/bibliothecary/parsers/hex.rb
bibliothecary-0.15.1 lib/bibliothecary/parsers/hex.rb
bibliothecary-0.15.0 lib/bibliothecary/parsers/hex.rb
bibliothecary-0.14.1 lib/bibliothecary/parsers/hex.rb
bibliothecary-0.14.0 lib/bibliothecary/parsers/hex.rb
bibliothecary-0.13.1 lib/bibliothecary/parsers/hex.rb
bibliothecary-0.13.0 lib/bibliothecary/parsers/hex.rb
bibliothecary-0.12.0 lib/bibliothecary/parsers/hex.rb
bibliothecary-0.11.0 lib/bibliothecary/parsers/hex.rb
bibliothecary-0.10.0 lib/bibliothecary/parsers/hex.rb
bibliothecary-0.9.0 lib/bibliothecary/parsers/hex.rb