lib/licit/licenser.rb in licit-0.0.1 vs lib/licit/licenser.rb in licit-0.1

- old
+ new

@@ -1,11 +1,11 @@ require 'erb' class Licit::Licenser def initialize(options = {}) - defaults = { dir: '.', license: 'GPLv3' } + defaults = { dir: '.', license: 'GPLv3', exclude: [] } @options = defaults.merge options end def dir @options[:dir] @@ -36,37 +36,42 @@ end end def check_headers result = [] - Dir.chdir(dir) do - Dir['**/*.rb'].each do |source_file| - if not check_file_header(source_file) - result << [:error, source_file, "Missing header in #{source_file}"] - end + each_source_file do |source_file| + if not check_file_header(source_file) + result << [:error, source_file, "Missing header in #{source_file}"] end end result end def fix_headers - Dir.chdir(dir) do - Dir['**/*.rb'].each do |source_file| - if not check_file_header(source_file) - source = File.read source_file - File.open source_file, 'w' do |f| - header.each_line do |header_line| - f.write "# #{header_line}" - end - f.write "\n" - f.write source + each_source_file do |source_file| + if not check_file_header(source_file) + source = File.read source_file + File.open source_file, 'w' do |f| + header.each_line do |header_line| + f.write "# #{header_line}" end + f.write "\n" + f.write source end end end end + def each_source_file + Dir.chdir(dir) do + Dir['**/*.rb'].each do |source_file| + next if should_exclude source_file + yield source_file + end + end + end + def check_file_header(file) File.open(file, 'r') do |f| begin header.each_line do |header_line| file_line = f.readline @@ -97,12 +102,21 @@ def path_for(file) File.join files_dir, file end + def should_exclude(file) + @options[:exclude].each do |excluded| + return true if file.start_with? excluded + end + false + end + def header - template = ERB.new File.read(File.join(license_dir, 'header.erb')) - template.result binding + @header ||= begin + template = ERB.new File.read(File.join(license_dir, 'header.erb')) + template.result binding + end end def copyright @options[:copyright] end \ No newline at end of file