aboutsummaryrefslogtreecommitdiffstats
path: root/src/tools/sdktool/addcmakeoperation.cpp
blob: 893b9c5b6122da850b88a9045c2d0da2b5a973a2 (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
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#include "addcmakeoperation.h"

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

#include "settings.h"

#include <iostream>

#include <QDir>

// CMakeTools file stuff:
const char COUNT[] = "CMakeTools.Count";
const char PREFIX[] = "CMakeTools.";
const char VERSION[] = "Version";

// CMake:
const char ID_KEY[] = "Id";
const char DISPLAYNAME_KEY[] = "DisplayName";
const char AUTODETECTED_KEY[] = "Autodetect";
const char PATH_KEY[] = "Binary";

QString AddCMakeOperation::name() const
{
    return QString("addCMake");
}

QString AddCMakeOperation::helpText() const
{
    return QString("add a cmake tool");
}

QString AddCMakeOperation::argumentsHelpText() const
{
    return QString(
           "    --id <ID>                                  id of the new cmake (required).\n"
           "    --name <NAME>                              display name of the new cmake (required).\n"
           "    --path <PATH>                              path to the cmake binary (required).\n"
           "    <KEY> <TYPE:VALUE>                         extra key value pairs\n");
}

bool AddCMakeOperation::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 (next.isNull() && current.startsWith("--")) {
            std::cerr << "No parameter for option '" << qPrintable(current) << "' given." << std::endl << std::endl;
            return false;
        }

        if (current == "--id") {
            ++i; // skip next;
            m_id = next;
            continue;
        }

        if (current == "--name") {
            ++i; // skip next;
            m_displayName = next;
            continue;
        }

        if (current == "--path") {
            ++i; // skip next;
            m_path = QDir::fromNativeSeparators(next);
            continue;
        }
        if (next.isNull()) {
            std::cerr << "No value given for key '" << qPrintable(current) << "'.";
            return false;
        }
        ++i; // skip next;
        KeyValuePair pair(current, next);
        if (!pair.value.isValid()) {
            std::cerr << "Value for key '" << qPrintable(current) << "' is not valid.";
            return false;
        }
        m_extra << pair;
    }

    if (m_id.isEmpty())
        std::cerr << "No id given for cmake tool." << std::endl;
    if (m_displayName.isEmpty())
        std::cerr << "No name given for cmake tool." << std::endl;
    if (m_path.isEmpty())
        std::cerr << "No path given for cmake tool." << std::endl;

    return !m_id.isEmpty() && !m_displayName.isEmpty() && !m_path.isEmpty();
}

