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
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
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/licit/licenser.rb', line 73 def check_file_header(file) File.open(file, 'r') do |f| begin header.each_line do |header_line| file_line = f.readline 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
121 122 123 |
# File 'lib/licit/licenser.rb', line 121 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
64 65 66 67 68 69 70 71 |
# File 'lib/licit/licenser.rb', line 64 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
89 90 91 92 93 |
# File 'lib/licit/licenser.rb', line 89 def files Dir.chdir(files_dir) do return Dir['**'] end end |
#files_dir ⇒ Object
99 100 101 |
# File 'lib/licit/licenser.rb', line 99 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 |
# File 'lib/licit/licenser.rb', line 49 def fix_headers 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 |
#header ⇒ Object
114 115 116 117 118 119 |
# File 'lib/licit/licenser.rb', line 114 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
95 96 97 |
# File 'lib/licit/licenser.rb', line 95 def license_dir File. "../../../templates/#{license}", __FILE__ end |
#path_for(file) ⇒ Object
103 104 105 |
# File 'lib/licit/licenser.rb', line 103 def path_for(file) File.join files_dir, file end |
#program_name ⇒ Object
125 126 127 |
# File 'lib/licit/licenser.rb', line 125 def program_name @options[:program_name] end |
#should_exclude(file) ⇒ Object
107 108 109 110 111 112 |
# File 'lib/licit/licenser.rb', line 107 def should_exclude(file) @options[:exclude].each do |excluded| return true if file.start_with? excluded end false end |