Class: Licit::Licenser
- Inherits:
-
Object
- Object
- Licit::Licenser
- Defined in:
- lib/licit/licenser.rb
Instance Method Summary collapse
- #check_file_header(file) ⇒ Object
- #check_files ⇒ Object
- #check_headers ⇒ Object
- #copyright ⇒ Object
- #dir ⇒ Object
- #each_source_file ⇒ Object
- #files ⇒ Object
- #files_dir ⇒ Object
- #fix_files ⇒ Object
- #fix_headers ⇒ Object
- #header ⇒ Object
-
#initialize(options = {}) ⇒ Licenser
constructor
A new instance of Licenser.
- #license ⇒ Object
- #license_dir ⇒ Object
- #path_for(file) ⇒ Object
- #program_name ⇒ Object
- #should_exclude(file) ⇒ Object
- #should_skip?(line) ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ Licenser
Returns a new instance of Licenser.
5 6 7 8 |
# File 'lib/licit/licenser.rb', line 5 def initialize( = {}) defaults = { :dir => '.', :license => 'GPLv3', :exclude => [] } @options = defaults.merge end |
Instance Method Details
#check_file_header(file) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/licit/licenser.rb', line 81 def check_file_header(file) at_beginning = true File.open(file, 'r') do |f| begin header.each_line do |header_line| begin file_line = f.readline end while at_beginning && should_skip?(file_line) at_beginning = false return false unless file_line.start_with? '#' file_line = file_line[1..-1].strip return false if file_line != header_line.chomp end rescue EOFError return false end end true end |
#check_files ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/licit/licenser.rb', line 18 def check_files result = [] files.each do |file| target = File.join dir, file if not File.exists?(target) result << [:error, file, "Missing file #{file}"] end end result end |
#check_headers ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/licit/licenser.rb', line 39 def check_headers result = [] 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 |
#copyright ⇒ Object
141 142 143 |
# File 'lib/licit/licenser.rb', line 141 def copyright @options[:copyright] end |
#dir ⇒ Object
10 11 12 |
# File 'lib/licit/licenser.rb', line 10 def dir @options[:dir] end |
#each_source_file ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/licit/licenser.rb', line 72 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 |
#files ⇒ Object
109 110 111 112 113 |
# File 'lib/licit/licenser.rb', line 109 def files Dir.chdir(files_dir) do return Dir['**'] end end |
#files_dir ⇒ Object
119 120 121 |
# File 'lib/licit/licenser.rb', line 119 def files_dir File.join license_dir, 'files' end |
#fix_files ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/licit/licenser.rb', line 30 def fix_files files.each do |file| target = File.join dir, file if not File.exists?(target) FileUtils.cp path_for(file), target end end end |
#fix_headers ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/licit/licenser.rb', line 49 def fix_headers each_source_file do |source_file| if not check_file_header(source_file) source_lines = File.readlines source_file File.open source_file, 'w' do |f| while should_skip? source_lines.first f.write source_lines.shift end header.each_line do |header_line| f.puts "# #{header_line}".strip end f.write "\n" source_lines.each do |line| f.write line end end end end end |
#header ⇒ Object
134 135 136 137 138 139 |
# File 'lib/licit/licenser.rb', line 134 def header @header ||= begin template = ERB.new File.read(File.join(license_dir, 'header.erb')) template.result binding end end |
#license ⇒ Object
14 15 16 |
# File 'lib/licit/licenser.rb', line 14 def license @options[:license] end |
#license_dir ⇒ Object
115 116 117 |
# File 'lib/licit/licenser.rb', line 115 def license_dir File. "../../../templates/#{license}", __FILE__ end |
#path_for(file) ⇒ Object
123 124 125 |
# File 'lib/licit/licenser.rb', line 123 def path_for(file) File.join files_dir, file end |
#program_name ⇒ Object
145 146 147 |
# File 'lib/licit/licenser.rb', line 145 def program_name @options[:program_name] end |
#should_exclude(file) ⇒ Object
127 128 129 130 131 132 |
# File 'lib/licit/licenser.rb', line 127 def should_exclude(file) @options[:exclude].each do |excluded| return true if file.start_with? excluded end false end |
#should_skip?(line) ⇒ Boolean
102 103 104 105 106 107 |
# File 'lib/licit/licenser.rb', line 102 def should_skip? line line = line.strip return true if line.empty? return true if line.start_with? '#!' return line =~ /^#\s*(encoding|coding)/ end |