aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/extensionsystem/optionsparser.cpp
blob: 21dfba16ecbe7343adc418aedd07d225743f477f (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
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "optionsparser.h"

#include "extensionsystemtr.h"
#include "pluginmanager.h"
#include "pluginmanager_p.h"

#include <utils/algorithm.h>

#include <QCoreApplication>

using namespace ExtensionSystem;
using namespace ExtensionSystem::Internal;

const char END_OF_OPTIONS[] = "--";
const char *OptionsParser::NO_LOAD_OPTION = "-noload";
const char *OptionsParser::LOAD_OPTION = "-load";
const char *OptionsParser::TEST_OPTION = "-test";
const char *OptionsParser::NOTEST_OPTION = "-notest";
const char *OptionsParser::SCENARIO_OPTION = "-scenario";
const char *OptionsParser::PROFILE_OPTION = "-profile";
const char *OptionsParser::TRACE_OPTION = "-trace";
const char *OptionsParser::NO_CRASHCHECK_OPTION = "-no-crashcheck";

OptionsParser::OptionsParser(const QStringList &args,
        const QMap<QString, bool> &appOptions,
        QMap<QString, QString> *foundAppOptions,
        QString *errorString,
        PluginManagerPrivate *pmPrivate)
    : m_args(args), m_appOptions(appOptions),
      m_foundAppOptions(foundAppOptions),
      m_errorString(errorString),
      m_pmPrivate(pmPrivate),
      m_it(m_args.constBegin()),
      m_end(m_args.constEnd()),
      m_isDependencyRefreshNeeded(false),
      m_hasError(false)
{
    ++m_it; // jump over program name
    if (m_errorString)
        m_errorString->clear();
    if (m_foundAppOptions)
        m_foundAppOptions->clear();
    m_pmPrivate->arguments.clear();
    m_pmPrivate->argumentsForRestart.clear();
}

bool OptionsParser::parse()
{
    while (!m_hasError) {
        if (!nextToken()) // move forward
            break;
        if (checkForEndOfOptions())
            break;
        if (checkForLoadOption())
            continue;
        if (checkForNoLoadOption())
            continue;
        if (checkForProfilingOption())
            continue;
        if (checkForTraceOption())
            continue;
        if (checkForNoCrashcheckOption())
            continue;
#ifdef EXTENSIONSYSTEM_WITH_TESTOPTION
        if (checkForTestOptions())
            continue;
        if (checkForScenarioOption())
            continue;
#endif
        if (checkForAppOption())
            continue;
        if (checkForPluginOption())
            continue;
        if (checkForUnknownOption())
            break;
        // probably a file or something
        m_pmPrivate->arguments << m_currentArg;
    }
    if (PluginManager::testRunRequested()) {
        m_isDependencyRefreshNeeded = true;
        forceDisableAllPluginsExceptTestedAndForceEnabled();
    }
    if (m_isDependencyRefreshNeeded)
        m_pmPrivate->enableDependenciesIndirectly();
    return !m_hasError;
}

bool OptionsParser::checkForEndOfOptions()
{
    if (m_currentArg != QLatin1String(END_OF_OPTIONS))
        return false;
    while (nextToken()) {
        m_pmPrivate->arguments << m_currentArg;
    }
    return true;
}

bool OptionsParser::checkForTestOptions()
{
    if (m_currentArg == QLatin1String(TEST_OPTION)) {
        if (nextToken(RequiredToken)) {
            if (m_currentArg == QLatin1String("all")) {
                m_pmPrivate->testSpecs
                    = Utils::transform<std::vector>(m_pmPrivate->loadQueue(), [](PluginSpec *spec) {
                          return PluginManagerPrivate::TestSpec(spec);
                      });
            } else {
                QStringList args = m_currentArg.split(QLatin1Char(','));
                const QString pluginName = args.takeFirst();
                if (PluginSpec *spec = m_pmPrivate->pluginByName(pluginName)) {
                    if (m_pmPrivate->containsTestSpec(spec)) {
                        if (m_errorString)
                            *m_errorString = Tr::tr("The plugin \"%1\" is specified twice for testing.").arg(pluginName);
                        m_hasError = true;
                    } else {
                        m_pmPrivate->testSpecs.emplace_back(spec, args);
                    }
                } else {
                    if (m_errorString)
                        *m_errorString = Tr::tr("The plugin \"%1\" does not exist.").arg(pluginName);
                    m_hasError = true;
                }
            }
        }
        return true;
    } else if (m_currentArg == QLatin1String(NOTEST_OPTION)) {
        if (nextToken(RequiredToken)) {
            if (PluginSpec *spec = m_pmPrivate->pluginByName(m_currentArg)) {
                if (!m_pmPrivate->containsTestSpec(spec)) {
                    if (m_errorString)
                        *m_errorString = Tr::tr("The plugin \"%1\" is not tested.").arg(m_currentArg);
                    m_hasError = true;
                } else {
                    m_pmPrivate->removeTestSpec(spec);
                }
            } else {
                if (m_errorString)
                    *m_errorString = Tr::tr("The plugin \"%1\" does not exist.").arg(m_currentArg);
                m_hasError = true;
            }
        }
        return true;
    }
    return false;
}

bool OptionsParser::checkForScenarioOption()
{
    if (m_currentArg == QLatin1String(SCENARIO_OPTION)) {
        if (nextToken(RequiredToken)) {
            if (!m_pmPrivate->m_requestedScenario.isEmpty()) {
                if (m_errorString) {
                    *m_errorString = Tr::tr(
                        "Cannot request scenario \"%1\" as it was already requested.")
                        .arg(m_currentArg, m_pmPrivate->m_requestedScenario);
                }
                m_hasError = true;
            } else {
                // It's called before we register scenarios, so we don't check if the requested
                // scenario was already registered yet.
                m_pmPrivate->m_requestedScenario = m_currentArg;
            }
        }
        return true;
    }
    return false;
}

bool OptionsParser::checkForLoadOption()
{
    if (m_currentArg != QLatin1String(LOAD_OPTION))
        return false;
    if (nextToken(RequiredToken)) {
        if (m_currentArg == QLatin1String("all")) {
            for (PluginSpec *spec : std::as_const(m_pmPrivate->pluginSpecs))
                spec->setForceEnabled(true);
            m_isDependencyRefreshNeeded = true;
        } else {
            PluginSpec *spec = m_pmPrivate->pluginByName(m_currentArg);
            if (!spec) {
                if (m_errorString)
                    *m_errorString = Tr::tr("The plugin \"%1\" does not exist.").arg(m_currentArg);
                m_hasError = true;
            } else {
                spec->setForceEnabled(true);
                m_isDependencyRefreshNeeded = true;
            }
        }
        m_pmPrivate->argumentsForRestart << QLatin1String(LOAD_OPTION) << m_currentArg;
    }
    return true;
}

bool OptionsParser::checkForNoLoadOption()
{
    if (m_currentArg != QLatin1String(NO_LOAD_OPTION))
        return false;
    if (nextToken(RequiredToken)) {
        if (m_currentArg == QLatin1String("all")) {
            for (PluginSpec *spec : std::as_const(m_pmPrivate->pluginSpecs))
                spec->setForceDisabled(true);
            m_isDependencyRefreshNeeded = true;
        } else {
            PluginSpec *spec = m_pmPrivate->pluginByName(m_currentArg);
            if (!spec) {
                if (m_errorString)
                    *m_errorString = Tr::tr("The plugin \"%1\" does not exist.").arg(m_currentArg);
                m_hasError = true;
            } else {
                spec->setForceDisabled(true);
                // recursively disable all plugins that require this plugin
                for (PluginSpec *dependantSpec : PluginManager::pluginsRequiringPlugin(spec))
                    dependantSpec->setForceDisabled(true);
                m_isDependencyRefreshNeeded = true;
            }
        }
        m_pmPrivate->argumentsForRestart << QLatin1String(NO_LOAD_OPTION) << m_currentArg;
    }
    return true;
}

bool OptionsParser::checkForAppOption()
{
    if (!m_appOptions.contains(m_currentArg))
        return false;
    QString option = m_currentArg;
    QString argument;
    if (m_appOptions.value(m_currentArg) && nextToken(RequiredToken)) {
        //argument required
        argument = m_currentArg;
    }
    if (m_foundAppOptions)
        m_foundAppOptions->insert(option, argument);
    return true;
}

bool OptionsParser::checkForProfilingOption()
{
    if (m_currentArg != QLatin1String(PROFILE_OPTION))
        return false;
    m_pmPrivate->increaseProfilingVerbosity();
    return true;
}

bool OptionsParser::checkForTraceOption()
{
    if (m_currentArg != QLatin1String(TRACE_OPTION))
        return false;
    if (nextToken(RequiredToken)) {
        m_pmPrivate->enableTracing(m_currentArg);
    }
    return true;
}

bool OptionsParser::checkForNoCrashcheckOption()
{
    if (m_currentArg != QLatin1String(NO_CRASHCHECK_OPTION))
        return false;
    m_pmPrivate->enableCrashCheck = false;
    return true;
}

bool OptionsParser::checkForPluginOption()
{
    bool requiresParameter;
    PluginSpec *spec = m_pmPrivate->pluginForOption(m_currentArg, &requiresParameter);
    if (!spec)
        return false;
    spec->addArgument(m_currentArg);
    m_pmPrivate->argumentsForRestart << m_currentArg;
    if (requiresParameter && nextToken(RequiredToken)) {
        spec->addArgument(m_currentArg);
        m_pmPrivate->argumentsForRestart << m_currentArg;
    }
    return true;
}

bool OptionsParser::checkForUnknownOption()
{
    if (!m_currentArg.startsWith(QLatin1Char('-')))
        return false;
    if (m_errorString)
        *m_errorString = Tr::tr("Unknown option %1").arg(m_currentArg);
    m_hasError = true;
    return true;
}

void OptionsParser::forceDisableAllPluginsExceptTestedAndForceEnabled()
{
    for (const PluginManagerPrivate::TestSpec &testSpec : m_pmPrivate->testSpecs)
        testSpec.pluginSpec->setForceEnabled(true);
    for (PluginSpec *spec : std::as_const(m_pmPrivate->pluginSpecs)) {
        if (!spec->isForceEnabled() && !spec->isRequired())
            spec->setForceDisabled(true);
    }
}

bool OptionsParser::nextToken(OptionsParser::TokenType type)
{
    if (m_it == m_end) {
        if (type == OptionsParser::RequiredToken) {
            m_hasError = true;
            if (m_errorString)
                *m_errorString = Tr::tr("The option %1 requires an argument.").arg(m_currentArg);
        }
        return false;
    }
    m_currentArg = *m_it;
    ++m_it;
    return true;
}