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, type, type_and_version

Constructor Details

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

Instance Method Details

#composer_application_pathObject



51
52
53
54
# File 'lib/licensed/sources/composer.rb', line 51

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

#composer_lockObject



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

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

#package_pathsObject

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



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

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
# File 'lib/licensed/sources/composer.rb', line 30

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