forked from qt-creator/qt-creator
CMake: Clean up API of FileApiParser
Clean up the API of FileApiParser in preparation of moving the FileSystemWatcher out of that class and into FileApiReader. Change-Id: I5618252dd98a39f6a70f56b50fb50327e8b43e84 Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
@@ -47,10 +47,17 @@ const char CMAKE_RELATIVE_QUERY_PATH[] = ".cmake/api/v1/query";
|
||||
|
||||
static Q_LOGGING_CATEGORY(cmakeFileApi, "qtc.cmake.fileApi", QtWarningMsg);
|
||||
|
||||
const QStringList CMAKE_QUERY_FILENAMES = {"cache-v2", "codemodel-v2", "cmakeFiles-v1"};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// Helper:
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
static FilePath cmakeReplyDirectory(const FilePath &buildDirectory)
|
||||
{
|
||||
return buildDirectory.pathAppended(CMAKE_RELATIVE_REPLY_PATH);
|
||||
}
|
||||
|
||||
static void reportFileApiSetupFailure()
|
||||
{
|
||||
Core::MessageManager::write(QCoreApplication::translate(
|
||||
@@ -499,7 +506,7 @@ static std::vector<Configuration> readCodemodelFile(const QString &codemodelFile
|
||||
std::vector<FileApiDetails::FragmentInfo> extractFragments(const QJsonObject &obj)
|
||||
{
|
||||
const QJsonArray fragments = obj.value("commandFragments").toArray();
|
||||
return Utils::transform<std::vector>(fragments, [](const QJsonValue &v) {
|
||||
return transform<std::vector>(fragments, [](const QJsonValue &v) {
|
||||
const QJsonObject o = v.toObject();
|
||||
return FileApiDetails::FragmentInfo{o.value("fragment").toString(),
|
||||
o.value("role").toString()};
|
||||
@@ -807,49 +814,38 @@ QString FileApiDetails::ReplyFileContents::jsonFile(const QString &kind, const Q
|
||||
// FileApi:
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
FileApiParser::FileApiParser(const FilePath &sourceDirectory, const FilePath &buildDirectory)
|
||||
: m_sourceDirectory(sourceDirectory)
|
||||
, m_buildDirectory(buildDirectory)
|
||||
FileApiParser::FileApiParser(const FilePath &buildDirectory) : m_buildDirectory(buildDirectory)
|
||||
{
|
||||
setupCMakeFileApi();
|
||||
setupCMakeFileApi(buildDirectory, m_watcher);
|
||||
|
||||
QObject::connect(&m_watcher,
|
||||
&FileSystemWatcher::directoryChanged,
|
||||
this,
|
||||
&FileApiParser::replyDirectoryHasChanged);
|
||||
|
||||
m_watcher.addDirectory(cmakeReplyDirectory().toString(), FileSystemWatcher::WatchAllChanges);
|
||||
}
|
||||
|
||||
FilePath FileApiParser::cmakeReplyDirectory() const
|
||||
{
|
||||
return m_buildDirectory.pathAppended(CMAKE_RELATIVE_REPLY_PATH);
|
||||
}
|
||||
|
||||
FileApiParser::~FileApiParser() = default;
|
||||
|
||||
void FileApiParser::setupCMakeFileApi() const
|
||||
bool FileApiParser::setupCMakeFileApi(const FilePath &buildDirectory, Utils::FileSystemWatcher &watcher)
|
||||
{
|
||||
const QDir buildDir = QDir(m_buildDirectory.toString());
|
||||
const QString relativeQueryPath = QString::fromLatin1(CMAKE_RELATIVE_QUERY_PATH);
|
||||
|
||||
buildDir.mkpath(relativeQueryPath);
|
||||
const QDir buildDir = QDir(buildDirectory.toString());
|
||||
buildDir.mkpath(
|
||||
QString::fromLatin1(CMAKE_RELATIVE_REPLY_PATH)); // So that we have a directory to watch!
|
||||
|
||||
const QString relativeQueryPath = QString::fromLatin1(CMAKE_RELATIVE_QUERY_PATH);
|
||||
buildDir.mkpath(relativeQueryPath);
|
||||
|
||||
QDir queryDir = buildDir;
|
||||
queryDir.cd(relativeQueryPath);
|
||||
|
||||
if (!queryDir.exists()) {
|
||||
reportFileApiSetupFailure();
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
QTC_ASSERT(queryDir.exists(), );
|
||||
|
||||
bool failedBefore = false;
|
||||
for (const QString &fileName : cmakeQueryFileNames()) {
|
||||
const QString filePath = queryDir.filePath(fileName);
|
||||
|
||||
for (const QString &filePath : cmakeQueryFilePaths(buildDirectory)) {
|
||||
QFile f(filePath);
|
||||
if (!f.exists()) {
|
||||
f.open(QFile::WriteOnly);
|
||||
@@ -861,6 +857,9 @@ void FileApiParser::setupCMakeFileApi() const
|
||||
reportFileApiSetupFailure();
|
||||
}
|
||||
}
|
||||
|
||||
watcher.addDirectory(cmakeReplyDirectory(buildDirectory).toString(), FileSystemWatcher::WatchAllChanges);
|
||||
return true;
|
||||
}
|
||||
|
||||
static QStringList uniqueTargetFiles(const std::vector<Configuration> &configs)
|
||||
@@ -910,9 +909,9 @@ FileApiData FileApiParser::parseData(const QFileInfo &replyFileInfo, QString &er
|
||||
return result;
|
||||
}
|
||||
|
||||
QFileInfo FileApiParser::scanForCMakeReplyFile() const
|
||||
QFileInfo FileApiParser::scanForCMakeReplyFile(const FilePath &buildDirectory)
|
||||
{
|
||||
QDir replyDir(cmakeReplyDirectory().toString());
|
||||
QDir replyDir(cmakeReplyDirectory(buildDirectory).toString());
|
||||
if (!replyDir.exists())
|
||||
return {};
|
||||
|
||||
@@ -922,16 +921,10 @@ QFileInfo FileApiParser::scanForCMakeReplyFile() const
|
||||
return fis.isEmpty() ? QFileInfo() : fis.last();
|
||||
}
|
||||
|
||||
QStringList FileApiParser::cmakeQueryFileNames() const
|
||||
QStringList FileApiParser::cmakeQueryFilePaths(const Utils::FilePath &buildDirectory)
|
||||
{
|
||||
return {"cache-v2", "codemodel-v2", "cmakeFiles-v1"};
|
||||
}
|
||||
|
||||
QStringList FileApiParser::cmakeQueryFilePaths() const
|
||||
{
|
||||
QDir queryDir(QDir::cleanPath(m_sourceDirectory.toString() + "/"
|
||||
+ QString::fromLatin1(CMAKE_RELATIVE_QUERY_PATH)));
|
||||
return transform(cmakeQueryFileNames(),
|
||||
QDir queryDir(QDir::cleanPath(buildDirectory.pathAppended(CMAKE_RELATIVE_QUERY_PATH).toString()));
|
||||
return transform(CMAKE_QUERY_FILENAMES,
|
||||
[&queryDir](const QString &name) { return queryDir.absoluteFilePath(name); });
|
||||
}
|
||||
|
||||
@@ -942,8 +935,8 @@ void FileApiParser::setParsedReplyFilePath(const QString &filePath)
|
||||
|
||||
void FileApiParser::replyDirectoryHasChanged(const QString &directory) const
|
||||
{
|
||||
if (directory == cmakeReplyDirectory().toString()) {
|
||||
QFileInfo fi = scanForCMakeReplyFile();
|
||||
if (directory == cmakeReplyDirectory(m_buildDirectory).toString()) {
|
||||
QFileInfo fi = scanForCMakeReplyFile(m_buildDirectory);
|
||||
if (fi.isFile() && fi.filePath() != m_lastParsedReplyFile) {
|
||||
emit dirty();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user