aboutsummaryrefslogtreecommitdiffstats
path: root/src/tools/sdktool/rmtoolchainoperation.cpp
blob: 8d01f3c7610832d160226b1823c05e5d4ebfdf84 (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
// 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 "rmtoolchainoperation.h"

#include "addkeysoperation.h"
#include "addtoolchainoperation.h"
#include "findkeyoperation.h"
#include "findvalueoperation.h"
#include "getoperation.h"
#include "rmkeysoperation.h"

#include <iostream>

#ifdef WITH_TESTS
#include <QTest>
#endif

#include <QLoggingCategory>

Q_LOGGING_CATEGORY(rmtoolchainlog, "qtc.sdktool.operations.rmtoolchain", QtWarningMsg)

// ToolChain file stuff:
const char COUNT[] = "ToolChain.Count";
const char PREFIX[] = "ToolChain.";

// ToolChain:
const char ID[] = "ProjectExplorer.ToolChain.Id";

QString RmToolChainOperation::name() const
{
    return QString("rmTC");
}

QString RmToolChainOperation::helpText() const
{
    return QString("remove a tool chain");
}

QString RmToolChainOperation::argumentsHelpText() const
{
    return QString("    --id <ID>  The id of the tool chain to remove.\n");
}

bool RmToolChainOperation::setArguments(const QStringList &args)
{
    for (int i = 0; i < args.count(); ++i) {
        const QString current = args.at(i);
        const QString next = ((i + 1) < args.count()) ? args.at(i + 1) : QString();

        if (current == "--id") {
            if (next.isNull()) {
                qCCritical(rmtoolchainlog) << "No parameter for --id given.";
                return false;
            }
            ++i; // skip next;
            m_id = next;
            continue;
        }
    }

    if (m_id.isEmpty())
        qCCritical(rmtoolchainlog) << "No id given.";

    return !m_id.isEmpty();
}

int RmToolChainOperation::execute() const
{
    QVariantMap map = load("ToolChains");
    if (map.isEmpty())
        return 0;

    QVariantMap result = rmToolChain(map, m_id);
    if (result == map)
        return 2;

    return save(result, "ToolChains") ? 0 : 3;
}

#ifdef WITH_TESTS
void RmToolChainOperation::unittest()
{
    // Add toolchain:
    QVariantMap map = AddToolChainOperation::initializeToolChains();

    AddToolChainData d;
    d.m_id = "testId";
    d.m_languageId = "langId";
    d.m_displayName = "name";
    d.m_path = "/tmp/test";
    d.m_targetAbi = "test-abi";
    d.m_supportedAbis = "test-abi,test-abi2";
    d.m_extra = {{"ExtraKey", QVariant("ExtraValue")}};

    map = d.addToolChain(map);

    d.m_id = "testId2";
    d.m_languageId = "langId";
    d.m_displayName = "other name";
    d.m_path = "/tmp/test2";
    d.m_targetAbi = "test-abi";
    d.m_supportedAbis = "test-abi,test-abi2";
    d.m_extra = {};
    map = d.addToolChain(map);

    QTest::ignoreMessage(QtCriticalMsg, "Error: Count found in toolchains file seems wrong.");
    QVariantMap result = rmToolChain(QVariantMap(), "nonexistent");
    QVERIFY(result.isEmpty());

    result = rmToolChain(map, "nonexistent");
    QCOMPARE(result, map);

    result = rmToolChain(map, "testId2");
    QVERIFY(result != map);
    QCOMPARE(result.value(COUNT, 0).toInt(), 1);
    QVERIFY(result.contains("ToolChain.0"));
    QCOMPARE(result.value("ToolChain.0"), map.value("ToolChain.0"));

    result = rmToolChain(map, "testId");
    QVERIFY(result != map);
    QCOMPARE(result.value(COUNT, 0).toInt(), 1);
    QVERIFY(result.contains("ToolChain.0"));
    QCOMPARE(result.value("ToolChain.0"), map.value("ToolChain.1"));

    result = rmToolChain(result, "testId2");
    QVERIFY(result != map);

    QCOMPARE(result.value(COUNT, 0).toInt(), 0);
}
#endif

QVariantMap RmToolChainOperation::rmToolChain(const QVariantMap &map, const QString &id)
{

    // Find count of tool chains:
    bool ok;
    int count = GetOperation::get(map, COUNT).toInt(&ok);
    if (!ok || count < 0) {
        qCCritical(rmtoolchainlog) << "Error: Count found in toolchains file seems wrong.";
        return map;
    }

    QVariantList tcList;
    for (int i = 0; i < count; ++i) {
        QVariantMap tcData
            = GetOperation::get(map, QString::fromLatin1(PREFIX) + QString::number(i)).toMap();
        if (tcData.value(ID).toString() != id)
            tcList.append(tcData);
    }

    QVariantMap newMap = AddToolChainOperation::initializeToolChains();
    for (int i = 0; i < tcList.count(); ++i)
        newMap.insert(QString::fromLatin1(PREFIX) + QString::number(i), tcList.at(i));
    newMap.insert(COUNT, tcList.count());

    return newMap;
}