Sha256: e1f9845a505155856c9999861293d9b8792fda4916d237b0abc3cd24b3dc74ba

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
licensed-0.6.0 lib/licensed/dependency.rb