File indexing completed on 2024-04-14 05:35:09

0001 #!/bin/sh
0002 
0003 # Laurent Montel <montel@kde.org> (2014-2022)
0004 # cd <directory> ; kde-dev-scripts/kf5/clean-includes.sh
0005 
0006 
0007 remove_include() {
0008         echo "$file" |xargs perl -pi -e "s!#include <$originalStr>\n!!"
0009         echo "$file" |xargs perl -pi -e "s!#include \"$originalStr\"\n!!"
0010 }
0011 
0012 file_header() {
0013         filewithoutext=$(echo "$file" |perl -pi -e 's!\.h!!');
0014         if test "$filewithoutext" != "$file" ; then
0015                 echo "$file is a header file";
0016                 existInclude=$(grep -c "$newname" "$file"|grep "#include");
0017                 echo "$newname : $existInclude";
0018                 if test "$existInclude" != 0 ; then
0019                         newNameClass=$(grep -c "$newname\s*\*"  "$file");
0020                         echo "number $number ";
0021                         echo "newNameClass $newNameClass";
0022                         number=$(expr "$number" - 1);
0023                         if test "$number" = "$newNameClass"; then
0024                                 remove_include;
0025                                 classNumber=$(grep -c "class"  "$file");
0026                                 if test "$classNumber" != 0 ; then
0027                                         perl -pi  -e "s!class!class $newname;\nclass!g" "$file";
0028                                         perl -pi -e "s!#include!#include <$newname>\n#include!g" "$filewithoutext".cpp;
0029                                 else
0030                                         perl -pi -e "s!#include!class $newname;\n#include!g" "$file";
0031                                         perl -pi -e "s!#include!#include <$newname>\n#include!g" "$filewithoutext".cpp;
0032                                 fi
0033                         fi
0034                 fi
0035         fi
0036 }
0037 
0038 test_include() {
0039         for file in $path/* ;
0040         do
0041                 if [[ $file == *"build"* ]]; then
0042                         continue;
0043                 fi
0044                 #echo $file;
0045                 if test -d "$file" ; then
0046                         echo "Check include into directory : $file";
0047                         path=$file;
0048                         test_include;
0049                 else
0050                         # echo "search in file $file";
0051                         include=$(grep "#include" "$file");
0052                         new=$(echo "$include" |perl -pi -e 's!#include !!');
0053 
0054                         new=$(echo "$new" |perl -pi -e 's!\\"!!g');
0055                         new=$(echo "$new" |perl -pi -e 's!<!!g');
0056                         new=$(echo "$new" |perl -pi -e 's!>!!g');
0057 
0058                         newname=$(echo "$new" |perl -pi -e 's!.h!!');
0059 
0060 
0061        echo "before go : $new";
0062        for i in $new ;
0063        do
0064                         originalStr=$i;
0065                         newname=$(echo "$i" | sed 's,.*/,,');
0066 
0067                newname=$(echo "$newname" |perl -pi -e 's!.h!!');
0068            if test 1 ; then
0069                        echo "egal $i";
0070                        number=$(grep -c "$newname" "$file");
0071                        echo "number $number";
0072                        if test "$number" = 1 ; then
0073                                #echo "$file" |xargs perl -pi -e "s!#include <$newname>\n!!";
0074                                # echo "number = 1 $newname class $file";
0075                                firstCar=$(echo "$i" | perl -pi -e "s/^(.)(.*)(.)$/\1/");
0076                                echo "first car : $firstCar";
0077                                if test "$firstCar" = "Q" || test "$firstCar" = "K" ; then
0078                                        case $newname in
0079                                            QtMath)
0080                                                        number=$(grep -c "qPow"  "$file");
0081                                                        echo "qPow $number";
0082                                                        if test "$number" = 0 ; then
0083                                                                remove_include;
0084                                                        fi
0085                                                        ;;
0086 
0087                                            QQmlContext)
0088                                                        number=$(grep -c "rootContext()"  "$file");
0089                                                        echo "rootContext() $number";
0090                                                        if test "$number" = 0 ; then
0091                                                                remove_include;
0092                                                        fi
0093                                                        ;;
0094                                                    QtQuickTest)
0095                                                        number=$(grep -c "QUICK_TEST_MAIN"  "$file");
0096                                                        echo "QUICK_TEST_MAIN $number";
0097                                                        if test "$number" = 0 ; then
0098                                                                remove_include;
0099                                                        fi
0100                                                        ;;
0101                                            QtConcurrentRun)
0102                                                        number=$(grep -c "QtConcurrent::run"  "$file");
0103                                                        echo "QtConcurrentRun $number";
0104                                                        if test "$number" = 0 ; then
0105                                                                remove_include;
0106                                                        fi
0107                                                        ;;
0108 
0109                                            QObject)
0110                                                        number=$(grep -c "QT_VERSION_CHECK"  "$file");
0111                                                        echo "QObject $number";
0112                                                        if test "$number" = 0 ; then
0113                                                                remove_include;
0114                                                        fi
0115                                                        ;;
0116 
0117                                            KJobUiDelegate)
0118                                                        number=$(grep -c "uiDelegate()"  "$file");
0119                                                        echo "KJobUiDelegate $number";
0120                                                        if test "$number" = 0 ; then
0121                                                                remove_include;
0122                                                        fi
0123                                                        ;;
0124                                                    QScopeGuard)
0125                                                        number=$(grep -c "qScopeGuard"  "$file");
0126                                                        echo "QScopeGuard $number";
0127                                                        if test "$number" = 0 ; then
0128                                                                remove_include;
0129                                                        fi
0130                                                        ;;
0131 
0132                                                QtDBus)
0133                                                        number=$(grep -c QDBusInterface  "$file");
0134                                                        echo "QDBusInterface $number";
0135                                                        if test "$number" = 0 ; then
0136                                                                remove_include;
0137                                                        fi
0138                                                        ;;
0139                                                QApplication)
0140                                                        number=$(grep -c qApp  "$file");
0141                                                        echo "qApp $number";
0142                                                        if test "$number" = 0 ; then
0143                                                                remove_include;
0144                                                        fi
0145                                                        ;;
0146                                                QGuiApplication)
0147                                                        number=$(grep -c qGuiApp  "$file");
0148                                                        echo "qApp $number";
0149                                                        if test "$number" = 0 ; then
0150                                                                remove_include;
0151                                                        fi
0152                                                        ;;
0153                                                QDesktopWidget)
0154                                                        number=$(grep -c "Application::desktop"  "$file");
0155                                                        if test "$number" = 0 ; then
0156                                                                number=$(grep -Ec "qApp->desktop()|kApp->desktop()"  "$file");
0157                                                                if test "$number" = 0 ; then
0158                                                                        remove_include;
0159                                                                fi
0160                                                        fi
0161                                                        ;;
0162                                                QDBusArgument)
0163                                                        number=$(grep -c "qdbus_cast"  "$file");
0164                                                        if test "$number" = 0 ; then
0165                                                                remove_include;
0166                                                        fi
0167                                                        ;;
0168                                                QDBusPendingCall)
0169                                                        number=$(grep -c "QDBusConnection::sessionBus().asyncCall"  "$file");
0170                                                        if test "$number" = 0 ; then
0171                                                                remove_include;
0172                                                        fi
0173                                                        ;;
0174 
0175                                                QDBusMetaType)
0176                                                        number=$(grep -c "qDBusRegisterMetaType"  "$file");
0177                                                        if test "$number" = 0 ; then
0178                                                                remove_include;
0179                                                        fi
0180                                                        ;;
0181 
0182                                                QScrollBar)
0183                                                        number=$(grep -c "verticalScrollBar()"  "$file");
0184                                                        if test "$number" = 0 ; then
0185                                                                number=$(grep -c  "horizontalScrollBar()"  "$file");
0186                                                                if test "$number" = 0 ; then
0187                                                                        remove_include;
0188                                                                fi
0189                                                        fi
0190                                                        ;;
0191                                                QClipboard)
0192                                                        number=$(grep -Ec "Application::clipboard|clipboard()"  "$file");
0193                                                        if test "$number" = 0 ; then
0194                                                                remove_include;
0195                                                        fi
0196                                                        ;;
0197                                                QMetaType)
0198                                                        number=$(grep -c "Q_DECLARE_METATYPE"  "$file");
0199                                                        if test "$number" = 0 ; then
0200                                                                remove_include;
0201                                                        fi
0202                                                        ;;
0203                                                KLocale)
0204                                                        number=$(grep  -c "i18n"  "$file");
0205                                                        if test "$number" = 0 ; then
0206                                                                number=$(grep  -c "locale()"  "$file");
0207                                                                if test "$number" = 0 ; then
0208                                                                        remove_include;
0209                                                                fi
0210                                                        fi
0211                                                        ;;
0212                                                KActionCollection)
0213                                                        number=$(grep  -c "actionCollection"  "$file");
0214                                                        if test "$number" = 0 ; then
0215                                                                remove_include;
0216                                                        fi
0217                                                        ;;
0218                                                KApplication)
0219                                                        number=$(grep  -c '\bkapp\b'  "$file");
0220                                                        echo "qApp $number";
0221                                                        if test "$number" = 0 ; then
0222                                                                remove_include;
0223                                                        fi
0224                                                        ;;
0225                                                KDebug)
0226                                                        ;;
0227                                                KPluginFactory)
0228                                                        ;;
0229                                                KPluginLoader)
0230                                                        ;;
0231                                                KXMLGUIFactory)
0232                                                        number=$(grep  -c "factory()"  "$file");
0233                                                        if test "$number" = 0 ; then
0234                                                                number=$(grep  -c "guiFactory()"  "$file");
0235                                                                if test "$number" = 0 ; then
0236                                                                        remove_include;
0237                                                                fi
0238                                                        fi
0239                                                        ;;
0240                                                KGenericFactory)
0241                                                        number=$(grep  -c "K_EXPORT_PLUGIN"  "$file");
0242                                                        if test "$number" = 0 ; then
0243                                                                remove_include;
0244                                                        fi
0245                                                        ;;
0246                                                QCoreApplication)
0247                                                        number=$(grep  -c "qApp"  "$file");
0248                                                        if test "$number" = 0 ; then
0249                                                            number=$(grep  -c "qAddPostRoutine"  "$file");
0250                                                            if test "$number" = 0 ; then
0251                                                                    remove_include;
0252                                                                fi
0253                                                        fi
0254                                                        ;;
0255                                                QTreeView)
0256                                                        ;;
0257 
0258                                                QDateTime)
0259                                                        number=$(grep  -c "QDate::currentDate"  "$file");
0260                                                        if test "$number" = 0 ; then
0261                                                                remove_include;
0262                                                        fi
0263                                                        ;;
0264                                                KLineEdit)
0265                                                        number=$(grep  -c "lineEdit()"  "$file");
0266                                                        if test "$number" = 0 ; then
0267                                                                remove_include;
0268                                                        fi
0269                                                        ;;
0270                                                QDBusConnectionInterface)
0271                                                        ;;
0272                                                KLocalizedString)
0273                                                        number=$(grep -E "i18n|I18N_NOOP"  "$file");
0274                                                        if test "$number" = 0 ; then
0275                                                                remove_include;
0276                                                        fi
0277                                                        ;;
0278                                                QAbstractTextDocumentLayout)
0279                                                        ;;
0280                                                QAbstractItemView)
0281                                                        ;;
0282                                                QMetaEnum)
0283                                                        number=$(grep  -c "enumerator"  "$file");
0284                                                        if test "$number" = 0 ; then
0285                                                                remove_include;
0286                                                        fi
0287                                                        ;;
0288 
0289                                                QLayout)
0290                                                        number=$(grep  -c "layout()"  "$file");
0291                                                        if test "$number" = 0 ; then
0292                                                                remove_include;
0293                                                        fi
0294                                                        ;;
0295                                                QAction)
0296                                                        number=$(grep  -c "\baddAction\b"  "$file");
0297                                                        if test "$number" = 0 ; then
0298                                                                remove_include;
0299                                                        fi
0300                                                        ;;
0301                                                QTextDocument)
0302                                                        number=$(grep  -c "Qt::escape"  "$file");
0303                                                        if test "$number" = 0 ; then
0304                                                                remove_include;
0305                                                        fi
0306                                                        ;;
0307                                                QHeaderView)
0308                                                        number=$(grep  -c "header()"  "$file");
0309                                                        if test "$number" = 0 ; then
0310                                                                number=$(grep  -c "horizontalHeader()" "$file");
0311                                                                if test "$number" = 0 ; then
0312                                                                        number=$(grep  -c "verticalHeader()" "$file");
0313                                                                        if test "$number" = 0 ; then
0314                                                                                remove_include;
0315                                                                        fi
0316                                                                fi
0317                                                        fi
0318                                                        ;;
0319                                                KRecentFilesAction)
0320                                                        number=$(grep  -c "KStandardAction::openRecent" "$file");
0321                                                        if test "$number" = 0 ; then
0322                                                                remove_include;
0323                                                        fi
0324                                                        ;;
0325                                                KStatusBar)
0326                                                        number=$(grep  -c "statusBar()" "$file");
0327                                                        if test "$number" = 0 ; then
0328                                                                remove_include;
0329                                                        fi
0330                                                        ;;
0331                                                QStatusBar)
0332                                                        number=$(grep  -c "statusBar()" "$file");
0333                                                        if test "$number" = 0 ; then
0334                                                                remove_include;
0335                                                        fi
0336                                                        ;;
0337                                                KGlobal)
0338                                                        number=$(grep  -c "K_GLOBAL_STATIC" "$file");
0339                                                        if test "$number" = 0 ; then
0340                                                                remove_include;
0341                                                        fi
0342                                                        ;;
0343                                                KStandardDirs)
0344                                                        number=$(grep  -c "KGlobal::dirs()->" "$file");
0345                                                        if test "$number" = 0 ; then
0346                                                                remove_include;
0347                                                        fi
0348                                                        ;;
0349                                                QTabBar)
0350                                                        number=$(grep  -c "tabBar()" "$file");
0351                                                        if test "$number" = 0 ; then
0352                                                                remove_include;
0353                                                        fi
0354                                                        ;;
0355                                                KCompletionBox)
0356                                                        number=$(grep  -c "completionBox()" "$file");
0357                                                        if test "$number" = 0 ; then
0358                                                                remove_include;
0359                                                        fi
0360                                                        ;;
0361 
0362                                                KIconLoader)
0363                                                        number=$(grep -c  "SmallIcon" "$file");
0364                                                        if test "$number" = 0 ; then
0365                                                                remove_include;
0366                                                        fi
0367                                                        ;;
0368 
0369                                                KZip)
0370                                                        number=$(grep  -c "archive()" "$file");
0371                                                        if test "$number" = 0 ; then
0372                                                                remove_include;
0373                                                        fi
0374                                                        ;;
0375                                                QDebug)
0376                                                        number=$(grep -Ec "qDebug|qWarning|qCritical"  "$file");
0377                                                        if test "$number" = 0 ; then
0378                                                                remove_include;
0379                                                        fi
0380                                                        #number=`grep "qDebug"  $file|wc -l`;
0381                                                        #if test $number = 0 ; then
0382                                                        #     number=`grep "qWarning"  $file|wc -l`;
0383                                                        #     if test $number = 0 ; then
0384                                                        #        remove_include;
0385                                                        #     fi
0386                                                        #fi
0387                                                        ;;
0388                                                QtDebug)
0389                                                        number=$(grep -Ec "qDebug|qWarning|qCritical"  "$file");
0390                                                        if test "$number" = 0 ; then
0391                                                                remove_include;
0392                                                        fi
0393                                                        #number=`grep "qDebug"  $file|wc -l`;
0394                                                        #if test $number = 0 ; then
0395                                                        #     number=`grep "qWarning"  $file|wc -l`;
0396                                                        #     if test $number = 0 ; then
0397                                                        #        remove_include;
0398                                                        #     fi
0399                                                        #fi
0400                                                        ;;
0401 
0402                                                QLoggingCategory)
0403                                                        number=$(grep -Ec "Q_DECLARE_LOGGING_CATEGORY|Q_LOGGING_CATEGORY" "$file");
0404                                                        if test "$number" = 0 ; then
0405                                                                number=$(grep -Ec "qDebug|qWarning|qCritical"  "$file");
0406                                                                if test "$number" = 0 ; then
0407                                                                        remove_include;
0408                                                                fi
0409                                                        fi
0410                                                        ;;
0411                                                QScreen)
0412                                                        number=$(grep  -c "physicalDotsPerInch()" "$file");
0413                                                        if test "$number" = 0 ; then
0414                                                            number=$(grep  -c "screen()" "$file");
0415                                                            if test "$number" = 0 ; then
0416                                                                  remove_include;
0417                                                                    fi
0418                                                        fi
0419                                                        ;;
0420                                                QMimeData)
0421                                                        number=$(grep  -c "mimeData()" "$file");
0422                                                        if test "$number" = 0 ; then
0423                                                                remove_include;
0424                                                        fi
0425                                                        ;;
0426                                                KToolBar)
0427                                                        number=$(grep  -c "toolBar()" "$file");
0428                                                        if test "$number" = 0 ; then
0429                                                                remove_include;
0430                                                        fi
0431                                                        ;;
0432                                                QPixmap)
0433                                                        number=$(grep  -c "SmallIcon" "$file");
0434                                                        if test "$number" = 0 ; then
0435                                                                remove_include;
0436                                                        fi
0437                                                        ;;
0438                                                QHostAddress)
0439                                                        ;;
0440                                                QMenuBar)
0441                                                        number=$(grep  -c "menuBar()" "$file");
0442                                                        if test "$number" = 0 ; then
0443                                                                remove_include;
0444                                                        fi
0445                                                        ;;
0446                                                QTest)
0447                                                        number=$(grep -Ec "QCOMPARE|QVERIFY|QTEST_GUILESS_MAIN|QTEST" "$file");
0448                                                        if test "$number" = 0 ; then
0449                                                                remove_include;
0450                                                        fi
0451                                                        ;;
0452                                                QtTestGui)
0453                                                        number=$(grep -Ec "QCOMPARE|QVERIFY|QTEST_GUILESS_MAIN|QTEST" "$file");
0454                                                        if test "$number" = 0 ; then
0455                                                                remove_include;
0456                                                        fi
0457                                                        ;;
0458                                                QtTestWidgets)
0459                                                        number=$(grep -Ec "QCOMPARE|QVERIFY|QTEST_GUILESS_MAIN|QTEST" "$file");
0460                                                        if test "$number" = 0 ; then
0461                                                                remove_include;
0462                                                        fi
0463                                                        ;;
0464                                                QtTest)
0465                                                        number=$(grep -Ec "QCOMPARE|QVERIFY|QTEST_GUILESS_MAIN|QTEST" "$file");
0466                                                        if test "$number" = 0 ; then
0467                                                                remove_include;
0468                                                        fi
0469                                                        ;;
0470                                                *)
0471                                                echo "remove it !!!!!";
0472                                                        remove_include;
0473                                                        ;;
0474                                        esac
0475                                elif [ "$number" -gt 1 ]; then
0476                                        #file_header;
0477                                        echo "tot";
0478              fi
0479           fi
0480           fi
0481   done;
0482    fi
0483 done;
0484 }
0485 
0486 path=$PWD;
0487 test_include
0488 git diff .