summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKatja Marttila <katja.marttila@qt.io>2021-04-15 15:52:33 +0300
committerKatja Marttila <katja.marttila@qt.io>2021-05-21 12:09:32 +0300
commitbe2c54531eeb00822259d6f6bfcb90b43768cea3 (patch)
treecece9822ae77972f4382f8f99031ee761b4aa92f
parenteb4b39099231254c850d897ede4ceaf79b91b4b6 (diff)
Add possibility to list components with regexp
Task-number: QTIFW-2225 Change-Id: I6a7fdfc1070ad54d520563cae7d2446e97e2e87c Reviewed-by: Arttu Tarkiainen <arttu.tarkiainen@qt.io>
-rw-r--r--doc/scripting-api/packagemanagercore.qdoc5
-rw-r--r--src/libs/installer/packagemanagercore.cpp14
-rw-r--r--src/libs/installer/packagemanagercore.h2
-rw-r--r--src/libs/installer/scriptengine.cpp6
-rw-r--r--src/libs/installer/scriptengine_p.h4
-rw-r--r--tests/auto/installer/scriptengine/tst_scriptengine.cpp15
6 files changed, 37 insertions, 9 deletions
diff --git a/doc/scripting-api/packagemanagercore.qdoc b/doc/scripting-api/packagemanagercore.qdoc
index bacf50f27..39387b3ae 100644
--- a/doc/scripting-api/packagemanagercore.qdoc
+++ b/doc/scripting-api/packagemanagercore.qdoc
@@ -33,11 +33,12 @@
*/
/*!
- \qmlmethod array installer::components()
+ \qmlmethod array installer::components(string regexp)
Returns an array of all components currently available.
If the repository metadata have not been fetched yet,
- the array will be empty.
+ the array will be empty. Optionally, a \a regexp expression
+ can be used to further filter the listed packages.
\sa component, finishAllComponentsReset(), finishUpdaterComponentsReset()
*/
diff --git a/src/libs/installer/packagemanagercore.cpp b/src/libs/installer/packagemanagercore.cpp
index a18374f0c..145eb30d7 100644
--- a/src/libs/installer/packagemanagercore.cpp
+++ b/src/libs/installer/packagemanagercore.cpp
@@ -1863,8 +1863,9 @@ void PackageManagerCore::appendRootComponent(Component *component)
/*!
Returns a list of components depending on the component types passed in \a mask.
+ Optionally, a \a regexp expression can be used to further filter the listed packages.
*/
-QList<Component *> PackageManagerCore::components(ComponentTypes mask) const
+QList<Component *> PackageManagerCore::components(ComponentTypes mask, const QString &regexp) const
{
QList<Component *> components;
@@ -1885,6 +1886,17 @@ QList<Component *> PackageManagerCore::components(ComponentTypes mask) const
// No descendants here, updates are always a flat list and cannot have children!
}
+ if (!regexp.isEmpty()) {
+ QRegularExpression re(regexp);
+ QList<Component*>::iterator iter = components.begin();
+ while (iter != components.end()) {
+ if (!re.match(iter.i->t()->name()).hasMatch())
+ iter = components.erase(iter);
+ else
+ iter++;
+ }
+ }
+
return components;
}
diff --git a/src/libs/installer/packagemanagercore.h b/src/libs/installer/packagemanagercore.h
index 9d89e4763..d4833a979 100644
--- a/src/libs/installer/packagemanagercore.h
+++ b/src/libs/installer/packagemanagercore.h
@@ -224,7 +224,7 @@ public:
void appendRootComponent(Component *components);
void appendUpdaterComponent(Component *components);
- QList<Component *> components(ComponentTypes mask) const;
+ QList<Component *> components(ComponentTypes mask, const QString &regexp = QString()) const;
Component *componentByName(const QString &identifier) const;
Q_INVOKABLE bool calculateComponentsToInstall() const;
diff --git a/src/libs/installer/scriptengine.cpp b/src/libs/installer/scriptengine.cpp
index 5dd56939c..c8fc65d34 100644
--- a/src/libs/installer/scriptengine.cpp
+++ b/src/libs/installer/scriptengine.cpp
@@ -1,6 +1,6 @@
/**************************************************************************
**
-** Copyright (C) 2020 The Qt Company Ltd.
+** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Installer Framework.
@@ -75,10 +75,10 @@ namespace QInstaller {
\internal
*/
-QJSValue InstallerProxy::components() const
+QJSValue InstallerProxy::components(const QString &regexp) const
{
if (m_core) {
- const QList<Component*> all = m_core->components(PackageManagerCore::ComponentType::All);
+ const QList<Component*> all = m_core->components(PackageManagerCore::ComponentType::All, regexp);
QJSValue scriptComponentsObject = m_engine->newArray(all.count());
for (int i = 0; i < all.count(); ++i) {
Component *const component = all.at(i);
diff --git a/src/libs/installer/scriptengine_p.h b/src/libs/installer/scriptengine_p.h
index c5f04c0dc..e5c39663c 100644
--- a/src/libs/installer/scriptengine_p.h
+++ b/src/libs/installer/scriptengine_p.h
@@ -1,6 +1,6 @@
/**************************************************************************
**
-** Copyright (C) 2020 The Qt Company Ltd.
+** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Installer Framework.
@@ -63,7 +63,7 @@ public:
: m_engine(engine), m_core(core) {}
public slots:
- QJSValue components() const;
+ QJSValue components(const QString &regexp = QString()) const;
QJSValue componentByName(const QString &componentName);
private:
diff --git a/tests/auto/installer/scriptengine/tst_scriptengine.cpp b/tests/auto/installer/scriptengine/tst_scriptengine.cpp
index b7c602e26..105bcf5d7 100644
--- a/tests/auto/installer/scriptengine/tst_scriptengine.cpp
+++ b/tests/auto/installer/scriptengine/tst_scriptengine.cpp
@@ -349,6 +349,21 @@ private slots:
}
}
+ void testComponentsWithRegexp()
+ {
+ const QString script = QString::fromLatin1("var components = installer.components(\"component.test.addOperation\");"
+ "\n"
+ "for (i = 0; i < components.length; i++)"
+ "print(components[i].name);");
+
+ setExpectedScriptOutput("component.test.addOperation");
+ const QJSValue value = m_scriptEngine->evaluate(script);
+ if (value.isError()) {
+ QFAIL(qPrintable(QString::fromLatin1("ScriptEngine error:\n %1").arg(
+ value.toString())));
+ }
+ }
+
void testFindFiles()
{
const QString expectedOutput = QString::fromLatin1("Found file %1/tst_scriptengine.moc").arg(m_applicatonDirPath);