Sha256: b2e5a67c69f8650335c2b2056d52b21e98752e0e5e30e4262cadd617aa0064ff

Contents?: true

Size: 1.83 KB

Versions: 1

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

require 'tmpdir'

class LicenseeCLI < Thor
  desc 'diff [PATH]', 'Compare the given license text to a known license'
  option :license, type: :string, desc: 'The SPDX ID or key of the license to compare'
  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

  private

  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

  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
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
licensee-9.14.1 lib/licensee/commands/diff.rb