Class: Kamaze::Project::Tools::Licenser
- Inherits:
-
BaseTool
- Object
- Observable
- BaseTool
- Kamaze::Project::Tools::Licenser
- Defined in:
- lib/kamaze/project/tools/licenser.rb
Overview
Apply provided license on project files
Samples of use:
Licenser.process do |pr|
pr.license = project.version.license_header
pr.patterns = ['bin/*', 'lib/**/**.rb']
end
Licenser.process do |pr|
pr.files += Dir.glob('bin/*')
end
Sample of use (with DSL):
require 'kamaze/project/dsl'
require 'kamaze/project/tools/licenser'
project do |c|
c.working_dir = "#{__dir__}/.."
c.subject = Kamaze::Project
c.name = 'kamaze-project'
c.tasks = []
end
version = project.subject.const_get('VERSION')
licenser = Kamaze::Project::Tools::Licenser.process do |process|
process.working_dir = project.working_dir
process.license = project.version.license_header
process.patterns = ['bin/*', 'lib/**/**.rb']
process.output = STDOUT
end
Instance Attribute Summary collapse
-
#files ⇒ Array<Pathname>
Get files.
-
#license ⇒ String
Get license, formatted (using comments).
- #observer_peers ⇒ Hash|nil included from Concern::Observable readonly protected
-
#output ⇒ Pathname|IO|nil
Where (default is current opened file) to write output.
-
#patterns ⇒ Array<String>
Patterns used to match files.
-
#working_dir ⇒ Pathname
Get working-dir.
Class Method Summary collapse
Instance Method Summary collapse
-
#apply_license(file) ⇒ Pathname
protected
Apply license on a file.
-
#index_lines(lines) ⇒ Fixnum
protected
Get an index, skipping comments.
-
#initialize {|_self| ... } ⇒ Licenser
constructor
A new instance of Licenser.
-
#license_lines(lines) ⇒ Array<String>
protected
Apply license on lines.
-
#license_regexp ⇒ Regexp
Get license regexp.
-
#process {|_self| ... } ⇒ self
Apply license on processable files.
Constructor Details
#initialize {|_self| ... } ⇒ Licenser
Returns a new instance of Licenser.
81 82 83 84 85 86 87 88 |
# File 'lib/kamaze/project/tools/licenser.rb', line 81 def initialize yield self if block_given? @working_dir ||= Dir.pwd @patterns ||= [] @files ||= [] @license ||= '' # project.version.license_header end |
Instance Attribute Details
#files ⇒ Array<Pathname>
Get files
114 115 116 117 118 119 120 |
# File 'lib/kamaze/project/tools/licenser.rb', line 114 def files @files.map { |file| Pathname.new(file) } .delete_if { |file| !file.file? } .delete_if { |file| file.basename.to_s[0] == '.' } .sort.uniq .map(&:realpath) end |
#license ⇒ String
Get license, formatted (using comments)
125 126 127 128 129 130 |
# File 'lib/kamaze/project/tools/licenser.rb', line 125 def license @license.to_s.gsub(/\n{3}/mi, "\n\n").lines.map do |line| line.chomp! "# #{line}" if line and line[0] != '#' end.join("\n") end |
#observer_peers ⇒ Hash|nil (readonly, protected) Originally defined in module Concern::Observable
#output ⇒ Pathname|IO|nil
Where (default is current opened file) to write output
69 70 71 |
# File 'lib/kamaze/project/tools/licenser.rb', line 69 def output @output end |
#patterns ⇒ Array<String>
Patterns used to match files
64 65 66 |
# File 'lib/kamaze/project/tools/licenser.rb', line 64 def patterns @patterns end |
#working_dir ⇒ Pathname
Get working-dir
93 94 95 |
# File 'lib/kamaze/project/tools/licenser.rb', line 93 def working_dir Pathname.new(@working_dir || Dir.pwd).freeze end |
Class Method Details
.process(&block) ⇒ Object
76 77 78 |
# File 'lib/kamaze/project/tools/licenser.rb', line 76 def process(&block) self.new.process(&block) end |
Instance Method Details
#apply_license(file) ⇒ Pathname (protected)
Apply license on a file
171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/kamaze/project/tools/licenser.rb', line 171 def apply_license(file) content = file.read licensable = !content.scan(license_regexp)[0] and !license.strip.empty? output = self.output || file if licensable content = license_lines(content.lines).join('') output.write(content) end file end |
#index_lines(lines) ⇒ Fixnum (protected)
Get an index, skipping comments
156 157 158 159 160 161 162 163 164 165 |
# File 'lib/kamaze/project/tools/licenser.rb', line 156 def index_lines(lines) index = 0 loop do break unless lines[index] and lines[index][0] == '#' index += 1 end index end |
#license_lines(lines) ⇒ Array<String> (protected)
Apply license on lines
189 190 191 192 193 194 195 196 |
# File 'lib/kamaze/project/tools/licenser.rb', line 189 def license_lines(lines) index = index_lines(lines) lines = lines.clone return lines if index <= 0 lines[0..index] + license.lines + ["\n"] + lines[index..-1] end |
#license_regexp ⇒ Regexp
Get license regexp
135 136 137 |
# File 'lib/kamaze/project/tools/licenser.rb', line 135 def license_regexp /#{Regexp.quote(license)}/mi end |
#process {|_self| ... } ⇒ self
Apply license on processable files
142 143 144 145 146 147 148 |
# File 'lib/kamaze/project/tools/licenser.rb', line 142 def process yield self if block_given? files.each { |file| apply_license(file) } self end |