forked from qt-creator/qt-creator
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:
@@ -270,59 +270,33 @@ 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();
|
||||
}
|
||||
|
||||
const expected_str<qint64> writeResult = filePath.writeFileContents(newContents);
|
||||
if (writeResult)
|
||||
result = true;
|
||||
else
|
||||
MessageManager::writeDisrupting(writeResult.error());
|
||||
|
||||
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 *)
|
||||
{
|
||||
const Utils::FilePath projectDir = projectDirectory();
|
||||
|
||||
Reference in New Issue
Block a user