forked from qt-creator/qt-creator
ProjectExplorer: More customwizard code cosmetics
Change-Id: I42311bb5f8e6503a8f81dafd5c7487aaaaf84a0c Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
This commit is contained in:
@@ -160,11 +160,10 @@ 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 Result<> createFile(CustomWizardFile cwFile,
|
static Result<GeneratedFile> createFile(CustomWizardFile cwFile,
|
||||||
const QString &sourceDirectory,
|
const QString &sourceDirectory,
|
||||||
const FilePath &targetDirectory,
|
const FilePath &targetDirectory,
|
||||||
const CustomProjectWizard::FieldReplacementMap &fm,
|
const CustomProjectWizard::FieldReplacementMap &fm)
|
||||||
GeneratedFiles *files)
|
|
||||||
{
|
{
|
||||||
const QChar slash = QLatin1Char('/');
|
const QChar slash = QLatin1Char('/');
|
||||||
const QString sourcePath = sourceDirectory + slash + cwFile.source;
|
const QString sourcePath = sourceDirectory + slash + cwFile.source;
|
||||||
@@ -197,8 +196,7 @@ static Result<> createFile(CustomWizardFile cwFile,
|
|||||||
if (cwFile.openProject)
|
if (cwFile.openProject)
|
||||||
attributes |= GeneratedFile::OpenProjectAttribute;
|
attributes |= GeneratedFile::OpenProjectAttribute;
|
||||||
generatedFile.setAttributes(attributes);
|
generatedFile.setAttributes(attributes);
|
||||||
files->push_back(generatedFile);
|
return generatedFile;
|
||||||
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.
|
||||||
@@ -242,7 +240,13 @@ GeneratedFiles CustomWizard::generateFiles(const QWizard *dialog, QString *error
|
|||||||
str << " '" << it.key() << "' -> '" << it.value() << "'\n";
|
str << " '" << it.key() << "' -> '" << it.value() << "'\n";
|
||||||
qWarning("%s", qPrintable(logText));
|
qWarning("%s", qPrintable(logText));
|
||||||
}
|
}
|
||||||
return generateWizardFiles(errorMessage);
|
const Result<GeneratedFiles> res = generateWizardFiles();
|
||||||
|
if (!res) {
|
||||||
|
if (errorMessage)
|
||||||
|
*errorMessage = res.error();
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
return res.value();
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<> CustomWizard::writeFiles(const GeneratedFiles &files) const
|
Result<> CustomWizard::writeFiles(const GeneratedFiles &files) const
|
||||||
@@ -285,7 +289,7 @@ Result<> CustomWizard::writeFiles(const GeneratedFiles &files) const
|
|||||||
return ResultOk;
|
return ResultOk;
|
||||||
}
|
}
|
||||||
|
|
||||||
GeneratedFiles CustomWizard::generateWizardFiles(QString *errorMessage) const
|
Result<GeneratedFiles> CustomWizard::generateWizardFiles() const
|
||||||
{
|
{
|
||||||
GeneratedFiles rc;
|
GeneratedFiles rc;
|
||||||
const CustomWizardContextPtr ctx = context();
|
const CustomWizardContextPtr ctx = context();
|
||||||
@@ -296,24 +300,24 @@ GeneratedFiles CustomWizard::generateWizardFiles(QString *errorMessage) const
|
|||||||
qDebug() << "CustomWizard::generateWizardFiles: in "
|
qDebug() << "CustomWizard::generateWizardFiles: in "
|
||||||
<< ctx->targetPath << ", using: " << ctx->replacements;
|
<< ctx->targetPath << ", using: " << ctx->replacements;
|
||||||
|
|
||||||
// If generator script is non-empty, do a dry run to get it's files.
|
// If generator script is non-empty, do a dry run to get its files.
|
||||||
if (!d->m_parameters->filesGeneratorScript.isEmpty()) {
|
if (!d->m_parameters->filesGeneratorScript.isEmpty()) {
|
||||||
rc += dryRunCustomWizardGeneratorScript(scriptWorkingDirectory(ctx, d->m_parameters),
|
Result<QList<GeneratedFile>> res =
|
||||||
d->m_parameters->filesGeneratorScript,
|
dryRunCustomWizardGeneratorScript(scriptWorkingDirectory(ctx, d->m_parameters),
|
||||||
d->m_parameters->filesGeneratorScriptArguments,
|
d->m_parameters->filesGeneratorScript,
|
||||||
ctx->replacements,
|
d->m_parameters->filesGeneratorScriptArguments,
|
||||||
errorMessage);
|
ctx->replacements);
|
||||||
if (rc.isEmpty())
|
if (!res)
|
||||||
return rc;
|
return res;
|
||||||
|
rc.append(res.value());
|
||||||
}
|
}
|
||||||
// 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)) {
|
||||||
const Result<> res = createFile(file, d->m_parameters->directory, ctx->targetPath, context()->replacements, &rc);
|
const Result<GeneratedFile> res = createFile(file, d->m_parameters->directory,
|
||||||
if (!res) {
|
ctx->targetPath, context()->replacements);
|
||||||
if (errorMessage)
|
if (!res)
|
||||||
errorMessage->append(res.error());
|
return ResultError(res.error());
|
||||||
return {};
|
rc.append(res.value());
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return rc;
|
return rc;
|
||||||
@@ -514,8 +518,13 @@ GeneratedFiles CustomProjectWizard::generateFiles(const QWizard *w, QString *err
|
|||||||
ctx->replacements = fieldReplacementMap;
|
ctx->replacements = fieldReplacementMap;
|
||||||
if (CustomWizardPrivate::verbose)
|
if (CustomWizardPrivate::verbose)
|
||||||
qDebug() << "CustomProjectWizard::generateFiles" << dialog << ctx->targetPath << ctx->replacements;
|
qDebug() << "CustomProjectWizard::generateFiles" << dialog << ctx->targetPath << ctx->replacements;
|
||||||
const GeneratedFiles generatedFiles = generateWizardFiles(errorMessage);
|
const Result<GeneratedFiles> generatedFiles = generateWizardFiles();
|
||||||
return generatedFiles;
|
if (!generatedFiles) {
|
||||||
|
if (errorMessage)
|
||||||
|
*errorMessage = generatedFiles.error();
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
return *generatedFiles;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
@@ -80,7 +80,7 @@ protected:
|
|||||||
using CustomWizardContextPtr = std::shared_ptr<Internal::CustomWizardContext>;
|
using CustomWizardContextPtr = std::shared_ptr<Internal::CustomWizardContext>;
|
||||||
|
|
||||||
// generate files in path
|
// generate files in path
|
||||||
Core::GeneratedFiles generateWizardFiles(QString *errorMessage) const;
|
Utils::Result<Core::GeneratedFiles> generateWizardFiles() const;
|
||||||
// Create replacement map as static base fields + QWizard fields
|
// Create replacement map as static base fields + QWizard fields
|
||||||
FieldReplacementMap replacementMap(const QWizard *w) const;
|
FieldReplacementMap replacementMap(const QWizard *w) const;
|
||||||
Utils::Result<> writeFiles(const Core::GeneratedFiles &files) const override;
|
Utils::Result<> writeFiles(const Core::GeneratedFiles &files) const override;
|
||||||
|
@@ -14,6 +14,7 @@
|
|||||||
#include <QFileInfo>
|
#include <QFileInfo>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
|
||||||
|
using namespace Core;
|
||||||
using namespace Utils;
|
using namespace Utils;
|
||||||
|
|
||||||
namespace ProjectExplorer::Internal {
|
namespace ProjectExplorer::Internal {
|
||||||
@@ -114,39 +115,37 @@ static Result<>
|
|||||||
\sa runCustomWizardGeneratorScript, ProjectExplorer::CustomWizard
|
\sa runCustomWizardGeneratorScript, ProjectExplorer::CustomWizard
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Core::GeneratedFiles
|
Result<QList<GeneratedFile>>
|
||||||
dryRunCustomWizardGeneratorScript(const QString &targetPath,
|
dryRunCustomWizardGeneratorScript(const QString &targetPath,
|
||||||
const QStringList &script,
|
const QStringList &script,
|
||||||
const QList<GeneratorScriptArgument> &arguments,
|
const QList<GeneratorScriptArgument> &arguments,
|
||||||
const QMap<QString, QString> &fieldMap,
|
const QMap<QString, QString> &fieldMap)
|
||||||
QString *errorMessage)
|
|
||||||
{
|
{
|
||||||
// Run in temporary directory as the target path may not exist yet.
|
// Run in temporary directory as the target path may not exist yet.
|
||||||
QString stdOut;
|
QString stdOut;
|
||||||
const Result<> res = runGenerationScriptHelper(Utils::TemporaryDirectory::masterDirectoryFilePath(),
|
const Result<> res = runGenerationScriptHelper(TemporaryDirectory::masterDirectoryFilePath(),
|
||||||
script, arguments, true, fieldMap, &stdOut);
|
script, arguments, true, fieldMap, &stdOut);
|
||||||
if (!res) {
|
if (!res)
|
||||||
*errorMessage = res.error();
|
return ResultError(res.error());
|
||||||
return Core::GeneratedFiles();
|
|
||||||
}
|
GeneratedFiles files;
|
||||||
Core::GeneratedFiles files;
|
|
||||||
// Parse the output consisting of lines with ',' separated tokens.
|
// Parse the output consisting of lines with ',' separated tokens.
|
||||||
// (file name + attributes matching those of the <file> element)
|
// (file name + attributes matching those of the <file> element)
|
||||||
const QStringList lines = stdOut.split(QLatin1Char('\n'));
|
const QStringList lines = stdOut.split(QLatin1Char('\n'));
|
||||||
for (const QString &line : lines) {
|
for (const QString &line : lines) {
|
||||||
const QString trimmed = line.trimmed();
|
const QString trimmed = line.trimmed();
|
||||||
if (!trimmed.isEmpty()) {
|
if (!trimmed.isEmpty()) {
|
||||||
Core::GeneratedFile file;
|
GeneratedFile file;
|
||||||
Core::GeneratedFile::Attributes attributes = Core::GeneratedFile::CustomGeneratorAttribute;
|
GeneratedFile::Attributes attributes = GeneratedFile::CustomGeneratorAttribute;
|
||||||
const QStringList tokens = line.split(QLatin1Char(','));
|
const QStringList tokens = line.split(QLatin1Char(','));
|
||||||
const int count = tokens.count();
|
const int count = tokens.count();
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < count; i++) {
|
||||||
const QString &token = tokens.at(i);
|
const QString &token = tokens.at(i);
|
||||||
if (i) {
|
if (i) {
|
||||||
if (token == QLatin1String(customWizardFileOpenEditorAttributeC))
|
if (token == QLatin1String(customWizardFileOpenEditorAttributeC))
|
||||||
attributes |= Core::GeneratedFile::OpenEditorAttribute;
|
attributes |= GeneratedFile::OpenEditorAttribute;
|
||||||
else if (token == QLatin1String(customWizardFileOpenProjectAttributeC))
|
else if (token == QLatin1String(customWizardFileOpenProjectAttributeC))
|
||||||
attributes |= Core::GeneratedFile::OpenProjectAttribute;
|
attributes |= GeneratedFile::OpenProjectAttribute;
|
||||||
} else {
|
} else {
|
||||||
// Token 0 is file name. Wizard wants native names.
|
// Token 0 is file name. Wizard wants native names.
|
||||||
// Expand to full path if relative
|
// Expand to full path if relative
|
||||||
@@ -165,7 +164,7 @@ Core::GeneratedFiles
|
|||||||
if (CustomWizard::verbose()) {
|
if (CustomWizard::verbose()) {
|
||||||
QDebug nospace = qDebug().nospace();
|
QDebug nospace = qDebug().nospace();
|
||||||
nospace << script << " generated:\n";
|
nospace << script << " generated:\n";
|
||||||
for (const Core::GeneratedFile &f : std::as_const(files))
|
for (const GeneratedFile &f : std::as_const(files))
|
||||||
nospace << ' ' << f.filePath() << f.attributes() << '\n';
|
nospace << ' ' << f.filePath() << f.attributes() << '\n';
|
||||||
}
|
}
|
||||||
return files;
|
return files;
|
||||||
|
@@ -18,17 +18,16 @@ class GeneratorScriptArgument;
|
|||||||
QStringList fixGeneratorScript(const QString &configFile, QString attributeIn);
|
QStringList fixGeneratorScript(const QString &configFile, QString attributeIn);
|
||||||
|
|
||||||
// Step 1) Do a dry run of the generation script to get a list of files on stdout
|
// Step 1) Do a dry run of the generation script to get a list of files on stdout
|
||||||
QList<Core::GeneratedFile>
|
Utils::Result<QList<Core::GeneratedFile>>
|
||||||
dryRunCustomWizardGeneratorScript(const QString &targetPath,
|
dryRunCustomWizardGeneratorScript(const QString &targetPath,
|
||||||
const QStringList &script,
|
const QStringList &script,
|
||||||
const QList<GeneratorScriptArgument> &arguments,
|
const QList<GeneratorScriptArgument> &arguments,
|
||||||
const QMap<QString, QString> &fieldMap,
|
const QMap<QString, QString> &fieldMap);
|
||||||
QString *errorMessage);
|
|
||||||
|
|
||||||
// Step 2) Generate files
|
// Step 2) Generate files
|
||||||
Utils::Result<> runCustomWizardGeneratorScript(const QString &targetPath,
|
Utils::Result<> runCustomWizardGeneratorScript(const QString &targetPath,
|
||||||
const QStringList &script,
|
const QStringList &script,
|
||||||
const QList<GeneratorScriptArgument> &arguments,
|
const QList<GeneratorScriptArgument> &arguments,
|
||||||
const QMap<QString, QString> &fieldMap);
|
const QMap<QString, QString> &fieldMap);
|
||||||
|
|
||||||
} // namespace ProjectExplorer::Internal
|
} // namespace ProjectExplorer::Internal
|
||||||
|
Reference in New Issue
Block a user