diff options
-rw-r--r-- | doc/scripting-api/packagemanagercore.qdoc | 5 | ||||
-rw-r--r-- | src/libs/installer/packagemanagercore.cpp | 14 | ||||
-rw-r--r-- | src/libs/installer/packagemanagercore.h | 2 | ||||
-rw-r--r-- | src/libs/installer/scriptengine.cpp | 6 | ||||
-rw-r--r-- | src/libs/installer/scriptengine_p.h | 4 | ||||
-rw-r--r-- | tests/auto/installer/scriptengine/tst_scriptengine.cpp | 15 |
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 ®exp) 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 ®exp = 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 ®exp) 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 ®exp = 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); |