2013-08-23 13:03:20 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
|
|
|
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
|
|
|
|
** Contact: http://www.qt-project.org/legal
|
|
|
|
**
|
|
|
|
** This file is part of the tools applications of the Qt Toolkit.
|
|
|
|
**
|
|
|
|
** $QT_BEGIN_LICENSE:LGPL$
|
|
|
|
** 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 Digia. For licensing terms and
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
|
|
|
**
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
|
|
** General Public License version 2.1 as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
|
|
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
|
|
**
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
**
|
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 3.0 as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.GPL included in the
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
** ensure the GNU General Public License version 3.0 requirements will be
|
|
|
|
** met: http://www.gnu.org/copyleft/gpl.html.
|
|
|
|
**
|
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <private/qqmljslexer_p.h>
|
|
|
|
#include <private/qqmljsparser_p.h>
|
|
|
|
#include <private/qqmljsast_p.h>
|
|
|
|
#include <private/qv4codegen_p.h>
|
|
|
|
#include <private/qqmlpool_p.h>
|
|
|
|
|
|
|
|
#include <QtCore/QCoreApplication>
|
|
|
|
#include <QtCore/QDir>
|
|
|
|
#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>
|
|
|
|
|
|
|
|
QT_USE_NAMESPACE
|
|
|
|
|
|
|
|
QStringList g_qmlImportPaths;
|
|
|
|
|
|
|
|
void printUsage(const QString &appName)
|
|
|
|
{
|
2013-09-25 14:21:38 +00:00
|
|
|
std::cerr << qPrintable(QString::fromLatin1(
|
2013-08-23 13:03:20 +00:00
|
|
|
"Usage: %1 -rootPath qmldir -importPath importPath \n"
|
|
|
|
"Example: %1 -rootPath qmldir -importPath importPath").arg(
|
|
|
|
appName));
|
|
|
|
}
|
|
|
|
|
2013-09-25 14:26:42 +00:00
|
|
|
QVariantList findImportsInAst(QQmlJS::AST::UiHeaderItemList *headerItemList, const QByteArray &code, const QString &path)
|
|
|
|
{
|
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;
|
|
|
|
import[QStringLiteral("type")] = QStringLiteral("directory");
|
|
|
|
import[QStringLiteral("path")] = 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan a single qml file for import statements
|
2013-09-25 14:26:42 +00:00
|
|
|
QVariantList findQmlImportsInFile(const QString &qmlFilePath)
|
|
|
|
{
|
2013-08-23 13:03:20 +00:00
|
|
|
QFile qmlFile(qmlFilePath);
|
|
|
|
if (!qmlFile.open(QIODevice::ReadOnly)) {
|
2013-09-25 14:21:38 +00:00
|
|
|
std::cerr << "Cannot open input file " << qPrintable(QDir::toNativeSeparators(qmlFile.fileName()))
|
|
|
|
<< ':' << qPrintable(qmlFile.errorString()) << std::endl;
|
2013-08-23 13:03:20 +00:00
|
|
|
return QVariantList();
|
|
|
|
}
|
|
|
|
QByteArray code = qmlFile.readAll();
|
|
|
|
|
|
|
|
QQmlJS::Engine engine;
|
|
|
|
QQmlJS::Lexer lexer(&engine);
|
2013-09-25 14:13:27 +00:00
|
|
|
lexer.setCode(QString::fromUtf8(code), /*line = */ 1);
|
2013-08-23 13:03:20 +00:00
|
|
|
QQmlJS::Parser parser(&engine);
|
|
|
|
|
|
|
|
if (!parser.parse() || !parser.diagnosticMessages().isEmpty()) {
|
|
|
|
// Extract errors from the parser
|
|
|
|
foreach (const QQmlJS::DiagnosticMessage &m, parser.diagnosticMessages()) {
|
2013-09-25 14:21:38 +00:00
|
|
|
std::cerr << qPrintable(QDir::toNativeSeparators(qmlFile.fileName())) << ':'
|
2013-09-25 14:13:27 +00:00
|
|
|
<< m.loc.startLine << ':' << qPrintable(m.message) << std::endl;
|
2013-08-23 13:03:20 +00:00
|
|
|
}
|
|
|
|
return QVariantList();
|
|
|
|
}
|
|
|
|
|
|
|
|
return findImportsInAst(parser.ast()->headers, code, QFileInfo(qmlFilePath).absolutePath());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan all qml files in directory for import statements
|
|
|
|
QVariantList findQmlImportsInDirectory(const QString &qmlDir)
|
|
|
|
{
|
|
|
|
QVariantList ret;
|
|
|
|
if (qmlDir.isEmpty())
|
|
|
|
return ret;
|
|
|
|
|
2013-09-25 14:13:27 +00:00
|
|
|
QStringList qmlFileNames = QDir(qmlDir).entryList(QStringList(QStringLiteral("*.qml")));
|
2013-08-23 13:03:20 +00:00
|
|
|
foreach (const QString &qmlFileName, qmlFileNames) {
|
2013-09-25 14:13:27 +00:00
|
|
|
QString qmlFilePath = qmlDir + QLatin1Char('/') + qmlFileName;
|
2013-08-23 13:03:20 +00:00
|
|
|
QVariantList imports = findQmlImportsInFile(qmlFilePath);
|
|
|
|
ret.append(imports);
|
|
|
|
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
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));
|
2013-09-26 09:24:30 +00:00
|
|
|
plugins += QStringLiteral(" ");
|
|
|
|
} else if (line.startsWith("classname")) {
|
|
|
|
classnames += QString::fromUtf8(line.split(' ').at(1));
|
|
|
|
classnames += QStringLiteral(" ");
|
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();
|
|
|
|
return pluginInfo;
|
2013-08-23 13:03:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Construct a file system path from a module uri and version.
|
|
|
|
// Special case for 1.x: QtQuick.Dialogs 1.x -> QtQuick/Dialogs
|
|
|
|
// Genral casefor y.x: QtQuick.Dialogs y.x -> QtQuick/Dialogs.y
|
2013-09-25 14:26:42 +00:00
|
|
|
QString localPathForModule(const QString &moduleUri, const QString &version)
|
|
|
|
{
|
2013-08-23 13:03:20 +00:00
|
|
|
QString path;
|
2013-09-25 14:13:27 +00:00
|
|
|
foreach (const QString &part, moduleUri.split(QLatin1Char('.')))
|
|
|
|
path += QLatin1Char('/') + part;
|
2013-08-23 13:03:20 +00:00
|
|
|
|
2013-09-25 14:13:27 +00:00
|
|
|
if (version.startsWith(QLatin1String("1.")))
|
2013-08-23 13:03:20 +00:00
|
|
|
return path;
|
|
|
|
|
2013-09-25 14:13:27 +00:00
|
|
|
if (version.contains(QLatin1Char('.')))
|
|
|
|
path += QLatin1Char('.') + version.split(QLatin1Char('.')).at(0);
|
2013-08-23 13:03:20 +00:00
|
|
|
else
|
2013-09-25 14:13:27 +00:00
|
|
|
path += QLatin1Char('.') + version;
|
2013-08-23 13:03:20 +00:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search for a given qml import in g_qmlImportPaths
|
2013-09-25 14:26:42 +00:00
|
|
|
QString findPathForImport(const QString&localModulePath)
|
|
|
|
{
|
2013-08-23 13:03:20 +00:00
|
|
|
foreach (const QString &qmlImportPath, g_qmlImportPaths) {
|
2013-09-25 14:13:27 +00:00
|
|
|
QString candidatePath = QDir::cleanPath(qmlImportPath + QLatin1Char('/') + localModulePath);
|
2013-08-23 13:03:20 +00:00
|
|
|
if (QDir(candidatePath).exists())
|
|
|
|
return candidatePath;
|
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
foreach (QVariant importVariant, imports) {
|
|
|
|
QVariantMap import = qvariant_cast<QVariantMap>(importVariant);
|
2013-09-25 14:13:27 +00:00
|
|
|
if (import[QStringLiteral("type")] == QStringLiteral("module")) {
|
2013-09-26 09:24:30 +00:00
|
|
|
import[QStringLiteral("path")] = findPathForImport(localPathForModule(import[QStringLiteral("name")].toString(), import[QStringLiteral("version")].toString()));
|
|
|
|
QVariantMap plugininfo = pluginsForModulePath(import[QStringLiteral("path")].toString());
|
|
|
|
QString plugins = plugininfo[QStringLiteral("plugins")].toString();
|
|
|
|
QString classnames = plugininfo[QStringLiteral("classnames")].toString();
|
|
|
|
if (!plugins.isEmpty()) {
|
|
|
|
import[QStringLiteral("plugin")] = plugins;
|
|
|
|
import[QStringLiteral("classname")] = classnames;
|
|
|
|
}
|
2013-08-23 13:03:20 +00:00
|
|
|
}
|
2013-09-25 14:13:27 +00:00
|
|
|
if (!import[QStringLiteral("path")].isNull())
|
2013-08-23 13:03:20 +00:00
|
|
|
done.append(import);
|
|
|
|
}
|
|
|
|
return done;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find Qml Imports Recursively
|
2013-09-25 14:26:42 +00:00
|
|
|
QVariantList findQmlImportsRecursively(const QStringList &qmlDirs)
|
|
|
|
{
|
2013-08-23 13:03:20 +00:00
|
|
|
QVariantList ret;
|
|
|
|
|
|
|
|
QSet<QString> toVisit = qmlDirs.toSet();
|
|
|
|
QSet<QString> visited;
|
|
|
|
while (!toVisit.isEmpty()) {
|
|
|
|
QString qmlDir = *toVisit.begin();
|
|
|
|
toVisit.erase(toVisit.begin());
|
|
|
|
visited.insert(qmlDir);
|
|
|
|
|
|
|
|
QVariantList imports = findQmlImportsInDirectory(qmlDir);
|
|
|
|
imports = findPathsForModuleImports(imports);
|
|
|
|
|
|
|
|
// schedule recursive visit of imports
|
|
|
|
foreach (const QVariant &importVariant, imports) {
|
|
|
|
QVariantMap import = qvariant_cast<QVariantMap>(importVariant);
|
2013-09-25 14:13:27 +00:00
|
|
|
QString path = import[QStringLiteral("path")].toString();
|
2013-08-23 13:03:20 +00:00
|
|
|
if (!path.isEmpty() && !visited.contains(path)) {
|
|
|
|
toVisit.insert(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = mergeImports(ret, imports);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
QStringList qmlImportPaths;
|
|
|
|
|
|
|
|
int i = 1;
|
|
|
|
while (i < args.count()) {
|
|
|
|
const QString &arg = args.at(i);
|
|
|
|
++i;
|
2013-09-25 14:13:27 +00:00
|
|
|
if (!arg.startsWith(QLatin1Char('-'))) {
|
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-08-23 13:03:20 +00:00
|
|
|
|
|
|
|
while (i < args.count()) {
|
|
|
|
const QString arg = args.at(i);
|
2013-09-25 14:13:27 +00:00
|
|
|
if (arg.startsWith(QLatin1Char('-')))
|
2013-08-23 13:03:20 +00:00
|
|
|
break;
|
|
|
|
++i;
|
|
|
|
qmlRootPaths += arg;
|
|
|
|
}
|
|
|
|
} 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-08-23 13:03:20 +00:00
|
|
|
|
|
|
|
while (i < args.count()) {
|
|
|
|
const QString arg = args.at(i);
|
2013-09-25 14:13:27 +00:00
|
|
|
if (arg.startsWith(QLatin1Char('-')))
|
2013-08-23 13:03:20 +00:00
|
|
|
break;
|
|
|
|
++i;
|
|
|
|
qmlImportPaths += arg;
|
|
|
|
}
|
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_qmlImportPaths = qmlImportPaths;
|
|
|
|
|
|
|
|
// Find the imports!
|
|
|
|
QVariantList imports = findQmlImportsRecursively(qmlRootPaths);
|
|
|
|
|
|
|
|
// Convert to JSON
|
|
|
|
QByteArray json = QJsonDocument(QJsonArray::fromVariantList(imports)).toJson();
|
|
|
|
std::cout << json.constData() << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|