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.



63
64
65
66
# File 'lib/license_finder/package_manager.rb', line 63

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

Class Method Details

.active_package_managers(options = { project_path: Pathname.new('') }) ⇒ Object



29
30
31
32
33
# File 'lib/license_finder/package_manager.rb', line 29

def active_package_managers(options = { project_path: Pathname.new('') })
  active_pm_classes = package_managers.select { |pm_class| pm_class.new(options).active? }
  active_pm_classes -= active_pm_classes.map(&:takes_priority_over)
  active_pm_classes.map { |pm_class| pm_class.new(options) }
end

.active_packages(options) ⇒ Object



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

def active_packages(options)
  active_package_managers(options).flat_map(&:current_packages_with_relations)
end

.command_exists?(command) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
108
109
110
111
112
113
# File 'lib/license_finder/package_manager.rb', line 105

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

  status.success?
end

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

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/license_finder/package_manager.rb', line 39

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

.package_management_commandObject

see class description



53
54
55
# File 'lib/license_finder/package_manager.rb', line 53

def package_management_command
  nil
end

.package_managersObject



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

def package_managers
  [GoDep, GoWorkspace, Go15VendorExperiment, Glide, Gvt, Govendor, Dep, Bundler, NPM, Pip,
   Yarn, Bower, Maven, Gradle, CocoaPods, Rebar, Nuget, Carthage, Mix, Conan]
end

.prepare_commandObject

see class description



58
59
60
# File 'lib/license_finder/package_manager.rb', line 58

def prepare_command
  nil
end

.takes_priority_overObject



35
36
37
# File 'lib/license_finder/package_manager.rb', line 35

def takes_priority_over
  nil
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/license_finder/package_manager.rb', line 68

def active?
  path = detected_package_path
  self.class.installed?(logger) &&
    !path.nil? &&
    path.exist?.tap do |is_active|
      if is_active
        logger.log self.class, 'is active', color: :green
      else
        logger.log self.class, 'is not active'
      end
    end
end

#current_packages_with_relationsObject



94
95
96
97
98
99
100
101
102
103
# File 'lib/license_finder/package_manager.rb', line 94

def current_packages_with_relations
  packages = current_packages
  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



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

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

#prepareObject



85
86
87
88
89
90
91
92
# File 'lib/license_finder/package_manager.rb', line 85

def prepare
  if self.class.prepare_command
    _stdout, _stderr, status = Cmd.run(self.class.prepare_command)
    raise "Prepare command '#{self.class.prepare_command}' failed" unless status.success?
  else
    logger.log self.class, 'no prepare step provided', color: :red
  end
end