Sha256: b5c2ad3183415fd30c3bc0591ce4ff81fcaf4078bc20f4b0009194e675439d6e

Contents?: true

Size: 1.15 KB

Versions: 7

Compression:

Stored size: 1.15 KB

Contents

require 'json'

module Bibliothecary
  module Parsers
    class Packagist
      include Bibliothecary::Analyser

      def self.mapping
        {
          /^composer\.json$/ => {
            kind: 'manifest',
            parser: :parse_manifest
          },
          /^composer\.lock$/ => {
            kind: 'lockfile',
            parser: :parse_lockfile
          }
        }
      end

      def self.parse_lockfile(file_contents)
        manifest = JSON.parse file_contents
        manifest.fetch('packages',[]).map do |dependency|
          {
            name: dependency["name"],
            requirement: dependency["version"],
            type: 'runtime'
          }
        end
      end

      def self.parse_manifest(file_contents)
        manifest = JSON.parse file_contents
        map_dependencies(manifest, 'require', 'runtime') +
        map_dependencies(manifest, 'require-dev', 'development')
      end

      def self.map_dependencies(hash, key, type)
        hash.fetch(key,[]).map do |name, requirement|
          {
            name: name,
            requirement: requirement,
            type: type
          }
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
bibliothecary-5.3.4 lib/bibliothecary/parsers/packagist.rb
bibliothecary-5.3.3 lib/bibliothecary/parsers/packagist.rb
bibliothecary-5.3.2 lib/bibliothecary/parsers/packagist.rb
bibliothecary-5.3.1 lib/bibliothecary/parsers/packagist.rb
bibliothecary-5.3.0 lib/bibliothecary/parsers/packagist.rb
bibliothecary-5.2.0 lib/bibliothecary/parsers/packagist.rb
bibliothecary-5.1.0 lib/bibliothecary/parsers/packagist.rb