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, Composer, Conan, Conda, Dep, Dotnet, Erlangmk, Glide, Go15VendorExperiment, GoDep, GoModules, GoWorkspace, Govendor, Gradle, Gvt, Maven, Mix, NPM, Nuget, Pip, Pipenv, Rebar, Sbt, Spm, Trash, Yarn
Class Method Summary collapse
Instance Method Summary collapse
- #active? ⇒ Boolean
- #command_exists?(command) ⇒ Boolean
- #current_packages_with_relations ⇒ Object
- #detected_package_path ⇒ Object
-
#initialize(options = {}) ⇒ PackageManager
constructor
A new instance of PackageManager.
- #installed?(logger = Core.default_logger) ⇒ Boolean
-
#package_management_command ⇒ Object
see class description.
- #prepare ⇒ Object
-
#prepare_command ⇒ Object
see class description.
- #project_root? ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ PackageManager
Returns a new instance of PackageManager.
65 66 67 68 69 70 71 |
# File 'lib/license_finder/package_manager.rb', line 65 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
.id ⇒ Object
26 27 28 |
# File 'lib/license_finder/package_manager.rb', line 26 def id name.split('::').last.downcase 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
73 74 75 76 |
# File 'lib/license_finder/package_manager.rb', line 73 def active? path = detected_package_path path&.exist? end |
#command_exists?(command) ⇒ Boolean
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/license_finder/package_manager.rb', line 54 def command_exists?(command) _stdout, _stderr, status = if LicenseFinder::Platform.windows? Cmd.run("where #{command}") else Cmd.run("which #{command}") end status.success? end |
#current_packages_with_relations ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/license_finder/package_manager.rb', line 103 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
82 83 84 |
# File 'lib/license_finder/package_manager.rb', line 82 def detected_package_path possible_package_paths.find(&:exist?) end |
#installed?(logger = Core.default_logger) ⇒ Boolean
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/license_finder/package_manager.rb', line 31 def installed?(logger = Core.default_logger) if package_management_command.nil? logger.debug self.class, 'no command defined' # TODO: comment me out true elsif command_exists?(package_management_command) logger.debug self.class, 'is installed', color: :green true else logger.info self.class, 'is not installed', color: :red false end end |
#package_management_command ⇒ Object
see class description
45 46 47 |
# File 'lib/license_finder/package_manager.rb', line 45 def package_management_command nil end |
#prepare ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/license_finder/package_manager.rb', line 86 def prepare if prepare_command stdout, stderr, status = Dir.chdir(project_path) { Cmd.run(prepare_command) } unless status.success? log_errors stderr = "Prepare command '#{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 |
#prepare_command ⇒ Object
see class description
50 51 52 |
# File 'lib/license_finder/package_manager.rb', line 50 def prepare_command nil end |
#project_root? ⇒ Boolean
78 79 80 |
# File 'lib/license_finder/package_manager.rb', line 78 def project_root? active? end |