Sha256: 7b77715b0eb9e08a7eca1b94222e1cb442ef8050fe4dafb91ba92546db5d4500

Contents?: true

Size: 1.78 KB

Versions: 37

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true
require "json"

module Licensed
  module Sources
    class Composer < Source
      DEFAULT_COMPOSER_APPLICATON_PATH = "composer.phar"

      def enabled?
        return false unless Licensed::Shell.tool_available?("php")
        File.exist?(composer_lock) && File.exist?(composer_application_path)
      end

      def enumerate_dependencies
        packages.map do |package|
          Dependency.new(
            name: package["name"],
            version: package["version"],
            path: package_paths[package["name"]],
            metadata: {
              "type"     => Composer.type,
              "name"     => package["name"],
              "summary"  => package["description"],
              "homepage" => package["homepage"]
            }
          )
        end
      end

      def packages
        JSON.parse(File.read(composer_lock))["packages"]
      end

      # Returns the output from running `php composer.phar` to get package metadata
      def package_paths
        return @package_paths if defined?(@package_paths)

        @package_paths = begin
          output = Licensed::Shell.execute("php", composer_application_path, "show", "--format", "json", "--path", allow_failure: true)
          return {} if output.to_s.empty?

          path_json = JSON.parse(output)
          return {} unless path_json["installed"]

          path_json["installed"].each_with_object({}) do |package, hsh|
            hsh[package["name"]] = package["path"]
          end
        end
      end

      def composer_application_path
        setting = config.dig("composer", "application_path") || DEFAULT_COMPOSER_APPLICATON_PATH
        File.expand_path(setting, config.pwd)
      end

      def composer_lock
        config.pwd.join("composer.lock")
      end
    end
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
licensed-3.5.0 lib/licensed/sources/composer.rb
licensed-3.4.4 lib/licensed/sources/composer.rb
licensed-3.4.3 lib/licensed/sources/composer.rb
licensed-3.4.2 lib/licensed/sources/composer.rb
licensed-3.4.1 lib/licensed/sources/composer.rb
licensed-3.4.0 lib/licensed/sources/composer.rb
licensed-3.3.1 lib/licensed/sources/composer.rb
licensed-3.3.0 lib/licensed/sources/composer.rb
licensed-3.2.3 lib/licensed/sources/composer.rb
licensed-3.2.2 lib/licensed/sources/composer.rb
licensed-3.2.1 lib/licensed/sources/composer.rb
licensed-3.2.0 lib/licensed/sources/composer.rb
licensed-3.1.0 lib/licensed/sources/composer.rb
licensed-3.0.1 lib/licensed/sources/composer.rb
licensed-3.0.0 lib/licensed/sources/composer.rb
licensed-2.15.2 lib/licensed/sources/composer.rb
licensed-2.15.1 lib/licensed/sources/composer.rb
licensed-2.15.0 lib/licensed/sources/composer.rb
licensed-2.14.4 lib/licensed/sources/composer.rb
licensed-2.14.3 lib/licensed/sources/composer.rb