lib/licensed/dependency.rb in licensed-0.10.0 vs lib/licensed/dependency.rb in licensed-0.11.0

- old
+ new

@@ -24,45 +24,77 @@ def project @project ||= Licensee::Projects::FSProject.new(path, search_root: search_root, 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"]) + self["license"] = license_key + self.text = ([license_text] + self.notices).compact.join("\n" + "-" * 80 + "\n") + end - # 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 + # Extract legal notices from the dependency source + def notices + files = local_files + + if project.license_file + files.unshift File.join(project.license_file[:dir], project.license_file.filename) end - self["license"] = license || package_manager_license || "other" - self.text = ([content] + self.notices).compact.join("\n" + "-" * 80 + "\n") + files.uniq.map { |f| File.read(f) } end - def package_manager_license - project.license.key if project.license + def local_files + return [] unless Dir.exist?(path) + + Dir.foreach(path).map do |file| + next unless file.match(LEGAL_FILES) + + file_path = File.join(path, file) + next unless File.file?(file_path) + + file_path + end.compact end - # Extract legal notices from the dependency source - def notices - return [] unless Dir.exist? path + private - files = Dir.foreach(path).select do |file| - File.file?(File.join(path, file)) && file.match(LEGAL_FILES) - end + # Returns the Licensee::ProjectFile representing the matched_project_file + # or remote_license_file + def project_file + matched_project_file || remote_license_file + end - if license_file = project.license_file - files.unshift File.join(license_file[:dir], license_file.filename) - end + # Returns the Licensee::LicenseFile, Licensee::PackageManagerFile, or + # Licensee::ReadmeFile with a matched license, in that order or nil + # if no license file matched a known license + def matched_project_file + @matched_project_file ||= project.matched_files + .select { |f| f.license && !f.license.other? } + .first + end - files.uniq.map do |f| - absolute_path = Pathname.new(f).absolute? ? f : File.join(path, f) - File.read(absolute_path) + # Returns a Licensee::LicenseFile with the content of the license in the + # dependency's repository to account for LICENSE files not being distributed + def remote_license_file + @remote_license_file ||= Licensed.from_github(self["homepage"]) + end + + # Regardless of the license detected, try to pull the license content + # from the local LICENSE, remote LICENSE, or the README, in that order + def license_text + content_file = project.license_file || remote_license_file || project.readme_file + content_file.content if content_file + end + + # Returns a string representing the project's license + # Note, this will be "other" if a license file was found but the license + # could not be identified and "none" if no license file was found at all + def license_key + if project_file && project_file.license + project_file.license.key + elsif project.license_file || remote_license_file + "other" + else + "none" end end end end