class Kamaze::Project::Tools::Licenser

Apply provided license on project files

Samples of use:

“`ruby Licenser.process do |pr|

pr.license  = project.version.license_header
pr.patterns = ['bin/*', 'lib/**/**.rb']

end “`

“`ruby Licenser.process do |pr|

pr.files += Dir.glob('bin/*')

end “`

Sample of use (with DSL):

“`ruby 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 “`

Attributes

files[W]

Files to be licensed

@type [Array<String|Pathname>] @return [Array<Pathname>]

license[W]

License chapter/header

@return [String]

output[RW]

Where (default is current opened file) to write output

@return [Pathname|IO|nil]

patterns[R]

Patterns used to match files

@return [Array<String>]

working_dir[W]

@type [String|Pathname] @return [Pathname]

Public Class Methods

new() { |self| ... } click to toggle source
# 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
process(&block) click to toggle source
# File lib/kamaze/project/tools/licenser.rb, line 76
def process(&block)
  self.new.process(&block)
end

Public Instance Methods

files() click to toggle source

Get files

@return [Array<Pathname>]

# 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() click to toggle source

Get license, formatted (using comments)

@return [String]

# 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
license_regexp() click to toggle source

Get license regexp

@return [Regexp]

# File lib/kamaze/project/tools/licenser.rb, line 135
def license_regexp
  /#{Regexp.quote(license)}/mi
end
patterns=(patterns) click to toggle source

Set patterns

@param [Array<String>] patterns

# File lib/kamaze/project/tools/licenser.rb, line 100
def patterns=(patterns)
  @files    ||= []
  @patterns ||= []

  patterns.each do |pattern|
    @files += Dir.glob(working_dir.join(pattern))
  end

  @patterns += patterns
end
process() { |self| ... } click to toggle source

Apply license on processable files

@return [self]

# File lib/kamaze/project/tools/licenser.rb, line 142
def process
  yield self if block_given?

  files.each { |file| apply_license(file) }

  self
end
working_dir() click to toggle source

Get working-dir

@return [Pathname]

# File lib/kamaze/project/tools/licenser.rb, line 93
def working_dir
  Pathname.new(@working_dir || Dir.pwd).freeze
end

Protected Instance Methods

apply_license(file) click to toggle source

Apply license on a file

@param [Pathname] file @return [Pathname] file

# 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) click to toggle source

Get an index, skipping comments

@param [Array<String>] lines @return [Fixnum]

# 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) click to toggle source

Apply license on lines

@param [Array<String>] lines @return [Array<String>] with license applied

# 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