Class: LicenseFinder::PackageManager
- Inherits:
-
Object
- Object
- LicenseFinder::PackageManager
show all
- 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 #package_path, a ‘Pathname` which, if the file exists, indicates the package manager is in use on this project
Direct Known Subclasses
Bower, Bundler, CocoaPods, GoDep, GoVendor, GoWorkspace, Gradle, Maven, NPM, Nuget, Pip, Rebar
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of PackageManager.
45
46
47
48
|
# File 'lib/license_finder/package_manager.rb', line 45
def initialize options={}
@logger = options[:logger] || Core.default_logger
@project_path = options[:project_path]
end
|
Class Method Details
.command_exists?(command) ⇒ Boolean
70
71
72
73
74
75
76
77
78
|
# File 'lib/license_finder/package_manager.rb', line 70
def self.command_exists? command
if LicenseFinder::Platform.windows?
`where #{command} 2>NUL`
else
`which #{command} 2>/dev/null`
end
status = $?
return status.success?
end
|
.current_packages(options) ⇒ Object
19
20
21
22
23
24
|
# File 'lib/license_finder/package_manager.rb', line 19
def self.current_packages(options)
active_package_managers = package_managers
.map { |pm| pm.new(options) }
.select(&:active?)
active_package_managers.flat_map(&:current_packages_with_relations)
end
|
.installed?(logger = Core.default_logger) ⇒ Boolean
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/license_finder/package_manager.rb', line 26
def self.installed?(logger=Core.default_logger)
if package_management_command.nil?
logger.installed self, "no command defined"
return true
end
if command_exists?(package_management_command)
logger.installed self, true
return true
end
logger.installed self, false
return false
end
|
.package_management_command ⇒ Object
41
42
43
|
# File 'lib/license_finder/package_manager.rb', line 41
def self.package_management_command
nil
end
|
.package_managers ⇒ Object
15
16
17
|
# File 'lib/license_finder/package_manager.rb', line 15
def self.package_managers
[GoDep, GoWorkspace, GoVendor, Bundler, NPM, Pip, Bower, Maven, Gradle, CocoaPods, Rebar, Nuget]
end
|
Instance Method Details
#active? ⇒ Boolean
50
51
52
53
|
# File 'lib/license_finder/package_manager.rb', line 50
def active?
self.class.installed?(logger) &&
package_path.exist?.tap { |is_active| logger.active self.class, is_active }
end
|
#capture(command) ⇒ Object
55
56
57
|
# File 'lib/license_finder/package_manager.rb', line 55
def capture(command)
[`#{command}`, $?.success?]
end
|
#current_packages_with_relations ⇒ Object
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/license_finder/package_manager.rb', line 59
def current_packages_with_relations
packages = current_packages
packages.each do |parent|
parent.children.each do |child_name|
child = packages.detect { |child| child.name == child_name }
child.parents << parent.name if child
end
end
packages
end
|