From fbc463e84d5cc6012953140c93f6b18f78e66bf8 Mon Sep 17 00:00:00 2001 From: Andy Shaw Date: Tue, 22 Oct 2019 16:53:26 +0200 Subject: Add support for scanning qrc files in qmlimportscanner This reuses the ResourceFileMapper and extends it slightly to return full paths on request. Subsequently, this is moved into a shared directory inside tools. Fixes: QTBUG-55259 Change-Id: Ice5fc68d03b767a4185742e182556ab4290bd28d Reviewed-by: Simon Hausmann Reviewed-by: Fabian Kosmale --- tools/qmlcachegen/qmlcachegen.pro | 8 +- tools/qmlcachegen/resourcefilemapper.cpp | 167 --------------------------- tools/qmlcachegen/resourcefilemapper.h | 50 -------- tools/qmlimportscanner/main.cpp | 11 +- tools/qmlimportscanner/qmlimportscanner.pro | 1 + tools/shared/resourcefilemapper.cpp | 170 ++++++++++++++++++++++++++++ tools/shared/resourcefilemapper.h | 54 +++++++++ tools/shared/shared.pri | 3 + 8 files changed, 242 insertions(+), 222 deletions(-) delete mode 100644 tools/qmlcachegen/resourcefilemapper.cpp delete mode 100644 tools/qmlcachegen/resourcefilemapper.h create mode 100644 tools/shared/resourcefilemapper.cpp create mode 100644 tools/shared/resourcefilemapper.h create mode 100644 tools/shared/shared.pri diff --git a/tools/qmlcachegen/qmlcachegen.pro b/tools/qmlcachegen/qmlcachegen.pro index bee0b9a37e..ec65cdb5e6 100644 --- a/tools/qmlcachegen/qmlcachegen.pro +++ b/tools/qmlcachegen/qmlcachegen.pro @@ -5,8 +5,10 @@ DEFINES += QT_NO_CAST_TO_ASCII QT_NO_CAST_FROM_ASCII SOURCES = qmlcachegen.cpp \ resourcefilter.cpp \ - generateloader.cpp \ - resourcefilemapper.cpp + generateloader.cpp + +include(../shared/shared.pri) + TARGET = qmlcachegen build_integration.files = qmlcache.prf qtquickcompiler.prf @@ -38,5 +40,3 @@ QMAKE_TARGET_DESCRIPTION = QML Cache Generator load(qt_tool) -HEADERS += \ - resourcefilemapper.h diff --git a/tools/qmlcachegen/resourcefilemapper.cpp b/tools/qmlcachegen/resourcefilemapper.cpp deleted file mode 100644 index 244874717f..0000000000 --- a/tools/qmlcachegen/resourcefilemapper.cpp +++ /dev/null @@ -1,167 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtQml module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "resourcefilemapper.h" - -#include -#include -#include - -ResourceFileMapper::ResourceFileMapper(const QStringList &resourceFiles) -{ - for (const QString &fileName: resourceFiles) { - QFile f(fileName); - if (!f.open(QIODevice::ReadOnly)) - continue; - populateFromQrcFile(f); - } -} - -bool ResourceFileMapper::isEmpty() const -{ - return qrcPathToFileSystemPath.isEmpty(); -} - -QStringList ResourceFileMapper::resourcePaths(const QString &fileName) -{ - const QString absPath = QDir::cleanPath(QDir::current().absoluteFilePath(fileName)); - QStringList resourcePaths; - for (auto it = qrcPathToFileSystemPath.cbegin(), end = qrcPathToFileSystemPath.cend(); it != end; ++it) { - if (QFileInfo(it.value()) == QFileInfo(absPath)) - resourcePaths.append(it.key()); - } - return resourcePaths; -} - -QStringList ResourceFileMapper::qmlCompilerFiles() const -{ - QStringList files; - for (auto it = qrcPathToFileSystemPath.constBegin(), end = qrcPathToFileSystemPath.constEnd(); - it != end; ++it) { - const QString &qrcPath = it.key(); - const QString suffix = QFileInfo(qrcPath).suffix(); - if (suffix != QStringLiteral("qml") && suffix != QStringLiteral("js") && suffix != QStringLiteral("mjs")) - continue; - files << qrcPath; - } - return files; -} - -void ResourceFileMapper::populateFromQrcFile(QFile &file) -{ - enum State { - InitialState, - InRCC, - InResource, - InFile - }; - State state = InitialState; - - QDir qrcDir = QFileInfo(file).absoluteDir(); - - QString prefix; - QString currentFileName; - QXmlStreamAttributes currentFileAttributes; - - QXmlStreamReader reader(&file); - while (!reader.atEnd()) { - switch (reader.readNext()) { - case QXmlStreamReader::StartElement: - if (reader.name() == QStringLiteral("RCC")) { - if (state != InitialState) - return; - state = InRCC; - continue; - } else if (reader.name() == QStringLiteral("qresource")) { - if (state != InRCC) - return; - state = InResource; - QXmlStreamAttributes attributes = reader.attributes(); - if (attributes.hasAttribute(QStringLiteral("prefix"))) - prefix = attributes.value(QStringLiteral("prefix")).toString(); - if (!prefix.startsWith(QLatin1Char('/'))) - prefix.prepend(QLatin1Char('/')); - if (!prefix.endsWith(QLatin1Char('/'))) - prefix.append(QLatin1Char('/')); - continue; - } else if (reader.name() == QStringLiteral("file")) { - if (state != InResource) - return; - state = InFile; - currentFileAttributes = reader.attributes(); - continue; - } - return; - - case QXmlStreamReader::EndElement: - if (reader.name() == QStringLiteral("file")) { - if (state != InFile) - return; - state = InResource; - continue; - } else if (reader.name() == QStringLiteral("qresource")) { - if (state != InResource) - return; - state = InRCC; - continue; - } else if (reader.name() == QStringLiteral("RCC")) { - if (state != InRCC) - return; - state = InitialState; - continue; - } - return; - - case QXmlStreamReader::Characters: { - if (reader.isWhitespace()) - break; - if (state != InFile) - return; - currentFileName = reader.text().toString(); - if (currentFileName.isEmpty()) - continue; - - const QString fsPath = QDir::cleanPath(qrcDir.absoluteFilePath(currentFileName)); - - if (currentFileAttributes.hasAttribute(QStringLiteral("alias"))) - currentFileName = currentFileAttributes.value(QStringLiteral("alias")).toString(); - - currentFileName = QDir::cleanPath(currentFileName); - while (currentFileName.startsWith(QLatin1String("../"))) - currentFileName.remove(0, 3); - - const QString qrcPath = prefix + currentFileName; - if (QFile::exists(fsPath)) - qrcPathToFileSystemPath.insert(qrcPath, fsPath); - continue; - } - - default: break; - } - } -} diff --git a/tools/qmlcachegen/resourcefilemapper.h b/tools/qmlcachegen/resourcefilemapper.h deleted file mode 100644 index 2e0ab45171..0000000000 --- a/tools/qmlcachegen/resourcefilemapper.h +++ /dev/null @@ -1,50 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtQml module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ -#ifndef RESOURCEFILEMAPPER_H -#define RESOURCEFILEMAPPER_H - -#include -#include -#include - -struct ResourceFileMapper -{ - ResourceFileMapper(const QStringList &resourceFiles); - - bool isEmpty() const; - - QStringList resourcePaths(const QString &fileName); - QStringList qmlCompilerFiles() const; - -private: - void populateFromQrcFile(QFile &file); - - QHash qrcPathToFileSystemPath; -}; - -#endif // RESOURCEFILEMAPPER_H diff --git a/tools/qmlimportscanner/main.cpp b/tools/qmlimportscanner/main.cpp index 6d48f6203d..c37910bdaf 100644 --- a/tools/qmlimportscanner/main.cpp +++ b/tools/qmlimportscanner/main.cpp @@ -49,6 +49,8 @@ #include #include +#include + #include #include @@ -80,7 +82,8 @@ void printUsage(const QString &appNameIn) #endif std::wcerr << "Usage: " << appName << " -rootPath path/to/app/qml/directory -importPath path/to/qt/qml/directory\n" - " " << appName << " -qmlFiles file1 file2 -importPath path/to/qt/qml/directory\n\n" + " " << appName << " -qmlFiles file1 file2 -importPath path/to/qt/qml/directory\n" + " " << appName << " -qrcFiles file1.qrc file2.qrc -importPath path/to/qt/qml/directory\n\n" "Example: " << appName << " -rootPath . -importPath " << QDir::toNativeSeparators(qmlPath).toStdWString() << '\n'; @@ -542,6 +545,7 @@ int main(int argc, char *argv[]) QStringList qmlRootPaths; QStringList scanFiles; QStringList qmlImportPaths; + QStringList qrcFiles; bool generateCmakeContent = false; int i = 1; @@ -569,6 +573,8 @@ int main(int argc, char *argv[]) argReceiver = &qmlImportPaths; } else if (arg == QLatin1String("-cmake-output")) { generateCmakeContent = true; + } else if (arg == QLatin1String("-qrcFiles")) { + argReceiver = &qrcFiles; } else { std::cerr << qPrintable(appName) << ": Invalid argument: \"" << qPrintable(arg) << "\"\n"; @@ -590,6 +596,9 @@ int main(int argc, char *argv[]) } } + if (!qrcFiles.isEmpty()) + scanFiles << ResourceFileMapper(qrcFiles).qmlCompilerFiles(ResourceFileMapper::FileOutput::AbsoluteFilePath); + g_qmlImportPaths = qmlImportPaths; // Find the imports! diff --git a/tools/qmlimportscanner/qmlimportscanner.pro b/tools/qmlimportscanner/qmlimportscanner.pro index a29b582274..d69f1a3b0b 100644 --- a/tools/qmlimportscanner/qmlimportscanner.pro +++ b/tools/qmlimportscanner/qmlimportscanner.pro @@ -4,6 +4,7 @@ QT = core qmldevtools-private DEFINES += QT_NO_CAST_TO_ASCII QT_NO_CAST_FROM_ASCII SOURCES += main.cpp +include(../shared/shared.pri) load(cmake_functions) diff --git a/tools/shared/resourcefilemapper.cpp b/tools/shared/resourcefilemapper.cpp new file mode 100644 index 0000000000..b9cf463575 --- /dev/null +++ b/tools/shared/resourcefilemapper.cpp @@ -0,0 +1,170 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtQml module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "resourcefilemapper.h" + +#include +#include +#include + +ResourceFileMapper::ResourceFileMapper(const QStringList &resourceFiles) +{ + for (const QString &fileName: resourceFiles) { + QFile f(fileName); + if (!f.open(QIODevice::ReadOnly)) + continue; + populateFromQrcFile(f); + } +} + +bool ResourceFileMapper::isEmpty() const +{ + return qrcPathToFileSystemPath.isEmpty(); +} + +QStringList ResourceFileMapper::resourcePaths(const QString &fileName) +{ + const QString absPath = QDir::cleanPath(QDir::current().absoluteFilePath(fileName)); + QStringList resourcePaths; + for (auto it = qrcPathToFileSystemPath.cbegin(), end = qrcPathToFileSystemPath.cend(); it != end; ++it) { + if (QFileInfo(it.value()) == QFileInfo(absPath)) + resourcePaths.append(it.key()); + } + return resourcePaths; +} + +QStringList ResourceFileMapper::qmlCompilerFiles(FileOutput fo) const +{ + QStringList files; + for (auto it = qrcPathToFileSystemPath.constBegin(), end = qrcPathToFileSystemPath.constEnd(); + it != end; ++it) { + const QString &qrcPath = it.key(); + const QString suffix = QFileInfo(qrcPath).suffix(); + if (suffix != QStringLiteral("qml") && suffix != QStringLiteral("js") && suffix != QStringLiteral("mjs")) + continue; + if (fo == FileOutput::AbsoluteFilePath) + files << it.value(); + else + files << qrcPath; + } + return files; +} + +void ResourceFileMapper::populateFromQrcFile(QFile &file) +{ + enum State { + InitialState, + InRCC, + InResource, + InFile + }; + State state = InitialState; + + QDir qrcDir = QFileInfo(file).absoluteDir(); + + QString prefix; + QString currentFileName; + QXmlStreamAttributes currentFileAttributes; + + QXmlStreamReader reader(&file); + while (!reader.atEnd()) { + switch (reader.readNext()) { + case QXmlStreamReader::StartElement: + if (reader.name() == QStringLiteral("RCC")) { + if (state != InitialState) + return; + state = InRCC; + continue; + } else if (reader.name() == QStringLiteral("qresource")) { + if (state != InRCC) + return; + state = InResource; + QXmlStreamAttributes attributes = reader.attributes(); + if (attributes.hasAttribute(QStringLiteral("prefix"))) + prefix = attributes.value(QStringLiteral("prefix")).toString(); + if (!prefix.startsWith(QLatin1Char('/'))) + prefix.prepend(QLatin1Char('/')); + if (!prefix.endsWith(QLatin1Char('/'))) + prefix.append(QLatin1Char('/')); + continue; + } else if (reader.name() == QStringLiteral("file")) { + if (state != InResource) + return; + state = InFile; + currentFileAttributes = reader.attributes(); + continue; + } + return; + + case QXmlStreamReader::EndElement: + if (reader.name() == QStringLiteral("file")) { + if (state != InFile) + return; + state = InResource; + continue; + } else if (reader.name() == QStringLiteral("qresource")) { + if (state != InResource) + return; + state = InRCC; + continue; + } else if (reader.name() == QStringLiteral("RCC")) { + if (state != InRCC) + return; + state = InitialState; + continue; + } + return; + + case QXmlStreamReader::Characters: { + if (reader.isWhitespace()) + break; + if (state != InFile) + return; + currentFileName = reader.text().toString(); + if (currentFileName.isEmpty()) + continue; + + const QString fsPath = QDir::cleanPath(qrcDir.absoluteFilePath(currentFileName)); + + if (currentFileAttributes.hasAttribute(QStringLiteral("alias"))) + currentFileName = currentFileAttributes.value(QStringLiteral("alias")).toString(); + + currentFileName = QDir::cleanPath(currentFileName); + while (currentFileName.startsWith(QLatin1String("../"))) + currentFileName.remove(0, 3); + + const QString qrcPath = prefix + currentFileName; + if (QFile::exists(fsPath)) + qrcPathToFileSystemPath.insert(qrcPath, fsPath); + continue; + } + + default: break; + } + } +} diff --git a/tools/shared/resourcefilemapper.h b/tools/shared/resourcefilemapper.h new file mode 100644 index 0000000000..ed3e486149 --- /dev/null +++ b/tools/shared/resourcefilemapper.h @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtQml module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef RESOURCEFILEMAPPER_H +#define RESOURCEFILEMAPPER_H + +#include +#include +#include + +struct ResourceFileMapper +{ + enum class FileOutput { + RelativeFilePath, + AbsoluteFilePath + }; + ResourceFileMapper(const QStringList &resourceFiles); + + bool isEmpty() const; + + QStringList resourcePaths(const QString &fileName); + QStringList qmlCompilerFiles(FileOutput fo = FileOutput::RelativeFilePath) const; + +private: + void populateFromQrcFile(QFile &file); + + QHash qrcPathToFileSystemPath; +}; + +#endif // RESOURCEFILEMAPPER_H diff --git a/tools/shared/shared.pri b/tools/shared/shared.pri new file mode 100644 index 0000000000..c094b51d5f --- /dev/null +++ b/tools/shared/shared.pri @@ -0,0 +1,3 @@ +INCLUDEPATH += $$PWD +SOURCES += $$PWD/resourcefilemapper.cpp +HEADERS += $$PWD/resourcefilemapper.h -- cgit v1.2.3