require "licensee" module Licensed class Dependency < License LEGAL_FILES = /\A(COPYING|NOTICE|LEGAL)(?:\..*)?\z/i attr_reader :path def initialize(path, metadata = {}) @path = path super metadata end def project @project ||= Licensee::FSProject.new(path, detect_packages: true, detect_readme: true) end def detect_license! if project.license_file license = project.license.key if project.license else # No license file detected, see if there is one in the GitHub repository license, content = Licensed.from_github(self["homepage"]) # No license in the GitHub repository, see if there is one in the README if !license && project.readme license = project.license.key if project.license content = project.readme.content end end self["license"] = license || package_manager_license || "other" self.text = ([content] + self.notices).compact.join("\n" + "-" * 80 + "\n") end def package_manager_license project.license.key if project.license end # Extract legal notices from the dependency source def notices files = Dir.foreach(path).select do |file| File.file?(File.join(path, file)) && file.match(LEGAL_FILES) end files.unshift project.license_file.filename if project.license_file files.uniq.map { |f| File.read(File.join(path, f)) } end end end