OLD | NEW |
1 #!/usr/bin/ruby | 1 #!/usr/bin/ruby |
2 | 2 |
3 require 'find' | 3 require 'find' |
4 require 'optparse' | 4 require 'optparse' |
5 | 5 |
6 options = {} | 6 options = {} |
7 OptionParser.new do |opts| | 7 OptionParser.new do |opts| |
8 opts.banner = "Usage: clean-header-guards [options]" | 8 opts.banner = "Usage: clean-header-guards [options]" |
9 | 9 |
10 opts.on("--prefix [PREFIX]", "Append a header prefix to all guards") do |prefi
x| | 10 opts.on("--prefix [PREFIX]", "Append a header prefix to all guards") do |prefi
x| |
11 options[:prefix] = prefix | 11 options[:prefix] = prefix |
12 end | 12 end |
13 end.parse! | 13 end.parse! |
14 | 14 |
15 IgnoredFilenamePatterns = [ | 15 IgnoredFilenamePatterns = [ |
16 # ignore headers which are known not to have guard | 16 # ignore headers which are known not to have guard |
17 /WebCorePrefix/, | 17 /WebCorePrefix/, |
18 /ForwardingHeaders/, | 18 /ForwardingHeaders/, |
19 %r|bindings/objc|, | 19 %r|bindings/objc|, |
20 /vcproj/, # anything inside a vcproj is in the windows wasteland | 20 /vcproj/, # anything inside a vcproj is in the windows wasteland |
21 | 21 |
22 # we don't own any of these headers | 22 # we don't own any of these headers |
23 %r|icu/unicode|, | 23 %r|icu/unicode|, |
24 %r|platform/graphics/cairo|, | 24 %r|platform/graphics/cairo|, |
25 %r|platform/image-decoders|, | 25 %r|platform/image-decoders|, |
26 | 26 |
27 /config.h/ # changing this one sounds scary | 27 /config.h/ # changing this one sounds scary |
28 ].freeze | 28 ].freeze |
29 | 29 |
30 IgnoreFileNamesPattern = Regexp.union(*IgnoredFilenamePatterns).freeze | 30 IgnoreFileNamesPattern = Regexp.union(*IgnoredFilenamePatterns).freeze |
31 | 31 |
32 Find::find(".") do |filename| | 32 Find::find(".") do |filename| |
33 next unless filename =~ /\.h$/ | 33 next unless filename =~ /\.h$/ |
34 next if filename.match(IgnoreFileNamesPattern) | 34 next if filename.match(IgnoreFileNamesPattern) |
35 | 35 |
36 File.open(filename, "r+") do |file| | 36 File.open(filename, "r+") do |file| |
37 contents = file.read | 37 contents = file.read |
38 match_results = contents.match(/#ifndef (\S+)\n#define \1/s) | 38 match_results = contents.match(/#ifndef (\S+)\n#define \1/s) |
39 if match_results | 39 if match_results |
40 current_guard = match_results[1] | 40 current_guard = match_results[1] |
41 new_guard = File.basename(filename).sub('.', '_') | 41 new_guard = File.basename(filename).sub('.', '_') |
42 new_guard = options[:prefix] + '_' + new_guard if options[:prefix] | 42 new_guard = options[:prefix] + '_' + new_guard if options[:prefix] |
43 contents.gsub!(/#{current_guard}\b/, new_guard) | 43 contents.gsub!(/#{current_guard}\b/, new_guard) |
44 else | 44 else |
45 puts "Ignoring #{filename}, failed to find existing header guards." | 45 puts "Ignoring #{filename}, failed to find existing header guards." |
46 end | 46 end |
47 tmp_filename = filename + ".tmp" | 47 tmp_filename = filename + ".tmp" |
48 File.open(tmp_filename, "w+") do |new_file| | 48 File.open(tmp_filename, "w+") do |new_file| |
49 new_file.write(contents) | 49 new_file.write(contents) |
50 end | 50 end |
51 File.rename tmp_filename, filename | 51 File.rename tmp_filename, filename |
52 end | 52 end |
53 end | 53 end |
OLD | NEW |