detect

in lib/licensee/commands/detect.rb [15:75]


  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

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

    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