Skip to content
Permalink
Browse files

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)
  • Loading branch information
3nids committed Jun 10, 2015
1 parent 95570eb commit 684b2bd110c0da0fd65912ab82b47cf2e1e3d94f
Showing with 75 additions and 0 deletions.
  1. +75 −0 scripts/sort_include.sh
@@ -0,0 +1,75 @@
#!/bin/bash
###########################################################################
# sort_include.sh
# ---------------------
# Date : June 2015
# Copyright : (C) 2015 by Denis Rouzaud
# Email : denis.rouzaud@gmail.com
###########################################################################
# #
# This program is free software; you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; either version 2 of the License, or #
# (at your option) any later version. #
# #
###########################################################################


# 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)

SORTING=false
FILE1=.sort_include_1.tmp
FILE2=.sort_include_2.tmp
FILE3=.sort_include_3.tmp

# files not to be sorted (leads to compile errors otherwise)
DoNotSort="(sqlite3.h)|(spatialite.h)"

for file in $(find . \
! -path "src/app/gps/qwtpolar-*" \
! -path "src/core/gps/qextserialport/*" \
! -path "src/plugins/grass/qtermwidget/*" \
! -path "src/astyle/*" \
! -path "python/ext-libs/*" \
! -path "src/providers/spatialite/qspatialite/*" \
! -path "src/plugins/dxf2shp_converter/dxflib/src/*" \
! -path "src/plugins/globe/osgEarthQt/*" \
! -path "src/plugins/globe/osgEarthUtil/*" \
-type f -regex "(src)|(tests)/(.+/)*.*\.\(h\|cpp\)")
do
echo "$file"
touch $FILE1
while IFS= read -r line
do
if [[ "$line" =~ ^[[:space:]]*"#"include ]] && [[ ! "$line" =~ $DoNotSort ]]; then
if ! $SORTING; then
touch $FILE2
touch $FILE3
fi
SORTING=true
if [[ "$line" =~ ^"#"include[[:space:]]*\"ui_ ]]; then
echo "$line" >> $FILE1 # keep ui_ on top of list
elif [[ "$line" =~ ^"#"include[[:space:]]*\<[^[:space:]]+\> ]]; then
echo "$line" >> $FILE2
else
echo "$line" >> $FILE3
fi
else
if $SORTING; then
sort -u $FILE2 >> $FILE1
sort -u $FILE3 >> $FILE1
rm -f $FILE2 $FILE3
SORTING=false
fi
echo "$line" >> $FILE1
fi
done < "$file"
rm -f $FILE2 $FILE3
mv $FILE1 $file
rm -f $FILE1
done

3 comments on commit 684b2bd

@nyalldawson

This comment has been minimized.

Copy link
Collaborator

@nyalldawson nyalldawson replied Jun 10, 2015

@3nids this is cool! Have you thought about adding it to the prepare commit script?

@3nids

This comment has been minimized.

Copy link
Member Author

@3nids 3nids replied Jun 10, 2015

still testing...
I'll see if I can integrate it later on!

@jef-n

This comment has been minimized.

Copy link
Member

@jef-n jef-n replied Jun 10, 2015

Done in facfe6e. I'll do a test build on Windows after astyle-all.sh finishes...

Thoughts on also integrating autopep8 for python (instead of just warning with pep8)?

Please sign in to comment.
You can’t perform that action at this time.