class LicenseHeader::TagLicense

Public Class Methods

new(options = {}) click to toggle source
# File lib/license_header/tag_license.rb, line 21
def initialize(options =  {})
  @dry_run = options[:dry_run]
  @license_content = options[:license_content] || LICENSE
  @extensions = options[:extension] || ['rb']
end
run!() click to toggle source
# File lib/license_header/tag_license.rb, line 81
def self.run!
  options = { :target => '.' }

  options_parser = OptionParser.new do |opts|
    opts.banner = "Usager: license_header [options]"
    opts.separator ""
    opts.separator "Options:"

    opts.on("-t", "--target=val", String, "specify the target directory, default to current directory") do |v|
      options[:target] = v 
    end

    opts.on("-l", "--license=val", String, "specify the header to apply to the source code") do |v|
      options[:license_content] = File.read(v) if File.exist?(v)
    end

    opts.on("--extensions=[x,y,z]", Array, "rb,cs") do |v|
     options[:extensions] = v
    end

    opts.on("-d", "--dry-run", "List the files to changes") do |v|
      options[:dry_run] = v
    end

    opts.on_tail
  end
  
  begin
    options_parser.parse!

    if options[:license_content].nil?
      puts "Missing the license header file"
      puts options_parser
      exit
    else
      tag = TagLicense.new(options)
      modified = tag.tag_directory_recursively(options[:target])
      modified.each { |f| puts "Modified: #{f}" }
    end
  rescue OptionParser::InvalidOption
    puts "Invalid option"
    puts options_parser
  end
end

Public Instance Methods

add_license(file) click to toggle source
# File lib/license_header/tag_license.rb, line 48
def add_license(file)
  temp = tempfile
  File.open(temp, 'w+') do |fd|
    fd.write(@license_content)
    fd.write(File.read(file))
  end

  FileUtils.cp(temp.path, file)
  FileUtils.rm_rf(temp.path)
end
dry_run?() click to toggle source
# File lib/license_header/tag_license.rb, line 77
def dry_run?
  @dry_run
end
license_check() click to toggle source
# File lib/license_header/tag_license.rb, line 59
def license_check
  /#{@license_check ||= @license_content.split("\n").shift}$/
end
tag_directory_recursively(directory) click to toggle source
# File lib/license_header/tag_license.rb, line 63
def tag_directory_recursively(directory)
  modified_files = []


  Dir[File.join(File.expand_path(directory), '**/*')].each do |f|
    if !File.directory?(f) && whitelisted?(f) && !has_copyright?(f)
      modified_files << f
      add_license(f) unless dry_run?
    end
  end

  modified_files
end
tempfile() click to toggle source
# File lib/license_header/tag_license.rb, line 44
def tempfile
  Tempfile.new(SecureRandom.hex(30))
end
whitelisted?(file) click to toggle source
# File lib/license_header/tag_license.rb, line 27
def whitelisted?(file)
  extension = File.basename(file).split('.').pop
  @extensions.include?(extension)
end