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, Dotnet, Glide, Go15VendorExperiment, GoDep, GoModules, GoWorkspace, Govendor, Gradle, Gvt, Maven, Mix, NPM, Nuget, Pip, Rebar, Sbt, Trash, 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.
61 62 63 64 65 66 67 |
# File 'lib/license_finder/package_manager.rb', line 61 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
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/license_finder/package_manager.rb', line 50 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
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/license_finder/package_manager.rb', line 26 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
40 41 42 |
# File 'lib/license_finder/package_manager.rb', line 40 def package_management_command nil end |
.prepare_command ⇒ Object
see class description
45 46 47 |
# File 'lib/license_finder/package_manager.rb', line 45 def prepare_command nil end |
.takes_priority_over ⇒ Object
22 23 24 |
# File 'lib/license_finder/package_manager.rb', line 22 def takes_priority_over nil end |
Instance Method Details
#active? ⇒ Boolean
69 70 71 72 |
# File 'lib/license_finder/package_manager.rb', line 69 def active? path = detected_package_path path&.exist? end |
#current_packages_with_relations ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/license_finder/package_manager.rb', line 95 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
74 75 76 |
# File 'lib/license_finder/package_manager.rb', line 74 def detected_package_path possible_package_paths.find(&:exist?) end |
#prepare ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/license_finder/package_manager.rb', line 78 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 = "Prepare command '#{self.class.prepare_command}' failed\n#{stderr}" == .concat("\n#{stdout}\n") if !stdout.nil? && !stdout.empty? raise unless @prepare_no_fail end else logger.debug self.class, 'no prepare step provided', color: :red end end |