-
Notifications
You must be signed in to change notification settings - Fork 33
/
packageimport.rb
62 lines (58 loc) · 1.6 KB
/
packageimport.rb
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
require 'find'
require 'fileutils'
require 'set'
def ProcessModule( path, modName, modList )
puts "ProcessModule #{path}"
identifiers = Set.new
File.open(path).each_line do |s|
if s =~ /^[ \t]*\*/ then
next
end
s.split( /[^0-9A-Za-z_]/ ).each do |tok|
if tok =~ /^[_A-Z][_A-Za-z0-9]*$/ then
identifiers << tok
end
end
end
lines = IO.readlines(path)
w=File.new(path,"wb+")
lines.each do |s|
if s =~/\/\/ packageimport$/ then
else
w.print "#{s}"
end
if s =~ /^module +((([a-zA-Z0-9_]+)\.)*)([a-zA-Z0-9_]+);/ then
packname = $1
(modList-modName).intersection(identifiers).each do|id|
w.print "import #{packname}#{id};\n"
end
end
end
end
def ProcessDirectory( path )
puts path
modList = Set.new
Dir.foreach(path) do |entry|
filename = File.join(path,entry)
if FileTest.file?(filename) && filename =~ /\.d$/ then
modList << entry[ 0 .. -3 ]
end
end
Dir.foreach(path) do |entry|
filename = File.join(path,entry)
if FileTest.file?(filename) && filename =~ /\.d$/ then
ProcessModule( filename, entry[ 0 .. -3 ], modList )
end
end
end
ARGV.each do|startpath|
puts "processing #{startpath}"
if !FileTest.directory?(startpath) then
raise "Argument is not a directory"
end
Find.find( startpath ) do |path|
if FileTest.directory?(path) then
ProcessDirectory(path)
end
end
end