aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/projectexplorer/jsonwizard/jsonwizard.cpp
blob: 16fc894930dbaae746b588cea6d54329ec88bf37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "jsonwizard.h"

#include "jsonwizardfactory.h"
#include "jsonwizardgeneratorfactory.h"

#include "../project.h"
#include "../projectexplorer.h"
#include "../projectexplorerconstants.h"
#include "../projecttree.h"
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/messagemanager.h>

#include <utils/algorithm.h>
#include <utils/itemviews.h>
#include <utils/qtcassert.h>
#include <utils/treemodel.h>
#include <utils/wizardpage.h>

#include <QDialog>
#include <QDialogButtonBox>
#include <QDir>
#include <QFileInfo>
#include <QJSEngine>
#include <QLabel>
#include <QMessageBox>
#include <QPushButton>
#include <QVBoxLayout>
#include <QVariant>

#ifdef WITH_TESTS
#include "jsonwizard_test.cpp"
#endif

using namespace Utils;

namespace ProjectExplorer {
namespace Internal {

class ProjectFileTreeItem : public TreeItem
{
public:
    ProjectFileTreeItem(JsonWizard::GeneratorFile *candidate) : m_candidate(candidate)
    {
        toggleProjectFileStatus(false);
    }

    void toggleProjectFileStatus(bool on)
    {
        m_candidate->file.setAttributes(m_candidate->file.attributes()
                                        .setFlag(Core::GeneratedFile::OpenProjectAttribute, on));
    }

private:
    QVariant data(int column, int role) const override
    {
        if (column != 0 || role != Qt::DisplayRole)
            return QVariant();
        return m_candidate->file.filePath().toUserOutput();
    }

    JsonWizard::GeneratorFile * const m_candidate;
};

class ProjectFilesModel : public TreeModel<TreeItem, ProjectFileTreeItem>
{
public:
    ProjectFilesModel(const QList<JsonWizard::GeneratorFile *> &candidates, QObject *parent)
        : TreeModel(parent)
    {
        setHeader({QCoreApplication::translate("ProjectExplorer::JsonWizard", "Project File")});
        for (JsonWizard::GeneratorFile * const candidate : candidates)
            rootItem()->appendChild(new ProjectFileTreeItem(candidate));
    }
};

class ProjectFileChooser : public QDialog
{
public:
    ProjectFileChooser(const QList<JsonWizard::GeneratorFile *> &candidates, QWidget *parent)
        : QDialog(parent), m_view(new TreeView(this))
    {
        setWindowTitle(QCoreApplication::translate("ProjectExplorer::JsonWizard",
                                                   "Choose Project File"));
        const auto model = new ProjectFilesModel(candidates, this);
        m_view->setSelectionMode(TreeView::ExtendedSelection);
        m_view->setSelectionBehavior(TreeView::SelectRows);
        m_view->setModel(model);
        const auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok);
        const auto updateOkButton = [buttonBox, this] {
            buttonBox->button(QDialogButtonBox::Ok)
                    ->setEnabled(m_view->selectionModel()->hasSelection());
        };
        connect(m_view->selectionModel(), &QItemSelectionModel::selectionChanged,
                this, updateOkButton);
        updateOkButton();
        connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
        const auto layout = new QVBoxLayout(this);
        layout->addWidget(new QLabel(QCoreApplication::translate("ProjectExplorer::JsonWizard",
            "The project contains more than one project file. "
            "Select the one you would like to use.")));
        layout->addWidget(m_view);
        layout->addWidget(buttonBox);
    }

private:
    void accept() override
    {
        const QModelIndexList selected = m_view->selectionModel()->selectedRows();
        const auto * const model = static_cast<ProjectFilesModel *>(m_view->model());
        for (const QModelIndex &index : selected) {
            const auto item = static_cast<ProjectFileTreeItem *>(model->itemForIndex(index));
            QTC_ASSERT(item, continue);
            item->toggleProjectFileStatus(true);
        }
        QDialog::accept();
    }

    TreeView * const m_view;
};

} // namespace Internal

