summaryrefslogtreecommitdiffstats
path: root/java/etc/replaceheaders.rb
blob: 92c45c0d55d8c8ce6b57179831f60fb2f1a88db0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Scans files - with a given extension - recursively from the current 
# directory replacing the old copyright/license header statement with a 
# new version
#
# Example command line usage
# >ruby replaceheaders.rb java oldheader.txt newheader.txt 
  
  def munge_file(fn, oh, nh)
    eoh = Regexp.escape(oh)
    new_contents = File.read(fn).gsub!(Regexp.new(eoh), nh) 
    if new_contents
      puts "  processing #{fn}" 
      File.open(fn, 'w') do |file|
        file.puts(new_contents)
      end 
      $num_files_processed += 1
    else
      puts "  processing #{fn} - NO MATCH!"
      $num_files_no_match += 1
    end
  end

if(!ARGV[0])
   STDERR.puts "usage: <file extension> <old header file> <new header file>"
   exit 0
end

ext = ARGV[0]
$num_files_processed = $num_files_no_match = 0
puts "Scanning files with #{ext} extension"
old_header = File.read(ARGV[1])
new_header = File.read(ARGV[2])
Dir["**/*.#{ext}"].each do |filename|
  munge_file( filename, old_header, new_header)
end
puts "Total files matched and processed = #{$num_files_processed}"
puts "Number of files with no match = #{$num_files_no_match}"