Class: LicenseLinter

Inherits:
Object
  • Object
show all
Defined in:
lib/learn-tool/license-linter.rb

Constant Summary collapse

VALID_FILE =
File.open(File.expand_path(File.dirname(__FILE__)) + '/support_files/LICENSE.md', "r:UTF-8")
VALID_YEARS =
["2015","2016","2017","2018","2019","2020","2021","2022","2023","2024","2025"]

Class Method Summary collapse

Class Method Details

.parse_file(file, learn_error) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/learn-tool/license-linter.rb', line 6

def self.parse_file(file, learn_error)
  directory_file_array = sanitize_whitespace(File.open(file, "r:UTF-8").read)
  valid_file_array = sanitize_whitespace(VALID_FILE.read)
  diff = directory_file_array - valid_file_array
  
  VALID_YEARS.each do |year| 
    diff.delete(year)
  end
  
  if diff.empty?
    learn_error.license_error[:valid_license] = true
    learn_error.valid_license = {message: "valid LICENSE.md", color: :green}
  else
    learn_error.license_error[:valid_license] = false
    learn_error.valid_license = {message: "invalid LICENSE.md - Errors: #{diff}", color: :red}
  end
end

.sanitize_whitespace(file) ⇒ Object



24
25
26
# File 'lib/learn-tool/license-linter.rb', line 24

def self.sanitize_whitespace(file)
  file.split.delete_if {|char| char.empty? || char == "\n"}
end