Class: LicenseHeader::TagLicense

Inherits:
Object
  • Object
show all
Defined in:
lib/license_header/tag_license.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ TagLicense

Returns a new instance of TagLicense.



21
22
23
24
25
# 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

Class Method Details

.run!Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# 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

Instance Method Details

#add_license(file) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# 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?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/license_header/tag_license.rb', line 77

def dry_run?
  @dry_run
end

#has_copyright?(file) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
# File 'lib/license_header/tag_license.rb', line 32

def has_copyright?(file)
  fd = File.open(file)
  line = fd.gets
  fd.close

  if line =~ license_check
    return true
  else
    return false
  end
end

#license_checkObject



59
60
61
# File 'lib/license_header/tag_license.rb', line 59

def license_check
  /#{@license_check ||= @license_content.split("\n").shift}$/
end

#tag_directory_recursively(directory) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# 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

#tempfileObject



44
45
46
# File 'lib/license_header/tag_license.rb', line 44

def tempfile
  Tempfile.new(SecureRandom.hex(30))
end

#whitelisted?(file) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
# File 'lib/license_header/tag_license.rb', line 27

def whitelisted?(file)
  extension = File.basename(file).split('.').pop
  @extensions.include?(extension)
end