JsonWizard::JsonWizard(QWidget *parent)
    : Wizard(parent)
{
    setMinimumSize(800, 500);
    m_expander.registerExtraResolver([this](const QString &name, QString *ret) -> bool {
        *ret = stringValue(name);
        return !ret->isNull();
    });
    m_expander.registerPrefix("Exists", tr("Check whether a variable exists.<br>"
                                           "Returns \"true\" if it does and an empty string if not."),
                   [this](const QString &value) -> QString
    {
        const QString key = QString::fromLatin1("%{") + value + QLatin1Char('}');
        return m_expander.expand(key) == key ? QString() : QLatin1String("true");
    });
    // override default JS macro by custom one that adds Wizard specific features
    m_jsExpander.registerObject("Wizard", new Internal::JsonWizardJsExtension(this));
    m_jsExpander.engine().evaluate("var value = Wizard.value");
    m_jsExpander.registerForExpander(&m_expander);
}

JsonWizard::~JsonWizard()
{
    qDeleteAll(m_generators);
}

void JsonWizard::addGenerator(JsonWizardGenerator *gen)
{
    QTC_ASSERT(gen, return);
    QTC_ASSERT(!m_generators.contains(gen), return);

    m_generators.append(gen);
}

MacroExpander *JsonWizard::expander()
{
    return &m_expander;
}

JsonWizard::GeneratorFiles JsonWizard::generateFileList()
{
    QString errorMessage;
    GeneratorFiles list;

    const FilePath targetPath =
            FilePath::fromString(stringValue(QLatin1String("TargetPath")));
    if (targetPath.isEmpty())
        errorMessage = tr("Could not determine target path. \"TargetPath\" was not set on any page.");

    if (m_files.isEmpty() && errorMessage.isEmpty()) {
        emit preGenerateFiles();
        for (JsonWizardGenerator *gen : std::as_const(m_generators)) {
            const FilePath wizardDir =
                    FilePath::fromString(stringValue(QLatin1String("WizardDir")));
            const Core::GeneratedFiles tmp =
                    gen->fileList(&m_expander, wizardDir, targetPath, &errorMessage);
            if (!errorMessage.isEmpty())
                break;
            list.append(Utils::transform(tmp, [&gen](const Core::GeneratedFile &f)
                                              { return JsonWizard::GeneratorFile(f, gen); }));
        }
    }

    if (!errorMessage.isEmpty()) {
        QMessageBox::critical(this, tr("File Generation Failed"),
                              tr("The wizard failed to generate files.<br>"
                                 "The error message was: \"%1\".").arg(errorMessage));
        reject();
        return GeneratorFiles();
    }

    QList<GeneratorFile *> projectFiles;
    for (JsonWizard::GeneratorFile &f : list) {
        if (f.file.attributes().testFlag(Core::GeneratedFile::OpenProjectAttribute))
            projectFiles << &f;
    }
    if (projectFiles.count() > 1)
        Internal::ProjectFileChooser(projectFiles, this).exec();

    return list;
}

void JsonWizard::commitToFileList(const JsonWizard::GeneratorFiles &list)
{
    m_files = list;
    emit postGenerateFiles(m_files);
}

QString JsonWizard::stringValue(const QString &n) const
{
    QVariant v = value(n);
    if (!v.isValid())
        return QString();

    if (v.type() == QVariant::String) {
        QString tmp = m_expander.expand(v.toString());
        if (tmp.isEmpty())
            tmp = QString::fromLatin1(""); // Make sure isNull() is *not* true.
        return tmp;
    }

    if (v.type() == QVariant::StringList)
        return stringListToArrayString(v.toStringList(), &m_expander);

    return v.toString();
}

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);
        for (const QVariant &o : optList) {
            QVariantMap optionObject = o.toMap();
            JsonWizard::OptionDefinition odef;
            odef.m_key = optionObject.value(QLatin1String("key")).toString();
            odef.m_value = optionObject.value(QLatin1String("value")).toString();
            odef.m_condition = optionObject.value(QLatin1String("condition"), true);
            odef.m_evaluate = optionObject.value(QLatin1String("evaluate"), false);

            if (odef.m_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());
    if (v.isValid())
        return v;
    if (hasField(n))
        return field(n); // Cannot contain macros!
    return QVariant();
}

bool JsonWizard::boolFromVariant(const QVariant &v, MacroExpander *expander)
{
    if (v.type() == QVariant::String) {
        const QString tmp = expander->expand(v.toString());
        return !(tmp.isEmpty() || tmp == QLatin1String("false"));
    }
    return v.toBool();
}

