Commit 480f40c0 authored by Tobias Hunger's avatar Tobias Hunger
Browse files

JSONWizard: Unify "options" handling



Code was duplicated between the factory of the wizard and the file
generator.

Change-Id: Ied2ba99218a5f06e8a0dce0fbc12f277195de8ad
Reviewed-by: default avatarTim Jenssen <tim.jenssen@theqtcompany.com>
parent 0f34f9e6
......@@ -30,6 +30,7 @@
#include "jsonwizard.h"
#include "jsonwizardfactory.h"
#include "jsonwizardgeneratorfactory.h"
#include "../project.h"
......@@ -146,6 +147,34 @@ void JsonWizard::setValue(const QString &key, const QVariant &value)
setProperty(key.toUtf8(), value);
}
QList<JsonWizard::OptionDefinition> JsonWizard::parseOptions(const QVariant &v, QString *errorMessage)
{
QTC_ASSERT(errorMessage, return { });
QList<JsonWizard::OptionDefinition> result;
if (!v.isNull()) {
const QVariantList optList = JsonWizardFactory::objectOrList(v, errorMessage);
foreach (const QVariant &o, optList) {
QVariantMap optionObject = o.toMap();
JsonWizard::OptionDefinition odef;
odef.key = optionObject.value(QLatin1String("key")).toString();
odef.value = optionObject.value(QLatin1String("value")).toString();
odef.condition = optionObject.value(QLatin1String("condition"), QLatin1String("true")).toString();
if (odef.key.isEmpty()) {
*errorMessage = QCoreApplication::translate("ProjectExplorer::Internal::JsonWizardFileGenerator",
"No 'key' in options object.");
result.clear();
break;
}
result.append(odef);
}
}
QTC_ASSERT(errorMessage->isEmpty() || (!errorMessage->isEmpty() && result.isEmpty()), return result);
return result;
}
QVariant JsonWizard::value(const QString &n) const
{
QVariant v = property(n.toUtf8());
......
......@@ -78,6 +78,14 @@ public:
QVariant value(const QString &n) const;
void setValue(const QString &key, const QVariant &value);
class OptionDefinition {
public:
QString key;
QString value;
QString condition;
};
static QList<OptionDefinition> parseOptions(const QVariant &v, QString *errorMessage);
static bool boolFromVariant(const QVariant &v, Utils::MacroExpander *expander);
static QString stringListToArrayString(const QStringList &list,
const Utils::MacroExpander *expander);
......
......@@ -395,8 +395,8 @@ Utils::Wizard *JsonWizardFactory::runWizardImpl(const QString &path, QWidget *pa
wizard->setValue(QStringLiteral("category"), category());
wizard->setValue(QStringLiteral("id"), id().toString());
for (auto i = m_options.constBegin(); i != m_options.constEnd(); ++i)
wizard->setValue(i.key(), i.value());
foreach (const JsonWizard::OptionDefinition &od, m_options)
wizard->setValue(od.key, od.value);
bool havePage = false;
foreach (const Page &data, m_pages) {
......@@ -625,29 +625,8 @@ bool JsonWizardFactory::initialize(const QVariantMap &data, const QDir &baseDir,
setFlags(flags);
// Options:
const QVariant optionValue = data.value(QLatin1String(OPTIONS_KEY));
const QVariantList optionList = optionValue.isNull() ? QVariantList() : objectOrList(optionValue, errorMessage);
foreach (const QVariant &v, optionList) {
if (v.type() != QVariant::Map) {
*errorMessage = tr("List element of \"options\" is not an object.");
return false;
}
QVariantMap data = v.toMap();
const QString key = data.value(QStringLiteral("key"), QString()).toString();
const QString value = data.value(QStringLiteral("value"), QString()).toString();
if (key.isEmpty()) {
*errorMessage = tr("No \"key\" given for entry in \"options\".");
return false;
}
if (m_options.contains(key)) {
*errorMessage = tr("When parsing \"options\": Key \"%1\" set more than once.").arg(key);
return false;
}
m_options.insert(key, value);
}
return true;
m_options = JsonWizard::parseOptions(data.value(QLatin1String(OPTIONS_KEY)), errorMessage);
return errorMessage->isEmpty();
}
} // namespace ProjectExplorer
......@@ -33,6 +33,8 @@
#include "../projectexplorer_export.h"
#include "jsonwizard.h"
#include <coreplugin/iwizardfactory.h>
#include <utils/fileutils.h>
......@@ -115,7 +117,7 @@ private:
QList<Generator> m_generators;
QList<Page> m_pages;
QMap<QString, QString> m_options;
QList<JsonWizard::OptionDefinition> m_options;
QSet<Core::Id> m_preferredFeatures;
......
......@@ -52,6 +52,8 @@ namespace Internal {
bool JsonWizardFileGenerator::setup(const QVariant &data, QString *errorMessage)
{
QTC_ASSERT(errorMessage && errorMessage->isEmpty(), return false);
QVariantList list = JsonWizardFactory::objectOrList(data, errorMessage);
if (list.isEmpty())
return false;
......@@ -74,26 +76,9 @@ bool JsonWizardFileGenerator::setup(const QVariant &data, QString *errorMessage)
f.openInEditor = tmp.value(QLatin1String("openInEditor"), false);
f.openAsProject = tmp.value(QLatin1String("openAsProject"), false);
const QVariant options = tmp.value(QLatin1String("options"));
if (!options.isNull()) {
const QVariantList optList = JsonWizardFactory::objectOrList(options, errorMessage);
if (optList.isEmpty())
return false;
foreach (const QVariant &o, optList) {
QVariantMap optionObject = o.toMap();
File::OptionDefinition odef;
odef.key = optionObject.value(QLatin1String("key")).toString();
odef.value = optionObject.value(QLatin1String("value")).toString();
odef.condition = optionObject.value(QLatin1String("condition"), QLatin1String("true")).toString();
if (odef.key.isEmpty()) {
*errorMessage = QCoreApplication::translate("ProjectExplorer::Internal::JsonWizardFileGenerator",
"No 'key' in options object.");
return false;
}
f.options.append(odef);
}
}
f.options = JsonWizard::parseOptions(tmp.value(QLatin1String("options")), errorMessage);
if (!errorMessage->isEmpty())
return false;
if (f.source.isEmpty() && f.target.isEmpty()) {
*errorMessage = QCoreApplication::translate("ProjectExplorer::JsonFieldPage",
......@@ -133,18 +118,20 @@ Core::GeneratedFile JsonWizardFileGenerator::generateFile(const File &file,
// TODO: Document that input files are UTF8 encoded!
gf.setBinary(false);
Utils::MacroExpander nested;
const File *fPtr = &file;
Utils::MacroExpander *thisExpander = &nested;
nested.registerExtraResolver([fPtr, thisExpander](QString n, QString *ret) -> bool {
foreach (const File::OptionDefinition &od, fPtr->options) {
if (!JsonWizard::boolFromVariant(od.condition, thisExpander))
continue;
if (n == od.key) {
*ret = od.value;
return true;
}
}
return false;
// evaluate file options once:
QHash<QString, QString> options;
foreach (const JsonWizard::OptionDefinition &od, file.options) {
if (!JsonWizard::boolFromVariant(od.condition, expander))
continue;
options.insert(od.key, od.value);
}
nested.registerExtraResolver([&options](QString n, QString *ret) -> bool {
if (!options.contains(n))
return false;
*ret = options.value(n);
return true;
});
nested.registerExtraResolver([expander](QString n, QString *ret) { return expander->resolveMacro(n, ret); });
......
......@@ -62,13 +62,7 @@ private:
QVariant openInEditor = false;
QVariant openAsProject = false;
class OptionDefinition {
public:
QString key;
QString value;
QString condition;
};
QList<OptionDefinition> options;
QList<JsonWizard::OptionDefinition> options;
};
Core::GeneratedFile generateFile(const File &file, Utils::MacroExpander *expander,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment