class LicenseeCLI

Constants

MATCHED_FILE_METHODS

Methods to call when displaying information about ProjectFiles

Public Instance Methods

detect(_path = nil) click to toggle source
# File lib/licensee/commands/detect.rb, line 16
def detect(_path = nil)
  Licensee.confidence_threshold = options[:confidence]

  if options[:json]
    say project.to_h.to_json
    exit !project.licenses.empty?
  end

  rows = []
  rows << if project.license
            ['License:', project.license.spdx_id]
          elsif !project.licenses.empty?
            ['Licenses:', project.licenses.map(&:spdx_id)]
          else
            ['License:', set_color('None', :red)]
  end

  unless project.matched_files.empty?
    rows << ['Matched files:', project.matched_files.map(&:filename).join(', ')]
  end

  print_table rows

  project.matched_files.each do |matched_file|
    rows = []
    say "#{matched_file.filename}:"

    MATCHED_FILE_METHODS.each do |method|
      next unless matched_file.respond_to? method

      value = matched_file.public_send method
      next if value.nil?

      rows << [humanize(method, :method), humanize(value, method)]
    end
    print_table rows, indent: 2

    next unless matched_file.is_a? Licensee::ProjectFiles::LicenseFile
    next if matched_file.confidence == 100

    licenses = licenses_by_similarity(matched_file)
    next if licenses.empty?

    say '  Closest non-matching licenses:'
    rows = licenses[0...3].map do |license, similarity|
      spdx_id = license.meta['spdx-id']
      percent = Licensee::ContentHelper.format_percent(similarity)
      ["#{spdx_id} similarity:", percent]
    end
    print_table rows, indent: 4
  end

  if project.license_file && (options[:license] || options[:diff])
    license = options[:license] || closest_license_key(project.license_file)
    if license
      invoke(:diff, nil,
             license: license, license_to_diff: project.license_file)
    end
  end

  exit !project.licenses.empty?
end
diff(_path = nil) click to toggle source
# File lib/licensee/commands/diff.rb, line 8
def diff(_path = nil)
  say "Comparing to #{expected_license.name}:"
  rows = []

  left = expected_license.content_normalized(wrap: 80)
  right = license_to_diff.content_normalized(wrap: 80)
  similarity = expected_license.similarity(license_to_diff)
  similarity = Licensee::ContentHelper.format_percent(similarity)

  rows << ['Input Length:', license_to_diff.length]
  rows << ['License length:', expected_license.length]
  rows << ['Similarity:', similarity]
  print_table rows

  if left == right
    say 'Exact match!', :green
    exit
  end

  Dir.mktmpdir do |dir|
    path = File.expand_path 'LICENSE', dir
    Dir.chdir(dir) do
      `git init`
      File.write(path, left)
      `git add LICENSE`
      `git commit -m 'left'`
      File.write(path, right)
      say `git diff --word-diff`
    end
  end
end
license_path(_path) click to toggle source
# File lib/licensee/commands/license_path.rb, line 5
def license_path(_path)
  if project.license_file
    if remote?
      say project.license_file.path
    else
      say File.expand_path project.license_file.path
    end
  else
    exit 1
  end
end
version() click to toggle source
# File lib/licensee/commands/version.rb, line 5
def version
  say Licensee::VERSION
end

Private Instance Methods

closest_license_key(matched_file) click to toggle source
# File lib/licensee/commands/detect.rb, line 104
def closest_license_key(matched_file)
  licenses = licenses_by_similarity(matched_file)
  licenses.first.first.key unless licenses.empty?
end
expected_license() click to toggle source
# File lib/licensee/commands/diff.rb, line 51
def expected_license
  if options[:license]
    @expected_license ||= Licensee::License.find options[:license]
  end
  return @expected_license if @expected_license

  if options[:license]
    error "#{options[:license]} is not a valid license"
  else
    error 'Usage: provide a license to diff against with --license (spdx name)'
  end

  error "Valid licenses: #{Licensee::License.all(hidden: true).map(&:key).join(', ')}"
  exit 1
end
humanize(value, type = nil) click to toggle source

Given a string or object, prepares it for output and human consumption

# File lib/licensee/commands/detect.rb, line 82
def humanize(value, type = nil)
  case type
  when :license
    value.spdx_id
  when :matcher
    value.class
  when :confidence
    Licensee::ContentHelper.format_percent(value)
  when :method
    value.to_s.tr('_', ' ').capitalize + ':'
  else
    value
  end
end
license_to_diff() click to toggle source
# File lib/licensee/commands/diff.rb, line 42
def license_to_diff
  return options[:license_to_diff] if options[:license_to_diff]
  return project.license_file if remote? || STDIN.tty? && project.license_file

  @license_to_diff ||= begin
    Licensee::ProjectFiles::LicenseFile.new(STDIN.read, 'LICENSE')
  end
end
licenses_by_similarity(matched_file) click to toggle source
# File lib/licensee/commands/detect.rb, line 97
def licenses_by_similarity(matched_file)
  matcher = Licensee::Matchers::Dice.new(matched_file)
  potential_licenses = Licensee.licenses(hidden: true).select(&:wordset)
  matcher.instance_variable_set('@potential_licenses', potential_licenses)
  matcher.licenses_by_similarity
end