Class: LicenseFinder::PackageManager
- Inherits:
-
Object
- Object
- LicenseFinder::PackageManager
- 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)
Direct Known Subclasses
Bower, Bundler, Cargo, Carthage, CocoaPods, Conan, Dep, Glide, Go15VendorExperiment, GoDep, GoWorkspace, Govendor, Gradle, Gvt, Maven, Mix, NPM, Nuget, Pip, Rebar, Sbt, Yarn
Class Method Summary collapse
- .command_exists?(command) ⇒ Boolean
- .installed?(logger = Core.default_logger) ⇒ Boolean
-
.package_management_command ⇒ Object
see class description.
-
.prepare_command ⇒ Object
see class description.
- .takes_priority_over ⇒ Object
Instance Method Summary collapse
- #active? ⇒ Boolean
- #current_packages_with_relations ⇒ Object
- #detected_package_path ⇒ Object
-
#initialize(options = {}) ⇒ PackageManager
constructor
A new instance of PackageManager.
- #prepare ⇒ Object
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( = {}) @prepare_no_fail = [:prepare_no_fail] @logger = [:logger] || Core.default_logger @project_path = [:project_path] @log_directory = [:log_directory] @ignored_groups = [:ignored_groups] end |
Class Method Details
.command_exists?(command) ⇒ 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
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_command ⇒ Object
see class description
38 39 40 |
# File 'lib/license_finder/package_manager.rb', line 38 def package_management_command nil end |
.prepare_command ⇒ Object
see class description
43 44 45 |
# File 'lib/license_finder/package_manager.rb', line 43 def prepare_command nil end |
.takes_priority_over ⇒ Object
20 21 22 |
# File 'lib/license_finder/package_manager.rb', line 20 def takes_priority_over nil end |
Instance Method Details
#active? ⇒ 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_relations ⇒ Object
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_path ⇒ Object
72 73 74 |
# File 'lib/license_finder/package_manager.rb', line 72 def detected_package_path possible_package_paths.find(&:exist?) end |
#prepare ⇒ Object
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 |