QString JsonWizard::stringListToArrayString(const QStringList &list, const MacroExpander *expander)
{
    // Todo: Handle ' embedded in the strings better.
    if (list.isEmpty())
        return QString();

    QStringList tmp = Utils::transform(list, [expander](const QString &i) {
        return expander->expand(i).replace(QLatin1Char('\''), QLatin1String("\\'"));
    });

    QString result;
    result.append(QLatin1Char('\''));
    result.append(tmp.join(QLatin1String("', '")));
    result.append(QLatin1Char('\''));

    return result;
}

void JsonWizard::removeAttributeFromAllFiles(Core::GeneratedFile::Attribute a)
{
    for (int i = 0; i < m_files.count(); ++i) {
        if (m_files.at(i).file.attributes() & a)
            m_files[i].file.setAttributes(m_files.at(i).file.attributes() ^ a);
    }
}

QHash<QString, QVariant> JsonWizard::variables() const
{
    QHash<QString, QVariant> result = Wizard::variables();
    const QList<QByteArray> properties = dynamicPropertyNames();
    for (const QByteArray &p : properties) {
        QString key = QString::fromUtf8(p);
        result.insert(key, value(key));
    }
    return result;
}

void JsonWizard::accept()
{
    auto page = qobject_cast<WizardPage *>(currentPage());
    if (page && page->handleAccept())
        return;

    Wizard::accept();

    QString errorMessage;
    if (m_files.isEmpty()) {
        commitToFileList(generateFileList()); // The Summary page does this for us, but a wizard
                                              // does not need to have one.
    }
    QTC_ASSERT(!m_files.isEmpty(), return);

    emit prePromptForOverwrite(m_files);
    JsonWizardGenerator::OverwriteResult overwrite =
            JsonWizardGenerator::promptForOverwrite(&m_files, &errorMessage);
    if (overwrite != JsonWizardGenerator::OverwriteOk) {
        if (!errorMessage.isEmpty())
            QMessageBox::warning(this, tr("Failed to Overwrite Files"), errorMessage);
        return;
    }

    emit preFormatFiles(m_files);
    if (!JsonWizardGenerator::formatFiles(this, &m_files, &errorMessage)) {
        if (!errorMessage.isEmpty())
            QMessageBox::warning(this, tr("Failed to Format Files"), errorMessage);
        return;
    }

    emit preWriteFiles(m_files);
    if (!JsonWizardGenerator::writeFiles(this, &m_files, &errorMessage)) {
        if (!errorMessage.isEmpty())
            QMessageBox::warning(this, tr("Failed to Write Files"), errorMessage);
        return;
    }

    emit postProcessFiles(m_files);
    if (!JsonWizardGenerator::postWrite(this, &m_files, &errorMessage)) {
        if (!errorMessage.isEmpty())
            QMessageBox::warning(this, tr("Failed to Post-Process Files"), errorMessage);
        return;
    }
    emit filesReady(m_files);
    if (!JsonWizardGenerator::polish(this, &m_files, &errorMessage)) {
        if (!errorMessage.isEmpty())
            QMessageBox::warning(this, tr("Failed to Polish Files"), errorMessage);
        return;
    }
    emit filesPolished(m_files);
    if (!JsonWizardGenerator::allDone(this, &m_files, &errorMessage)) {
        if (!errorMessage.isEmpty())
            QMessageBox::warning(this, tr("Failed to Open Files"), errorMessage);
        return;
    }
    emit allDone(m_files);

    openFiles(m_files);

    auto node = static_cast<ProjectExplorer::Node*>(value(ProjectExplorer::Constants::PREFERRED_PROJECT_NODE).value<void*>());
    if (node && ProjectTree::hasNode(node)) // PREFERRED_PROJECT_NODE is not set for newly created projects
        openProjectForNode(node);
}

void JsonWizard::reject()
{
    auto page = qobject_cast<WizardPage *>(currentPage());
    if (page && page->handleReject())
        return;

    Wizard::reject();
}

void JsonWizard::handleNewPages(int pageId)
{
    auto wp = qobject_cast<WizardPage *>(page(pageId));
    if (!wp)
        return;

    connect(wp, &WizardPage::reportError, this, &JsonWizard::handleError);
}

void JsonWizard::handleError(const QString &message)
{
    Core::MessageManager::writeDisrupting(message);
}

QString JsonWizard::stringify(const QVariant &v) const
{
    if (v.type() == QVariant::StringList)
        return stringListToArrayString(v.toStringList(), &m_expander);
    return Wizard::stringify(v);
}