int AddCMakeOperation::execute() const
{
    QVariantMap map = load("cmaketools");
    if (map.isEmpty())
        map = initializeCMake();

    QVariantMap result = addCMake(map, m_id, m_displayName, m_path, m_extra);
    if (result.isEmpty() || map == result)
        return 2;

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

#ifdef WITH_TESTS
bool AddCMakeOperation::test() const
{
    QVariantMap map = initializeCMake();

    // Add toolchain:
    map = addCMake(map, "testId", "name", "/tmp/test",
                   KeyValuePairList() << KeyValuePair("ExtraKey", QVariant("ExtraValue")));
    if (map.value(COUNT).toInt() != 1
            || !map.contains(QString::fromLatin1(PREFIX) + '0'))
        return false;
    QVariantMap cmData = map.value(QString::fromLatin1(PREFIX) + '0').toMap();
    if (cmData.count() != 5
            || cmData.value(ID_KEY).toString() != "testId"
            || cmData.value(DISPLAYNAME_KEY).toString() != "name"
            || cmData.value(AUTODETECTED_KEY).toBool() != true
            || cmData.value(PATH_KEY).toString() != "/tmp/test"
            || cmData.value("ExtraKey").toString() != "ExtraValue")
        return false;

    // Ignore same Id:
    QVariantMap unchanged = addCMake(map, "testId", "name2", "/tmp/test2",
                                     KeyValuePairList() << KeyValuePair("ExtraKey", QVariant("ExtraValue2")));
    if (!unchanged.isEmpty())
        return false;

    // Make sure name stays unique:
    map = addCMake(map, "{some-cm-id}", "name", "/tmp/test",
                   KeyValuePairList() << KeyValuePair("ExtraKey", QVariant("ExtraValue")));
    if (map.value(COUNT).toInt() != 2
            || !map.contains(QString::fromLatin1(PREFIX) + '0')
            || !map.contains(QString::fromLatin1(PREFIX) + '1'))
        return false;
    cmData = map.value(QString::fromLatin1(PREFIX) + '0').toMap();
    if (cmData.count() != 5
            || cmData.value(ID_KEY).toString() != "testId"
            || cmData.value(DISPLAYNAME_KEY).toString() != "name"
            || cmData.value(AUTODETECTED_KEY).toBool() != true
            || cmData.value(PATH_KEY).toString() != "/tmp/test"
            || cmData.value("ExtraKey").toString() != "ExtraValue")
        return false;
    cmData = map.value(QString::fromLatin1(PREFIX) + '1').toMap();
        if (cmData.count() != 5
                || cmData.value(ID_KEY).toString() != "{some-cm-id}"
                || cmData.value(DISPLAYNAME_KEY).toString() != "name2"
                || cmData.value(AUTODETECTED_KEY).toBool() != true
                || cmData.value(PATH_KEY).toString() != "/tmp/test"
                || cmData.value("ExtraKey").toString() != "ExtraValue")
            return false;

    return true;
}
#endif

QVariantMap AddCMakeOperation::addCMake(const QVariantMap &map, const QString &id,
                                        const QString &displayName, const QString &path,
                                        const KeyValuePairList &extra)
{
    // Sanity check: Does the Id already exist?
    if (exists(map, id)) {
        std::cerr << "Error: Id " << qPrintable(id) << " already defined for tool chains." << std::endl;
        return QVariantMap();
    }

    // Find position to insert Tool Chain at:
    bool ok;
    int count = GetOperation::get(map, COUNT).toInt(&ok);
    if (!ok || count < 0) {
        std::cerr << "Error: Count found in toolchains file seems wrong." << std::endl;
        return QVariantMap();
    }

    QVariantMap result = RmKeysOperation::rmKeys(map, {COUNT});

    const QString cm = QString::fromLatin1(PREFIX) + QString::number(count);

    KeyValuePairList data;
    data << KeyValuePair({cm, ID_KEY}, QVariant(id));
    data << KeyValuePair({cm, DISPLAYNAME_KEY}, QVariant(displayName));
    data << KeyValuePair({cm, AUTODETECTED_KEY}, QVariant(true));
    data << KeyValuePair({cm, PATH_KEY}, QVariant(path));
    KeyValuePairList extraList;
    foreach (const KeyValuePair &pair, extra)
        extraList << KeyValuePair(QStringList({cm}) << pair.key, pair.value);
    data.append(extraList);
    data << KeyValuePair(COUNT, QVariant(count + 1));

    return AddKeysOperation::addKeys(result, data);
}

QVariantMap AddCMakeOperation::initializeCMake()
{
    QVariantMap map;
    map.insert(COUNT, 0);
    map.insert(VERSION, 1);
    return map;
}

bool AddCMakeOperation::exists(const QVariantMap &map, const QString &id)
{
    QStringList valueKeys = FindValueOperation::findValue(map, id);
    // support old settings using QByteArray for id's
    valueKeys.append(FindValueOperation::findValue(map, id.toUtf8()));

    foreach (const QString &k, valueKeys) {
        if (k.endsWith(QString('/') + ID_KEY)) {
            return true;
        }
    }
    return false;
}

bool AddCMakeOperation::exists(const QString &id)
{
    QVariantMap map = Operation::load("cmaketools");
    return exists(map, id);
}