Skip to content

Commit 684b2bd

Browse files
committed
bash cript to sort includes and remove duplicates
* this sorts and remove duplicates in #include in src and tests folders * sorts includes in <...> before "..." * keep #include "ui_..." on top of list * can skip includes if an order should be kept * can exlcude directories (hard-copies of external libraries)
1 parent 95570eb commit 684b2bd

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

scripts/sort_include.sh

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/bin/bash
2+
###########################################################################
3+
# sort_include.sh
4+
# ---------------------
5+
# Date : June 2015
6+
# Copyright : (C) 2015 by Denis Rouzaud
7+
# Email : denis.rouzaud@gmail.com
8+
###########################################################################
9+
# #
10+
# This program is free software; you can redistribute it and/or modify #
11+
# it under the terms of the GNU General Public License as published by #
12+
# the Free Software Foundation; either version 2 of the License, or #
13+
# (at your option) any later version. #
14+
# #
15+
###########################################################################
16+
17+
18+
# this sorts and remove duplicates in #include in src and tests folders
19+
# sorts includes in <...> before "..."
20+
# keep #include "ui_..." on top of list
21+
# can skip includes if an order should be kept
22+
# can exlcude directories (hard-copies of external libraries)
23+
24+
SORTING=false
25+
FILE1=.sort_include_1.tmp
26+
FILE2=.sort_include_2.tmp
27+
FILE3=.sort_include_3.tmp
28+
29+
# files not to be sorted (leads to compile errors otherwise)
30+
DoNotSort="(sqlite3.h)|(spatialite.h)"
31+
32+
for file in $(find . \
33+
! -path "src/app/gps/qwtpolar-*" \
34+
! -path "src/core/gps/qextserialport/*" \
35+
! -path "src/plugins/grass/qtermwidget/*" \
36+
! -path "src/astyle/*" \
37+
! -path "python/ext-libs/*" \
38+
! -path "src/providers/spatialite/qspatialite/*" \
39+
! -path "src/plugins/dxf2shp_converter/dxflib/src/*" \
40+
! -path "src/plugins/globe/osgEarthQt/*" \
41+
! -path "src/plugins/globe/osgEarthUtil/*" \
42+
-type f -regex "(src)|(tests)/(.+/)*.*\.\(h\|cpp\)")
43+
do
44+
echo "$file"
45+
touch $FILE1
46+
while IFS= read -r line
47+
do
48+
if [[ "$line" =~ ^[[:space:]]*"#"include ]] && [[ ! "$line" =~ $DoNotSort ]]; then
49+
if ! $SORTING; then
50+
touch $FILE2
51+
touch $FILE3
52+
fi
53+
SORTING=true
54+
if [[ "$line" =~ ^"#"include[[:space:]]*\"ui_ ]]; then
55+
echo "$line" >> $FILE1 # keep ui_ on top of list
56+
elif [[ "$line" =~ ^"#"include[[:space:]]*\<[^[:space:]]+\> ]]; then
57+
echo "$line" >> $FILE2
58+
else
59+
echo "$line" >> $FILE3
60+
fi
61+
else
62+
if $SORTING; then
63+
sort -u $FILE2 >> $FILE1
64+
sort -u $FILE3 >> $FILE1
65+
rm -f $FILE2 $FILE3
66+
SORTING=false
67+
fi
68+
echo "$line" >> $FILE1
69+
fi
70+
done < "$file"
71+
rm -f $FILE2 $FILE3
72+
mv $FILE1 $file
73+
rm -f $FILE1
74+
done
75+

0 commit comments

Comments
 (0)