Class: LicenseeCLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/licensee/commands/diff.rb,
lib/licensee/commands/detect.rb,
lib/licensee/commands/version.rb,
lib/licensee/commands/license_path.rb

Constant Summary collapse

MATCHED_FILE_METHODS =

Methods to call when displaying information about ProjectFiles

%i[
  content_hash attribution confidence matcher license
].freeze

Instance Method Summary collapse

Instance Method Details

#detect(_path = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 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

  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

#diff(_path = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# 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

#versionObject



5
6
7
# File 'lib/licensee/commands/version.rb', line 5

def version
  say Licensee::VERSION
end