|
3 | 3 | # Inputs: |
4 | 4 | # ARGV[0] - path to top level cmake source directory (one level above 'src' directory) |
5 | 5 |
|
6 | | -require 'pathname' |
7 | | -require 'rubygems' |
8 | 6 | require 'fileutils' |
| 7 | +require 'pathname' |
9 | 8 |
|
10 | 9 | include FileUtils |
11 | 10 |
|
12 | | -# check that called from command line directly |
13 | | -if not ($0 == __FILE__) |
14 | | - puts "#{__FILE__} called from external script" |
15 | | - exit |
16 | | -end |
| 11 | +ROOT_DIR = Pathname.new(__dir__).parent.parent |
17 | 12 |
|
18 | | -basepath = ARGV[0].gsub("\\", "/") |
| 13 | +license_lines = [ |
| 14 | + "OpenStudio(R), Copyright (c) OpenStudio Coalition and other contributors.\n", |
| 15 | + "See also https://openstudiocoalition.org/about/software_license/\n", |
| 16 | +] |
19 | 17 |
|
20 | 18 | copyright = "/***********************************************************************************************************************\n" |
21 | 19 | ruby_copyright = "########################################################################################################################\n" |
22 | | -File.open(basepath + "/LICENSE.md") do |file| |
23 | | - while (line = file.gets) |
24 | | - if line.strip.empty? |
25 | | - copyright += "*" + line |
26 | | - ruby_copyright += "#" + line |
27 | | - |
28 | | - else |
29 | | - copyright += "* " + line |
30 | | - ruby_copyright += "# " + line |
31 | | - end |
| 20 | +license_lines.each do |line| |
| 21 | + if line.strip.empty? |
| 22 | + copyright += '*' + line |
| 23 | + ruby_copyright += '#' + line |
| 24 | + |
| 25 | + else |
| 26 | + copyright += '* ' + line |
| 27 | + ruby_copyright += '# ' + line |
32 | 28 | end |
33 | 29 | end |
34 | 30 | copyright += "***********************************************************************************************************************/\n\n" |
35 | 31 | ruby_copyright += "########################################################################################################################\n\n" |
36 | 32 |
|
37 | 33 | # first do c++ |
38 | 34 |
|
39 | | -# exceptions are files that are not part of OpenStudio |
40 | | -exceptions = [basepath + "/src/qtwinmigrate/", |
41 | | - "mainpage.hpp"] |
| 35 | +# exclusions are files that are not part of OpenStudio |
| 36 | +folder_exclusions = [ |
| 37 | + ROOT_DIR / 'src/qtwinmigrate', |
| 38 | +] |
| 39 | +filename_exclusions = [ |
| 40 | + 'mainpage.hpp', |
| 41 | +] |
42 | 42 |
|
43 | 43 | # glob for hpp and cpp |
44 | | -files = Dir.glob(basepath + "/src/**/*.[ch]pp") |
45 | | -files.concat Dir.glob(basepath + "/ruby/**/*.[ch]pp") |
46 | | -files.concat Dir.glob(basepath + "/src/**/*.cxx.in") |
47 | | -files.concat Dir.glob(basepath + "/src/**/*.tmp") |
| 44 | +files = ROOT_DIR.glob('src/**/*.[ch]pp') |
| 45 | +files += ROOT_DIR.glob('ruby/**/*.[ch]pp') |
| 46 | +files += ROOT_DIR.glob('src/**/*.[ch]xx.in') |
| 47 | +files += ROOT_DIR.glob('src/**/*.tmp') |
48 | 48 |
|
49 | 49 | # reject exceptions |
50 | 50 | files.reject! do |p| |
51 | | - result = false |
52 | | - exceptions.each do |e| |
53 | | - if p.include?(e) |
54 | | - result = true |
55 | | - puts p |
56 | | - break |
57 | | - end |
58 | | - end |
59 | | - result |
| 51 | + filename_exclusions.any? { |fname| p.basename.to_s == fname } || |
| 52 | + p.ascend { |path| break true if folder_exclusions.any? { |p2| path == p2 } } |
60 | 53 | end |
61 | 54 |
|
62 | 55 | # loop over all files |
63 | 56 | files.each do |p| |
64 | | - |
65 | | - # start with copyright |
66 | | - text = copyright |
67 | | - |
68 | | - # read file |
69 | | - File.open(p, "r") do |file| |
70 | | - # read until end of current copyright |
71 | | - while (line = file.gets) |
72 | | - if not /^\s?[\/\*]/.match(line) |
73 | | - if not line.chomp.empty? |
74 | | - text += line |
75 | | - end |
76 | | - break |
77 | | - end |
78 | | - end |
79 | | - |
80 | | - # now keep rest of file |
81 | | - while (line = file.gets) |
82 | | - text += line |
83 | | - end |
84 | | - end |
| 57 | + # Read lines and remove copyright |
| 58 | + lines = p.readlines |
| 59 | + lines.shift(lines.find_index { |line| !(line.chomp.empty? || %r{^\s?[/*]}.match(line)) }) |
85 | 60 |
|
86 | 61 | # write file |
87 | | - File.open(p, "w") do |file| |
88 | | - file << text |
89 | | - end |
90 | | - |
| 62 | + p.write(copyright + lines.join) |
91 | 63 | end |
92 | 64 |
|
93 | 65 | # now do ruby |
94 | 66 |
|
95 | 67 | # exceptions are files that are not part of OpenStudio |
96 | | -exceptions = [] |
| 68 | +folder_exclusions = [] |
| 69 | +filename_exclusions = [] |
97 | 70 |
|
98 | 71 | # glob for rb |
99 | | -files = Dir.glob(basepath + "/ruby/**/*.rb") |
| 72 | +files = ROOT_DIR.glob('ruby/**/*.rb') |
100 | 73 |
|
101 | 74 | # reject exceptions |
102 | 75 | files.reject! do |p| |
103 | | - result = false |
104 | | - exceptions.each do |e| |
105 | | - if p.include?(e) |
106 | | - result = true |
107 | | - break |
108 | | - end |
109 | | - end |
110 | | - result |
| 76 | + filename_exclusions.any? { |fname| p.basename.to_s == fname } || |
| 77 | + p.ascend { |path| break true if folder_exclusions.any? { |p2| path == p2 } } |
111 | 78 | end |
112 | 79 |
|
113 | 80 | # loop over all files |
114 | 81 | files.each do |p| |
115 | | - |
116 | | - # start with copyright |
117 | | - text = ruby_copyright |
118 | | - |
119 | | - # read file |
120 | | - File.open(p, "r") do |file| |
121 | | - # read until end of current copyright |
122 | | - while (line = file.gets) |
123 | | - if not /^#/.match(line) |
124 | | - if not line.chomp.empty? |
125 | | - text += line |
126 | | - end |
127 | | - break |
128 | | - end |
129 | | - end |
130 | | - |
131 | | - # now keep rest of file |
132 | | - while (line = file.gets) |
133 | | - text += line |
134 | | - end |
135 | | - end |
| 82 | + # Read lines and remove copyright |
| 83 | + lines = p.readlines |
| 84 | + lines.shift(lines.find_index { |line| !(line.chomp.empty? || /^#/.match(line)) }) |
136 | 85 |
|
137 | 86 | # write file |
138 | | - File.open(p, "w") do |file| |
139 | | - file << text |
140 | | - end |
141 | | - |
| 87 | + p.write(ruby_copyright + lines.join) |
142 | 88 | end |
0 commit comments