From ed74b03d1a938e44d1f58c4662c745a9dc7030b3 Mon Sep 17 00:00:00 2001
From: Tobias Hunger <tobias.hunger@theqtcompany.com>
Date: Mon, 14 Sep 2015 16:51:37 +0200
Subject: [PATCH] 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>
---
 .../jsonwizard/jsonwizardfilegenerator.cpp    | 41 ++++++++++++++++++-
 .../jsonwizard/jsonwizardfilegenerator.h      |  8 ++++
 2 files changed, 47 insertions(+), 2 deletions(-)

diff --git a/src/plugins/projectexplorer/jsonwizard/jsonwizardfilegenerator.cpp b/src/plugins/projectexplorer/jsonwizard/jsonwizardfilegenerator.cpp
index 9898501bfa5..9eeed19cef3 100644
--- a/src/plugins/projectexplorer/jsonwizard/jsonwizardfilegenerator.cpp
+++ b/src/plugins/projectexplorer/jsonwizard/jsonwizardfilegenerator.cpp
@@ -100,7 +100,7 @@ bool JsonWizardFileGenerator::setup(const QVariant &data, QString *errorMessage)
 
         File f;
 
-        QVariantMap tmp = d.toMap();
+        const QVariantMap tmp = d.toMap();
         f.source = tmp.value(QLatin1String("source")).toString();
         f.target = tmp.value(QLatin1String("target")).toString();
         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.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()) {
             *errorMessage = QCoreApplication::translate("ProjectExplorer::JsonFieldPage",
                                                         "Source and target are both empty.");
@@ -165,7 +186,23 @@ Core::GeneratedFiles JsonWizardFileGenerator::fileList(Utils::MacroExpander *exp
             } else {
                 // TODO: Document that input files are UTF8 encoded!
                 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()) {
                     *errorMessage = QCoreApplication::translate("ProjectExplorer::JsonWizard", "When processing \"%1\":<br>%2")
                             .arg(sourcePath, *errorMessage);
diff --git a/src/plugins/projectexplorer/jsonwizard/jsonwizardfilegenerator.h b/src/plugins/projectexplorer/jsonwizard/jsonwizardfilegenerator.h
index 2a66f496230..03ee2dc557b 100644
--- a/src/plugins/projectexplorer/jsonwizard/jsonwizardfilegenerator.h
+++ b/src/plugins/projectexplorer/jsonwizard/jsonwizardfilegenerator.h
@@ -65,6 +65,14 @@ private:
         QVariant overwrite;
         QVariant openInEditor;
         QVariant openAsProject;
+
+        class OptionDefinition {
+        public:
+            QString key;
+            QString value;
+            QString condition;
+        };
+        QList<OptionDefinition> options;
     };
 
     QList<File> m_fileList;
-- 
GitLab