make resource file handling able to deal with QMakeProject's VFS

resources.prf may create virtual qrc files when RESOURCES contains
non-qrc files.

Change-Id: If591de9b32b775059d67e94bc3cb06d23ee44b08
Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
This commit is contained in:
Oswald Buddenhagen
2016-10-24 19:30:24 +02:00
parent 1589ce3ce8
commit 424639ecac
16 changed files with 150 additions and 77 deletions

View File

@@ -566,8 +566,9 @@ void ModelManagerInterface::updateProjectInfo(const ProjectInfo &pinfo, ProjectE
updateSourceFiles(newFiles, false);
// update qrc cache
m_qrcContents = pinfo.resourceFileContents;
foreach (const QString &newQrc, pinfo.allResourceFiles)
m_qrcCache.addPath(newQrc);
m_qrcCache.addPath(newQrc, m_qrcContents.value(newQrc));
foreach (const QString &oldQrc, oldInfo.allResourceFiles)
m_qrcCache.removePath(oldQrc);
@@ -656,7 +657,7 @@ void ModelManagerInterface::emitDocumentChangedOnDisk(Document::Ptr doc)
void ModelManagerInterface::updateQrcFile(const QString &path)
{
m_qrcCache.updatePath(path);
m_qrcCache.updatePath(path, m_qrcContents.value(path));
}
void ModelManagerInterface::updateDocument(Document::Ptr doc)

View File

@@ -86,6 +86,7 @@ public:
PathsAndLanguages importPaths;
QStringList activeResourceFiles;
QStringList allResourceFiles;
QHash<QString, QString> resourceFileContents;
// whether trying to run qmldump makes sense
bool tryQmlDump;
@@ -273,6 +274,7 @@ private:
QHash<QString, QPair<CPlusPlus::Document::Ptr, bool> > m_queuedCppDocuments;
QFuture<void> m_cppQmlTypesUpdater;
QrcCache m_qrcCache;
QHash<QString, QString> m_qrcContents;
CppDataHash m_cppDataHash;
QHash<QString, QList<CPlusPlus::Document::Ptr> > m_cppDeclarationFiles;

View File

@@ -68,7 +68,7 @@ class QrcParserPrivate
public:
typedef QMap<QString,QStringList> SMap;
QrcParserPrivate(QrcParser *q);
bool parseFile(const QString &path);
bool parseFile(const QString &path, const QString &contents);
QString firstFileAtPath(const QString &path, const QLocale &locale) const;
void collectFilesAtPath(const QString &path, QStringList *res, const QLocale *locale = 0) const;
bool hasDirAtPath(const QString &path, const QLocale *locale = 0) const;
@@ -94,9 +94,9 @@ class QrcCachePrivate
Q_DECLARE_TR_FUNCTIONS(QmlJS::QrcCachePrivate)
public:
QrcCachePrivate(QrcCache *q);
QrcParser::Ptr addPath(const QString &path);
QrcParser::Ptr addPath(const QString &path, const QString &contents);
void removePath(const QString &path);
QrcParser::Ptr updatePath(const QString &path);
QrcParser::Ptr updatePath(const QString &path, const QString &contents);
QrcParser::Ptr parsedPath(const QString &path);
void clear();
private:
@@ -149,9 +149,9 @@ QrcParser::~QrcParser()
delete d;
}
bool QrcParser::parseFile(const QString &path)
bool QrcParser::parseFile(const QString &path, const QString &contents)
{
return d->parseFile(path);
return d->parseFile(path, contents);
}
/*! \brief returns fs path of the first (active) file at the given qrc path
@@ -217,11 +217,11 @@ bool QrcParser::isValid() const
return errorMessages().isEmpty();
}
QrcParser::Ptr QrcParser::parseQrcFile(const QString &path)
QrcParser::Ptr QrcParser::parseQrcFile(const QString &path, const QString &contents)
{
Ptr res(new QrcParser);
if (!path.isEmpty())
res->parseFile(path);
res->parseFile(path, contents);
return res;
}
@@ -237,9 +237,9 @@ QrcCache::~QrcCache()
delete d;
}
QrcParser::ConstPtr QrcCache::addPath(const QString &path)
QrcParser::ConstPtr QrcCache::addPath(const QString &path, const QString &contents)
{
return d->addPath(path);
return d->addPath(path, contents);
}
void QrcCache::removePath(const QString &path)
@@ -247,9 +247,9 @@ void QrcCache::removePath(const QString &path)
d->removePath(path);
}
QrcParser::ConstPtr QrcCache::updatePath(const QString &path)
QrcParser::ConstPtr QrcCache::updatePath(const QString &path, const QString &contents)
{
return d->updatePath(path);
return d->updatePath(path, contents);
}
QrcParser::ConstPtr QrcCache::parsedPath(const QString &path)
@@ -269,17 +269,19 @@ namespace Internal {
QrcParserPrivate::QrcParserPrivate(QrcParser *)
{ }
bool QrcParserPrivate::parseFile(const QString &path)
bool QrcParserPrivate::parseFile(const QString &path, const QString &contents)
{
QDomDocument doc;
QDir baseDir(QFileInfo(path).path());
if (contents.isEmpty()) {
// Regular file
QFile file(path);
if (!file.open(QIODevice::ReadOnly)) {
m_errorMessages.append(file.errorString());
return false;
}
QDomDocument doc;
QString error_msg;
int error_line, error_col;
if (!doc.setContent(&file, &error_msg, &error_line, &error_col)) {
@@ -287,6 +289,16 @@ bool QrcParserPrivate::parseFile(const QString &path)
.arg(error_line).arg(error_col).arg(error_msg));
return false;
}
} else {
// Virtual file from qmake evaluator
QString error_msg;
int error_line, error_col;
if (!doc.setContent(contents, &error_msg, &error_line, &error_col)) {
m_errorMessages.append(tr("XML error on line %1, col %2: %3")
.arg(error_line).arg(error_col).arg(error_msg));
return false;
}
}
QDomElement root = doc.firstChildElement(QLatin1String("RCC"));
if (root.isNull()) {
@@ -470,7 +482,7 @@ QStringList QrcParserPrivate::allUiLanguages(const QLocale *locale) const
QrcCachePrivate::QrcCachePrivate(QrcCache *)
{ }
QrcParser::Ptr QrcCachePrivate::addPath(const QString &path)
QrcParser::Ptr QrcCachePrivate::addPath(const QString &path, const QString &contents)
{
QPair<QrcParser::Ptr,int> currentValue;
{
@@ -482,7 +494,7 @@ QrcParser::Ptr QrcCachePrivate::addPath(const QString &path)
return currentValue.first;
}
}
QrcParser::Ptr newParser = QrcParser::parseQrcFile(path);
QrcParser::Ptr newParser = QrcParser::parseQrcFile(path, contents);
if (!newParser->isValid())
qCWarning(qmljsLog) << "adding invalid qrc " << path << " to the cache:" << newParser->errorMessages();
{
@@ -513,9 +525,9 @@ void QrcCachePrivate::removePath(const QString &path)
}
}
QrcParser::Ptr QrcCachePrivate::updatePath(const QString &path)
QrcParser::Ptr QrcCachePrivate::updatePath(const QString &path, const QString &contents)
{
QrcParser::Ptr newParser = QrcParser::parseQrcFile(path);
QrcParser::Ptr newParser = QrcParser::parseQrcFile(path, contents);
{
QMutexLocker l(&m_mutex);
QPair<QrcParser::Ptr,int> currentValue = m_cache.value(path, qMakePair(QrcParser::Ptr(0), 0));

View File

@@ -46,7 +46,7 @@ public:
typedef QSharedPointer<QrcParser> Ptr;
typedef QSharedPointer<const QrcParser> ConstPtr;
~QrcParser();
bool parseFile(const QString &path);
bool parseFile(const QString &path, const QString &contents);
QString firstFileAtPath(const QString &path, const QLocale &locale) const;
void collectFilesAtPath(const QString &path, QStringList *res, const QLocale *locale = 0) const;
bool hasDirAtPath(const QString &path, const QLocale *locale = 0) const;
@@ -59,7 +59,7 @@ public:
QStringList languages() const;
bool isValid() const;
static Ptr parseQrcFile(const QString &path);
static Ptr parseQrcFile(const QString &path, const QString &contents);
static QString normalizedQrcFilePath(const QString &path);
static QString normalizedQrcDirectoryPath(const QString &path);
static QString qrcDirectoryPathForQrcFilePath(const QString &file);
@@ -74,9 +74,9 @@ class QMLJS_EXPORT QrcCache
public:
QrcCache();
~QrcCache();
QrcParser::ConstPtr addPath(const QString &path);
QrcParser::ConstPtr addPath(const QString &path, const QString &contents);
void removePath(const QString &path);
QrcParser::ConstPtr updatePath(const QString &path);
QrcParser::ConstPtr updatePath(const QString &path, const QString &contents);
QrcParser::ConstPtr parsedPath(const QString &path);
void clear();
private:

View File

@@ -527,7 +527,7 @@ void QbsGroupNode::setupFolder(ProjectExplorer::FolderNode *root,
using ResourceEditor::ResourceTopLevelNode;
if (!fn) {
if (isQrcFile) {
fn = new ResourceTopLevelNode(Utils::FileName::fromString(c->path()), root);
fn = new ResourceTopLevelNode(Utils::FileName::fromString(c->path()), QString(), root);
} else {
fn = new QbsFolderNode(Utils::FileName::fromString(c->path()),
ProjectExplorer::FolderNodeType,

View File

@@ -578,8 +578,12 @@ struct InternalNode
QList<FolderNode *> nodesToAdd;
nodesToAdd.reserve(resourcesToAdd.size());
foreach (const FileName &file, resourcesToAdd)
nodesToAdd.append(new ResourceEditor::ResourceTopLevelNode(file, folder));
foreach (const FileName &file, resourcesToAdd) {
auto vfs = static_cast<QmakePriFileNode *>(folder->projectNode())->m_project->qmakeVfs();
QString contents;
vfs->readVirtualFile(file.toString(), &contents);
nodesToAdd.append(new ResourceEditor::ResourceTopLevelNode(file, contents, folder));
}
folder->removeFolderNodes(resourcesToRemove);
folder->addFolderNodes(nodesToAdd);

View File

@@ -566,6 +566,11 @@ void QmakeProject::updateQmlJSCodeModel()
QmlJS::Dialect::Qml);
projectInfo.activeResourceFiles.append(node->variableValue(ExactResourceVar));
projectInfo.allResourceFiles.append(node->variableValue(ResourceVar));
foreach (const QString &rc, projectInfo.allResourceFiles) {
QString contents;
if (m_qmakeVfs->readVirtualFile(rc, &contents))
projectInfo.resourceFileContents[rc] = contents;
}
if (!hasQmlLib) {
QStringList qtLibs = node->variableValue(QtVar);
hasQmlLib = qtLibs.contains(QLatin1String("declarative")) ||

View File

@@ -99,9 +99,10 @@ bool FileList::containsFile(File *file)
** ResourceFile
*/
ResourceFile::ResourceFile(const QString &file_name)
ResourceFile::ResourceFile(const QString &file_name, const QString &contents)
{
setFileName(file_name);
m_contents = contents;
}
ResourceFile::~ResourceFile()
@@ -118,6 +119,13 @@ Core::IDocument::OpenResult ResourceFile::load()
return Core::IDocument::OpenResult::ReadError;
}
clearPrefixList();
QDomDocument doc;
if (m_contents.isEmpty()) {
// Regular file
QFile file(m_file_name);
if (!file.open(QIODevice::ReadOnly)) {
m_error_message = file.errorString();
@@ -130,10 +138,6 @@ Core::IDocument::OpenResult ResourceFile::load()
m_textFileFormat.codec = QTextCodec::codecForName("UTF-8");
file.close();
clearPrefixList();
QDomDocument doc;
QString error_msg;
int error_line, error_col;
if (!doc.setContent(data, &error_msg, &error_line, &error_col)) {
@@ -142,6 +146,19 @@ Core::IDocument::OpenResult ResourceFile::load()
return Core::IDocument::OpenResult::CannotHandle;
}
} else {
// Virtual file from qmake evaluator
QString error_msg;
int error_line, error_col;
if (!doc.setContent(m_contents, &error_msg, &error_line, &error_col)) {
m_error_message = tr("XML error on line %1, col %2: %3")
.arg(error_line).arg(error_col).arg(error_msg);
return Core::IDocument::OpenResult::CannotHandle;
}
}
QDomElement root = doc.firstChildElement(QLatin1String("RCC"));
if (root.isNull()) {
m_error_message = tr("The <RCC> root element is missing.");

View File

@@ -127,7 +127,7 @@ class ResourceFile
{
Q_DECLARE_TR_FUNCTIONS(ResourceFile)
public:
ResourceFile(const QString &file_name = QString());
ResourceFile(const QString &file_name = QString(), const QString &contents = QString());
~ResourceFile();
void setFileName(const QString &file_name) { m_file_name = file_name; }
@@ -175,6 +175,7 @@ public:
private:
PrefixList m_prefix_list;
QString m_file_name;
QString m_contents;
QString m_error_message;
Utils::TextFileFormat m_textFileFormat;

View File

@@ -108,12 +108,19 @@ static bool sortNodesByPath(ProjectExplorer::Node *a, ProjectExplorer::Node *b)
return a->filePath() < b->filePath();
}
ResourceTopLevelNode::ResourceTopLevelNode(const Utils::FileName &filePath, FolderNode *parent)
ResourceTopLevelNode::ResourceTopLevelNode(
const Utils::FileName &filePath, const QString &contents,
ProjectExplorer::FolderNode *parent)
: ProjectExplorer::FolderNode(filePath)
{
setIcon(Core::FileIconProvider::icon(filePath.toString()));
if (contents.isEmpty()) {
m_document = new ResourceFileWatcher(this);
Core::DocumentManager::addDocument(m_document);
} else {
m_contents = contents;
m_document = nullptr;
}
Utils::FileName base = parent->filePath();
if (filePath.isChildOf(base))
@@ -124,6 +131,7 @@ ResourceTopLevelNode::ResourceTopLevelNode(const Utils::FileName &filePath, Fold
ResourceTopLevelNode::~ResourceTopLevelNode()
{
if (m_document)
Core::DocumentManager::removeDocument(m_document);
delete m_document;
}
@@ -135,7 +143,7 @@ void ResourceTopLevelNode::update()
QMap<PrefixFolderLang, QList<ProjectExplorer::FolderNode *>> foldersToAddToFolders;
QMap<PrefixFolderLang, QList<ProjectExplorer::FolderNode *>> foldersToAddToPrefix;
ResourceFile file(filePath().toString());
ResourceFile file(filePath().toString(), m_contents);
if (file.load() == Core::IDocument::OpenResult::Success) {
QMap<PrefixFolderLang, ProjectExplorer::FolderNode *> prefixNodes;
QMap<PrefixFolderLang, ProjectExplorer::FolderNode *> folderNodes;

View File

@@ -39,7 +39,7 @@ namespace Internal { class ResourceFileWatcher; }
class RESOURCE_EXPORT ResourceTopLevelNode : public ProjectExplorer::FolderNode
{
public:
ResourceTopLevelNode(const Utils::FileName &filePath, FolderNode *parent);
ResourceTopLevelNode(const Utils::FileName &filePath, const QString &contents, FolderNode *parent);
~ResourceTopLevelNode() override;
void update();
@@ -58,6 +58,7 @@ public:
private:
Internal::ResourceFileWatcher *m_document;
QString m_contents;
};
namespace Internal {

View File

@@ -26,6 +26,7 @@
#include "profileevaluator.h"
#include "qmakeglobals.h"
#include "qmakevfs.h"
#include "ioutils.h"
#include <QDir>
@@ -41,7 +42,8 @@ void ProFileEvaluator::initialize()
ProFileEvaluator::ProFileEvaluator(QMakeGlobals *option, QMakeParser *parser, QMakeVfs *vfs,
QMakeHandler *handler)
: d(new QMakeEvaluator(option, parser, vfs, handler))
: d(new QMakeEvaluator(option, parser, vfs, handler)),
m_vfs(vfs)
{
}
@@ -104,6 +106,7 @@ QStringList ProFileEvaluator::fixifiedValues(
return result;
}
// VFS note: all search paths are assumed to be real.
QStringList ProFileEvaluator::absolutePathValues(
const QString &variable, const QString &baseDirectory) const
{
@@ -124,25 +127,24 @@ QStringList ProFileEvaluator::absoluteFileValues(
foreach (const QString &el, pro ? values(variable, pro) : values(variable)) {
QString absEl;
if (IoUtils::isAbsolutePath(el)) {
if (IoUtils::exists(el)) {
if (m_vfs->exists(el)) {
result << el;
goto next;
}
absEl = el;
} else {
foreach (const QString &dir, searchDirs) {
QString fn = dir + QLatin1Char('/') + el;
if (IoUtils::exists(fn)) {
result << QDir::cleanPath(fn);
QString fn = QDir::cleanPath(dir + QLatin1Char('/') + el);
if (m_vfs->exists(fn)) {
result << fn;
goto next;
}
}
if (baseDirectory.isEmpty())
goto next;
absEl = baseDirectory + QLatin1Char('/') + el;
absEl = QDir::cleanPath(baseDirectory + QLatin1Char('/') + el);
}
{
absEl = QDir::cleanPath(absEl);
int nameOff = absEl.lastIndexOf(QLatin1Char('/'));
QString absDir = d->m_tmp1.setRawData(absEl.constData(), nameOff);
if (IoUtils::exists(absDir)) {

View File

@@ -86,6 +86,7 @@ public:
private:
QMakeEvaluator *d;
QMakeVfs *m_vfs;
};
QT_END_NAMESPACE

View File

@@ -97,6 +97,23 @@ bool QMakeVfs::writeFile(const QString &fn, QIODevice::OpenMode mode, bool exe,
#endif
}
#ifndef PROEVALUATOR_FULL
bool QMakeVfs::readVirtualFile(const QString &fn, QString *contents)
{
# ifdef PROEVALUATOR_THREAD_SAFE
QMutexLocker locker(&m_mutex);
# endif
QHash<QString, QString>::ConstIterator it = m_files.constFind(fn);
if (it != m_files.constEnd()
&& it->constData() != m_magicMissing.constData()
&& it->constData() != m_magicExisting.constData()) {
*contents = *it;
return true;
}
return false;
}
#endif
bool QMakeVfs::readFile(const QString &fn, QString *contents, QString *errStr)
{
#ifndef PROEVALUATOR_FULL

View File

@@ -48,6 +48,8 @@ public:
bool exists(const QString &fn);
#ifndef PROEVALUATOR_FULL
bool readVirtualFile(const QString &fn, QString *contents);
void invalidateCache();
void invalidateContents();
#endif

View File

@@ -82,7 +82,7 @@ QStringList tst_QrcParser::allPaths(QrcParser::ConstPtr p)
void tst_QrcParser::firstAtTest()
{
QFETCH(QString, path);
QrcParser::Ptr p = QrcParser::parseQrcFile(path);
QrcParser::Ptr p = QrcParser::parseQrcFile(path, QString());
foreach (const QString &qrcPath, allPaths(p)) {
QString s1 = p->firstFileAtPath(qrcPath, m_locale);
if (s1.isEmpty())
@@ -99,7 +99,7 @@ void tst_QrcParser::firstAtTest()
void tst_QrcParser::firstInTest()
{
QFETCH(QString, path);
QrcParser::Ptr p = QrcParser::parseQrcFile(path);
QrcParser::Ptr p = QrcParser::parseQrcFile(path, QString());
foreach (const QString &qrcPath, allPaths(p)) {
if (!qrcPath.endsWith(QLatin1Char('/')))
continue;
@@ -137,15 +137,15 @@ void tst_QrcParser::cacheTest()
{
QFETCH(QString, path);
QVERIFY(m_cache.parsedPath(path).isNull());
QrcParser::ConstPtr p0 = m_cache.addPath(path);
QrcParser::ConstPtr p0 = m_cache.addPath(path, QString());
QVERIFY(!p0.isNull());
QrcParser::ConstPtr p1 = m_cache.parsedPath(path);
QVERIFY(p1.data() == p0.data());
QrcParser::ConstPtr p2 = m_cache.addPath(path);
QrcParser::ConstPtr p2 = m_cache.addPath(path, QString());
QVERIFY(p2.data() == p1.data());
QrcParser::ConstPtr p3 = m_cache.parsedPath(path);
QVERIFY(p3.data() == p2.data());
QrcParser::ConstPtr p4 = m_cache.updatePath(path);
QrcParser::ConstPtr p4 = m_cache.updatePath(path, QString());
QVERIFY(p4.data() != p3.data());
QrcParser::ConstPtr p5 = m_cache.parsedPath(path);
QVERIFY(p5.data() == p4.data());
@@ -159,7 +159,7 @@ void tst_QrcParser::cacheTest()
void tst_QrcParser::simpleTest()
{
QrcParser::Ptr p = QrcParser::parseQrcFile(QString::fromLatin1(TESTSRCDIR).append(QLatin1String("/simple.qrc")));
QrcParser::Ptr p = QrcParser::parseQrcFile(QString::fromLatin1(TESTSRCDIR).append(QLatin1String("/simple.qrc")), QString());
QStringList paths = allPaths(p);
paths.sort();
QVERIFY(paths == QStringList()