Class: LicenseFinder::PackageManager

Inherits:
Object
  • Object
show all
Includes:
SharedHelpers
Defined in:
lib/license_finder/package_manager.rb

Overview

Super-class for the different package managers (Bundler, NPM, Pip, etc.)

For guidance on adding a new package manager use the shared behavior

it_behaves_like "a PackageManager"

Additional guidelines are:

  • implement #current_packages, to return a list of ‘Package`s this package manager is tracking

  • implement #possible_package_paths, an array of ‘Pathname`s which are the possible locations which contain a configuration file/folder indicating the package manager is in use.

  • implement(Optional) #package_management_command, string for invoking the package manager

  • implement(Optional) #prepare_command, string for fetching dependencies for package manager (runs when the –prepare flag is passed to license_finder)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ PackageManager

Returns a new instance of PackageManager.



65
66
67
68
69
70
71
# File 'lib/license_finder/package_manager.rb', line 65

def initialize(options = {})
  @prepare_no_fail = options[:prepare_no_fail]
  @logger = options[:logger] || Core.default_logger
  @project_path = options[:project_path]
  @log_directory = options[:log_directory]
  @ignored_groups = options[:ignored_groups]
end

Class Method Details

.idObject



26
27
28
# File 'lib/license_finder/package_manager.rb', line 26

def id
  name.split('::').last.downcase
end

.takes_priority_overObject



22
23
24
# File 'lib/license_finder/package_manager.rb', line 22

def takes_priority_over
  nil
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


73
74
75
76
# File 'lib/license_finder/package_manager.rb', line 73

def active?
  path = detected_package_path
  path&.exist?
end

#command_exists?(command) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
62
63
# File 'lib/license_finder/package_manager.rb', line 54

def command_exists?(command)
  _stdout, _stderr, status =
    if LicenseFinder::Platform.windows?
      Cmd.run("where #{command}")
    else
      Cmd.run("which #{command}")
    end

  status.success?
end

#current_packages_with_relationsObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/license_finder/package_manager.rb', line 103

def current_packages_with_relations
  begin
    packages = current_packages
  rescue StandardError => e
    raise e unless @prepare_no_fail

    packages = []
  end

  packages.each do |parent|
    parent.children.each do |child_name|
      child = packages.detect { |child_package| child_package.name == child_name }
      child.parents << parent.name if child
    end
  end
  packages
end

#detected_package_pathObject



82
83
84
# File 'lib/license_finder/package_manager.rb', line 82

def detected_package_path
  possible_package_paths.find(&:exist?)
end

#installed?(logger = Core.default_logger) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/license_finder/package_manager.rb', line 31

def installed?(logger = Core.default_logger)
  if package_management_command.nil?
    logger.debug self.class, 'no command defined' # TODO: comment me out
    true
  elsif command_exists?(package_management_command)
    logger.debug self.class, 'is installed', color: :green
    true
  else
    logger.info self.class, 'is not installed', color: :red
    false
  end
end

#package_management_commandObject

see class description



45
46
47
# File 'lib/license_finder/package_manager.rb', line 45

def package_management_command
  nil
end

#prepareObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/license_finder/package_manager.rb', line 86

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

      error_message = "Prepare command '#{prepare_command}' failed\n#{stderr}"

      error_message == error_message.concat("\n#{stdout}\n") if !stdout.nil? && !stdout.empty?

      raise error_message unless @prepare_no_fail
    end
  else
    logger.debug self.class, 'no prepare step provided', color: :red
  end
end

#prepare_commandObject

see class description



50
51
52
# File 'lib/license_finder/package_manager.rb', line 50

def prepare_command
  nil
end

#project_root?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/license_finder/package_manager.rb', line 78

def project_root?
  active?
end