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.



59
60
61
62
63
64
65
# File 'lib/license_finder/package_manager.rb', line 59

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

.command_exists?(command) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
57
# File 'lib/license_finder/package_manager.rb', line 48

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

  status.success?
end

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

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/license_finder/package_manager.rb', line 24

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

.package_management_commandObject

see class description



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

def package_management_command
  nil
end

.prepare_commandObject

see class description



43
44
45
# File 'lib/license_finder/package_manager.rb', line 43

def prepare_command
  nil
end

.takes_priority_overObject



20
21
22
# File 'lib/license_finder/package_manager.rb', line 20

def takes_priority_over
  nil
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


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

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

#current_packages_with_relationsObject



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

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



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

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

#prepareObject



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/license_finder/package_manager.rb', line 76

def prepare
  if self.class.prepare_command
    _stdout, stderr, status = Dir.chdir(project_path) { Cmd.run(self.class.prepare_command) }
    unless status.success?
      log_errors stderr
      raise "Prepare command '#{self.class.prepare_command}' failed" unless @prepare_no_fail
    end
  else
    logger.debug self.class, 'no prepare step provided', color: :red
  end
end