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>
This commit is contained in:
David Schulz
2023-06-06 15:12:34 +02:00
parent 45acf2d49e
commit 8d733f95e6

View File

@@ -270,59 +270,33 @@ bool PythonBuildSystem::save()
const FileChangeBlocker changeGuarg(filePath); const FileChangeBlocker changeGuarg(filePath);
bool result = false; bool result = false;
QByteArray newContents;
// New project file // New project file
if (filePath.endsWith(".pyproject")) { if (filePath.endsWith(".pyproject")) {
FileSaver saver(filePath, QIODevice::ReadOnly | QIODevice::Text); expected_str<QByteArray> contents = filePath.fileContents();
if (!saver.hasError()) { if (contents) {
QString content = QTextStream(saver.file()).readAll(); QJsonDocument doc = QJsonDocument::fromJson(*contents);
if (saver.finalize(ICore::dialogParent())) { QJsonObject project = doc.object();
QString errorMessage; project["files"] = QJsonArray::fromStringList(rawList);
result = writePyProjectFile(filePath, content, rawList, &errorMessage); doc.setObject(project);
if (!errorMessage.isEmpty()) newContents = doc.toJson();
MessageManager::writeDisrupting(errorMessage); } else {
} MessageManager::writeDisrupting(contents.error());
} }
} else { // Old project file } else { // Old project file
FileSaver saver(filePath, QIODevice::WriteOnly | QIODevice::Text); newContents = rawList.join('\n').toUtf8();
if (!saver.hasError()) {
QTextStream stream(saver.file());
for (const QString &filePath : rawList)
stream << filePath << '\n';
saver.setResult(&stream);
result = saver.finalize(ICore::dialogParent());
}
} }
const expected_str<qint64> writeResult = filePath.writeFileContents(newContents);
if (writeResult)
result = true;
else
MessageManager::writeDisrupting(writeResult.error());
return result; return result;
} }
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;
}
bool PythonBuildSystem::addFiles(Node *, const FilePaths &filePaths, FilePaths *) bool PythonBuildSystem::addFiles(Node *, const FilePaths &filePaths, FilePaths *)
{ {
const Utils::FilePath projectDir = projectDirectory(); const Utils::FilePath projectDir = projectDirectory();