QmlJS: Move plugin dumping code and redump on file change.

Task-number: QTCREATORBUG-3047
Reviewed-by: Erik Verbruggen
This commit is contained in:
Christian Kamm
2010-11-10 15:07:53 +01:00
parent ca4439bcef
commit 2b1fe08641
5 changed files with 401 additions and 106 deletions

View File

@@ -29,12 +29,11 @@
#include "qmljsmodelmanager.h" #include "qmljsmodelmanager.h"
#include "qmljstoolsconstants.h" #include "qmljstoolsconstants.h"
//#include "qmljseditor.h" #include "qmljsplugindumper.h"
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
#include <coreplugin/editormanager/editormanager.h> #include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/progressmanager/progressmanager.h> #include <coreplugin/progressmanager/progressmanager.h>
#include <coreplugin/messagemanager.h>
#include <coreplugin/mimedatabase.h> #include <coreplugin/mimedatabase.h>
#include <qmljs/qmljsinterpreter.h> #include <qmljs/qmljsinterpreter.h>
#include <qmljs/qmljsbind.h> #include <qmljs/qmljsbind.h>
@@ -64,7 +63,8 @@ static QStringList environmentImportPaths();
ModelManager::ModelManager(QObject *parent): ModelManager::ModelManager(QObject *parent):
ModelManagerInterface(parent), ModelManagerInterface(parent),
m_core(Core::ICore::instance()) m_core(Core::ICore::instance()),
m_pluginDumper(new PluginDumper(this))
{ {
m_synchronizer.setCancelOnWait(true); m_synchronizer.setCancelOnWait(true);
@@ -502,43 +502,6 @@ static QStringList environmentImportPaths()
return paths; return paths;
} }
void ModelManager::loadPluginTypes(const QString &libraryPath, const QString &importPath, const QString &importUri)
{
// make sure loading is always triggered in ModelManager's thread
metaObject()->invokeMethod(this, "onLoadPluginTypes",
Q_ARG(QString, libraryPath),
Q_ARG(QString, importPath),
Q_ARG(QString, importUri));
}
void ModelManager::onLoadPluginTypes(const QString &libraryPath, const QString &importPath, const QString &importUri)
{
const QString canonicalLibraryPath = QDir::cleanPath(libraryPath);
if (m_runningQmldumps.values().contains(canonicalLibraryPath))
return;
if (_snapshot.libraryInfo(canonicalLibraryPath).isDumped())
return;
ProjectExplorer::Project *activeProject = ProjectExplorer::ProjectExplorerPlugin::instance()->startupProject();
if (!activeProject)
return;
ProjectInfo info = projectInfo(activeProject);
if (info.qmlDumpPath.isEmpty())
return;
QProcess *process = new QProcess(this);
process->setEnvironment(info.qmlDumpEnvironment.toStringList());
connect(process, SIGNAL(finished(int)), SLOT(qmlPluginTypeDumpDone(int)));
connect(process, SIGNAL(error(QProcess::ProcessError)), SLOT(qmlPluginTypeDumpError(QProcess::ProcessError)));
QStringList args;
args << importPath;
args << importUri;
process->start(info.qmlDumpPath, args);
m_runningQmldumps.insert(process, canonicalLibraryPath);
}
void ModelManager::updateImportPaths() void ModelManager::updateImportPaths()
{ {
m_allImportPaths.clear(); m_allImportPaths.clear();
@@ -560,63 +523,7 @@ void ModelManager::updateImportPaths()
updateSourceFiles(importedFiles, true); updateSourceFiles(importedFiles, true);
} }
static QString qmldumpErrorMessage(const QString &libraryPath, const QString &error) void ModelManager::loadPluginTypes(const QString &libraryPath, const QString &importPath, const QString &importUri)
{ {
return ModelManager::tr("Type dump of QML plugin in %0 failed.\nErrors:\n%1\n").arg(libraryPath, error); m_pluginDumper->loadPluginTypes(libraryPath, importPath, importUri);
}
void ModelManager::qmlPluginTypeDumpDone(int exitCode)
{
QProcess *process = qobject_cast<QProcess *>(sender());
if (!process)
return;
process->deleteLater();
const QString libraryPath = m_runningQmldumps.take(process);
LibraryInfo libraryInfo = _snapshot.libraryInfo(libraryPath);
libraryInfo.setDumped(true);
if (exitCode != 0) {
Core::MessageManager *messageManager = Core::MessageManager::instance();
messageManager->printToOutputPane(qmldumpErrorMessage(libraryPath, process->readAllStandardError()));
}
const QByteArray output = process->readAllStandardOutput();
QMap<QString, Interpreter::FakeMetaObject *> newObjects;
const QString error = Interpreter::CppQmlTypesLoader::parseQmlTypeXml(output, &newObjects);
if (exitCode == 0 && error.isEmpty()) {
// convert from QList<T *> to QList<const T *>
QList<const Interpreter::FakeMetaObject *> objectsList;
QMapIterator<QString, Interpreter::FakeMetaObject *> it(newObjects);
while (it.hasNext()) {
it.next();
objectsList.append(it.value());
}
libraryInfo.setMetaObjects(objectsList);
if (libraryPath.isEmpty())
Interpreter::CppQmlTypesLoader::builtinObjects.append(objectsList);
}
if (!libraryPath.isEmpty())
updateLibraryInfo(libraryPath, libraryInfo);
}
void ModelManager::qmlPluginTypeDumpError(QProcess::ProcessError)
{
QProcess *process = qobject_cast<QProcess *>(sender());
if (!process)
return;
process->deleteLater();
const QString libraryPath = m_runningQmldumps.take(process);
Core::MessageManager *messageManager = Core::MessageManager::instance();
messageManager->printToOutputPane(qmldumpErrorMessage(libraryPath, process->readAllStandardError()));
if (!libraryPath.isEmpty()) {
LibraryInfo libraryInfo = _snapshot.libraryInfo(libraryPath);
libraryInfo.setDumped(true);
updateLibraryInfo(libraryPath, libraryInfo);
}
} }

