class Licensee::Projects::Project

Constants

HASH_METHODS

Attributes

detect_packages[R]
detect_packages?[R]
detect_readme[R]
detect_readme?[R]

Public Class Methods

new(detect_packages: false, detect_readme: false) click to toggle source
# File lib/licensee/projects/project.rb, line 18
def initialize(detect_packages: false, detect_readme: false)
  @detect_packages = detect_packages
  @detect_readme = detect_readme
end

Public Instance Methods

license() click to toggle source

Returns the matching License instance if a license can be detected

# File lib/licensee/projects/project.rb, line 24
def license
  return @license if defined? @license

  @license = if licenses_without_copyright.count == 1 || lgpl?
               licenses_without_copyright.first
             elsif licenses_without_copyright.count > 1
               Licensee::License.find('other')
  end
end
license_file() click to toggle source

Returns the LicenseFile used to determine the License

# File lib/licensee/projects/project.rb, line 52
def license_file
  license_files.first if license_files.count == 1 || lgpl?
end
license_files() click to toggle source
# File lib/licensee/projects/project.rb, line 56
def license_files
  @license_files ||= begin
    return [] if files.empty? || files.nil?

    files = find_files do |n|
      Licensee::ProjectFiles::LicenseFile.name_score(n)
    end
    files = files.map do |file|
      Licensee::ProjectFiles::LicenseFile.new(load_file(file), file)
    end

    prioritize_lgpl(files)
  end
end
licenses() click to toggle source

Returns an array of detected Licenses

# File lib/licensee/projects/project.rb, line 35
def licenses
  @licenses ||= matched_files.map(&:license).uniq
end
matched_file() click to toggle source

Returns the ProjectFile used to determine the License

# File lib/licensee/projects/project.rb, line 40
def matched_file
  matched_files.first if matched_files.count == 1 || lgpl?
end
matched_files() click to toggle source

Returns an array of matches LicenseFiles

# File lib/licensee/projects/project.rb, line 45
def matched_files
  @matched_files ||= begin
    project_files.select(&:license)
  end
end
package_file() click to toggle source
# File lib/licensee/projects/project.rb, line 88
def package_file
  return unless detect_packages?
  return @package_file if defined? @package_file

  @package_file = begin
    content, file = find_file do |n|
      Licensee::ProjectFiles::PackageManagerFile.name_score(n)
    end

    return unless content && file

    Licensee::ProjectFiles::PackageManagerFile.new(content, file)
  end
end
readme()
Alias for: readme_file
readme_file() click to toggle source
# File lib/licensee/projects/project.rb, line 71
def readme_file
  return unless detect_readme?
  return @readme if defined? @readme

  @readme = begin
    content, file = find_file do |n|
      Licensee::ProjectFiles::ReadmeFile.name_score(n)
    end
    content = Licensee::ProjectFiles::ReadmeFile.license_content(content)

    return unless content && file

    Licensee::ProjectFiles::ReadmeFile.new(content, file)
  end
end
Also aliased as: readme

Private Instance Methods

files() click to toggle source
# File lib/licensee/projects/project.rb, line 162
def files
  raise 'Not implemented'
end
find_file(&block) click to toggle source

Given a block, passes each filename to that block, and expects a numeric score in response. Returns a hash representing the top scoring file or nil, if no file scored > 0

# File lib/licensee/projects/project.rb, line 125
def find_file(&block)
  return if files.empty? || files.nil?

  file = find_files(&block).first
  [load_file(file), file] if file
end
find_files() { |file| ... } click to toggle source

Given a block, passes each filename to that block, and expects a numeric score in response. Returns an array of all files with a score > 0, sorted by file score descending

# File lib/licensee/projects/project.rb, line 114
def find_files
  return [] if files.empty? || files.nil?

  found = files.map { |file| file.merge(score: yield(file[:name])) }
  found.select! { |file| file[:score].positive? }
  found.sort { |a, b| b[:score] <=> a[:score] }
end
lgpl?() click to toggle source
# File lib/licensee/projects/project.rb, line 105
def lgpl?
  return false unless licenses.count == 2 && license_files.count == 2

  license_files[0].lgpl? && license_files[1].gpl?
end
load_file(_file) click to toggle source
# File lib/licensee/projects/project.rb, line 166
def load_file(_file)
  raise 'Not implemented'
end
prioritize_lgpl(files) click to toggle source

Given an array of LicenseFiles, ensures LGPL is the first entry, if the first entry is otherwise GPL, and a valid LGPL file exists

This is becaues LGPL actually lives in COPYING.lesser, alongside an otherwise GPL-licensed project, per the license instructions. See git.io/viwyK.

Returns an array of LicenseFiles with LPGL first

# File lib/licensee/projects/project.rb, line 140
def prioritize_lgpl(files)
  return files if files.empty?
  return files unless files.first.license&.gpl?

  lesser = files.find_index(&:lgpl?)
  files.unshift(files.delete_at(lesser)) if lesser

  files
end
project_files() click to toggle source
# File lib/licensee/projects/project.rb, line 150
def project_files
  @project_files ||= [license_files, readme, package_file].flatten.compact
end