2021-02-15 16:20:38 +01:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2020 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 "mcusupportversiondetection.h"
|
|
|
|
|
|
|
|
|
|
#include <utils/fileutils.h>
|
|
|
|
|
#include <QDir>
|
|
|
|
|
#include <QRegularExpression>
|
|
|
|
|
#include <QProcess>
|
|
|
|
|
|
|
|
|
|
namespace McuSupport {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
2021-03-01 11:07:44 +01:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-15 16:20:38 +01:00
|
|
|
McuPackageVersionDetector::McuPackageVersionDetector()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
McuPackageExecutableVersionDetector::McuPackageExecutableVersionDetector(
|
|
|
|
|
const QString &detectionPath,
|
|
|
|
|
const QStringList &detectionArgs,
|
|
|
|
|
const QString &detectionRegExp)
|
|
|
|
|
: McuPackageVersionDetector()
|
|
|
|
|
, m_detectionPath(detectionPath)
|
|
|
|
|
, m_detectionArgs(detectionArgs)
|
|
|
|
|
, m_detectionRegExp(detectionRegExp)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString McuPackageExecutableVersionDetector::parseVersion(const QString &packagePath) const
|
|
|
|
|
{
|
|
|
|
|
if (m_detectionPath.isEmpty() || m_detectionRegExp.isEmpty())
|
|
|
|
|
return QString();
|
|
|
|
|
|
2021-09-27 22:12:34 +02:00
|
|
|
const Utils::FilePath binaryPath = Utils::FilePath::fromString(packagePath) / m_detectionPath;
|
|
|
|
|
if (!binaryPath.exists())
|
2021-02-15 16:20:38 +01:00
|
|
|
return QString();
|
|
|
|
|
|
|
|
|
|
const int execTimeout = 3000; // usually runs below 1s, but we want to be on the safe side
|
|
|
|
|
QProcess binaryProcess;
|
2021-09-27 22:12:34 +02:00
|
|
|
binaryProcess.start(binaryPath.toString(), m_detectionArgs);
|
2021-02-15 16:20:38 +01:00
|
|
|
if (!binaryProcess.waitForStarted())
|
|
|
|
|
return QString();
|
|
|
|
|
binaryProcess.waitForFinished(execTimeout);
|
|
|
|
|
if (binaryProcess.exitCode() == QProcess::ExitStatus::NormalExit) {
|
|
|
|
|
const QString processOutput = QString::fromUtf8(
|
|
|
|
|
binaryProcess.readAllStandardOutput().append(
|
|
|
|
|
binaryProcess.readAllStandardError()));
|
2021-03-01 11:07:44 +01:00
|
|
|
return matchRegExp(processOutput, m_detectionRegExp);
|
2021-02-15 16:20:38 +01:00
|
|
|
}
|
|
|
|
|
|
2021-03-01 11:07:44 +01:00
|
|
|
// Fail gracefully: return empty string if execution failed
|
2021-02-15 16:20:38 +01:00
|
|
|
return QString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 QString &packagePath) const
|
|
|
|
|
{
|
|
|
|
|
const auto files = QDir(packagePath, 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();
|
2021-03-01 11:07:44 +01:00
|
|
|
const QString matched = matchRegExp(versionString, m_versionRegExp);
|
|
|
|
|
return !matched.isEmpty() ? matched : versionString;
|
2021-02-15 16:20:38 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 QString &packagePath) const
|
|
|
|
|
{
|
|
|
|
|
const auto files = QDir(packagePath, m_filePattern)
|
|
|
|
|
.entryInfoList(m_isFile ? QDir::Filter::Files : QDir::Filter::Dirs);
|
|
|
|
|
for (const auto &entry: files) {
|
2021-03-01 11:07:44 +01:00
|
|
|
const QString matched = matchRegExp(entry.fileName(), m_versionRegExp);
|
|
|
|
|
if (!matched.isEmpty())
|
|
|
|
|
return matched;
|
2021-02-15 16:20:38 +01:00
|
|
|
}
|
|
|
|
|
return QString();
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-01 11:07:44 +01:00
|
|
|
McuPackagePathVersionDetector::McuPackagePathVersionDetector(const QString &versionRegExp)
|
|
|
|
|
: m_versionRegExp(versionRegExp)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString McuPackagePathVersionDetector::parseVersion(const QString &packagePath) const
|
|
|
|
|
{
|
|
|
|
|
if (!Utils::FilePath::fromString(packagePath).exists())
|
|
|
|
|
return QString();
|
|
|
|
|
return matchRegExp(packagePath, m_versionRegExp);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-15 16:20:38 +01:00
|
|
|
} // Internal
|
|
|
|
|
} // McuSupport
|