View File

@@ -48,6 +48,8 @@ class MimeType;
namespace QmlJSTools { namespace QmlJSTools {
namespace Internal { namespace Internal {
class PluginDumper;
class QMLJSTOOLS_EXPORT ModelManager: public QmlJS::ModelManagerInterface class QMLJSTOOLS_EXPORT ModelManager: public QmlJS::ModelManagerInterface
{ {
Q_OBJECT Q_OBJECT
@@ -78,11 +80,6 @@ public:
Q_SIGNALS: Q_SIGNALS:
void projectPathChanged(const QString &projectPath); void projectPathChanged(const QString &projectPath);
private Q_SLOTS:
void onLoadPluginTypes(const QString &libraryPath, const QString &importPath, const QString &importUri);
void qmlPluginTypeDumpDone(int exitCode);
void qmlPluginTypeDumpError(QProcess::ProcessError error);
protected: protected:
QFuture<void> refreshSourceFiles(const QStringList &sourceFiles, QFuture<void> refreshSourceFiles(const QStringList &sourceFiles,
bool emitDocumentOnDiskChanged); bool emitDocumentOnDiskChanged);
@@ -106,12 +103,13 @@ private:
QmlJS::Snapshot _snapshot; QmlJS::Snapshot _snapshot;
QStringList m_allImportPaths; QStringList m_allImportPaths;
QStringList m_defaultImportPaths; QStringList m_defaultImportPaths;
QHash<QProcess *, QString> m_runningQmldumps;
QFutureSynchronizer<void> m_synchronizer; QFutureSynchronizer<void> m_synchronizer;
// project integration // project integration
QMap<ProjectExplorer::Project *, ProjectInfo> m_projects; QMap<ProjectExplorer::Project *, ProjectInfo> m_projects;
PluginDumper *m_pluginDumper;
}; };
} // namespace Internal } // namespace Internal

View File

@@ -0,0 +1,295 @@
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#include "qmljsplugindumper.h"
#include "qmljsmodelmanager.h"
#include <qmljs/qmljsdocument.h>
#include <qmljs/qmljsinterpreter.h>
#include <projectexplorer/filewatcher.h>
#include <projectexplorer/projectexplorer.h>
#include <coreplugin/messagemanager.h>
#include <QtCore/QDir>
using namespace QmlJS;
using namespace QmlJSTools;
using namespace QmlJSTools::Internal;
PluginDumper::PluginDumper(ModelManager *modelManager)
: QObject(modelManager)
, m_modelManager(modelManager)
, m_pluginWatcher(new ProjectExplorer::FileWatcher(this))
{
connect(m_pluginWatcher, SIGNAL(fileChanged(QString)), SLOT(pluginChanged(QString)));
}
void PluginDumper::loadPluginTypes(const QString &libraryPath, const QString &importPath, const QString &importUri)
{
// move to the owning thread
metaObject()->invokeMethod(this, "onLoadPluginTypes",
Q_ARG(QString, libraryPath),
Q_ARG(QString, importPath),
Q_ARG(QString, importUri));
}
void PluginDumper::onLoadPluginTypes(const QString &libraryPath, const QString &importPath, const QString &importUri)
{
const QString canonicalLibraryPath = QDir::cleanPath(libraryPath);
if (m_runningQmldumps.values().contains(canonicalLibraryPath))
return;
const Snapshot snapshot = m_modelManager->snapshot();
if (snapshot.libraryInfo(canonicalLibraryPath).isDumped())
return;
// avoid inserting the same plugin twice
int index;
for (index = 0; index < m_plugins.size(); ++index) {
if (m_plugins.at(index).qmldirPath == libraryPath)
break;
}
if (index == m_plugins.size())
m_plugins.append(Plugin());
Plugin &plugin = m_plugins[index];
plugin.qmldirPath = canonicalLibraryPath;
plugin.importPath = importPath;
plugin.importUri = importUri;
foreach (const QmlDirParser::Plugin &plugin, snapshot.libraryInfo(canonicalLibraryPath).plugins()) {
const QString pluginLibrary = resolvePlugin(canonicalLibraryPath, plugin.path, plugin.name);
m_pluginWatcher->addFile(pluginLibrary);
m_libraryToPluginIndex.insert(pluginLibrary, index);
}
dump(plugin);
}
static QString qmldumpErrorMessage(const QString &libraryPath, const QString &error)
{
return PluginDumper::tr("Type dump of QML plugin in %0 failed.\nErrors:\n%1\n").arg(libraryPath, error);
}
void PluginDumper::qmlPluginTypeDumpDone(int exitCode)
{
QProcess *process = qobject_cast<QProcess *>(sender());
if (!process)
return;
process->deleteLater();
const QString libraryPath = m_runningQmldumps.take(process);
const Snapshot snapshot = m_modelManager->snapshot();
LibraryInfo libraryInfo = snapshot.libraryInfo(libraryPath);
libraryInfo.setDumped(true);
if (exitCode != 0) {
Core::MessageManager *messageManager = Core::MessageManager::instance();
messageManager->printToOutputPane(qmldumpErrorMessage(libraryPath, process->readAllStandardError()));
}
const QByteArray output = process->readAllStandardOutput();
QMap<QString, Interpreter::FakeMetaObject *> newObjects;
const QString error = Interpreter::CppQmlTypesLoader::parseQmlTypeXml(output, &newObjects);
if (exitCode == 0 && error.isEmpty()) {
// convert from QList<T *> to QList<const T *>
QList<const Interpreter::FakeMetaObject *> objectsList;
QMapIterator<QString, Interpreter::FakeMetaObject *> it(newObjects);
while (it.hasNext()) {
it.next();
objectsList.append(it.value());
}
libraryInfo.setMetaObjects(objectsList);
if (libraryPath.isEmpty())
Interpreter::CppQmlTypesLoader::builtinObjects.append(objectsList);
}
if (!libraryPath.isEmpty())
m_modelManager->updateLibraryInfo(libraryPath, libraryInfo);
}
void PluginDumper::qmlPluginTypeDumpError(QProcess::ProcessError)
{
QProcess *process = qobject_cast<QProcess *>(sender());
if (!process)
return;
process->deleteLater();
const QString libraryPath = m_runningQmldumps.take(process);
Core::MessageManager *messageManager = Core::MessageManager::instance();
messageManager->printToOutputPane(qmldumpErrorMessage(libraryPath, process->readAllStandardError()));
if (!libraryPath.isEmpty()) {
const Snapshot snapshot = m_modelManager->snapshot();
LibraryInfo libraryInfo = snapshot.libraryInfo(libraryPath);
libraryInfo.setDumped(true);
m_modelManager->updateLibraryInfo(libraryPath, libraryInfo);
}
}
void PluginDumper::pluginChanged(const QString &pluginLibrary)
{
const int pluginIndex = m_libraryToPluginIndex.value(pluginLibrary, -1);
if (pluginIndex == -1)
return;
const Plugin &plugin = m_plugins.at(pluginIndex);
dump(plugin);
}
void PluginDumper::dump(const Plugin &plugin)
{
ProjectExplorer::Project *activeProject = ProjectExplorer::ProjectExplorerPlugin::instance()->startupProject();
if (!activeProject)
return;
ModelManagerInterface::ProjectInfo info = m_modelManager->projectInfo(activeProject);
if (info.qmlDumpPath.isEmpty())
return;
QProcess *process = new QProcess(this);
process->setEnvironment(info.qmlDumpEnvironment.toStringList());
connect(process, SIGNAL(finished(int)), SLOT(qmlPluginTypeDumpDone(int)));
connect(process, SIGNAL(error(QProcess::ProcessError)), SLOT(qmlPluginTypeDumpError(QProcess::ProcessError)));
QStringList args;
args << plugin.importPath;
args << plugin.importUri;
process->start(info.qmlDumpPath, args);
m_runningQmldumps.insert(process, plugin.qmldirPath);
}
/*!
Returns the result of the merge of \a baseName with \a path, \a suffixes, and \a prefix.
The \a prefix must contain the dot.
\a qmldirPath is the location of the qmldir file.
Adapted from QDeclarativeImportDatabase::resolvePlugin.
*/
QString PluginDumper::resolvePlugin(const QDir &qmldirPath, const QString &qmldirPluginPath,
const QString &baseName, const QStringList &suffixes,
const QString &prefix)
{
QStringList searchPaths;
searchPaths.append(QLatin1String("."));
bool qmldirPluginPathIsRelative = QDir::isRelativePath(qmldirPluginPath);
if (!qmldirPluginPathIsRelative)
searchPaths.prepend(qmldirPluginPath);
foreach (const QString &pluginPath, searchPaths) {
QString resolvedPath;
if (pluginPath == QLatin1String(".")) {
if (qmldirPluginPathIsRelative)
resolvedPath = qmldirPath.absoluteFilePath(qmldirPluginPath);
else
resolvedPath = qmldirPath.absolutePath();
} else {
resolvedPath = pluginPath;
}
QDir dir(resolvedPath);
foreach (const QString &suffix, suffixes) {
QString pluginFileName = prefix;
pluginFileName += baseName;
pluginFileName += suffix;
QFileInfo fileInfo(dir, pluginFileName);
if (fileInfo.exists())
return fileInfo.absoluteFilePath();
}
}
return QString();
}
/*!
Returns the result of the merge of \a baseName with \a dir and the platform suffix.
Adapted from QDeclarativeImportDatabase::resolvePlugin.
\table
\header \i Platform \i Valid suffixes
\row \i Windows \i \c .dll
\row \i Unix/Linux \i \c .so
\row \i AIX \i \c .a
\row \i HP-UX \i \c .sl, \c .so (HP-UXi)
\row \i Mac OS X \i \c .dylib, \c .bundle, \c .so
\row \i Symbian \i \c .dll
\endtable
Version number on unix are ignored.
*/
QString PluginDumper::resolvePlugin(const QDir &qmldirPath, const QString &qmldirPluginPath,
const QString &baseName)
{
#if defined(Q_OS_WIN32) || defined(Q_OS_WINCE)
return resolvePlugin(qmldirPath, qmldirPluginPath, baseName,
QStringList()
<< QLatin1String("d.dll") // try a qmake-style debug build first
<< QLatin1String(".dll"));
#elif defined(Q_OS_DARWIN)
return resolvePlugin(qmldirPath, qmldirPluginPath, baseName,
QStringList()
<< QLatin1String("_debug.dylib") // try a qmake-style debug build first
<< QLatin1String(".dylib")
<< QLatin1String(".so")
<< QLatin1String(".bundle"),
QLatin1String("lib"));
#else // Generic Unix
QStringList validSuffixList;
# if defined(Q_OS_HPUX)
/*
See "HP-UX Linker and Libraries User's Guide", section "Link-time Differences between PA-RISC and IPF":
"In PA-RISC (PA-32 and PA-64) shared libraries are suffixed with .sl. In IPF (32-bit and 64-bit),
the shared libraries are suffixed with .so. For compatibility, the IPF linker also supports the .sl suffix."
*/
validSuffixList << QLatin1String(".sl");
# if defined __ia64
validSuffixList << QLatin1String(".so");
# endif
# elif defined(Q_OS_AIX)
validSuffixList << QLatin1String(".a") << QLatin1String(".so");
# elif defined(Q_OS_UNIX)
validSuffixList << QLatin1String(".so");
# endif
// Examples of valid library names:
// libfoo.so
return resolvePlugin(qmldirPath, qmldirPluginPath, baseName, validSuffixList, QLatin1String("lib"));
#endif
}

View File

@@ -0,0 +1,93 @@
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#ifndef QMLJSPLUGINDUMPER_H
#define QMLJSPLUGINDUMPER_H
#include <QtCore/QObject>
#include <QtCore/QHash>
#include <QtCore/QProcess>
QT_BEGIN_NAMESPACE
class QDir;
QT_END_NAMESPACE
namespace ProjectExplorer {
class FileWatcher;
}
namespace QmlJSTools {
namespace Internal {
class ModelManager;
class PluginDumper : public QObject
{
Q_OBJECT
public:
explicit PluginDumper(ModelManager *modelManager);
public:
void loadPluginTypes(const QString &libraryPath, const QString &importPath,
const QString &importUri);
private slots:
void onLoadPluginTypes(const QString &libraryPath, const QString &importPath,
const QString &importUri);
void qmlPluginTypeDumpDone(int exitCode);
void qmlPluginTypeDumpError(QProcess::ProcessError error);
void pluginChanged(const QString &pluginLibrary);
private:
class Plugin {
public:
QString qmldirPath;
QString importPath;
QString importUri;
};
void dump(const Plugin &plugin);
QString resolvePlugin(const QDir &qmldirPath, const QString &qmldirPluginPath,
const QString &baseName);
QString resolvePlugin(const QDir &qmldirPath, const QString &qmldirPluginPath,
const QString &baseName, const QStringList &suffixes,
const QString &prefix);
private:
ModelManager *m_modelManager;
ProjectExplorer::FileWatcher *m_pluginWatcher;
QHash<QProcess *, QString> m_runningQmldumps;
QList<Plugin> m_plugins;
QHash<QString, int> m_libraryToPluginIndex;
};
} // namespace Internal
} // namespace QmlJSTools
#endif // QMLJSPLUGINDUMPER_H

View File

@@ -11,10 +11,12 @@ HEADERS += \
$$PWD/qmljstoolsconstants.h \ $$PWD/qmljstoolsconstants.h \
$$PWD/qmljsmodelmanager.h \ $$PWD/qmljsmodelmanager.h \
$$PWD/qmljsqtstylecodeformatter.h \ $$PWD/qmljsqtstylecodeformatter.h \
$$PWD/qmljsrefactoringchanges.h $$PWD/qmljsrefactoringchanges.h \
$$PWD/qmljsplugindumper.h
SOURCES += \ SOURCES += \
$$PWD/qmljstoolsplugin.cpp \ $$PWD/qmljstoolsplugin.cpp \
$$PWD/qmljsmodelmanager.cpp \ $$PWD/qmljsmodelmanager.cpp \
$$PWD/qmljsqtstylecodeformatter.cpp \ $$PWD/qmljsqtstylecodeformatter.cpp \
$$PWD/qmljsrefactoringchanges.cpp $$PWD/qmljsrefactoringchanges.cpp \
$$PWD/qmljsplugindumper.cpp