diff options
author | dzwdz | 2022-07-26 16:53:13 +0200 |
---|---|---|
committer | dzwdz | 2022-07-26 16:53:13 +0200 |
commit | 13519597d98c9271b7856242d5625896d18803c5 (patch) | |
tree | fc8ac2bb7ae2970bfa81aae9f89ce1e59bb5a2c7 /tools/sort_includes.rb | |
parent | 90af8825a41981ce2ee52e0a9ce84f624eb022e6 (diff) |
tools: add tools/sort_includes.rb
Diffstat (limited to 'tools/sort_includes.rb')
-rwxr-xr-x | tools/sort_includes.rb | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/tools/sort_includes.rb b/tools/sort_includes.rb new file mode 100755 index 0000000..c9f7076 --- /dev/null +++ b/tools/sort_includes.rb @@ -0,0 +1,42 @@ +#!/usr/bin/env ruby + +def is_include str + str.start_with? '#include' +end + +files = ARGV +if files.empty? + default = "src/**/*.[ch]" + puts "no arguments passed, defaulting to #{default}" + files = Dir[default] +end + +files.each { |path| + File.open(path, "r+") do |file| + lines = file.readlines + + last = nil + grouped = [] + lines.each do |line| + if is_include(line) != last + grouped << [line] + last = is_include(line) + else + grouped[-1] << line + end + end + + grouped.map do |group| + group.sort! if is_include group[0] + end + grouped = grouped.flatten + + next if grouped == lines + + puts path + file.truncate(0) + file.seek(0) + file.write grouped.join + end +} + |