Class: LicenseFinder::Composer

Inherits:
PackageManager show all
Defined in:
lib/license_finder/package_managers/composer.rb

Instance Method Summary collapse

Methods inherited from PackageManager

#active?, #command_exists?, #current_packages_with_relations, #detected_package_path, id, #installed?, #project_root?, takes_priority_over

Constructor Details

#initialize(options = {}) ⇒ Composer

Returns a new instance of Composer.



7
8
9
10
# File 'lib/license_finder/package_managers/composer.rb', line 7

def initialize(options = {})
  super
  @check_require_only = !!options[:composer_check_require_only]
end

Instance Method Details

#composer_jsonObject



55
56
57
58
59
60
61
# File 'lib/license_finder/package_managers/composer.rb', line 55

def composer_json
  command = "composer licenses --format=json#{@check_require_only ? ' --no-dev' : ''}"
  stdout, stderr, status = Dir.chdir(project_path) { Cmd.run(command) }
  raise "Command '#{command}' failed to execute: #{stderr}" unless status.success?

  JSON(stdout)
end

#current_packagesObject



16
17
18
19
20
21
22
23
24
# File 'lib/license_finder/package_managers/composer.rb', line 16

def current_packages
  dependency_list.map do |name, dependency|
    path_command = "composer show #{name} -P"
    stdout, _stderr, status = Dir.chdir(project_path) { Cmd.run(path_command) }

    path = status.success? ? stdout.split(' ').last : ''
    ComposerPackage.new(name, dependency['version'], spec_licenses: dependency['license'], install_path: path)
  end
end

#dependency_listObject



50
51
52
53
# File 'lib/license_finder/package_managers/composer.rb', line 50

def dependency_list
  json ||= composer_json
  json.fetch('dependencies', {}).reject { |_, d| d.is_a?(String) }
end

#lockfile_pathObject



46
47
48
# File 'lib/license_finder/package_managers/composer.rb', line 46

def lockfile_path
  project_path.join('composer.lock')
end

#package_management_commandObject



34
35
36
# File 'lib/license_finder/package_managers/composer.rb', line 34

def package_management_command
  'composer'
end

#package_pathObject



42
43
44
# File 'lib/license_finder/package_managers/composer.rb', line 42

def package_path
  project_path.join('composer.json')
end

#possible_package_pathsObject



12
13
14
# File 'lib/license_finder/package_managers/composer.rb', line 12

def possible_package_paths
  [project_path.join('composer.lock'), project_path.join('composer.json')]
end

#prepareObject



26
27
28
29
30
31
32
# File 'lib/license_finder/package_managers/composer.rb', line 26

def prepare
  _stdout, stderr, status = Dir.chdir(project_path) { Cmd.run(prepare_command) }
  return if status.success?

  log_errors stderr
  raise "Prepare command '#{Composer.prepare_command}' failed" unless @prepare_no_fail
end

#prepare_commandObject



38
39
40
# File 'lib/license_finder/package_managers/composer.rb', line 38

def prepare_command
  'composer install --no-plugins --no-scripts --ignore-platform-reqs --no-interaction'
end