Qbs, Qmake, Python, Generic: Introduce BuildSystem derived classes

... and move context menu action handling there.

This is a temporary measure to be able to move that functionality
alongside the actual BuildSystem to the BuildConfiguration.

There is a lot to be cleaned up left, to keep the patch small.

Change-Id: If4b0820a13b376fc97b70785052924972ce22705
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
hjk
2019-10-22 14:55:51 +02:00
parent e1baae2e20
commit 66c7d75dbd
24 changed files with 707 additions and 544 deletions

View File

@@ -67,6 +67,7 @@ QmlProject::QmlProject(const Utils::FilePath &fileName)
setDisplayName(fileName.toFileInfo().completeBaseName());
setNeedsBuildConfigurations(false);
setBuildSystem(std::make_unique<Internal::QmlBuildSystem>(this));
connect(this, &QmlProject::projectFileIsDirty, this, &QmlProject::refreshProjectFile);
}
@@ -398,5 +399,6 @@ void QmlProject::updateDeploymentData(ProjectExplorer::Target *target)
target->setDeploymentData(deploymentData);
}
} // namespace QmlProjectManager

View File

@@ -28,6 +28,7 @@
#include "qmlprojectmanager_global.h"
#include "qmlprojectnodes.h"
#include <projectexplorer/buildsystem.h>
#include <projectexplorer/project.h>
#include <utils/environment.h>
@@ -38,8 +39,31 @@ namespace ProjectExplorer { class RunConfiguration; }
namespace QmlProjectManager {
class QmlProject;
class QmlProjectItem;
namespace Internal {
class QmlBuildSystem : public ProjectExplorer::BuildSystem
{
public:
explicit QmlBuildSystem(ProjectExplorer::Project *project) : BuildSystem(project) {}
bool supportsAction(ProjectExplorer::Node *context,
ProjectExplorer::ProjectAction action,
const ProjectExplorer::Node *node) const override;
bool addFiles(ProjectExplorer::Node *context,
const QStringList &filePaths, QStringList *notAdded = nullptr) override;
bool deleteFiles(ProjectExplorer::Node *context,
const QStringList &filePaths) override;
bool renameFile(ProjectExplorer::Node *context,
const QString &filePath, const QString &newFilePath) override;
QmlProject *project() const;
};
} // Internal
class QMLPROJECTMANAGER_EXPORT QmlProject : public ProjectExplorer::Project
{
Q_OBJECT

View File

@@ -53,71 +53,90 @@ QmlProjectNode::QmlProjectNode(QmlProject *project) : ProjectNode(project->proje
setIcon(qmlProjectIcon);
}
bool QmlProjectNode::supportsAction(ProjectAction action, const Node *node) const
bool QmlBuildSystem::supportsAction(Node *context, ProjectAction action, const Node *node) const
{
if (action == AddNewFile || action == EraseFile)
if (dynamic_cast<QmlProjectNode *>(context)) {
if (action == AddNewFile || action == EraseFile)
return true;
QTC_ASSERT(node, return false);
if (action == Rename && node->asFileNode()) {
const FileNode *fileNode = node->asFileNode();
QTC_ASSERT(fileNode, return false);
return fileNode->fileType() != FileType::Project;
}
return false;
}
return BuildSystem::supportsAction(context, action, node);
}
QmlProject *QmlBuildSystem::project() const
{
return static_cast<QmlProject *>(BuildSystem::project());
}
bool QmlBuildSystem::addFiles(Node *context, const QStringList &filePaths, QStringList *notAdded)
{
if (dynamic_cast<QmlProjectNode *>(context))
return project()->addFiles(filePaths);
return BuildSystem::addFiles(context, filePaths, notAdded);
}
bool QmlBuildSystem::deleteFiles(Node *context, const QStringList &filePaths)
{
if (dynamic_cast<QmlProjectNode *>(context))
return true;
QTC_ASSERT(node, return false);
if (action == Rename && node->asFileNode()) {
const FileNode *fileNode = node->asFileNode();
QTC_ASSERT(fileNode, return false);
return fileNode->fileType() != FileType::Project;
}
return false;
return BuildSystem::deleteFiles(context, filePaths);
}
bool QmlProjectNode::addFiles(const QStringList &filePaths, QStringList * /*notAdded*/)
bool QmlBuildSystem::renameFile(Node * context, const QString &filePath, const QString &newFilePath)
{
return m_project->addFiles(filePaths);
}
if (dynamic_cast<QmlProjectNode *>(context)) {
if (filePath.endsWith(project()->mainFile())) {
project()->setMainFile(newFilePath);
bool QmlProjectNode::deleteFiles(const QStringList & /*filePaths*/)
{
return true;
}
// make sure to change it also in the qmlproject file
const QString qmlProjectFilePath = project()->projectFilePath().toString();
Core::FileChangeBlocker fileChangeBlocker(qmlProjectFilePath);
const QList<Core::IEditor *> editors = Core::DocumentModel::editorsForFilePath(qmlProjectFilePath);
TextEditor::TextDocument *document = nullptr;
if (!editors.isEmpty()) {
document = qobject_cast<TextEditor::TextDocument*>(editors.first()->document());
if (document && document->isModified())
if (!Core::DocumentManager::saveDocument(document))
return false;
}
bool QmlProjectNode::renameFile(const QString & filePath, const QString & newFilePath)
{
if (filePath.endsWith(m_project->mainFile())) {
m_project->setMainFile(newFilePath);
QString fileContent;
QString error;
Utils::TextFileFormat textFileFormat;
const QTextCodec *codec = QTextCodec::codecForName("UTF-8"); // qml files are defined to be utf-8
if (Utils::TextFileFormat::readFile(qmlProjectFilePath, codec, &fileContent, &textFileFormat, &error)
!= Utils::TextFileFormat::ReadSuccess) {
qWarning() << "Failed to read file" << qmlProjectFilePath << ":" << error;
}
// make sure to change it also in the qmlproject file
const QString qmlProjectFilePath = m_project->projectFilePath().toString();
Core::FileChangeBlocker fileChangeBlocker(qmlProjectFilePath);
const QList<Core::IEditor *> editors = Core::DocumentModel::editorsForFilePath(qmlProjectFilePath);
TextEditor::TextDocument *document = nullptr;
if (!editors.isEmpty()) {
document = qobject_cast<TextEditor::TextDocument*>(editors.first()->document());
if (document && document->isModified())
if (!Core::DocumentManager::saveDocument(document))
return false;
// find the mainFile and do the file name with brackets in a capture group and mask the . with \.
QString originalFileName = QFileInfo(filePath).fileName();
originalFileName.replace(".", "\\.");
const QRegularExpression expression(QString("mainFile:\\s*\"(%1)\"").arg(originalFileName));
const QRegularExpressionMatch match = expression.match(fileContent);
fileContent.replace(match.capturedStart(1), match.capturedLength(1), QFileInfo(newFilePath).fileName());
if (!textFileFormat.writeFile(qmlProjectFilePath, fileContent, &error))
qWarning() << "Failed to write file" << qmlProjectFilePath << ":" << error;
project()->refresh(QmlProject::Everything);
}
QString fileContent;
QString error;
Utils::TextFileFormat textFileFormat;
const QTextCodec *codec = QTextCodec::codecForName("UTF-8"); // qml files are defined to be utf-8
if (Utils::TextFileFormat::readFile(qmlProjectFilePath, codec, &fileContent, &textFileFormat, &error)
!= Utils::TextFileFormat::ReadSuccess) {
qWarning() << "Failed to read file" << qmlProjectFilePath << ":" << error;
}
// find the mainFile and do the file name with brackets in a capture group and mask the . with \.
QString originalFileName = QFileInfo(filePath).fileName();
originalFileName.replace(".", "\\.");
const QRegularExpression expression(QString("mainFile:\\s*\"(%1)\"").arg(originalFileName));
const QRegularExpressionMatch match = expression.match(fileContent);
fileContent.replace(match.capturedStart(1), match.capturedLength(1), QFileInfo(newFilePath).fileName());
if (!textFileFormat.writeFile(qmlProjectFilePath, fileContent, &error))
qWarning() << "Failed to write file" << qmlProjectFilePath << ":" << error;
m_project->refresh(QmlProject::Everything);
return true;
}
return true;
return BuildSystem::renameFile(context, filePath, newFilePath);
}
} // namespace Internal

View File

@@ -38,11 +38,6 @@ class QmlProjectNode : public ProjectExplorer::ProjectNode
public:
QmlProjectNode(QmlProject *project);
bool supportsAction(ProjectExplorer::ProjectAction action, const Node *node) const override;
bool addFiles(const QStringList &filePaths, QStringList *notAdded = nullptr) override;
bool deleteFiles(const QStringList &filePaths) override;
bool renameFile(const QString &filePath, const QString &newFilePath) override;
private:
QmlProject *m_project;
};