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
|
// Copyright (C) 2020 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 "mcusupportversiondetection.h"
#include <utils/qtcprocess.h>
#include <QDir>
#include <QRegularExpression>
#include <QXmlStreamReader>
using namespace Utils;
namespace McuSupport {
namespace Internal {
QString matchRegExp(const QString &text, const QString ®Exp)
{
const QRegularExpression regularExpression(regExp);
const QRegularExpressionMatch match = regularExpression.match(text);
if (match.hasMatch())
return match.captured(regularExpression.captureCount());
return QString();
}
McuPackageExecutableVersionDetector::McuPackageExecutableVersionDetector(
const FilePath &detectionPath, const QStringList &detectionArgs, const QString &detectionRegExp)
: McuPackageVersionDetector()
, m_detectionPath(detectionPath)
, m_detectionArgs(detectionArgs)
, m_detectionRegExp(detectionRegExp)
{}
QString McuPackageExecutableVersionDetector::parseVersion(const FilePath &packagePath) const
{
if (m_detectionPath.isEmpty() || m_detectionRegExp.isEmpty())
return {};
const FilePath binaryPath = packagePath / m_detectionPath.path();
if (!binaryPath.exists())
return {};
const int timeout = 3000; // usually runs below 1s, but we want to be on the safe side
QtcProcess process;
process.setCommand({binaryPath, m_detectionArgs});
process.start();
if (!process.waitForFinished(timeout) || process.result() != ProcessResult::FinishedWithSuccess)
return {};
return matchRegExp(process.allOutput(), m_detectionRegExp);
}
McuPackageXmlVersionDetector::McuPackageXmlVersionDetector(const QString &filePattern,
const QString &versionElement,
const QString &versionAttribute,
const QString &versionRegExp)
: m_filePattern(filePattern)
, m_versionElement(versionElement)
, m_versionAttribute(versionAttribute)
, m_versionRegExp(versionRegExp)
{}
QString McuPackageXmlVersionDetector::parseVersion(const FilePath &packagePath) const
{
const auto files = QDir(packagePath.toString(), m_filePattern).entryInfoList();
for (const auto &xmlFile : files) {
QFile sdkXmlFile = QFile(xmlFile.absoluteFilePath());
sdkXmlFile.open(QFile::OpenModeFlag::ReadOnly);
QXmlStreamReader xmlReader(&sdkXmlFile);
while (xmlReader.readNext()) {
if (xmlReader.name() == m_versionElement) {
const QString versionString
= xmlReader.attributes().value(m_versionAttribute).toString();
const QString matched = matchRegExp(versionString, m_versionRegExp);
return !matched.isEmpty() ? matched : versionString;
}
}
}
return QString();
}
McuPackageDirectoryVersionDetector::McuPackageDirectoryVersionDetector(const QString &filePattern,
const QString &versionRegExp,
const bool isFile)
: m_filePattern(filePattern)
, m_versionRegExp(versionRegExp)
, m_isFile(isFile)
{}
QString McuPackageDirectoryVersionDetector::parseVersion(const FilePath &packagePath) const
{
const auto files = QDir(packagePath.toString(), m_filePattern)
.entryInfoList(m_isFile ? QDir::Filter::Files : QDir::Filter::Dirs);
for (const auto &entry : files) {
const QString matched = matchRegExp(entry.fileName(), m_versionRegExp);
if (!matched.isEmpty())
return matched;
}
return QString();
}
McuPackagePathVersionDetector::McuPackagePathVersionDetector(const QString &versionRegExp)
: m_versionRegExp(versionRegExp)
{}
QString McuPackagePathVersionDetector::parseVersion(const FilePath &packagePath) const
{
if (!packagePath.exists())
return {};
return matchRegExp(packagePath.toString(), m_versionRegExp);
}
} // namespace Internal
} // namespace McuSupport
|