Class: Licensed::Sources::Composer

Inherits:
Source
  • Object
show all
Defined in:
lib/licensed/sources/composer.rb

Constant Summary collapse

DEFAULT_COMPOSER_APPLICATON_PATH =
"composer.phar"

Instance Attribute Summary

Attributes inherited from Source

#config

Instance Method Summary collapse

Methods inherited from Source

#dependencies, full_type, #ignored?, inherited, #initialize, register_source, require_matched_dependency_version, #source_config, type, type_and_version

Constructor Details

This class inherits a constructor from Licensed::Sources::Source

Instance Method Details

#composer_application_pathObject



54
55
56
57
# File 'lib/licensed/sources/composer.rb', line 54

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

#composer_lockObject



59
60
61
# File 'lib/licensed/sources/composer.rb', line 59

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

#enabled?Boolean

Returns:

  • (Boolean)


9
10
11
12
# File 'lib/licensed/sources/composer.rb', line 9

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

#enumerate_dependenciesObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/licensed/sources/composer.rb', line 14

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

#include_dev?Boolean

Returns whether to include dev packages based on the licensed configuration settings

Returns:

  • (Boolean)


64
65
66
# File 'lib/licensed/sources/composer.rb', line 64

def include_dev?
  config.dig("composer", "include_dev") == true
end

#package_pathsObject

Returns the output from running ‘php composer.phar` to get package metadata



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/licensed/sources/composer.rb', line 38

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

#packagesObject



30
31
32
33
34
35
# File 'lib/licensed/sources/composer.rb', line 30

def packages
  packages = JSON.parse(File.read(composer_lock))
  return packages["packages"] unless include_dev?

  packages["packages"] + packages["packages-dev"]
end