root/apply_boilerplate.rb

Revision ,131, 2.0 kB (checked in by Berkus <berkus@madfire.net>, 1 month ago)

Improved boilerplate script.

  • Property executable set to True
Line 
1 #!/usr/bin/env ruby
2 #
3 # Copyright 2007 - 2008, Stanislav Karchebnyy <berkus+metta@madfire.net>
4 #
5 # Distributed under the Boost Software License, Version 1.0.
6 # (See file LICENSE_1_0.txt or a copy at http:#www.boost.org/LICENSE_1_0.txt)
7 #
8 #
9 # Apply license and modeline changes to text source files.
10 #
11 require 'find'
12
13 exclude_dirs = ['./vesper/src/lib/bstrlib', './vesper/src/build']
14 no_license_dirs = ['./vesper/src/lib/oskit']
15
16 class Array
17     def do_not_has?(path)
18         count {|x| path.start_with?(x)} === 0
19     end
20 end
21
22 license = IO.readlines('license_header').join
23 modelines = IO.readlines('modelines.txt').join
24 exts = {
25     '.cpp'=>[license, modelines],
26     '.c'=>[license, modelines],
27     '.h'=>[license, modelines],
28     '.s'=>[license.gsub("//",";"), modelines.gsub("//",";")],
29     '.rb'=>[license.gsub("//","#"), modelines.gsub("//","#")]
30 }
31
32 ok_count = 0
33 modified_count = 0
34
35 Find.find('./') do |f|
36     if File.file?(f) && exts.include?(File.extname(f)) && exclude_dirs.do_not_has?(File.dirname(f))
37         lic = exts[File.extname(f)][0]
38         mod = exts[File.extname(f)][1]
39         modified = false
40         content = IO.readlines(f).join
41         if content.index(lic).nil? && no_license_dirs.do_not_has?(File.dirname(f))
42             content = lic + content
43             modified = true
44         end
45         if content.index(mod).nil?
46             content = content + mod
47             modified = true
48         end
49         if modified
50             File.open(f+".new", "w") do |out|
51                 out.write content
52             end
53             begin
54                 File.rename(f+".new", f)
55             rescue SystemCallError
56                 puts "Couldn't rename file #{f+".new"} to #{f}:", $!
57             end
58             puts "#{f} is UPDATED"
59             modified_count += 1
60         else
61             puts "#{f} is ok"
62             ok_count += 1
63         end
64     end
65 end
66
67 puts "#{modified_count} files changed, #{ok_count} files ok."
68
69 # kate: indent-width 4; replace-tabs on;
70 # vi:set ts=4:set expandtab=on:
Note: See TracBrowser for help on using the browser.