aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/python/pythonproject.cpp
diff options
context:
space:
mode:
authorDavid Schulz <david.schulz@qt.io>2023-06-06 15:12:34 +0200
committerDavid Schulz <david.schulz@qt.io>2023-06-09 11:37:26 +0000
commit8d733f95e68b25003ea14906379750870bdd37ec (patch)
treea4e4f0b04bf68a2833cca70c2cdb90a7dd1085bb /src/plugins/python/pythonproject.cpp
parent45acf2d49e660b8cd3e7cc9fc78b213eace686f2 (diff)
Python: simplify project file saving
use QJsonDocument to save the new style project files. This improves the formatting and prevents replacing the newly introduced qml import path field. Also used FilePath::fileContents/writeFileContents to support remote projects. Fixes: QTCREATORBUG-28541 Change-Id: Ie614726b6775dad2361437dc5d410438c7d01141 Reviewed-by: hjk <hjk@qt.io>
Diffstat (limited to 'src/plugins/python/pythonproject.cpp')
-rw-r--r--src/plugins/python/pythonproject.cpp62
1 files changed, 18 insertions, 44 deletions
diff --git a/src/plugins/python/pythonproject.cpp b/src/plugins/python/pythonproject.cpp
index d4f31495a5d..ee6fdcd6ca3 100644
--- a/src/plugins/python/pythonproject.cpp
+++ b/src/plugins/python/pythonproject.cpp
@@ -270,57 +270,31 @@ bool PythonBuildSystem::save()
const FileChangeBlocker changeGuarg(filePath);
bool result = false;
+ QByteArray newContents;
+
// New project file
if (filePath.endsWith(".pyproject")) {
- FileSaver saver(filePath, QIODevice::ReadOnly | QIODevice::Text);
- if (!saver.hasError()) {
- QString content = QTextStream(saver.file()).readAll();
- if (saver.finalize(ICore::dialogParent())) {
- QString errorMessage;
- result = writePyProjectFile(filePath, content, rawList, &errorMessage);
- if (!errorMessage.isEmpty())
- MessageManager::writeDisrupting(errorMessage);
- }
+ 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());
}
} else { // Old project file
- FileSaver saver(filePath, QIODevice::WriteOnly | QIODevice::Text);
- if (!saver.hasError()) {
- QTextStream stream(saver.file());
- for (const QString &filePath : rawList)
- stream << filePath << '\n';
- saver.setResult(&stream);
- result = saver.finalize(ICore::dialogParent());
- }
+ newContents = rawList.join('\n').toUtf8();
}
- return result;
-}
+ const expected_str<qint64> writeResult = filePath.writeFileContents(newContents);
+ if (writeResult)
+ result = true;
+ else
+ MessageManager::writeDisrupting(writeResult.error());
-bool PythonBuildSystem::writePyProjectFile(const FilePath &filePath, QString &content,
- const QStringList &rawList, QString *errorMessage)
-{
- QFile file(filePath.toString());
- if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
- *errorMessage = Tr::tr("Unable to open \"%1\" for writing: %2")
- .arg(filePath.toUserOutput(), file.errorString());
- return false;
- }
-
- // Build list of files with the current rawList for the JSON file
- QString files("[");
- for (const QString &f : rawList)
- if (!f.endsWith(".pyproject"))
- files += QString("\"%1\",").arg(f);
- files = files.left(files.lastIndexOf(',')); // Removing leading comma
- files += ']';
-
- // Removing everything inside square parenthesis
- // to replace it with the new list of files for the JSON file.
- QRegularExpression pattern(R"(\[.*\])");
- content.replace(pattern, files);
- file.write(content.toUtf8());
-
- return true;
+ return result;
}
bool PythonBuildSystem::addFiles(Node *, const FilePaths &filePaths, FilePaths *)