From 6da6aede615bc76ce54e90e95e027a5b6afef6c5 Mon Sep 17 00:00:00 2001 From: hjk Date: Thu, 17 Apr 2025 12:23:32 +0200 Subject: [PATCH] ProjectExplorer: Some modernization in customwizard.cpp More FilePath and Result, less FileReader. Change-Id: Iddcd516a8db05c2a860c3ae3e6f5024841260d08 Reviewed-by: Alessandro Portale --- .../customwizard/customwizard.cpp | 41 ++++++++++--------- .../customwizardscriptgenerator.cpp | 6 +-- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/plugins/projectexplorer/customwizard/customwizard.cpp b/src/plugins/projectexplorer/customwizard/customwizard.cpp index 65abdf0528c..a80beaec5cb 100644 --- a/src/plugins/projectexplorer/customwizard/customwizard.cpp +++ b/src/plugins/projectexplorer/customwizard/customwizard.cpp @@ -16,8 +16,8 @@ #include #include #include -#include #include +#include #include #include @@ -160,35 +160,34 @@ BaseFileWizard *CustomWizard::create(const WizardDialogParameters &p) const } // Read out files and store contents with field contents replaced. -static bool createFile(CustomWizardFile cwFile, - const QString &sourceDirectory, - const QString &targetDirectory, - const CustomProjectWizard::FieldReplacementMap &fm, - GeneratedFiles *files, - QString *errorMessage) +static Result<> createFile(CustomWizardFile cwFile, + const QString &sourceDirectory, + const FilePath &targetDirectory, + const CustomProjectWizard::FieldReplacementMap &fm, + GeneratedFiles *files) { const QChar slash = QLatin1Char('/'); const QString sourcePath = sourceDirectory + slash + cwFile.source; // Field replacement on target path CustomWizardContext::replaceFields(fm, &cwFile.target); - const QString targetPath = targetDirectory + slash + cwFile.target; + const FilePath targetPath = targetDirectory.pathAppended(cwFile.target); if (CustomWizardPrivate::verbose) qDebug() << "generating " << targetPath << sourcePath << fm; // Read contents of source file - FileReader reader; - if (!reader.fetch(FilePath::fromString(sourcePath), errorMessage)) - return false; + const Result contents = FilePath::fromString(sourcePath).fileContents(); + if (!contents) + return ResultError(contents.error()); GeneratedFile generatedFile; - generatedFile.setFilePath(FilePath::fromString(targetPath).cleanPath()); + generatedFile.setFilePath(targetPath.cleanPath()); if (cwFile.binary) { // Binary file: Set data. generatedFile.setBinary(true); - generatedFile.setBinaryContents(reader.data()); + generatedFile.setBinaryContents(*contents); } else { // Template file: Preprocess. - const QString contentsIn = QString::fromLocal8Bit(reader.text()); + const QString contentsIn = QString::fromLocal8Bit(normalizeNewlines(*contents)); generatedFile.setContents(CustomWizardContext::processFile(fm, contentsIn)); } @@ -199,7 +198,7 @@ static bool createFile(CustomWizardFile cwFile, attributes |= GeneratedFile::OpenProjectAttribute; generatedFile.setAttributes(attributes); files->push_back(generatedFile); - return true; + return ResultOk; } // 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) { QString 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(); for (FieldReplacementMap::const_iterator it = context()->replacements.constBegin(); it != cend; ++it) str << " '" << it.key() << "' -> '" << it.value() << "'\n"; @@ -308,10 +307,14 @@ GeneratedFiles CustomWizard::generateWizardFiles(QString *errorMessage) const return rc; } // Add the template files specified by the elements. - for (const CustomWizardFile &file : std::as_const(d->m_parameters->files)) - if (!createFile(file, d->m_parameters->directory, ctx->targetPath.toUrlishString(), context()->replacements, - &rc, errorMessage)) + for (const CustomWizardFile &file : std::as_const(d->m_parameters->files)) { + const Result<> res = createFile(file, d->m_parameters->directory, ctx->targetPath, context()->replacements, &rc); + if (!res) { + if (errorMessage) + errorMessage->append(res.error()); return {}; + } + } return rc; } diff --git a/src/plugins/projectexplorer/customwizard/customwizardscriptgenerator.cpp b/src/plugins/projectexplorer/customwizard/customwizardscriptgenerator.cpp index fd798072b39..bb0cab58c1e 100644 --- a/src/plugins/projectexplorer/customwizard/customwizardscriptgenerator.cpp +++ b/src/plugins/projectexplorer/customwizard/customwizardscriptgenerator.cpp @@ -16,8 +16,7 @@ using namespace Utils; -namespace ProjectExplorer { -namespace Internal { +namespace ProjectExplorer::Internal { // Parse helper: Determine the correct binary to run: // Expand to full wizard path if it is relative and located @@ -213,5 +212,4 @@ Result<> runCustomWizardGeneratorScript(const QString &targetPath, false, fieldMap, nullptr); } -} // namespace Internal -} // namespace ProjectExplorer +} // namespace ProjectExplorer::Internal