ProjectExplorer: Some modernization in customwizard.cpp

More FilePath and Result, less FileReader.

Change-Id: Iddcd516a8db05c2a860c3ae3e6f5024841260d08
Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
This commit is contained in:
hjk
2025-04-17 12:23:32 +02:00
parent 73da53157a
commit 6da6aede61
2 changed files with 24 additions and 23 deletions

View File

@@ -16,8 +16,8 @@
#include <extensionsystem/pluginmanager.h> #include <extensionsystem/pluginmanager.h>
#include <utils/algorithm.h> #include <utils/algorithm.h>
#include <utils/environment.h> #include <utils/environment.h>
#include <utils/fileutils.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <utils/stringutils.h>
#include <QDebug> #include <QDebug>
#include <QFile> #include <QFile>
@@ -160,35 +160,34 @@ BaseFileWizard *CustomWizard::create(const WizardDialogParameters &p) const
} }
// Read out files and store contents with field contents replaced. // Read out files and store contents with field contents replaced.
static bool createFile(CustomWizardFile cwFile, static Result<> createFile(CustomWizardFile cwFile,
const QString &sourceDirectory, const QString &sourceDirectory,
const QString &targetDirectory, const FilePath &targetDirectory,
const CustomProjectWizard::FieldReplacementMap &fm, const CustomProjectWizard::FieldReplacementMap &fm,
GeneratedFiles *files, GeneratedFiles *files)
QString *errorMessage)
{ {
const QChar slash = QLatin1Char('/'); const QChar slash = QLatin1Char('/');
const QString sourcePath = sourceDirectory + slash + cwFile.source; const QString sourcePath = sourceDirectory + slash + cwFile.source;
// Field replacement on target path // Field replacement on target path
CustomWizardContext::replaceFields(fm, &cwFile.target); CustomWizardContext::replaceFields(fm, &cwFile.target);
const QString targetPath = targetDirectory + slash + cwFile.target; const FilePath targetPath = targetDirectory.pathAppended(cwFile.target);
if (CustomWizardPrivate::verbose) if (CustomWizardPrivate::verbose)
qDebug() << "generating " << targetPath << sourcePath << fm; qDebug() << "generating " << targetPath << sourcePath << fm;
// Read contents of source file // Read contents of source file
FileReader reader; const Result<QByteArray> contents = FilePath::fromString(sourcePath).fileContents();
if (!reader.fetch(FilePath::fromString(sourcePath), errorMessage)) if (!contents)
return false; return ResultError(contents.error());
GeneratedFile generatedFile; GeneratedFile generatedFile;
generatedFile.setFilePath(FilePath::fromString(targetPath).cleanPath()); generatedFile.setFilePath(targetPath.cleanPath());
if (cwFile.binary) { if (cwFile.binary) {
// Binary file: Set data. // Binary file: Set data.
generatedFile.setBinary(true); generatedFile.setBinary(true);
generatedFile.setBinaryContents(reader.data()); generatedFile.setBinaryContents(*contents);
} else { } else {
// Template file: Preprocess. // Template file: Preprocess.
const QString contentsIn = QString::fromLocal8Bit(reader.text()); const QString contentsIn = QString::fromLocal8Bit(normalizeNewlines(*contents));
generatedFile.setContents(CustomWizardContext::processFile(fm, contentsIn)); generatedFile.setContents(CustomWizardContext::processFile(fm, contentsIn));
} }
@@ -199,7 +198,7 @@ static bool createFile(CustomWizardFile cwFile,
attributes |= GeneratedFile::OpenProjectAttribute; attributes |= GeneratedFile::OpenProjectAttribute;
generatedFile.setAttributes(attributes); generatedFile.setAttributes(attributes);
files->push_back(generatedFile); files->push_back(generatedFile);
return true; return ResultOk;
} }
// Helper to find a specific wizard page of a wizard by type. // Helper to find a specific wizard page of a wizard by type.
@@ -237,7 +236,7 @@ GeneratedFiles CustomWizard::generateFiles(const QWizard *dialog, QString *error
if (CustomWizardPrivate::verbose) { if (CustomWizardPrivate::verbose) {
QString logText; QString logText;
QTextStream str(&logText); QTextStream str(&logText);
str << "CustomWizard::generateFiles: " << ctx->targetPath << '\n'; str << "CustomWizard::generateFiles: " << ctx->targetPath.toUserOutput() << '\n';
const FieldReplacementMap::const_iterator cend = context()->replacements.constEnd(); const FieldReplacementMap::const_iterator cend = context()->replacements.constEnd();
for (FieldReplacementMap::const_iterator it = context()->replacements.constBegin(); it != cend; ++it) for (FieldReplacementMap::const_iterator it = context()->replacements.constBegin(); it != cend; ++it)
str << " '" << it.key() << "' -> '" << it.value() << "'\n"; str << " '" << it.key() << "' -> '" << it.value() << "'\n";
@@ -308,10 +307,14 @@ GeneratedFiles CustomWizard::generateWizardFiles(QString *errorMessage) const
return rc; return rc;
} }
// Add the template files specified by the <file> elements. // Add the template files specified by the <file> elements.
for (const CustomWizardFile &file : std::as_const(d->m_parameters->files)) for (const CustomWizardFile &file : std::as_const(d->m_parameters->files)) {
if (!createFile(file, d->m_parameters->directory, ctx->targetPath.toUrlishString(), context()->replacements, const Result<> res = createFile(file, d->m_parameters->directory, ctx->targetPath, context()->replacements, &rc);
&rc, errorMessage)) if (!res) {
if (errorMessage)
errorMessage->append(res.error());
return {}; return {};
}
}
return rc; return rc;
} }

View File

@@ -16,8 +16,7 @@
using namespace Utils; using namespace Utils;
namespace ProjectExplorer { namespace ProjectExplorer::Internal {
namespace Internal {
// Parse helper: Determine the correct binary to run: // Parse helper: Determine the correct binary to run:
// Expand to full wizard path if it is relative and located // Expand to full wizard path if it is relative and located
@@ -213,5 +212,4 @@ Result<> runCustomWizardGeneratorScript(const QString &targetPath,
false, fieldMap, nullptr); false, fieldMap, nullptr);
} }
} // namespace Internal } // namespace ProjectExplorer::Internal
} // namespace ProjectExplorer