Skip to content

Commit

Permalink
bash cript to sort includes and remove duplicates
Browse files Browse the repository at this point in the history
* 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 684b2bd
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@3nids
Copy link
Member Author

@3nids 3nids commented on 684b2bd Jun 10, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@jef-n
Copy link
Member

@jef-n jef-n commented on 684b2bd Jun 10, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.