2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2019 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2019-07-22 14:39:01 +02:00
|
|
|
|
|
|
|
|
#include "pythonproject.h"
|
|
|
|
|
|
|
|
|
|
#include "pythonconstants.h"
|
2022-07-15 11:49:34 +02:00
|
|
|
#include "pythontr.h"
|
2019-07-22 14:39:01 +02:00
|
|
|
|
2019-10-22 14:55:51 +02:00
|
|
|
#include <projectexplorer/buildsystem.h>
|
2019-07-22 14:39:01 +02:00
|
|
|
#include <projectexplorer/buildtargetinfo.h>
|
|
|
|
|
#include <projectexplorer/kitmanager.h>
|
|
|
|
|
#include <projectexplorer/projectexplorerconstants.h>
|
|
|
|
|
#include <projectexplorer/projectnodes.h>
|
|
|
|
|
#include <projectexplorer/target.h>
|
|
|
|
|
|
|
|
|
|
#include <QJsonArray>
|
|
|
|
|
#include <QJsonDocument>
|
|
|
|
|
#include <QJsonObject>
|
|
|
|
|
#include <QProcessEnvironment>
|
|
|
|
|
#include <QRegularExpression>
|
2019-10-25 09:55:32 +02:00
|
|
|
#include <QTimer>
|
2019-07-22 14:39:01 +02:00
|
|
|
|
|
|
|
|
#include <coreplugin/documentmanager.h>
|
|
|
|
|
#include <coreplugin/icontext.h>
|
|
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
|
#include <coreplugin/messagemanager.h>
|
|
|
|
|
|
2020-09-21 19:39:24 +03:00
|
|
|
#include <qmljs/qmljsmodelmanagerinterface.h>
|
|
|
|
|
|
2022-05-17 14:59:01 +02:00
|
|
|
#include <utils/algorithm.h>
|
2019-07-22 14:39:01 +02:00
|
|
|
#include <utils/fileutils.h>
|
2022-06-22 15:43:33 +02:00
|
|
|
#include <utils/mimeutils.h>
|
2019-07-22 14:39:01 +02:00
|
|
|
|
|
|
|
|
using namespace Core;
|
|
|
|
|
using namespace ProjectExplorer;
|
|
|
|
|
using namespace Utils;
|
|
|
|
|
|
2022-07-15 11:49:34 +02:00
|
|
|
namespace Python::Internal {
|
2019-07-22 14:39:01 +02:00
|
|
|
|
2019-10-22 14:55:51 +02:00
|
|
|
class PythonBuildSystem : public BuildSystem
|
2019-07-22 14:39:01 +02:00
|
|
|
{
|
|
|
|
|
public:
|
2019-10-25 09:55:32 +02:00
|
|
|
explicit PythonBuildSystem(Target *target);
|
2019-10-22 14:55:51 +02:00
|
|
|
|
|
|
|
|
bool supportsAction(Node *context, ProjectAction action, const Node *node) const override;
|
2022-07-15 11:49:34 +02:00
|
|
|
bool addFiles(Node *, const FilePaths &filePaths, FilePaths *) override;
|
|
|
|
|
RemovedFilesFromProject removeFiles(Node *, const FilePaths &filePaths, FilePaths *) override;
|
|
|
|
|
bool deleteFiles(Node *, const FilePaths &) override;
|
2021-06-11 14:34:34 +02:00
|
|
|
bool renameFile(Node *,
|
2022-07-15 11:49:34 +02:00
|
|
|
const FilePath &oldFilePath,
|
|
|
|
|
const FilePath &newFilePath) override;
|
2021-09-09 11:28:28 +02:00
|
|
|
QString name() const override { return QLatin1String("python"); }
|
2019-10-22 14:55:51 +02:00
|
|
|
|
|
|
|
|
void parse();
|
2023-06-06 12:53:58 +02:00
|
|
|
bool save();
|
2019-07-22 14:39:01 +02:00
|
|
|
|
2023-06-06 09:23:14 +02:00
|
|
|
bool writePyProjectFile(const FilePath &filePath, QString &content,
|
2019-10-22 14:55:51 +02:00
|
|
|
const QStringList &rawList, QString *errorMessage);
|
|
|
|
|
|
2019-10-25 09:55:32 +02:00
|
|
|
void triggerParsing() final;
|
2019-07-22 14:39:01 +02:00
|
|
|
|
|
|
|
|
private:
|
2023-06-06 12:53:58 +02:00
|
|
|
struct FileEntry {
|
|
|
|
|
QString rawEntry;
|
|
|
|
|
FilePath filePath;
|
|
|
|
|
};
|
|
|
|
|
QList<FileEntry> processEntries(const QStringList &paths) const;
|
|
|
|
|
|
|
|
|
|
QList<FileEntry> m_files;
|
|
|
|
|
QList<FileEntry> m_qmlImportPaths;
|
2019-07-22 14:39:01 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Provides displayName relative to project node
|
|
|
|
|
*/
|
|
|
|
|
class PythonFileNode : public FileNode
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
PythonFileNode(const FilePath &filePath, const QString &nodeDisplayName,
|
|
|
|
|
FileType fileType = FileType::Source)
|
|
|
|
|
: FileNode(filePath, fileType)
|
|
|
|
|
, m_displayName(nodeDisplayName)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
QString displayName() const override { return m_displayName; }
|
|
|
|
|
private:
|
|
|
|
|
QString m_displayName;
|
|
|
|
|
};
|
|
|
|
|
|
2020-09-21 19:39:24 +03:00
|
|
|
static QJsonObject readObjJson(const FilePath &projectFile, QString *errorMessage)
|
|
|
|
|
{
|
2023-06-06 09:34:24 +02:00
|
|
|
const expected_str<QByteArray> fileContentsResult = projectFile.fileContents();
|
|
|
|
|
if (!fileContentsResult) {
|
|
|
|
|
*errorMessage = fileContentsResult.error();
|
|
|
|
|
return {};
|
2020-09-21 19:39:24 +03:00
|
|
|
}
|
|
|
|
|
|
2023-06-06 09:34:24 +02:00
|
|
|
const QByteArray content = *fileContentsResult;
|
2020-09-21 19:39:24 +03:00
|
|
|
|
|
|
|
|
// This assumes the project file is formed with only one field called
|
|
|
|
|
// 'files' that has a list associated of the files to include in the project.
|
|
|
|
|
if (content.isEmpty()) {
|
2022-07-15 11:49:34 +02:00
|
|
|
*errorMessage = Tr::tr("Unable to read \"%1\": The file is empty.")
|
2020-09-21 19:39:24 +03:00
|
|
|
.arg(projectFile.toUserOutput());
|
|
|
|
|
return QJsonObject();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QJsonParseError error;
|
|
|
|
|
const QJsonDocument doc = QJsonDocument::fromJson(content, &error);
|
|
|
|
|
if (doc.isNull()) {
|
|
|
|
|
const int line = content.left(error.offset).count('\n') + 1;
|
2022-07-15 11:49:34 +02:00
|
|
|
*errorMessage = Tr::tr("Unable to parse \"%1\":%2: %3")
|
2020-09-21 19:39:24 +03:00
|
|
|
.arg(projectFile.toUserOutput()).arg(line)
|
|
|
|
|
.arg(error.errorString());
|
|
|
|
|
return QJsonObject();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return doc.object();
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-22 14:39:01 +02:00
|
|
|
static QStringList readLines(const FilePath &projectFile)
|
|
|
|
|
{
|
2023-06-12 07:26:21 +02:00
|
|
|
QSet<QString> visited;
|
|
|
|
|
QStringList lines;
|
2019-07-22 14:39:01 +02:00
|
|
|
|
2023-06-06 13:11:46 +02:00
|
|
|
const expected_str<QByteArray> contents = projectFile.fileContents();
|
|
|
|
|
if (contents) {
|
|
|
|
|
QTextStream stream(contents.value());
|
2019-07-22 14:39:01 +02:00
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
const QString line = stream.readLine();
|
|
|
|
|
if (line.isNull())
|
|
|
|
|
break;
|
|
|
|
|
if (visited.contains(line))
|
|
|
|
|
continue;
|
|
|
|
|
lines.append(line);
|
|
|
|
|
visited.insert(line);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return lines;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static QStringList readLinesJson(const FilePath &projectFile, QString *errorMessage)
|
|
|
|
|
{
|
2023-06-12 07:26:21 +02:00
|
|
|
QSet<QString> visited;
|
|
|
|
|
QStringList lines;
|
2019-07-22 14:39:01 +02:00
|
|
|
|
2020-09-21 19:39:24 +03:00
|
|
|
const QJsonObject obj = readObjJson(projectFile, errorMessage);
|
2023-06-06 13:11:46 +02:00
|
|
|
for (const QJsonValue &file : obj.value("files").toArray()) {
|
|
|
|
|
const QString fileName = file.toString();
|
|
|
|
|
if (visited.contains(fileName))
|
|
|
|
|
continue;
|
|
|
|
|
lines.append(fileName);
|
|
|
|
|
visited.insert(fileName);
|
2019-07-22 14:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return lines;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-21 19:39:24 +03:00
|
|
|
static QStringList readImportPathsJson(const FilePath &projectFile, QString *errorMessage)
|
|
|
|
|
{
|
|
|
|
|
QStringList importPaths;
|
|
|
|
|
|
|
|
|
|
const QJsonObject obj = readObjJson(projectFile, errorMessage);
|
|
|
|
|
if (obj.contains("qmlImportPaths")) {
|
|
|
|
|
const QJsonValue dirs = obj.value("qmlImportPaths");
|
|
|
|
|
const QJsonArray dirs_array = dirs.toArray();
|
|
|
|
|
|
|
|
|
|
QSet<QString> visited;
|
|
|
|
|
|
|
|
|
|
for (const auto &dir : dirs_array)
|
|
|
|
|
visited.insert(dir.toString());
|
|
|
|
|
|
|
|
|
|
importPaths.append(Utils::toList(visited));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return importPaths;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-22 14:55:51 +02:00
|
|
|
class PythonProjectNode : public ProjectNode
|
|
|
|
|
{
|
|
|
|
|
public:
|
2022-07-15 11:49:34 +02:00
|
|
|
PythonProjectNode(const FilePath &path)
|
2019-10-22 14:55:51 +02:00
|
|
|
: ProjectNode(path)
|
|
|
|
|
{
|
2021-06-04 07:59:00 +02:00
|
|
|
setDisplayName(path.completeBaseName());
|
2019-10-22 14:55:51 +02:00
|
|
|
setAddFileFilter("*.py");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-07-22 14:39:01 +02:00
|
|
|
PythonProject::PythonProject(const FilePath &fileName)
|
|
|
|
|
: Project(Constants::C_PY_MIMETYPE, fileName)
|
|
|
|
|
{
|
|
|
|
|
setId(PythonProjectId);
|
2020-12-02 13:33:59 +01:00
|
|
|
setProjectLanguages(Context(ProjectExplorer::Constants::PYTHON_LANGUAGE_ID));
|
2021-06-04 07:59:00 +02:00
|
|
|
setDisplayName(fileName.completeBaseName());
|
2019-07-22 14:39:01 +02:00
|
|
|
|
2019-10-25 09:55:32 +02:00
|
|
|
setBuildSystemCreator([](Target *t) { return new PythonBuildSystem(t); });
|
2019-07-22 14:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
2019-12-06 13:04:17 +02:00
|
|
|
static FileType getFileType(const FilePath &f)
|
2019-11-27 15:37:59 +01:00
|
|
|
{
|
2019-12-06 13:04:17 +02:00
|
|
|
if (f.endsWith(".py"))
|
|
|
|
|
return FileType::Source;
|
|
|
|
|
if (f.endsWith(".qrc"))
|
2019-11-27 15:37:59 +01:00
|
|
|
return FileType::Resource;
|
2019-12-06 13:04:17 +02:00
|
|
|
if (f.endsWith(".ui"))
|
2019-11-27 15:37:59 +01:00
|
|
|
return FileType::Form;
|
2019-12-06 13:04:17 +02:00
|
|
|
if (f.endsWith(".qml") || f.endsWith(".js"))
|
2019-11-27 15:37:59 +01:00
|
|
|
return FileType::QML;
|
2019-12-06 13:04:17 +02:00
|
|
|
return Node::fileTypeForFileName(f);
|
2019-11-27 15:37:59 +01:00
|
|
|
}
|
|
|
|
|
|
2019-10-25 09:55:32 +02:00
|
|
|
void PythonBuildSystem::triggerParsing()
|
2019-07-22 14:39:01 +02:00
|
|
|
{
|
2019-10-25 09:55:32 +02:00
|
|
|
ParseGuard guard = guardParsingRun();
|
2019-10-22 14:55:51 +02:00
|
|
|
parse();
|
2019-07-22 14:39:01 +02:00
|
|
|
|
|
|
|
|
QList<BuildTargetInfo> appTargets;
|
2019-10-22 14:55:51 +02:00
|
|
|
|
|
|
|
|
auto newRoot = std::make_unique<PythonProjectNode>(projectDirectory());
|
2023-06-12 07:26:21 +02:00
|
|
|
|
|
|
|
|
const FilePath projectFile = projectFilePath();
|
|
|
|
|
const QString displayName = projectFile.relativePathFrom(projectDirectory()).toUserOutput();
|
|
|
|
|
newRoot->addNestedNode(
|
|
|
|
|
std::make_unique<PythonFileNode>(projectFile, displayName, FileType::Project));
|
|
|
|
|
|
|
|
|
|
for (const FileEntry &entry : std::as_const(m_files)) {
|
2023-06-06 12:53:58 +02:00
|
|
|
const QString displayName = entry.filePath.relativePathFrom(projectDirectory()).toUserOutput();
|
|
|
|
|
const FileType fileType = getFileType(entry.filePath);
|
2019-11-14 15:43:05 +01:00
|
|
|
|
2023-06-06 12:53:58 +02:00
|
|
|
newRoot->addNestedNode(std::make_unique<PythonFileNode>(entry.filePath, displayName, fileType));
|
|
|
|
|
const MimeType mt = mimeTypeForFile(entry.filePath, MimeMatchMode::MatchExtension);
|
2022-06-22 14:15:35 +02:00
|
|
|
if (mt.matchesName(Constants::C_PY_MIMETYPE) || mt.matchesName(Constants::C_PY3_MIMETYPE)) {
|
2019-07-22 14:39:01 +02:00
|
|
|
BuildTargetInfo bti;
|
2022-03-18 07:09:59 +01:00
|
|
|
bti.displayName = displayName;
|
2023-06-06 12:53:58 +02:00
|
|
|
bti.buildKey = entry.filePath.toString();
|
|
|
|
|
bti.targetFilePath = entry.filePath;
|
2023-06-12 07:26:21 +02:00
|
|
|
bti.projectFilePath = projectFile;
|
2023-06-06 12:53:58 +02:00
|
|
|
bti.isQtcRunnable = entry.filePath.fileName() == "main.py";
|
2019-07-22 14:39:01 +02:00
|
|
|
appTargets.append(bti);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-20 15:21:54 +01:00
|
|
|
setRootProjectNode(std::move(newRoot));
|
2019-07-22 14:39:01 +02:00
|
|
|
|
2019-10-25 09:55:32 +02:00
|
|
|
setApplicationTargets(appTargets);
|
2019-07-22 14:39:01 +02:00
|
|
|
|
2020-09-21 19:39:24 +03:00
|
|
|
auto modelManager = QmlJS::ModelManagerInterface::instance();
|
|
|
|
|
if (modelManager) {
|
2022-10-25 08:57:49 +02:00
|
|
|
const auto hiddenRccFolders = project()->files(Project::HiddenRccFolders);
|
|
|
|
|
auto projectInfo = modelManager->defaultProjectInfoForProject(project(), hiddenRccFolders);
|
2020-09-21 19:39:24 +03:00
|
|
|
|
2023-06-06 12:53:58 +02:00
|
|
|
for (const FileEntry &importPath : std::as_const(m_qmlImportPaths)) {
|
|
|
|
|
if (!importPath.filePath.isEmpty())
|
|
|
|
|
projectInfo.importPaths.maybeInsert(importPath.filePath, QmlJS::Dialect::Qml);
|
2020-09-21 19:39:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
modelManager->updateProjectInfo(projectInfo, project());
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-22 14:39:01 +02:00
|
|
|
guard.markAsSuccess();
|
2019-11-15 15:44:45 +01:00
|
|
|
|
|
|
|
|
emitBuildSystemUpdated();
|
2019-07-22 14:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
2023-06-06 12:53:58 +02:00
|
|
|
bool PythonBuildSystem::save()
|
2019-07-22 14:39:01 +02:00
|
|
|
{
|
2023-06-06 12:53:58 +02:00
|
|
|
const FilePath filePath = projectFilePath();
|
|
|
|
|
const QStringList rawList = Utils::transform(m_files, &FileEntry::rawEntry);
|
2023-06-06 09:23:14 +02:00
|
|
|
const FileChangeBlocker changeGuarg(filePath);
|
2019-07-22 14:39:01 +02:00
|
|
|
bool result = false;
|
|
|
|
|
|
2023-06-06 15:12:34 +02:00
|
|
|
QByteArray newContents;
|
|
|
|
|
|
2019-07-22 14:39:01 +02:00
|
|
|
// New project file
|
2023-06-06 09:23:14 +02:00
|
|
|
if (filePath.endsWith(".pyproject")) {
|
2023-06-06 15:12:34 +02:00
|
|
|
expected_str<QByteArray> contents = filePath.fileContents();
|
|
|
|
|
if (contents) {
|
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(*contents);
|
|
|
|
|
QJsonObject project = doc.object();
|
|
|
|
|
project["files"] = QJsonArray::fromStringList(rawList);
|
|
|
|
|
doc.setObject(project);
|
|
|
|
|
newContents = doc.toJson();
|
|
|
|
|
} else {
|
|
|
|
|
MessageManager::writeDisrupting(contents.error());
|
2019-07-22 14:39:01 +02:00
|
|
|
}
|
|
|
|
|
} else { // Old project file
|
2023-06-06 15:12:34 +02:00
|
|
|
newContents = rawList.join('\n').toUtf8();
|
2019-07-22 14:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
2023-06-06 15:12:34 +02:00
|
|
|
const expected_str<qint64> writeResult = filePath.writeFileContents(newContents);
|
|
|
|
|
if (writeResult)
|
|
|
|
|
result = true;
|
|
|
|
|
else
|
|
|
|
|
MessageManager::writeDisrupting(writeResult.error());
|
2019-07-22 14:39:01 +02:00
|
|
|
|
2023-06-06 15:12:34 +02:00
|
|
|
return result;
|
2019-07-22 14:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-14 16:49:42 +02:00
|
|
|
bool PythonBuildSystem::addFiles(Node *, const FilePaths &filePaths, FilePaths *)
|
2019-07-22 14:39:01 +02:00
|
|
|
{
|
2023-06-06 12:53:58 +02:00
|
|
|
const Utils::FilePath projectDir = projectDirectory();
|
2019-07-22 14:39:01 +02:00
|
|
|
|
2023-06-06 12:53:58 +02:00
|
|
|
for (const FilePath &filePath : filePaths) {
|
|
|
|
|
if (!projectDir.isSameDevice(filePath))
|
|
|
|
|
return false;
|
|
|
|
|
m_files.append(FileEntry{filePath.relativePathFrom(projectDir).toString(), filePath});
|
|
|
|
|
}
|
2019-07-22 14:39:01 +02:00
|
|
|
|
2023-06-06 12:53:58 +02:00
|
|
|
return save();
|
2019-07-22 14:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-14 16:49:42 +02:00
|
|
|
RemovedFilesFromProject PythonBuildSystem::removeFiles(Node *, const FilePaths &filePaths, FilePaths *)
|
2019-07-22 14:39:01 +02:00
|
|
|
{
|
|
|
|
|
|
2021-07-14 16:49:42 +02:00
|
|
|
for (const FilePath &filePath : filePaths) {
|
2023-06-06 12:53:58 +02:00
|
|
|
Utils::eraseOne(m_files,
|
|
|
|
|
[filePath](const FileEntry &entry) { return filePath == entry.filePath; });
|
2019-07-22 14:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
2023-06-06 12:53:58 +02:00
|
|
|
return save() ? RemovedFilesFromProject::Ok : RemovedFilesFromProject::Error;
|
2019-07-22 14:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-14 16:49:42 +02:00
|
|
|
bool PythonBuildSystem::deleteFiles(Node *, const FilePaths &)
|
2019-10-22 14:55:51 +02:00
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-11 14:34:34 +02:00
|
|
|
bool PythonBuildSystem::renameFile(Node *, const FilePath &oldFilePath, const FilePath &newFilePath)
|
2019-07-22 14:39:01 +02:00
|
|
|
{
|
2023-06-06 12:53:58 +02:00
|
|
|
for (FileEntry &entry : m_files) {
|
|
|
|
|
if (entry.filePath == oldFilePath) {
|
|
|
|
|
entry.filePath = newFilePath;
|
|
|
|
|
entry.rawEntry = newFilePath.relativeChildPath(projectDirectory()).toString();
|
|
|
|
|
break;
|
2019-07-22 14:39:01 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-06 12:53:58 +02:00
|
|
|
return save();
|
2019-07-22 14:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
2019-10-22 14:55:51 +02:00
|
|
|
void PythonBuildSystem::parse()
|
2019-07-22 14:39:01 +02:00
|
|
|
{
|
2023-06-06 12:53:58 +02:00
|
|
|
m_files.clear();
|
|
|
|
|
m_qmlImportPaths.clear();
|
|
|
|
|
|
|
|
|
|
QStringList files;
|
|
|
|
|
QStringList qmlImportPaths;
|
2020-09-21 19:39:24 +03:00
|
|
|
|
2019-07-22 14:39:01 +02:00
|
|
|
const FilePath filePath = projectFilePath();
|
|
|
|
|
// The PySide project file is JSON based
|
|
|
|
|
if (filePath.endsWith(".pyproject")) {
|
|
|
|
|
QString errorMessage;
|
2023-06-06 12:53:58 +02:00
|
|
|
files = readLinesJson(filePath, &errorMessage);
|
2019-07-22 14:39:01 +02:00
|
|
|
if (!errorMessage.isEmpty())
|
2020-12-15 12:34:07 +01:00
|
|
|
MessageManager::writeFlashing(errorMessage);
|
2020-09-21 19:39:24 +03:00
|
|
|
|
|
|
|
|
errorMessage.clear();
|
2023-06-06 12:53:58 +02:00
|
|
|
qmlImportPaths = readImportPathsJson(filePath, &errorMessage);
|
2020-09-21 19:39:24 +03:00
|
|
|
if (!errorMessage.isEmpty())
|
2020-12-15 12:34:07 +01:00
|
|
|
MessageManager::writeFlashing(errorMessage);
|
2020-09-21 19:39:24 +03:00
|
|
|
} else if (filePath.endsWith(".pyqtc")) {
|
|
|
|
|
// To keep compatibility with PyQt we keep the compatibility with plain
|
|
|
|
|
// text files as project files.
|
2023-06-06 12:53:58 +02:00
|
|
|
files = readLines(filePath);
|
2020-09-21 19:39:24 +03:00
|
|
|
}
|
2019-07-22 14:39:01 +02:00
|
|
|
|
2023-06-06 12:53:58 +02:00
|
|
|
m_files = processEntries(files);
|
|
|
|
|
m_qmlImportPaths = processEntries(qmlImportPaths);
|
2019-07-22 14:39:01 +02:00
|
|
|
}
|
2019-10-22 14:55:51 +02:00
|
|
|
|
2019-07-22 14:39:01 +02:00
|
|
|
/**
|
|
|
|
|
* Expands environment variables in the given \a string when they are written
|
|
|
|
|
* like $$(VARIABLE).
|
|
|
|
|
*/
|
2023-06-06 10:12:13 +02:00
|
|
|
static void expandEnvironmentVariables(const Environment &env, QString &string)
|
2019-07-22 14:39:01 +02:00
|
|
|
{
|
2020-06-23 13:00:43 +02:00
|
|
|
const QRegularExpression candidate("\\$\\$\\((.+)\\)");
|
2019-07-22 14:39:01 +02:00
|
|
|
|
2020-06-23 13:00:43 +02:00
|
|
|
QRegularExpressionMatch match;
|
|
|
|
|
int index = string.indexOf(candidate, 0, &match);
|
2019-07-22 14:39:01 +02:00
|
|
|
while (index != -1) {
|
2020-06-23 13:00:43 +02:00
|
|
|
const QString value = env.value(match.captured(1));
|
2019-07-22 14:39:01 +02:00
|
|
|
|
2020-06-23 13:00:43 +02:00
|
|
|
string.replace(index, match.capturedLength(), value);
|
2019-07-22 14:39:01 +02:00
|
|
|
index += value.length();
|
|
|
|
|
|
2020-06-23 13:00:43 +02:00
|
|
|
index = string.indexOf(candidate, index, &match);
|
2019-07-22 14:39:01 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Expands environment variables and converts the path from relative to the
|
2023-06-06 12:53:58 +02:00
|
|
|
* project to an absolute path for all given raw paths
|
2019-07-22 14:39:01 +02:00
|
|
|
*/
|
2023-06-06 12:53:58 +02:00
|
|
|
QList<PythonBuildSystem::FileEntry> PythonBuildSystem::processEntries(
|
|
|
|
|
const QStringList &rawPaths) const
|
2019-07-22 14:39:01 +02:00
|
|
|
{
|
2023-06-06 12:53:58 +02:00
|
|
|
QList<FileEntry> processed;
|
|
|
|
|
const FilePath projectDir = projectDirectory();
|
2023-06-06 10:12:13 +02:00
|
|
|
const Environment env = projectDirectory().deviceEnvironment();
|
2019-07-22 14:39:01 +02:00
|
|
|
|
2023-06-06 12:53:58 +02:00
|
|
|
for (const QString &rawPath : rawPaths) {
|
|
|
|
|
FilePath resolvedPath;
|
|
|
|
|
QString path = rawPath.trimmed();
|
|
|
|
|
if (!path.isEmpty()) {
|
|
|
|
|
expandEnvironmentVariables(env, path);
|
|
|
|
|
resolvedPath = projectDir.resolvePath(path);
|
2019-07-22 14:39:01 +02:00
|
|
|
}
|
2023-06-06 12:53:58 +02:00
|
|
|
processed << FileEntry{rawPath, resolvedPath};
|
2019-07-22 14:39:01 +02:00
|
|
|
}
|
2023-06-06 12:53:58 +02:00
|
|
|
return processed;
|
2019-07-22 14:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Project::RestoreResult PythonProject::fromMap(const QVariantMap &map, QString *errorMessage)
|
|
|
|
|
{
|
|
|
|
|
Project::RestoreResult res = Project::fromMap(map, errorMessage);
|
|
|
|
|
if (res == RestoreResult::Ok) {
|
|
|
|
|
if (!activeTarget())
|
|
|
|
|
addTargetForDefaultKit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-25 09:55:32 +02:00
|
|
|
PythonBuildSystem::PythonBuildSystem(Target *target)
|
|
|
|
|
: BuildSystem(target)
|
2019-07-22 14:39:01 +02:00
|
|
|
{
|
2022-07-19 22:42:48 +02:00
|
|
|
connect(target->project(), &Project::projectFileIsDirty, this, [this] { triggerParsing(); });
|
2022-10-18 10:05:51 +02:00
|
|
|
triggerParsing();
|
2019-07-22 14:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
2019-10-22 14:55:51 +02:00
|
|
|
bool PythonBuildSystem::supportsAction(Node *context, ProjectAction action, const Node *node) const
|
2019-07-22 14:39:01 +02:00
|
|
|
{
|
|
|
|
|
if (node->asFileNode()) {
|
|
|
|
|
return action == ProjectAction::Rename
|
|
|
|
|
|| action == ProjectAction::RemoveFile;
|
|
|
|
|
}
|
|
|
|
|
if (node->isFolderNodeType() || node->isProjectNodeType()) {
|
|
|
|
|
return action == ProjectAction::AddNewFile
|
|
|
|
|
|| action == ProjectAction::RemoveFile
|
|
|
|
|
|| action == ProjectAction::AddExistingFile;
|
|
|
|
|
}
|
2019-10-22 14:55:51 +02:00
|
|
|
return BuildSystem::supportsAction(context, action, node);
|
2019-07-22 14:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
2022-07-15 11:49:34 +02:00
|
|
|
} // Python::Internal
|