2013-08-23 13:03:20 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2016-01-19 11:23:05 +00:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2013-08-23 13:03:20 +00:00
|
|
|
**
|
|
|
|
** This file is part of the tools applications of the Qt Toolkit.
|
|
|
|
**
|
2016-01-19 11:23:05 +00:00
|
|
|
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
|
2013-08-23 13:03:20 +00:00
|
|
|
** 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
|
2015-01-28 11:55:39 +00:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
2016-01-19 11:23:05 +00:00
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2013-08-23 13:03:20 +00:00
|
|
|
**
|
2016-01-19 11:23:05 +00:00
|
|
|
** 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.
|
2013-08-23 13:03:20 +00:00
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <private/qqmljslexer_p.h>
|
|
|
|
#include <private/qqmljsparser_p.h>
|
|
|
|
#include <private/qqmljsast_p.h>
|
|
|
|
#include <private/qv4codegen_p.h>
|
2015-02-14 21:46:41 +00:00
|
|
|
#include <private/qv4value_p.h>
|
2014-04-09 13:39:37 +00:00
|
|
|
#include <private/qqmlirbuilder_p.h>
|
2013-08-23 13:03:20 +00:00
|
|
|
|
|
|
|
#include <QtCore/QCoreApplication>
|
|
|
|
#include <QtCore/QDir>
|
2013-10-18 09:11:28 +00:00
|
|
|
#include <QtCore/QDirIterator>
|
2013-08-23 13:03:20 +00:00
|
|
|
#include <QtCore/QFile>
|
|
|
|
#include <QtCore/QFileInfo>
|
|
|
|
#include <QtCore/QSet>
|
|
|
|
#include <QtCore/QStringList>
|
|
|
|
#include <QtCore/QMetaObject>
|
|
|
|
#include <QtCore/QMetaProperty>
|
|
|
|
#include <QtCore/QVariant>
|
|
|
|
#include <QtCore/QJsonObject>
|
|
|
|
#include <QtCore/QJsonArray>
|
|
|
|
#include <QtCore/QJsonDocument>
|
|
|
|
#include <QtCore/QLibraryInfo>
|
|
|
|
|
|
|
|
#include <iostream>
|
2015-09-16 12:02:00 +00:00
|
|
|
#include <algorithm>
|
2013-08-23 13:03:20 +00:00
|
|
|
|
|
|
|
QT_USE_NAMESPACE
|
|
|
|
|
|
|
|
QStringList g_qmlImportPaths;
|
|
|
|
|
2015-11-13 07:30:21 +00:00
|
|
|
static void printUsage(const QString &appNameIn)
|
2013-08-23 13:03:20 +00:00
|
|
|
{
|
2015-11-13 07:30:21 +00:00
|
|
|
const std::wstring appName = appNameIn.toStdWString();
|
|
|
|
#ifndef QT_BOOTSTRAPPED
|
|
|
|
const QString qmlPath = QLibraryInfo::location(QLibraryInfo::Qml2ImportsPath);
|
|
|
|
#else
|
|
|
|
const QString qmlPath = QStringLiteral("/home/user/dev/qt-install/qml");
|
|
|
|
#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"
|
|
|
|
"Example: " << appName << " -rootPath . -importPath "
|
|
|
|
<< QDir::toNativeSeparators(qmlPath).toStdWString()
|
|
|
|
<< '\n';
|
2013-08-23 13:03:20 +00:00
|
|
|
}
|
|
|
|
|
2013-10-18 09:11:28 +00:00
|
|
|
QVariantList findImportsInAst(QQmlJS::AST::UiHeaderItemList *headerItemList, const QString &code, const QString &path)
|
2013-09-25 14:26:42 +00:00
|
|
|
{
|
2013-08-23 13:03:20 +00:00
|
|
|
QVariantList imports;
|
|
|
|
|
|
|
|
// extract uri and version from the imports (which look like "import Foo.Bar 1.2.3")
|
|
|
|
for (QQmlJS::AST::UiHeaderItemList *headerItemIt = headerItemList; headerItemIt; headerItemIt = headerItemIt->next) {
|
|
|
|
QVariantMap import;
|
|
|
|
QQmlJS::AST::UiImport *importNode = QQmlJS::AST::cast<QQmlJS::AST::UiImport *>(headerItemIt->headerItem);
|
|
|
|
if (!importNode)
|
|
|
|
continue;
|
|
|
|
// handle directory imports
|
|
|
|
if (!importNode->fileName.isEmpty()) {
|
|
|
|
QString name = importNode->fileName.toString();
|
2013-09-25 14:13:27 +00:00
|
|
|
import[QStringLiteral("name")] = name;
|
2016-01-18 15:33:12 +00:00
|
|
|
if (name.endsWith(QLatin1String(".js"))) {
|
2013-10-18 09:11:28 +00:00
|
|
|
import[QStringLiteral("type")] = QStringLiteral("javascript");
|
|
|
|
} else {
|
|
|
|
import[QStringLiteral("type")] = QStringLiteral("directory");
|
|
|
|
}
|
|
|
|
|
|
|
|
import[QStringLiteral("path")] = QDir::cleanPath(path + QLatin1Char('/') + name);
|
2013-08-23 13:03:20 +00:00
|
|
|
} else {
|
|
|
|
// Walk the id chain ("Foo" -> "Bar" -> etc)
|
|
|
|
QString name;
|
|
|
|
QQmlJS::AST::UiQualifiedId *uri = importNode->importUri;
|
|
|
|
while (uri) {
|
|
|
|
name.append(uri->name);
|
2013-09-25 14:13:27 +00:00
|
|
|
name.append(QLatin1Char('.'));
|
2013-08-23 13:03:20 +00:00
|
|
|
uri = uri->next;
|
|
|
|
}
|
|
|
|
name.chop(1); // remove trailing "."
|
|
|
|
if (!name.isEmpty())
|
2013-09-25 14:13:27 +00:00
|
|
|
import[QStringLiteral("name")] = name;
|
|
|
|
import[QStringLiteral("type")] = QStringLiteral("module");
|
|
|
|
import[QStringLiteral("version")] = code.mid(importNode->versionToken.offset, importNode->versionToken.length);
|
2013-08-23 13:03:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
imports.append(import);
|
|
|
|
}
|
|
|
|
|
|
|
|
return imports;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the qmldir file, extract a list of plugins by
|
2013-09-26 09:24:30 +00:00
|
|
|
// parsing the "plugin" and "classname" lines.
|
|
|
|
QVariantMap pluginsForModulePath(const QString &modulePath) {
|
2013-09-25 14:13:27 +00:00
|
|
|
QFile qmldirFile(modulePath + QStringLiteral("/qmldir"));
|
2013-09-25 14:26:42 +00:00
|
|
|
if (!qmldirFile.exists())
|
2013-09-26 09:24:30 +00:00
|
|
|
return QVariantMap();
|
|
|
|
|
2013-08-23 13:03:20 +00:00
|
|
|
qmldirFile.open(QIODevice::ReadOnly | QIODevice::Text);
|
|
|
|
|
2013-09-26 09:24:30 +00:00
|
|
|
// a qml import may contain several plugins
|
2013-08-23 13:03:20 +00:00
|
|
|
QString plugins;
|
2013-09-26 09:24:30 +00:00
|
|
|
QString classnames;
|
2014-09-25 16:28:27 +00:00
|
|
|
QStringList dependencies;
|
2013-08-23 13:03:20 +00:00
|
|
|
QByteArray line;
|
|
|
|
do {
|
|
|
|
line = qmldirFile.readLine();
|
|
|
|
if (line.startsWith("plugin")) {
|
|
|
|
plugins += QString::fromUtf8(line.split(' ').at(1));
|
2015-10-13 10:26:45 +00:00
|
|
|
plugins += QLatin1Char(' ');
|
2013-09-26 09:24:30 +00:00
|
|
|
} else if (line.startsWith("classname")) {
|
|
|
|
classnames += QString::fromUtf8(line.split(' ').at(1));
|
2015-10-13 10:26:45 +00:00
|
|
|
classnames += QLatin1Char(' ');
|
2014-09-25 16:28:27 +00:00
|
|
|
} else if (line.startsWith("depends")) {
|
|
|
|
QList<QByteArray> dep = line.split(' ');
|
|
|
|
if (dep.length() != 3)
|
|
|
|
std::cerr << "depends: expected 2 arguments: module identifier and version" << std::endl;
|
|
|
|
else
|
|
|
|
dependencies << QString::fromUtf8(dep[1]) + QStringLiteral(" ") + QString::fromUtf8(dep[2]).simplified();
|
2013-08-23 13:03:20 +00:00
|
|
|
}
|
2013-09-26 09:24:30 +00:00
|
|
|
|
2013-08-23 13:03:20 +00:00
|
|
|
} while (line.length() > 0);
|
|
|
|
|
2013-09-26 09:24:30 +00:00
|
|
|
QVariantMap pluginInfo;
|
|
|
|
pluginInfo[QStringLiteral("plugins")] = plugins.simplified();
|
|
|
|
pluginInfo[QStringLiteral("classnames")] = classnames.simplified();
|
2014-09-25 16:28:27 +00:00
|
|
|
if (dependencies.length())
|
|
|
|
pluginInfo[QStringLiteral("dependencies")] = dependencies;
|
2013-09-26 09:24:30 +00:00
|
|
|
return pluginInfo;
|
2013-08-23 13:03:20 +00:00
|
|
|
}
|
|
|
|
|
2013-10-04 10:29:03 +00:00
|
|
|
// Search for a given qml import in g_qmlImportPaths.
|
|
|
|
QString resolveImportPath(const QString &uri, const QString &version)
|
2013-09-25 14:26:42 +00:00
|
|
|
{
|
2016-04-25 14:45:21 +00:00
|
|
|
const QLatin1Char dot('.');
|
|
|
|
const QLatin1Char slash('/');
|
|
|
|
const QStringList parts = uri.split(dot, QString::SkipEmptyParts);
|
|
|
|
|
|
|
|
QString ver = version;
|
2013-10-04 10:29:03 +00:00
|
|
|
while (true) {
|
|
|
|
foreach (const QString &qmlImportPath, g_qmlImportPaths) {
|
2016-04-25 14:45:21 +00:00
|
|
|
// Search for the most specific version first, and search
|
|
|
|
// also for the version in parent modules. For example:
|
|
|
|
// - qml/QtQml/Models.2.0
|
|
|
|
// - qml/QtQml.2.0/Models
|
|
|
|
// - qml/QtQml/Models.2
|
|
|
|
// - qml/QtQml.2/Models
|
|
|
|
// - qml/QtQml/Models
|
|
|
|
if (ver.isEmpty()) {
|
|
|
|
const QString candidatePath = QDir::cleanPath(qmlImportPath + slash + parts.join(slash));
|
|
|
|
if (QDir(candidatePath).exists())
|
|
|
|
return candidatePath; // import found
|
|
|
|
} else {
|
|
|
|
for (int index = parts.count() - 1; index >= 0; --index) {
|
|
|
|
const QString candidatePath = QDir::cleanPath(qmlImportPath + slash
|
|
|
|
+ parts.mid(0, index + 1).join(slash)
|
|
|
|
+ dot + ver + slash
|
|
|
|
+ parts.mid(index + 1).join(slash));
|
|
|
|
|
|
|
|
if (QDir(candidatePath).exists())
|
|
|
|
return candidatePath; // import found
|
|
|
|
}
|
|
|
|
}
|
2013-10-04 10:29:03 +00:00
|
|
|
}
|
2013-08-23 13:03:20 +00:00
|
|
|
|
2013-10-04 10:29:03 +00:00
|
|
|
// remove the last version digit; stop if there are none left
|
2016-04-25 14:45:21 +00:00
|
|
|
if (ver.isEmpty())
|
2013-10-04 10:29:03 +00:00
|
|
|
break;
|
2016-04-25 14:45:21 +00:00
|
|
|
|
|
|
|
int lastDot = ver.lastIndexOf(dot);
|
|
|
|
if (lastDot == -1)
|
|
|
|
ver.clear();
|
|
|
|
else
|
|
|
|
ver = ver.mid(0, lastDot);
|
2013-08-23 13:03:20 +00:00
|
|
|
}
|
2013-10-04 10:29:03 +00:00
|
|
|
|
|
|
|
return QString(); // not found
|
2013-08-23 13:03:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find absolute file system paths and plugins for a list of modules.
|
2013-09-25 14:26:42 +00:00
|
|
|
QVariantList findPathsForModuleImports(const QVariantList &imports)
|
|
|
|
{
|
2013-08-23 13:03:20 +00:00
|
|
|
QVariantList done;
|
2014-09-25 16:28:27 +00:00
|
|
|
QVariantList importsCopy(imports);
|
2013-08-23 13:03:20 +00:00
|
|
|
|
2014-09-25 16:28:27 +00:00
|
|
|
for (int i = 0; i < importsCopy.length(); ++i) {
|
|
|
|
QVariantMap import = qvariant_cast<QVariantMap>(importsCopy[i]);
|
2016-01-18 15:33:12 +00:00
|
|
|
if (import[QStringLiteral("type")] == QLatin1String("module")) {
|
2013-10-18 09:11:28 +00:00
|
|
|
QString path = resolveImportPath(import.value(QStringLiteral("name")).toString(), import.value(QStringLiteral("version")).toString());
|
|
|
|
if (!path.isEmpty())
|
|
|
|
import[QStringLiteral("path")] = path;
|
|
|
|
QVariantMap plugininfo = pluginsForModulePath(import.value(QStringLiteral("path")).toString());
|
|
|
|
QString plugins = plugininfo.value(QStringLiteral("plugins")).toString();
|
|
|
|
QString classnames = plugininfo.value(QStringLiteral("classnames")).toString();
|
|
|
|
if (!plugins.isEmpty())
|
2013-09-26 09:24:30 +00:00
|
|
|
import[QStringLiteral("plugin")] = plugins;
|
2013-10-18 09:11:28 +00:00
|
|
|
if (!classnames.isEmpty())
|
2013-09-26 09:24:30 +00:00
|
|
|
import[QStringLiteral("classname")] = classnames;
|
2014-09-25 16:28:27 +00:00
|
|
|
if (plugininfo.contains(QStringLiteral("dependencies"))) {
|
|
|
|
QStringList dependencies = plugininfo.value(QStringLiteral("dependencies")).toStringList();
|
2015-07-04 09:15:15 +00:00
|
|
|
foreach (const QString &line, dependencies) {
|
2016-06-08 12:12:08 +00:00
|
|
|
const auto dep = line.splitRef(QLatin1Char(' '));
|
2014-09-25 16:28:27 +00:00
|
|
|
QVariantMap depImport;
|
|
|
|
depImport[QStringLiteral("type")] = QStringLiteral("module");
|
2016-06-08 12:12:08 +00:00
|
|
|
depImport[QStringLiteral("name")] = dep[0].toString();
|
|
|
|
depImport[QStringLiteral("version")] = dep[1].toString();
|
2014-09-25 16:28:27 +00:00
|
|
|
importsCopy.append(depImport);
|
|
|
|
}
|
|
|
|
}
|
2013-08-23 13:03:20 +00:00
|
|
|
}
|
2013-10-18 09:11:28 +00:00
|
|
|
done.append(import);
|
2013-08-23 13:03:20 +00:00
|
|
|
}
|
|
|
|
return done;
|
|
|
|
}
|
|
|
|
|
2013-10-18 09:11:28 +00:00
|
|
|
// Scan a single qml file for import statements
|
2014-09-18 20:40:09 +00:00
|
|
|
static QVariantList findQmlImportsInQmlCode(const QString &filePath, const QString &code)
|
2013-10-18 09:11:28 +00:00
|
|
|
{
|
|
|
|
QQmlJS::Engine engine;
|
|
|
|
QQmlJS::Lexer lexer(&engine);
|
|
|
|
lexer.setCode(code, /*line = */ 1);
|
|
|
|
QQmlJS::Parser parser(&engine);
|
|
|
|
|
|
|
|
if (!parser.parse() || !parser.diagnosticMessages().isEmpty()) {
|
|
|
|
// Extract errors from the parser
|
|
|
|
foreach (const QQmlJS::DiagnosticMessage &m, parser.diagnosticMessages()) {
|
2014-09-18 20:40:09 +00:00
|
|
|
std::cerr << QDir::toNativeSeparators(filePath).toStdString() << ':'
|
2013-10-18 09:11:28 +00:00
|
|
|
<< m.loc.startLine << ':' << m.message.toStdString() << std::endl;
|
|
|
|
}
|
|
|
|
return QVariantList();
|
|
|
|
}
|
2014-09-18 20:40:09 +00:00
|
|
|
return findImportsInAst(parser.ast()->headers, code, filePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan a single qml file for import statements
|
|
|
|
static QVariantList findQmlImportsInQmlFile(const QString &filePath)
|
|
|
|
{
|
|
|
|
QFile file(filePath);
|
|
|
|
if (!file.open(QIODevice::ReadOnly)) {
|
|
|
|
std::cerr << "Cannot open input file " << QDir::toNativeSeparators(file.fileName()).toStdString()
|
|
|
|
<< ':' << file.errorString().toStdString() << std::endl;
|
|
|
|
return QVariantList();
|
|
|
|
}
|
|
|
|
QString code = QString::fromUtf8(file.readAll());
|
|
|
|
return findQmlImportsInQmlCode(filePath, code);
|
2014-02-11 14:19:33 +00:00
|
|
|
}
|
|
|
|
|
2014-12-05 15:32:56 +00:00
|
|
|
struct ImportCollector : public QQmlJS::Directives
|
|
|
|
{
|
|
|
|
QVariantList imports;
|
|
|
|
|
|
|
|
virtual void importFile(const QString &jsfile, const QString &module, int line, int column)
|
|
|
|
{
|
|
|
|
QVariantMap entry;
|
|
|
|
entry[QLatin1String("type")] = QStringLiteral("javascript");
|
|
|
|
entry[QLatin1String("path")] = jsfile;
|
|
|
|
imports << entry;
|
|
|
|
|
|
|
|
Q_UNUSED(module);
|
|
|
|
Q_UNUSED(line);
|
|
|
|
Q_UNUSED(column);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void importModule(const QString &uri, const QString &version, const QString &module, int line, int column)
|
|
|
|
{
|
|
|
|
QVariantMap entry;
|
|
|
|
if (uri.contains(QLatin1Char('/'))) {
|
|
|
|
entry[QLatin1String("type")] = QStringLiteral("directory");
|
|
|
|
entry[QLatin1String("name")] = uri;
|
|
|
|
} else {
|
|
|
|
entry[QLatin1String("type")] = QStringLiteral("module");
|
|
|
|
entry[QLatin1String("name")] = uri;
|
|
|
|
entry[QLatin1String("version")] = version;
|
|
|
|
}
|
|
|
|
imports << entry;
|
|
|
|
|
|
|
|
Q_UNUSED(module);
|
|
|
|
Q_UNUSED(line);
|
|
|
|
Q_UNUSED(column);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-11 14:19:33 +00:00
|
|
|
// Scan a single javascrupt file for import statements
|
|
|
|
QVariantList findQmlImportsInJavascriptFile(const QString &filePath)
|
|
|
|
{
|
|
|
|
QFile file(filePath);
|
|
|
|
if (!file.open(QIODevice::ReadOnly)) {
|
|
|
|
std::cerr << "Cannot open input file " << QDir::toNativeSeparators(file.fileName()).toStdString()
|
|
|
|
<< ':' << file.errorString().toStdString() << std::endl;
|
|
|
|
return QVariantList();
|
|
|
|
}
|
|
|
|
|
2014-04-09 13:39:37 +00:00
|
|
|
QString sourceCode = QString::fromUtf8(file.readAll());
|
|
|
|
file.close();
|
|
|
|
|
2014-12-05 15:32:56 +00:00
|
|
|
QQmlJS::Engine ee;
|
|
|
|
ImportCollector collector;
|
|
|
|
ee.setDirectives(&collector);
|
|
|
|
QQmlJS::Lexer lexer(&ee);
|
|
|
|
lexer.setCode(sourceCode, /*line*/1, /*qml mode*/false);
|
|
|
|
QQmlJS::Parser parser(&ee);
|
|
|
|
parser.parseProgram();
|
2014-02-11 14:19:33 +00:00
|
|
|
|
2014-12-05 15:32:56 +00:00
|
|
|
foreach (const QQmlJS::DiagnosticMessage &m, parser.diagnosticMessages())
|
|
|
|
if (m.isError())
|
|
|
|
return QVariantList();
|
|
|
|
|
|
|
|
return collector.imports;
|
2014-02-11 14:19:33 +00:00
|
|
|
}
|
2013-10-18 09:11:28 +00:00
|
|
|
|
2014-02-11 14:19:33 +00:00
|
|
|
// Scan a single qml or js file for import statements
|
|
|
|
QVariantList findQmlImportsInFile(const QString &filePath)
|
|
|
|
{
|
|
|
|
QVariantList imports;
|
2014-09-18 20:40:09 +00:00
|
|
|
if (filePath == QLatin1String("-")) {
|
|
|
|
QFile f;
|
|
|
|
if (f.open(stdin, QIODevice::ReadOnly))
|
|
|
|
imports = findQmlImportsInQmlCode(QLatin1String("<stdin>"), QString::fromUtf8(f.readAll()));
|
2016-01-18 15:33:12 +00:00
|
|
|
} else if (filePath.endsWith(QLatin1String(".qml"))) {
|
2014-09-18 20:40:09 +00:00
|
|
|
imports = findQmlImportsInQmlFile(filePath);
|
2016-01-18 15:33:12 +00:00
|
|
|
} else if (filePath.endsWith(QLatin1String(".js"))) {
|
2014-02-11 14:19:33 +00:00
|
|
|
imports = findQmlImportsInJavascriptFile(filePath);
|
2014-09-18 20:40:09 +00:00
|
|
|
}
|
2014-02-11 14:19:33 +00:00
|
|
|
|
|
|
|
return findPathsForModuleImports(imports);
|
2013-10-18 09:11:28 +00:00
|
|
|
}
|
|
|
|
|
2013-08-23 13:03:20 +00:00
|
|
|
// Merge two lists of imports, discard duplicates.
|
2013-09-25 14:26:42 +00:00
|
|
|
QVariantList mergeImports(const QVariantList &a, const QVariantList &b)
|
|
|
|
{
|
2013-08-23 13:03:20 +00:00
|
|
|
QVariantList merged = a;
|
|
|
|
foreach (const QVariant &variant, b) {
|
|
|
|
if (!merged.contains(variant))
|
|
|
|
merged.append(variant);
|
|
|
|
}
|
|
|
|
return merged;
|
|
|
|
}
|
|
|
|
|
2016-01-27 08:18:02 +00:00
|
|
|
// Predicates needed by findQmlImportsInDirectory.
|
|
|
|
|
|
|
|
struct isMetainfo {
|
|
|
|
bool operator() (const QFileInfo &x) const {
|
|
|
|
return x.suffix() == QLatin1String("metainfo");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct pathStartsWith {
|
|
|
|
pathStartsWith(const QString &path) : _path(path) {}
|
|
|
|
bool operator() (const QString &x) const {
|
|
|
|
return _path.startsWith(x);
|
|
|
|
}
|
|
|
|
const QString _path;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-18 09:11:28 +00:00
|
|
|
// Scan all qml files in directory for import statements
|
|
|
|
QVariantList findQmlImportsInDirectory(const QString &qmlDir)
|
2013-09-25 14:26:42 +00:00
|
|
|
{
|
2013-08-23 13:03:20 +00:00
|
|
|
QVariantList ret;
|
2013-10-18 09:11:28 +00:00
|
|
|
if (qmlDir.isEmpty())
|
|
|
|
return ret;
|
|
|
|
|
2015-09-16 12:02:00 +00:00
|
|
|
QDirIterator iterator(qmlDir, QDir::AllDirs | QDir::NoDotDot, QDirIterator::Subdirectories);
|
|
|
|
QStringList blacklist;
|
|
|
|
|
2013-10-18 09:11:28 +00:00
|
|
|
while (iterator.hasNext()) {
|
|
|
|
iterator.next();
|
2015-09-16 12:02:00 +00:00
|
|
|
const QString path = iterator.filePath();
|
|
|
|
const QFileInfoList entries = QDir(path).entryInfoList();
|
|
|
|
|
|
|
|
// Skip designer related stuff
|
|
|
|
if (std::find_if(entries.cbegin(), entries.cend(), isMetainfo()) != entries.cend()) {
|
|
|
|
blacklist << path;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (std::find_if(blacklist.cbegin(), blacklist.cend(), pathStartsWith(path)) != blacklist.cend())
|
|
|
|
continue;
|
2013-10-18 09:11:28 +00:00
|
|
|
|
|
|
|
// skip obvious build output directories
|
2016-01-18 15:33:12 +00:00
|
|
|
if (path.contains(QLatin1String("Debug-iphoneos")) || path.contains(QLatin1String("Release-iphoneos")) ||
|
|
|
|
path.contains(QLatin1String("Debug-iphonesimulator")) || path.contains(QLatin1String("Release-iphonesimulator"))
|
2013-10-18 09:11:28 +00:00
|
|
|
#ifdef Q_OS_WIN
|
2016-01-18 15:33:12 +00:00
|
|
|
|| path.contains(QLatin1String("/release/")) || path.contains(QLatin1String("/debug/"))
|
2013-10-18 09:11:28 +00:00
|
|
|
#endif
|
|
|
|
){
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-09-16 12:02:00 +00:00
|
|
|
foreach (const QFileInfo &x, entries)
|
|
|
|
if (x.isFile())
|
|
|
|
ret = mergeImports(ret, findQmlImportsInFile(x.absoluteFilePath()));
|
2013-10-18 09:11:28 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSet<QString> importModulePaths(QVariantList imports) {
|
|
|
|
QSet<QString> ret;
|
|
|
|
foreach (const QVariant &importVariant, imports) {
|
|
|
|
QVariantMap import = qvariant_cast<QVariantMap>(importVariant);
|
|
|
|
QString path = import.value(QStringLiteral("path")).toString();
|
|
|
|
QString type = import.value(QStringLiteral("type")).toString();
|
2016-01-18 15:33:12 +00:00
|
|
|
if (type == QLatin1String("module") && !path.isEmpty())
|
2013-10-18 09:11:28 +00:00
|
|
|
ret.insert(QDir(path).canonicalPath());
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2013-08-23 13:03:20 +00:00
|
|
|
|
2013-10-18 09:11:28 +00:00
|
|
|
// Find Qml Imports Recursively from a root set of qml files.
|
|
|
|
// The directories in qmlDirs are searched recursively.
|
|
|
|
// The files in qmlFiles parsed directly.
|
2014-02-11 14:19:33 +00:00
|
|
|
QVariantList findQmlImportsRecursively(const QStringList &qmlDirs, const QStringList &scanFiles)
|
2013-10-18 09:11:28 +00:00
|
|
|
{
|
|
|
|
QVariantList ret;
|
|
|
|
|
|
|
|
// scan all app root qml directories for imports
|
|
|
|
foreach (const QString &qmlDir, qmlDirs) {
|
|
|
|
QVariantList imports = findQmlImportsInDirectory(qmlDir);
|
|
|
|
ret = mergeImports(ret, imports);
|
|
|
|
}
|
|
|
|
|
|
|
|
// scan app qml files for imports
|
2014-02-11 14:19:33 +00:00
|
|
|
foreach (const QString &file, scanFiles) {
|
|
|
|
QVariantList imports = findQmlImportsInFile(file);
|
2013-10-18 09:11:28 +00:00
|
|
|
ret = mergeImports(ret, imports);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// get the paths to theimports found in the app qml
|
|
|
|
QSet<QString> toVisit = importModulePaths(ret);
|
|
|
|
|
|
|
|
// recursivly scan for import dependencies.
|
2013-08-23 13:03:20 +00:00
|
|
|
QSet<QString> visited;
|
|
|
|
while (!toVisit.isEmpty()) {
|
|
|
|
QString qmlDir = *toVisit.begin();
|
|
|
|
toVisit.erase(toVisit.begin());
|
|
|
|
visited.insert(qmlDir);
|
|
|
|
|
|
|
|
QVariantList imports = findQmlImportsInDirectory(qmlDir);
|
|
|
|
ret = mergeImports(ret, imports);
|
|
|
|
|
2013-10-18 09:11:28 +00:00
|
|
|
QSet<QString> candidatePaths = importModulePaths(ret);
|
|
|
|
candidatePaths.subtract(visited);
|
|
|
|
toVisit.unite(candidatePaths);
|
|
|
|
}
|
2013-08-23 13:03:20 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
QCoreApplication app(argc, argv);
|
|
|
|
QStringList args = app.arguments();
|
|
|
|
const QString appName = QFileInfo(app.applicationFilePath()).baseName();
|
2013-09-25 13:55:12 +00:00
|
|
|
if (args.size() < 2) {
|
2013-08-23 13:03:20 +00:00
|
|
|
printUsage(appName);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList qmlRootPaths;
|
2014-02-11 14:19:33 +00:00
|
|
|
QStringList scanFiles;
|
2013-08-23 13:03:20 +00:00
|
|
|
QStringList qmlImportPaths;
|
|
|
|
|
|
|
|
int i = 1;
|
|
|
|
while (i < args.count()) {
|
|
|
|
const QString &arg = args.at(i);
|
|
|
|
++i;
|
2013-10-18 09:11:28 +00:00
|
|
|
QStringList *argReceiver = 0;
|
2014-09-18 20:40:09 +00:00
|
|
|
if (!arg.startsWith(QLatin1Char('-')) || arg == QLatin1String("-")) {
|
2013-08-23 13:03:20 +00:00
|
|
|
qmlRootPaths += arg;
|
|
|
|
} else if (arg == QLatin1String("-rootPath")) {
|
|
|
|
if (i >= args.count())
|
2013-09-25 14:21:38 +00:00
|
|
|
std::cerr << "-rootPath requires an argument\n";
|
2013-10-18 09:11:28 +00:00
|
|
|
argReceiver = &qmlRootPaths;
|
|
|
|
} else if (arg == QLatin1String("-qmlFiles")) {
|
|
|
|
if (i >= args.count())
|
|
|
|
std::cerr << "-qmlFiles requires an argument\n";
|
2014-02-11 14:19:33 +00:00
|
|
|
argReceiver = &scanFiles;
|
|
|
|
} else if (arg == QLatin1String("-jsFiles")) {
|
|
|
|
if (i >= args.count())
|
|
|
|
std::cerr << "-jsFiles requires an argument\n";
|
|
|
|
argReceiver = &scanFiles;
|
2013-08-23 13:03:20 +00:00
|
|
|
} else if (arg == QLatin1String("-importPath")) {
|
|
|
|
if (i >= args.count())
|
2013-09-25 14:21:38 +00:00
|
|
|
std::cerr << "-importPath requires an argument\n";
|
2013-10-18 09:11:28 +00:00
|
|
|
argReceiver = &qmlImportPaths;
|
2013-08-23 13:03:20 +00:00
|
|
|
} else {
|
2013-09-25 14:21:38 +00:00
|
|
|
std::cerr << "Invalid argument: \"" << qPrintable(arg) << "\"\n";
|
2013-08-23 13:03:20 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2013-10-18 09:11:28 +00:00
|
|
|
|
|
|
|
while (i < args.count()) {
|
|
|
|
const QString arg = args.at(i);
|
2014-09-18 20:40:09 +00:00
|
|
|
if (arg.startsWith(QLatin1Char('-')) && arg != QLatin1String("-"))
|
2013-10-18 09:11:28 +00:00
|
|
|
break;
|
|
|
|
++i;
|
|
|
|
*argReceiver += arg;
|
|
|
|
}
|
2013-08-23 13:03:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
g_qmlImportPaths = qmlImportPaths;
|
|
|
|
|
|
|
|
// Find the imports!
|
2014-02-11 14:19:33 +00:00
|
|
|
QVariantList imports = findQmlImportsRecursively(qmlRootPaths, scanFiles);
|
2013-08-23 13:03:20 +00:00
|
|
|
|
|
|
|
// Convert to JSON
|
|
|
|
QByteArray json = QJsonDocument(QJsonArray::fromVariantList(imports)).toJson();
|
|
|
|
std::cout << json.constData() << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|