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
|
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "sdkmanageroutputparser.h"
#include "androidconstants.h"
#include "androidsdkpackage.h"
#include "avdmanageroutputparser.h"
#include <utils/algorithm.h>
#include <QRegularExpression>
#include <QLoggingCategory>
namespace {
Q_LOGGING_CATEGORY(sdkManagerLog, "qtc.android.sdkManager", QtWarningMsg)
const char installLocationKey[] = "Installed Location:";
const char revisionKey[] = "Version:";
const char descriptionKey[] = "Description:";
} // namespace
using namespace Android;
using namespace Android::Internal;
using MarkerTagsType = std::map<SdkManagerOutputParser::MarkerTag, const char *>;
Q_GLOBAL_STATIC_WITH_ARGS(MarkerTagsType, markerTags,
({{SdkManagerOutputParser::MarkerTag::InstalledPackagesMarker, "Installed packages:"},
{SdkManagerOutputParser::MarkerTag::AvailablePackagesMarkers, "Available Packages:"},
{SdkManagerOutputParser::MarkerTag::AvailableUpdatesMarker, "Available Updates:"},
{SdkManagerOutputParser::MarkerTag::PlatformMarker, "platforms"},
{SdkManagerOutputParser::MarkerTag::SystemImageMarker, "system-images"},
{SdkManagerOutputParser::MarkerTag::BuildToolsMarker, "build-tools"},
{SdkManagerOutputParser::MarkerTag::SdkToolsMarker, "tools"},
{SdkManagerOutputParser::MarkerTag::CmdlineSdkToolsMarker, Constants::cmdlineToolsName},
{SdkManagerOutputParser::MarkerTag::PlatformToolsMarker, "platform-tools"},
{SdkManagerOutputParser::MarkerTag::EmulatorToolsMarker, "emulator"},
{SdkManagerOutputParser::MarkerTag::NdkMarker, Constants::ndkPackageName},
{SdkManagerOutputParser::MarkerTag::ExtrasMarker, "extras"}}));
void SdkManagerOutputParser::parsePackageListing(const QString &output)
{
QStringList packageData;
bool collectingPackageData = false;
MarkerTag currentPackageMarker = MarkerTag::None;
auto processCurrentPackage = [&] {
if (collectingPackageData) {
collectingPackageData = false;
parsePackageData(currentPackageMarker, packageData);
packageData.clear();
}
};
static const QRegularExpression delimiters("[\\n\\r]");
const auto lines = output.split(delimiters);
for (const QString &outputLine : lines) {
// NOTE: we don't want to parse Dependencies part as it does not add value
if (outputLine.startsWith(" "))
continue;
// We don't need to parse this because they would still be listed on available packages
if (m_currentSection == AvailableUpdatesMarker)
continue;
MarkerTag marker = parseMarkers(outputLine.trimmed());
if (marker & SectionMarkers) {
// Section marker found. Update the current section being parsed.
m_currentSection = marker;
processCurrentPackage();
continue;
}
if (m_currentSection == None)
continue; // Continue with the verbose output until a valid section starts.
if (marker == EmptyMarker) {
// Empty marker. Occurs at the end of a package details.
// Process the collected package data, if any.
processCurrentPackage();
continue;
}
if (marker == None) {
if (collectingPackageData)
packageData << outputLine; // Collect data until next marker.
else
continue;
} else {
// Package marker found.
processCurrentPackage(); // New package starts. Process the collected package data, if any.
currentPackageMarker = marker;
collectingPackageData = true;
packageData << outputLine;
}
}
compilePackageAssociations();
}
void SdkManagerOutputParser::compilePackageAssociations()
{
// Return true if package p is already installed i.e. there exists a installed package having
// same sdk style path and same revision as of p.
auto isInstalled = [](const AndroidSdkPackageList &container, AndroidSdkPackage *p) {
return Utils::anyOf(container, [p](AndroidSdkPackage *other) {
return other->state() == AndroidSdkPackage::Installed &&
other->sdkStylePath() == p->sdkStylePath() &&
other->revision() == p->revision();
});
};
auto deleteAlreadyInstalled = [isInstalled](AndroidSdkPackageList &packages) {
for (auto p = packages.begin(); p != packages.end();) {
if ((*p)->state() == AndroidSdkPackage::Available && isInstalled(packages, *p)) {
delete *p;
p = packages.erase(p);
} else {
++p;
}
}
};
// Remove already installed packages.
deleteAlreadyInstalled(m_packages);
// Filter out available images that are already installed.
AndroidSdkPackageList images = m_systemImages.keys();
deleteAlreadyInstalled(images);
// Associate the system images with sdk platforms.
for (AndroidSdkPackage *image : std::as_const(images)) {
int imageApi = m_systemImages[image];
auto itr = std::find_if(m_packages.begin(), m_packages.end(),
[imageApi](const AndroidSdkPackage *p) {
const SdkPlatform *platform = nullptr;
if (p->type() == AndroidSdkPackage::SdkPlatformPackage)
platform = static_cast<const SdkPlatform*>(p);
return platform && platform->apiLevel() == imageApi;
});
if (itr != m_packages.end()) {
auto platform = static_cast<SdkPlatform*>(*itr);
platform->addSystemImage(static_cast<SystemImage *>(image));
}
}
}
void SdkManagerOutputParser::parsePackageData(MarkerTag packageMarker, const QStringList &data)
{
QTC_ASSERT(!data.isEmpty() && packageMarker != None, return);
AndroidSdkPackage *package = nullptr;
auto createPackage = [&](std::function<AndroidSdkPackage *(SdkManagerOutputParser *,
const QStringList &)> creator) {
if ((package = creator(this, data)))
m_packages.append(package);
};
switch (packageMarker) {
case MarkerTag::BuildToolsMarker:
createPackage(&SdkManagerOutputParser::parseBuildToolsPackage);
break;
case MarkerTag::SdkToolsMarker:
createPackage(&SdkManagerOutputParser::parseSdkToolsPackage);
break;
case MarkerTag::CmdlineSdkToolsMarker:
createPackage(&SdkManagerOutputParser::parseSdkToolsPackage);
break;
case MarkerTag::PlatformToolsMarker:
createPackage(&SdkManagerOutputParser::parsePlatformToolsPackage);
break;
case MarkerTag::EmulatorToolsMarker:
createPackage(&SdkManagerOutputParser::parseEmulatorToolsPackage);
break;
case MarkerTag::PlatformMarker:
createPackage(&SdkManagerOutputParser::parsePlatform);
break;
case MarkerTag::SystemImageMarker:
{
QPair<SystemImage *, int> result = parseSystemImage(data);
if (result.first) {
m_systemImages[result.first] = result.second;
package = result.first;
}
}
break;
case MarkerTag::NdkMarker:
createPackage(&SdkManagerOutputParser::parseNdkPackage);
break;
case MarkerTag::ExtrasMarker:
createPackage(&SdkManagerOutputParser::parseExtraToolsPackage);
break;
case MarkerTag::GenericToolMarker:
createPackage(&SdkManagerOutputParser::parseGenericTools);
break;
default:
qCDebug(sdkManagerLog) << "Unhandled package: " << markerTags->at(packageMarker);
break;
}
if (package) {
switch (m_currentSection) {
case MarkerTag::InstalledPackagesMarker:
package->setState(AndroidSdkPackage::Installed);
break;
case MarkerTag::AvailablePackagesMarkers:
case MarkerTag::AvailableUpdatesMarker:
package->setState(AndroidSdkPackage::Available);
break;
default:
qCDebug(sdkManagerLog) << "Invalid section marker: " << markerTags->at(m_currentSection);
break;
}
}
}
/*!
Parses the \a line for a [spaces]key[spaces]value[spaces] pattern and returns
\c true if \a key is found, false otherwise. Result is copied into \a value.
*/
static bool valueForKey(QString key, const QString &line, QString *value = nullptr)
{
auto trimmedInput = line.trimmed();
if (trimmedInput.startsWith(key)) {
if (value)
*value = trimmedInput.section(key, 1, 1).trimmed();
return true;
}
return false;
}
bool SdkManagerOutputParser::parseAbstractData(SdkManagerOutputParser::GenericPackageData &output,
const QStringList &input, int minParts,
const QString &logStrTag,
const QStringList &extraKeys) const
{
if (input.isEmpty()) {
qCDebug(sdkManagerLog) << logStrTag + ": Empty input";
return false;
}
output.headerParts = input.at(0).split(';');
if (output.headerParts.count() < minParts) {
qCDebug(sdkManagerLog) << logStrTag + "%1: Unexpected header:" << input;
return false;
}
QStringList keys = extraKeys;
keys << installLocationKey << revisionKey << descriptionKey;
for (const QString &line : input) {
QString value;
for (const auto &key: std::as_const(keys)) {
if (valueForKey(key, line, &value)) {
if (key == installLocationKey)
output.installedLocation = Utils::FilePath::fromUserInput(value);
else if (key == revisionKey)
output.revision = QVersionNumber::fromString(value);
else if (key == descriptionKey)
output.description = value;
else
output.extraData[key] = value;
break;
}
}
}
return output.isValid();
}
AndroidSdkPackage *SdkManagerOutputParser::parsePlatform(const QStringList &data) const
{
SdkPlatform *platform = nullptr;
GenericPackageData packageData;
if (parseAbstractData(packageData, data, 2, "Platform")) {
const int apiLevel = platformNameToApiLevel(packageData.headerParts.at(1));
if (apiLevel == -1) {
qCDebug(sdkManagerLog) << "Platform: Cannot parse api level:"<< data;
return nullptr;
}
platform = new SdkPlatform(packageData.revision, data.at(0), apiLevel);
platform->setExtension(convertNameToExtension(packageData.headerParts.at(1)));
platform->setInstalledLocation(packageData.installedLocation);
platform->setDescriptionText(packageData.description);
} else {
qCDebug(sdkManagerLog) << "Platform: Parsing failed. Minimum required data unavailable:"
<< data;
}
return platform;
}
QPair<SystemImage *, int> SdkManagerOutputParser::parseSystemImage(const QStringList &data) const
{
QPair <SystemImage *, int> result(nullptr, -1);
GenericPackageData packageData;
if (parseAbstractData(packageData, data, 4, "System-image")) {
const int apiLevel = platformNameToApiLevel(packageData.headerParts.at(1));
if (apiLevel == -1) {
qCDebug(sdkManagerLog) << "System-image: Cannot parse api level:"<< data;
return result;
}
auto image = new SystemImage(packageData.revision, data.at(0),
packageData.headerParts.at(3));
image->setInstalledLocation(packageData.installedLocation);
image->setDisplayText(packageData.description);
image->setDescriptionText(packageData.description);
image->setApiLevel(apiLevel);
result = {image, apiLevel};
} else {
qCDebug(sdkManagerLog) << "System-image: Minimum required data unavailable: "<< data;
}
return result;
}
BuildTools *SdkManagerOutputParser::parseBuildToolsPackage(const QStringList &data) const
{
BuildTools *buildTools = nullptr;
GenericPackageData packageData;
if (parseAbstractData(packageData, data, 2, "Build-tools")) {
buildTools = new BuildTools(packageData.revision, data.at(0));
buildTools->setDescriptionText(packageData.description);
buildTools->setDisplayText(packageData.description);
buildTools->setInstalledLocation(packageData.installedLocation);
} else {
qCDebug(sdkManagerLog) << "Build-tools: Parsing failed. Minimum required data unavailable:"
<< data;
}
return buildTools;
}
SdkTools *SdkManagerOutputParser::parseSdkToolsPackage(const QStringList &data) const
{
SdkTools *sdkTools = nullptr;
GenericPackageData packageData;
if (parseAbstractData(packageData, data, 1, "SDK-tools")) {
sdkTools = new SdkTools(packageData.revision, data.at(0));
sdkTools->setDescriptionText(packageData.description);
sdkTools->setDisplayText(packageData.description);
sdkTools->setInstalledLocation(packageData.installedLocation);
} else {
qCDebug(sdkManagerLog) << "SDK-tools: Parsing failed. Minimum required data unavailable:"
<< data;
}
return sdkTools;
}
PlatformTools *SdkManagerOutputParser::parsePlatformToolsPackage(const QStringList &data) const
{
PlatformTools *platformTools = nullptr;
GenericPackageData packageData;
if (parseAbstractData(packageData, data, 1, "Platform-tools")) {
platformTools = new PlatformTools(packageData.revision, data.at(0));
platformTools->setDescriptionText(packageData.description);
platformTools->setDisplayText(packageData.description);
platformTools->setInstalledLocation(packageData.installedLocation);
} else {
qCDebug(sdkManagerLog) << "Platform-tools: Parsing failed. Minimum required data "
"unavailable:" << data;
}
return platformTools;
}
EmulatorTools *SdkManagerOutputParser::parseEmulatorToolsPackage(const QStringList &data) const
{
EmulatorTools *emulatorTools = nullptr;
GenericPackageData packageData;
if (parseAbstractData(packageData, data, 1, "Emulator-tools")) {
emulatorTools = new EmulatorTools(packageData.revision, data.at(0));
emulatorTools->setDescriptionText(packageData.description);
emulatorTools->setDisplayText(packageData.description);
emulatorTools->setInstalledLocation(packageData.installedLocation);
} else {
qCDebug(sdkManagerLog) << "Emulator-tools: Parsing failed. Minimum required data "
"unavailable:" << data;
}
return emulatorTools;
}
Ndk *SdkManagerOutputParser::parseNdkPackage(const QStringList &data) const
{
Ndk *ndk = nullptr;
GenericPackageData packageData;
if (parseAbstractData(packageData, data, 1, "NDK")) {
ndk = new Ndk(packageData.revision, data.at(0));
ndk->setDescriptionText(packageData.description);
ndk->setDisplayText(packageData.description);
ndk->setInstalledLocation(packageData.installedLocation);
} else {
qCDebug(sdkManagerLog) << "NDK: Parsing failed. Minimum required data unavailable:"
<< data;
}
return ndk;
}
ExtraTools *SdkManagerOutputParser::parseExtraToolsPackage(const QStringList &data) const
{
ExtraTools *extraTools = nullptr;
GenericPackageData packageData;
if (parseAbstractData(packageData, data, 1, "Extras")) {
extraTools = new ExtraTools(packageData.revision, data.at(0));
extraTools->setDescriptionText(packageData.description);
extraTools->setDisplayText(packageData.description);
extraTools->setInstalledLocation(packageData.installedLocation);
} else {
qCDebug(sdkManagerLog) << "Extra-tools: Parsing failed. Minimum required data "
"unavailable:" << data;
}
return extraTools;
}
GenericSdkPackage *SdkManagerOutputParser::parseGenericTools(const QStringList &data) const
{
GenericSdkPackage *sdkPackage = nullptr;
GenericPackageData packageData;
if (parseAbstractData(packageData, data, 1, "Generic")) {
sdkPackage = new GenericSdkPackage(packageData.revision, data.at(0));
sdkPackage->setDescriptionText(packageData.description);
sdkPackage->setDisplayText(packageData.description);
sdkPackage->setInstalledLocation(packageData.installedLocation);
} else {
qCDebug(sdkManagerLog) << "Generic: Parsing failed. Minimum required data "
"unavailable:" << data;
}
return sdkPackage;
}
SdkManagerOutputParser::MarkerTag SdkManagerOutputParser::parseMarkers(const QString &line)
{
if (line.isEmpty())
return EmptyMarker;
for (auto pair : *markerTags) {
if (line.startsWith(QLatin1String(pair.second)))
return pair.first;
}
static const QRegularExpression reg("^[a-zA-Z]+[A-Za-z0-9;._-]+");
const QRegularExpressionMatch match = reg.match(line);
if (match.hasMatch() && match.captured(0) == line)
return GenericToolMarker;
return None;
}
|