| 1 | // Copyright (C) 2023 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
| 3 | |
| 4 | #include "qqmltyperegistrarutils_p.h" |
| 5 | |
| 6 | #include "qanystringviewutils_p.h" |
| 7 | #include "qqmltyperegistrarconstants_p.h" |
| 8 | #include "qqmltyperegistrarconstants_p.h" |
| 9 | #include "qmetatypesjsonprocessor_p.h" |
| 10 | |
| 11 | #include <QtCore/qcborarray.h> |
| 12 | #include <QtCore/qcbormap.h> |
| 13 | #include <QtCore/qcborvalue.h> |
| 14 | #include <QtCore/qdebug.h> |
| 15 | #include <QtCore/qfile.h> |
| 16 | #include <QtCore/qdiriterator.h> |
| 17 | #include <QtCore/private/qduplicatetracker_p.h> |
| 18 | #include <QtCore/qtextstream.h> |
| 19 | |
| 20 | using namespace Qt::Literals::StringLiterals; |
| 21 | |
| 22 | QT_BEGIN_NAMESPACE |
| 23 | |
| 24 | QTypeRevision handleInMinorVersion(QTypeRevision revision, int majorVersion) |
| 25 | { |
| 26 | if (!revision.hasMajorVersion() && revision.hasMinorVersion()) { |
| 27 | // this version has been obtained by QML_{ADDED,REMOVED}_IN_MINOR_VERSION |
| 28 | revision = QTypeRevision::fromVersion(majorVersion, minorVersion: revision.minorVersion()); |
| 29 | } |
| 30 | return revision; |
| 31 | } |
| 32 | |
| 33 | QAnyStringView interfaceName(const Interface &iface) |
| 34 | { |
| 35 | return iface.className; |
| 36 | } |
| 37 | |
| 38 | static QDebug message(QDebug base, QAnyStringView message, QAnyStringView fileName, int lineNumber) |
| 39 | { |
| 40 | const QString lineString = lineNumber ? QString::number(lineNumber) : QString(); |
| 41 | return (base.noquote().nospace() |
| 42 | << message << ": " << fileName << ":" << lineString << ":" ).space(); |
| 43 | } |
| 44 | |
| 45 | QDebug warning(QAnyStringView fileName, int lineNumber) |
| 46 | { |
| 47 | return message(qWarning(), message: "Warning" , fileName, lineNumber); |
| 48 | } |
| 49 | |
| 50 | QDebug warning(const MetaType &classDef) |
| 51 | { |
| 52 | const QAnyStringView file = classDef.inputFile(); |
| 53 | int lineNo = classDef.lineNumber(); |
| 54 | if (!file.isEmpty()) |
| 55 | return warning(fileName: file, lineNumber: lineNo); |
| 56 | |
| 57 | return warning(fileName: classDef.qualifiedClassName()); |
| 58 | } |
| 59 | |
| 60 | QDebug error(QAnyStringView fileName, int lineNumber) |
| 61 | { |
| 62 | return message(qCritical(), message: "Error" , fileName, lineNumber); |
| 63 | } |
| 64 | |
| 65 | /*! |
| 66 | \internal |
| 67 | \a pathToList points to a file listing all qt.parts.conf files |
| 68 | In any given directory, there might be more than one qt.parts.conf file (especially on Winodws). |
| 69 | We need to merge all import paths for a a given folder (but want to avoid duplicate entries). |
| 70 | */ |
| 71 | int mergeQtConfFiles(const QString &pathToList) |
| 72 | { |
| 73 | QFile listFile(pathToList); |
| 74 | if (!listFile.open(flags: QFile::ReadOnly | QFile::Text)) |
| 75 | return EXIT_FAILURE; |
| 76 | QMultiHash<QString, QString> directoryToNecessaryImports; |
| 77 | while (!listFile.atEnd()) { |
| 78 | QByteArray partFilePath = listFile.readLine().trimmed(); |
| 79 | QString directoryPath = QFileInfo(QString::fromUtf8(ba: partFilePath)).absolutePath(); |
| 80 | QDirIterator dirIt(directoryPath, { "*_qt.part.conf"_L1 }, QDir::Filter::Files ); |
| 81 | while (dirIt.hasNext()) { |
| 82 | QFile partialFile(dirIt.next()); |
| 83 | if (!partialFile.open(flags: QFile::ReadOnly | QFile::Text)) { |
| 84 | qDebug() << "could not open" << partialFile.fileName(); |
| 85 | return EXIT_FAILURE; |
| 86 | } |
| 87 | while (!partialFile.atEnd()) { |
| 88 | QByteArray import = partialFile.readLine().trimmed(); |
| 89 | directoryToNecessaryImports.insert(key: directoryPath, value: QString::fromUtf8(ba: import)); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | for (const QString &directoryPath: directoryToNecessaryImports.keys()) { |
| 94 | QFile consolidatedQtConfFile(directoryPath + QDir::separator() + u"qt.conf" ); |
| 95 | if (!consolidatedQtConfFile.open(flags: QFile::WriteOnly | QFile::Text)) { |
| 96 | qDebug() << "could not open" << consolidatedQtConfFile.fileName(); |
| 97 | return EXIT_FAILURE; |
| 98 | } |
| 99 | QTextStream out(&consolidatedQtConfFile); |
| 100 | QStringList allIncludes = directoryToNecessaryImports.values(key: directoryPath); |
| 101 | allIncludes.removeDuplicates(); |
| 102 | out << "[Config]\nMergeQtConf = true\n" |
| 103 | << "[Paths]\nQmlImports = " |
| 104 | << allIncludes.join(sep: u"," ) << Qt::endl; |
| 105 | } |
| 106 | QFile outfile(pathToList + u".done" ); |
| 107 | if (!outfile.open(flags: QFile::WriteOnly | QFile::Text)) |
| 108 | return EXIT_FAILURE; |
| 109 | outfile.write(data: QByteArray::number(QDateTime::currentSecsSinceEpoch())); |
| 110 | return EXIT_SUCCESS; |
| 111 | } |
| 112 | |
| 113 | QT_END_NAMESPACE |
| 114 | |