Sha256: a50666c3f480bd7ed0b1a3e68b75ff42c510f2ae5dea684c171212cddb9a451b

Contents?: true

Size: 1.98 KB

Versions: 25

Compression:

Stored size: 1.98 KB

Contents

require 'json'

module Bibliothecary
  module Parsers
    class Packagist
      PLATFORM_NAME = 'Packagist'

      def self.parse(filename, file_contents)
        json = JSON.parse(file_contents)
        if filename.match(/^composer\.json$/)
          parse_manifest(json)
        elsif filename.match(/^composer\.lock$/)
          parse_lockfile(json)
        else
          []
        end
      end

      def self.analyse(folder_path, file_list)
        [analyse_composer_json(folder_path, file_list),
        analyse_composer_lock(folder_path, file_list)]
      end

      def self.analyse_composer_json(folder_path, file_list)
        path = file_list.find{|path| path.gsub(folder_path, '').gsub(/^\//, '').match(/^composer\.json$/) }
        return unless path

        manifest = JSON.parse File.open(path).read

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

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

        manifest = JSON.parse File.open(path).read

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

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

      def self.parse_manifest(manifest)
        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

25 entries across 25 versions & 1 rubygems

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