class CopyrightHeader::Parser

Attributes

options[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/copyright_header/parser.rb, line 176
def initialize(options = {})
  @options = options
  @exclude = [ /^LICENSE(|\.txt)$/i, /^holders(|\.txt)$/i, /^README/, /^\./]
  @license = License.new(:license_file => @options[:license_file],
                         :copyright_software => @options[:copyright_software],
                         :copyright_software_description => @options[:copyright_software_description],
                         :copyright_years => @options[:copyright_years],
                         :copyright_holders => @options[:copyright_holders],
                         :word_wrap => @options[:word_wrap])
  @syntax = Syntax.new(@options[:syntax], @options[:guess_extension])
end

Public Instance Methods

add(dir) click to toggle source

Add copyright header recursively

# File lib/copyright_header/parser.rb, line 240
def add(dir)
  transform(:add, dir)
end
execute() click to toggle source
# File lib/copyright_header/parser.rb, line 188
def execute
  if @options.has_key?(:add_path)
    @options[:add_path].split(File::PATH_SEPARATOR).each { |path| add(path) }
  end

  if @options.has_key?(:remove_path)
    @options[:remove_path].split(File::PATH_SEPARATOR).each { |path| remove(path) }
  end
end
remove(dir) click to toggle source

Remove copyright header recursively

# File lib/copyright_header/parser.rb, line 245
def remove(dir)
  transform(:remove, dir)
end
transform(method, path) click to toggle source
# File lib/copyright_header/parser.rb, line 198
def transform(method, path)
  paths = []
  if File.file?(path)
    paths << path
  else
    paths << Dir.glob("#{path}/**/*")
  end

  paths.flatten!

  paths.each do |path|
    begin
      if File.file?(path)
        if File.basename(path).match(Regexp.union(@exclude))
          STDERR.puts "SKIP #{path}; excluded"
          next
        end
      elsif File.directory?(path)
        next
      else
        STDERR.puts "SKIP #{path}; not file"
        next
      end

      if @syntax.supported?(path) 
        header = @syntax.header(path)
        contents = header.send(method, @license)
        if contents.nil?
          STDERR.puts "SKIP #{path}; failed to generate license"
        else
          write(path, contents)
        end
      else
        STDERR.puts "SKIP #{path}; unsupported"
      end
    rescue Exception => e
      STDERR.puts "SKIP #{path}; #{e.message}"
    end
  end
end
write(file, contents) click to toggle source
# File lib/copyright_header/parser.rb, line 249
def write(file, contents)
  if @options[:dry_run] 
    STDERR.puts "UPDATE #{file} [dry-run]"
    STDERR.puts contents
  elsif @options[:output_dir].nil?
    STDERR.puts "UPDATE #{file} [no output-dir]"
    STDERR.puts contents
  else
    dir = "#{@options[:output_dir]}/#{File.dirname(file).gsub(/^\/+/, '')}"
    STDERR.puts "UPDATE #{file} [output-dir #{dir}]"
    FileUtils.mkpath dir unless File.directory?(dir)
    output_path = @options[:output_dir] + "/" + file
    f =File.new(output_path, 'w')
    f.write(contents)
    f.close
  end
end