Class: Licit::Licenser

Inherits:
Object
  • Object
show all
Defined in:
lib/licit/licenser.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Licenser

Returns a new instance of Licenser.



5
6
7
8
# File 'lib/licit/licenser.rb', line 5

def initialize(options = {})
  defaults = { dir: '.', license: 'GPLv3', exclude: [] }
  @options = defaults.merge options
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_filesObject



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_headersObject



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


121
122
123
# File 'lib/licit/licenser.rb', line 121

def copyright
  @options[:copyright]
end

#dirObject



10
11
12
# File 'lib/licit/licenser.rb', line 10

def dir
  @options[:dir]
end

#each_source_fileObject



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

#filesObject



89
90
91
92
93
# File 'lib/licit/licenser.rb', line 89

def files
  Dir.chdir(files_dir) do
    return Dir['**']
  end
end

#files_dirObject



99
100
101
# File 'lib/licit/licenser.rb', line 99

def files_dir
  File.join license_dir, 'files'
end

#fix_filesObject



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_headersObject



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

#headerObject



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

#licenseObject



14
15
16
# File 'lib/licit/licenser.rb', line 14

def license
  @options[:license]
end

#license_dirObject



95
96
97
# File 'lib/licit/licenser.rb', line 95

def license_dir
  File.expand_path "../../../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_nameObject



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