forked from qt-creator/qt-creator
JsonWizard: Allow to set file-specific replacement
Using options: [ { "key": "a", "value": "b", "condition": "true" } ] Change-Id: I36692d4e8506c02759674922ee05197de3a5c4c1 Reviewed-by: Orgad Shaneh <orgads@gmail.com>
This commit is contained in:
@@ -100,7 +100,7 @@ bool JsonWizardFileGenerator::setup(const QVariant &data, QString *errorMessage)
|
|||||||
|
|
||||||
File f;
|
File f;
|
||||||
|
|
||||||
QVariantMap tmp = d.toMap();
|
const QVariantMap tmp = d.toMap();
|
||||||
f.source = tmp.value(QLatin1String("source")).toString();
|
f.source = tmp.value(QLatin1String("source")).toString();
|
||||||
f.target = tmp.value(QLatin1String("target")).toString();
|
f.target = tmp.value(QLatin1String("target")).toString();
|
||||||
f.condition = tmp.value(QLatin1String("condition"), true);
|
f.condition = tmp.value(QLatin1String("condition"), true);
|
||||||
@@ -109,6 +109,27 @@ bool JsonWizardFileGenerator::setup(const QVariant &data, QString *errorMessage)
|
|||||||
f.openInEditor = tmp.value(QLatin1String("openInEditor"), false);
|
f.openInEditor = tmp.value(QLatin1String("openInEditor"), false);
|
||||||
f.openAsProject = tmp.value(QLatin1String("openAsProject"), 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (f.source.isEmpty() && f.target.isEmpty()) {
|
if (f.source.isEmpty() && f.target.isEmpty()) {
|
||||||
*errorMessage = QCoreApplication::translate("ProjectExplorer::JsonFieldPage",
|
*errorMessage = QCoreApplication::translate("ProjectExplorer::JsonFieldPage",
|
||||||
"Source and target are both empty.");
|
"Source and target are both empty.");
|
||||||
@@ -165,7 +186,23 @@ Core::GeneratedFiles JsonWizardFileGenerator::fileList(Utils::MacroExpander *exp
|
|||||||
} else {
|
} else {
|
||||||
// TODO: Document that input files are UTF8 encoded!
|
// TODO: Document that input files are UTF8 encoded!
|
||||||
gf.setBinary(false);
|
gf.setBinary(false);
|
||||||
gf.setContents(processTextFileContents(expander, QString::fromUtf8(reader.data()), errorMessage));
|
Utils::MacroExpander nested;
|
||||||
|
const File *fPtr = &f;
|
||||||
|
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;
|
||||||
|
});
|
||||||
|
nested.registerExtraResolver([expander](QString n, QString *ret) { return expander->resolveMacro(n, ret); });
|
||||||
|
|
||||||
|
gf.setContents(processTextFileContents(&nested, QString::fromUtf8(reader.data()), errorMessage));
|
||||||
if (!errorMessage->isEmpty()) {
|
if (!errorMessage->isEmpty()) {
|
||||||
*errorMessage = QCoreApplication::translate("ProjectExplorer::JsonWizard", "When processing \"%1\":<br>%2")
|
*errorMessage = QCoreApplication::translate("ProjectExplorer::JsonWizard", "When processing \"%1\":<br>%2")
|
||||||
.arg(sourcePath, *errorMessage);
|
.arg(sourcePath, *errorMessage);
|
||||||
|
@@ -65,6 +65,14 @@ private:
|
|||||||
QVariant overwrite;
|
QVariant overwrite;
|
||||||
QVariant openInEditor;
|
QVariant openInEditor;
|
||||||
QVariant openAsProject;
|
QVariant openAsProject;
|
||||||
|
|
||||||
|
class OptionDefinition {
|
||||||
|
public:
|
||||||
|
QString key;
|
||||||
|
QString value;
|
||||||
|
QString condition;
|
||||||
|
};
|
||||||
|
QList<OptionDefinition> options;
|
||||||
};
|
};
|
||||||
|
|
||||||
QList<File> m_fileList;
|
QList<File> m_fileList;
|
||||||
|
Reference in New Issue
Block a user