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 62 63 64 65 66 67 68
|
#!/usr/bin/ruby -w
# Takes a list of files and sorts #include directives in them
# alphabetically, a extra newline will be inserted between system
# includes and local includes if not already present.
class Content
def initialize()
@content = []
@last_group = nil
end
def append(group, text)
if @content.empty? then
@content.push([group, [text]])
else
(current_group, current_lst) = @content.last
if current_group == group then
current_lst.push(text)
else
@content.push([group, [text]])
end
end
end
def sort_and_write(out)
@content.each{ |item|
(group, lst) = item
# sort includes
if group == :user_include or group == :sys_include then
lst.sort!
end
# insert empty lines between include groups
if (@last_group == :user_include and group == :sys_include or
@last_group == :sys_include and group == :user_include) then
out.puts
end
lst.each{|line|
out.puts line
}
@last_group = group
}
end
end
ARGV.each { |arg|
lines = File.new(arg).readlines
content = Content.new()
lines.each { |line|
if line =~ /^#include \"/ then
content.append(:user_include, line)
elsif line =~ /^#include </ then
content.append(:sys_include, line)
else
content.append(:content, line)
end
}
content.sort_and_write(File.new(arg, "w"))
}
# EOF #
|