This commit is contained in:
Ben Balter 2018-08-05 19:58:22 -04:00
parent 4944856570
commit 9e0e78b24c
No known key found for this signature in database
GPG Key ID: DBB67C246AD356C4
5 changed files with 14 additions and 167 deletions

View File

@ -14,10 +14,9 @@ module Licensee
autoload :Matchers, 'licensee/matchers'
autoload :Projects, 'licensee/projects'
autoload :ProjectFiles, 'licensee/project_files'
autoload :CodeOfConduct, 'licensee/code_of_conduct'
# Over which percent is a match considered a match by default
CONFIDENCE_THRESHOLD = 90
CONFIDENCE_THRESHOLD = 98
# Base domain from which to build license URLs
DOMAIN = 'http://choosealicense.com'.freeze

View File

@ -1,127 +0,0 @@
require 'toml'
module Licensee
class InvalidCodeOfConduct < ArgumentError; end
class CodeOfConduct
VENDORED_CODES_OF_CONDUCT = %w[
citizen-code-of-conduct
contributor-covenant
].freeze
class << self
def all
@all ||= keys.map { |key| new(key) }
end
def find(key)
all.find { |coc| coc.key == key }
end
alias [] find
alias find_by_key find
def vendor_dir
@vendor_dir ||= File.expand_path '../../vendor', __dir__
end
def keys
@keys ||= vendored_codes_of_conduct.map do |path|
if File.dirname(path).end_with? 'citizen-code-of-conduct'
'citizen-code-of-conduct'
else
path.gsub!(%r{#{Regexp.escape(vendor_dir)}/}, '')
path.gsub!('code-of-conduct.', '')
path.gsub(%r{/?\.?md$}, '')
end
end
end
private
def vendored_codes_of_conduct
@vendored_codes_of_conduct ||= begin
cocs = "{#{VENDORED_CODES_OF_CONDUCT.join(',')}}"
path = File.join vendor_dir, cocs, '**', '*.md'
Dir.glob(path)
end
end
end
attr_reader :key
include Licensee::ContentHelper
def initialize(key)
@key = key
end
def version
toml['version']
end
def language
@language ||= begin
parts = key.split('/')
parts.last if parts.last =~ /^[a-z-]{2,5}$/
end
end
def name
return @name if defined? @name
@name = name_without_version.dup
@name << " (#{language.upcase})" if language
@name << " v#{version}" if version
@name
end
def name_without_version
@name_without_version ||= begin
key.split('/').first.split('-').map(&:capitalize).join(' ')
end
end
def content
parts.last
end
def inspect
"#<Licensee::CodeOfConduct key=#{key}>"
end
private
def path
if key == 'citizen-code-of-conduct'
File.join self.class.vendor_dir, key, "#{key.tr('-', '_')}.md"
elsif key.start_with? 'contributor-covenant'
parts = key.split('/')
filename = 'code-of-conduct'
filename << '.' + parts.pop if language
filename << '.md'
path = File.join(*parts[0...5], filename)
File.expand_path path, self.class.vendor_dir
end
end
# Raw content of code of conduct file, including TOML front matter
def raw_content
unless File.exist?(path)
msg = "'#{key}' is not a valid code of conduct key"
raise Licensee::InvalidCodeOfConduct, msg
end
@raw_content ||= File.read(path, encoding: 'utf-8')
end
def toml
@toml ||= begin
if parts.length == 3
TOML::Parser.new(parts[1]).parsed
else
{}
end
end
end
def parts
@parts ||= raw_content.split('+++')
end
end
end

View File

@ -1,18 +1,17 @@
module Licensee
module Matchers
autoload :Matcher, 'licensee/matchers/matcher'
autoload :Cabal, 'licensee/matchers/cabal'
autoload :Cargo, 'licensee/matchers/cargo'
autoload :Copyright, 'licensee/matchers/copyright'
autoload :CodeOfConduct, 'licensee/matchers/code_of_conduct'
autoload :Cran, 'licensee/matchers/cran'
autoload :Dice, 'licensee/matchers/dice'
autoload :DistZilla, 'licensee/matchers/dist_zilla'
autoload :Exact, 'licensee/matchers/exact'
autoload :Gemspec, 'licensee/matchers/gemspec'
autoload :NpmBower, 'licensee/matchers/npm_bower'
autoload :Package, 'licensee/matchers/package'
autoload :Reference, 'licensee/matchers/reference'
autoload :Spdx, 'licensee/matchers/spdx'
autoload :Matcher, 'licensee/matchers/matcher'
autoload :Cabal, 'licensee/matchers/cabal'
autoload :Cargo, 'licensee/matchers/cargo'
autoload :Copyright, 'licensee/matchers/copyright'
autoload :Cran, 'licensee/matchers/cran'
autoload :Dice, 'licensee/matchers/dice'
autoload :DistZilla, 'licensee/matchers/dist_zilla'
autoload :Exact, 'licensee/matchers/exact'
autoload :Gemspec, 'licensee/matchers/gemspec'
autoload :NpmBower, 'licensee/matchers/npm_bower'
autoload :Package, 'licensee/matchers/package'
autoload :Reference, 'licensee/matchers/reference'
autoload :Spdx, 'licensee/matchers/spdx'
end
end

View File

@ -4,6 +4,5 @@ module Licensee
autoload :LicenseFile, 'licensee/project_files/license_file'
autoload :PackageManagerFile, 'licensee/project_files/package_manager_file'
autoload :ReadmeFile, 'licensee/project_files/readme_file'
autoload :CodeOfConductFile, 'licensee/project_files/code_of_conduct_file'
end
end

View File

@ -1,23 +0,0 @@
module Licensee
module ProjectFiles
class CodeOfConductFile < Licensee::ProjectFiles::ProjectFile
include Licensee::ContentHelper
PREFERRED_EXT = %w[md markdown txt].freeze
BASENAME_REGEX = /(citizen[_-])?code[_-]of[_-]conduct/i
FILENAME_REGEX = /^#{BASENAME_REGEX}\.#{Regexp.union(PREFERRED_EXT)}/i
def self.name_score(filename)
if filename =~ FILENAME_REGEX
1.0
else
0.0
end
end
def possible_matchers
[Matchers::Exact, Matchers::Dice] #Matchers::CodeOfConduct]
end
end
end
end