QString JsonWizard::evaluate(const QVariant &v) const
{
    return m_expander.expand(stringify(v));
}

void JsonWizard::openFiles(const JsonWizard::GeneratorFiles &files)
{
    QString errorMessage;
    bool openedSomething = stringValue("DoNotOpenFile") == "true";
    for (const JsonWizard::GeneratorFile &f : files) {
        const Core::GeneratedFile &file = f.file;
        if (!file.filePath().exists()) {
            errorMessage = QCoreApplication::translate("ProjectExplorer::JsonWizard",
                                                       "\"%1\" does not exist in the file system.")
                    .arg(file.filePath().toUserOutput());
            break;
        }
        if (file.attributes() & Core::GeneratedFile::OpenProjectAttribute) {
            ProjectExplorerPlugin::OpenProjectResult result
                    = ProjectExplorerPlugin::openProject(file.filePath());
            if (!result) {
                errorMessage = result.errorMessage();
                if (errorMessage.isEmpty()) {
                    errorMessage = QCoreApplication::translate("ProjectExplorer::JsonWizard",
                                                               "Failed to open \"%1\" as a project.")
                            .arg(file.filePath().toUserOutput());
                }
                break;
            }
            result.project()->setNeedsInitialExpansion(true);
            openedSomething = true;
        }
        if (file.attributes() & Core::GeneratedFile::OpenEditorAttribute) {
            Core::IEditor *editor = Core::EditorManager::openEditor(file.filePath(),
                                                                    file.editorId());
            if (!editor) {
                errorMessage = QCoreApplication::translate("ProjectExplorer::JsonWizard",
                                                           "Failed to open an editor for \"%1\".")
                        .arg(file.filePath().toUserOutput());
                break;
            } else if (file.attributes() & Core::GeneratedFile::TemporaryFile) {
                editor->document()->setTemporary(true);
            }
            openedSomething = true;
        }
    }

    const QString path
            = QDir::toNativeSeparators(m_expander.expand(value(QLatin1String("TargetPath")).toString()));

    // Now try to find the project file and open
    if (!openedSomething) {
        errorMessage = QCoreApplication::translate("ProjectExplorer::JsonWizard",
                                                   "No file to open found in \"%1\".")
                .arg(path);
    }

    if (!errorMessage.isEmpty()) {
        const QString text = path.isEmpty() ? tr("Failed to open project.")
                                            : tr("Failed to open project in \"%1\".").arg(path);
        QMessageBox msgBox(QMessageBox::Warning, tr("Cannot Open Project"), text);
        msgBox.setDetailedText(errorMessage);
        msgBox.addButton(QMessageBox::Ok);
        msgBox.exec();
    }
}

void JsonWizard::openProjectForNode(Node *node)
{
    const ProjectNode *projNode = node->asProjectNode();
    if (!projNode) {
        if (ContainerNode * const cn = node->asContainerNode())
            projNode = cn->rootProjectNode();
        else
            projNode = node->parentProjectNode();
    }
    QTC_ASSERT(projNode, return);

    std::optional<FilePath> projFilePath = projNode->visibleAfterAddFileAction();

    if (projFilePath && !Core::EditorManager::openEditor(projFilePath.value())) {
            auto errorMessage = QCoreApplication::translate("ProjectExplorer::JsonWizard",
                                                       "Failed to open an editor for \"%1\".")
                    .arg(QDir::toNativeSeparators(projFilePath.value().toString()));
            QMessageBox::warning(nullptr, tr("Cannot Open Project"), errorMessage);
    }
}

QString JsonWizard::OptionDefinition::value(MacroExpander &expander) const
{
    if (JsonWizard::boolFromVariant(m_evaluate, &expander))
        return expander.expand(m_value);
    return m_value;
}

bool JsonWizard::OptionDefinition::condition(MacroExpander &expander) const
{
    return JsonWizard::boolFromVariant(m_condition, &expander);
}

namespace Internal {

JsonWizardJsExtension::JsonWizardJsExtension(JsonWizard *wizard)
    : m_wizard(wizard)
{}

QVariant JsonWizardJsExtension::value(const QString &name) const
{
    return m_wizard->expander()->expandVariant(m_wizard->value(name));
}

} // namespace Internal
} // namespace ProjectExplorer