From ee4fe3e00d3887f49de0682538326cd96375e156 Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Sat, 24 Oct 2015 18:07:59 +0200 Subject: [PATCH] Add flag to enable readme detection --- bin/licensee | 2 +- lib/licensee/project.rb | 28 ++++++++++++++++++++++------ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/bin/licensee b/bin/licensee index 6511c1c..2becbde 100755 --- a/bin/licensee +++ b/bin/licensee @@ -3,7 +3,7 @@ require_relative "../lib/licensee" path = ARGV[0] || Dir.pwd -project = Licensee::GitProject.new(path, detect_packages: true) +project = Licensee::GitProject.new(path, detect_packages: true, detect_readme: true) file = project.matched_file if project.license_file diff --git a/lib/licensee/project.rb b/lib/licensee/project.rb index f8f7669..e098944 100644 --- a/lib/licensee/project.rb +++ b/lib/licensee/project.rb @@ -3,8 +3,13 @@ require 'rugged' class Licensee private class Project - def initialize(detect_packages) + def initialize(detect_packages: false, detect_readme: false) @detect_packages = detect_packages + @detect_readme = detect_readme + end + + def detect_readme? + @detect_readme end def detect_packages? @@ -17,7 +22,7 @@ class Licensee end def matched_file - @matched_file ||= (license_file || package_file) + @matched_file ||= (license_file || readme || package_file) end def license_file @@ -30,6 +35,17 @@ class Licensee end end + def readme + return unless detect_readme? + return @readme if defined? @readme + @readme = begin + content, name = find_file { |name| Readme.name_score(name) } + if content && name + Readme.new(content, name) + end + end + end + def package_file return unless detect_packages? return @package_file if defined? @package_file @@ -52,7 +68,7 @@ class Licensee class InvalidRepository < ArgumentError; end - def initialize(repo, revision: nil, detect_packages: false) + def initialize(repo, revision: nil, **args) if repo.kind_of? Rugged::Repository @repository = repo else @@ -60,7 +76,7 @@ class Licensee end @revision = revision - super(detect_packages) + super(**args) rescue Rugged::RepositoryError raise InvalidRepository end @@ -99,9 +115,9 @@ class Licensee class FSProject < Project attr_reader :path - def initialize(path, detect_packages: false) + def initialize(path, **args) @path = path - super(detect_packages) + super(**args) end private