Class: Kamaze::Project::Tools::Licenser

Inherits:
BaseTool show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Licenser

Returns a new instance of Licenser.

Yields:

  • (_self)

Yield Parameters:



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

#filesArray<Pathname>

Get files

Returns:

  • (Array<Pathname>)


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

#licenseString

Get license, formatted (using comments)

Returns:

  • (String)


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_peersHash|nil (readonly, protected) Originally defined in module Concern::Observable

Returns:

  • (Hash|nil)

#outputPathname|IO|nil

Where (default is current opened file) to write output

Returns:

  • (Pathname|IO|nil)


69
70
71
# File 'lib/kamaze/project/tools/licenser.rb', line 69

def output
  @output
end

#patternsArray<String>

Patterns used to match files

Returns:

  • (Array<String>)


64
65
66
# File 'lib/kamaze/project/tools/licenser.rb', line 64

def patterns
  @patterns
end

#working_dirPathname

Get working-dir

Returns:

  • (Pathname)


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

Parameters:

  • file (Pathname)

Returns:

  • (Pathname)

    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

Parameters:

  • lines (Array<String>)

Returns:

  • (Fixnum)


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

Parameters:

  • lines (Array<String>)

Returns:

  • (Array<String>)

    with license applied



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_regexpRegexp

Get license regexp

Returns:

  • (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

Yields:

  • (_self)

Yield Parameters:

Returns:

  • (self)


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