From 642b5fb0007b64c77d186fcb018d571152ee1d47 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Mon, 14 Aug 2023 18:51:07 +0200 Subject: reorganization: first steps --- contrib/sort_includes.rb | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100755 contrib/sort_includes.rb (limited to 'contrib/sort_includes.rb') diff --git a/contrib/sort_includes.rb b/contrib/sort_includes.rb new file mode 100755 index 0000000..c9f7076 --- /dev/null +++ b/contrib/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 +} + -- cgit v1.2.3