aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/nim/project/nimbletaskstep.cpp
blob: 6a858f32812cddaf5fce844244df351a90cdbca5 (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
// Copyright (C) Filippo Cucchetto <filippocucchetto@gmail.com>
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "nimbletaskstep.h"

#include "nimconstants.h"
#include "nimblebuildsystem.h"
#include "nimbleproject.h"

#include <projectexplorer/abstractprocessstep.h>
#include <projectexplorer/buildstep.h>
#include <projectexplorer/buildconfiguration.h>
#include <projectexplorer/processparameters.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/target.h>

#include <utils/aspects.h>
#include <utils/algorithm.h>
#include <utils/fileutils.h>
#include <utils/layoutbuilder.h>

#include <QFormLayout>
#include <QLineEdit>
#include <QListView>
#include <QStandardItemModel>
#include <QStandardPaths>

using namespace ProjectExplorer;
using namespace Utils;

namespace Nim {

class NimbleTaskStep final : public AbstractProcessStep
{
    Q_DECLARE_TR_FUNCTIONS(Nim::NimbleTaskStep)

public:
    NimbleTaskStep(BuildStepList *parentList, Id id);

private:
    QWidget *createConfigWidget() final;

    void setTaskName(const QString &name);

    void updateTaskList();
    void selectTask(const QString &name);
    void onDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles);
    void uncheckedAllDifferentFrom(QStandardItem *item);

    bool validate();

    StringAspect *m_taskName = nullptr;
    StringAspect *m_taskArgs = nullptr;

    QStandardItemModel m_tasks;
    bool m_selecting = false;
};

NimbleTaskStep::NimbleTaskStep(BuildStepList *parentList, Id id)
    : AbstractProcessStep(parentList, id)
{
    setDefaultDisplayName(tr(Constants::C_NIMBLETASKSTEP_DISPLAY));
    setDisplayName(tr(Constants::C_NIMBLETASKSTEP_DISPLAY));

    setCommandLineProvider([this] {
        QString args = m_taskName->value() + " " + m_taskArgs->value();
        return CommandLine(Nim::nimblePathFromKit(target()->kit()), args, CommandLine::Raw);
    });

    setWorkingDirectoryProvider([this] { return project()->projectDirectory(); });

    m_taskName = addAspect<StringAspect>();
    m_taskName->setSettingsKey(Constants::C_NIMBLETASKSTEP_TASKNAME);

    m_taskArgs = addAspect<StringAspect>();
    m_taskArgs->setSettingsKey(Constants::C_NIMBLETASKSTEP_TASKARGS);
    m_taskArgs->setDisplayStyle(StringAspect::LineEditDisplay);
    m_taskArgs->setLabelText(tr("Task arguments:"));
}

QWidget *NimbleTaskStep::createConfigWidget()
{
    auto taskList = new QListView;
    taskList->setFrameShape(QFrame::StyledPanel);
    taskList->setSelectionMode(QAbstractItemView::NoSelection);
    taskList->setSelectionBehavior(QAbstractItemView::SelectRows);
    taskList->setModel(&m_tasks);

    using namespace Layouting;
    auto widget = Form {
        m_taskArgs,
        tr("Tasks:"), taskList
    }.emerge(WithoutMargins);

    auto buildSystem = dynamic_cast<NimbleBuildSystem *>(this->buildSystem());
    QTC_ASSERT(buildSystem, return widget);

    updateTaskList();
    selectTask(m_taskName->value());

    connect(&m_tasks, &QAbstractItemModel::dataChanged, this, &NimbleTaskStep::onDataChanged);

    connect(buildSystem, &NimbleBuildSystem::tasksChanged, this, &NimbleTaskStep::updateTaskList);

    setSummaryUpdater([this] {
        return QString("<b>%1:</b> nimble %2 %3")
                .arg(displayName(), m_taskName->value(), m_taskArgs->value());
    });

    return widget;
}

void NimbleTaskStep::updateTaskList()
{
    auto buildSystem = dynamic_cast<NimbleBuildSystem *>(this->buildSystem());
    QTC_ASSERT(buildSystem, return);
    const std::vector<NimbleTask> &tasks = buildSystem->tasks();

    QSet<QString> newTasks;
    for (const NimbleTask &t : tasks)
        newTasks.insert(t.name);

    QSet<QString> currentTasks;
    for (int i = 0; i < m_tasks.rowCount(); ++i)
        currentTasks.insert(m_tasks.item(i)->text());

    const QSet<QString> added = newTasks - currentTasks;
    const QSet<QString> removed = currentTasks - newTasks;

    for (const QString &name : added) {
        auto item = new QStandardItem();
        item->setText(name);
        item->setCheckable(true);
        item->setCheckState(Qt::Unchecked);
        item->setEditable(false);
        item->setSelectable(false);
        m_tasks.appendRow(item);
    }

    for (int i = m_tasks.rowCount() - 1; i >= 0; i--)
        if (removed.contains(m_tasks.item(i)->text()))
            m_tasks.removeRow(i);

    m_tasks.sort(0);
}

void NimbleTaskStep::selectTask(const QString &name)
{
    if (m_selecting)
        return;
    m_selecting = true;

    QList<QStandardItem *> items = m_tasks.findItems(name);
    QStandardItem *item = items.empty() ? nullptr : items.front();
    uncheckedAllDifferentFrom(item);
    if (item)
        item->setCheckState(Qt::Checked);

    setTaskName(name);

    m_selecting = false;
}

void NimbleTaskStep::onDataChanged(const QModelIndex &topLeft,
                                   const QModelIndex &bottomRight,
                                   const QVector<int> &roles)
{
    QTC_ASSERT(topLeft == bottomRight, return );
    if (!roles.contains(Qt::CheckStateRole))
        return;

    auto item = m_tasks.itemFromIndex(topLeft);
    if (!item)
        return;

    if (m_selecting)
        return;
    m_selecting = true;

    if (item->checkState() == Qt::Checked) {
        uncheckedAllDifferentFrom(item);
        setTaskName(item->text());
    } else if (item->checkState() == Qt::Unchecked) {
        setTaskName(QString());
    }

    m_selecting = false;
}

void NimbleTaskStep::uncheckedAllDifferentFrom(QStandardItem *toSkip)
{
    for (int i = 0; i < m_tasks.rowCount(); ++i) {
        auto item = m_tasks.item(i);
        if (!item || item == toSkip)
            continue;
        item->setCheckState(Qt::Unchecked);
    }
}

void NimbleTaskStep::setTaskName(const QString &name)
{
    if (m_taskName->value() == name)
        return;
    m_taskName->setValue(name);
    selectTask(name);
}

bool NimbleTaskStep::validate()
{
    if (m_taskName->value().isEmpty())
        return true;

    auto nimbleBuildSystem = dynamic_cast<NimbleBuildSystem*>(buildSystem());
    QTC_ASSERT(nimbleBuildSystem, return false);

    auto matchName = [this](const NimbleTask &task) { return task.name == m_taskName->value(); };
    if (!Utils::contains(nimbleBuildSystem->tasks(), matchName)) {
        emit addTask(BuildSystemTask(Task::Error, tr("Nimble task %1 not found.")
                                     .arg(m_taskName->value())));
        emitFaultyConfigurationMessage();
        return false;
    }

    return true;
}

// Factory

NimbleTaskStepFactory::NimbleTaskStepFactory()
{
    registerStep<NimbleTaskStep>(Constants::C_NIMBLETASKSTEP_ID);
    setDisplayName(NimbleTaskStep::tr("Nimble Task"));
    setSupportedStepLists({ProjectExplorer::Constants::BUILDSTEPS_BUILD,
                           ProjectExplorer::Constants::BUILDSTEPS_CLEAN,
                           ProjectExplorer::Constants::BUILDSTEPS_DEPLOY});
    setSupportedConfiguration(Constants::C_NIMBLEBUILDCONFIGURATION_ID);
    setRepeatable(true);
}

} // Nim