From cf7bb1de9db4b24b00f00a3ff9f62899cf7718b7 Mon Sep 17 00:00:00 2001 From: Jarek Kobus Date: Mon, 9 Sep 2024 09:26:06 +0200 Subject: [PATCH] Vcpkg: Make vcpkgManifests() cancelable Hide this method in cpp. Pass a promise, so that the function is made cancelable. Make a promise of VcpkgManifest type (instead of returning a list of VcpkgManifest types). Deliver results via promise arg. Collect results via results() method. Remove alias for QList, as it's used just once now. Change-Id: I06b76da9f40617bd6397fbee31b6db711dddcfb3 Reviewed-by: Alessandro Portale --- src/plugins/vcpkg/vcpkgsearch.cpp | 63 ++++++++++++------------------- src/plugins/vcpkg/vcpkgsearch.h | 3 -- 2 files changed, 24 insertions(+), 42 deletions(-) diff --git a/src/plugins/vcpkg/vcpkgsearch.cpp b/src/plugins/vcpkg/vcpkgsearch.cpp index 833fa0dd2ca..602ed076a1b 100644 --- a/src/plugins/vcpkg/vcpkgsearch.cpp +++ b/src/plugins/vcpkg/vcpkgsearch.cpp @@ -31,6 +31,20 @@ using namespace Utils; namespace Vcpkg::Internal::Search { +static void vcpkgManifests(QPromise &promise, const FilePath &vcpkgRoot) +{ + const FilePath portsDir = vcpkgRoot / "ports"; + const FilePaths manifestFiles = + portsDir.dirEntries({{"vcpkg.json"}, QDir::Files, QDirIterator::Subdirectories}); + for (const FilePath &manifestFile : manifestFiles) { + if (promise.isCanceled()) + return; + FileReader reader; + if (reader.fetch(manifestFile)) + promise.addResult(parseVcpkgManifest(reader.data())); + } +} + class VcpkgPackageSearchDialog : public QDialog { public: @@ -44,7 +58,7 @@ private: void updateStatus(); void updatePackages(); - VcpkgManifests m_allPackages; + QList m_allPackages; VcpkgManifest m_selectedPackage; const VcpkgManifest m_projectManifest; @@ -140,8 +154,7 @@ VcpkgManifest VcpkgPackageSearchDialog::selectedPackage() const void VcpkgPackageSearchDialog::listPackages(const QString &filter) { - const VcpkgManifests filteredPackages = filtered(m_allPackages, - [&filter] (const VcpkgManifest &package) { + const auto filteredPackages = filtered(m_allPackages, [&filter](const VcpkgManifest &package) { return filter.isEmpty() || package.name.contains(filter, Qt::CaseInsensitive) || package.shortDescription.contains(filter, Qt::CaseInsensitive) @@ -187,33 +200,22 @@ void VcpkgPackageSearchDialog::updateStatus() void VcpkgPackageSearchDialog::updatePackages() { - using ResultType = VcpkgManifests; - - const auto parseManifests = [=](QPromise &promise, const FilePath &srcPath) { - promise.addResult(vcpkgManifests(srcPath)); - }; - using namespace Tasking; - Group group { - onGroupSetup([this]() { - m_spinner->show(); - }), - AsyncTask{ - [parseManifests](Async &task) { - task.setConcurrentCallData(parseManifests, - settings().vcpkgRoot()); + + const Group group { + onGroupSetup([this] { m_spinner->show(); }), + AsyncTask{ + [](Async &task) { + task.setConcurrentCallData(vcpkgManifests, settings().vcpkgRoot()); }, - [this](const Async &task) { - m_allPackages = task.result(); - } + [this](const Async &task) { m_allPackages = task.results(); } }, - onGroupDone([this]() { + onGroupDone([this] { m_spinner->hide(); listPackages({}); updateStatus(); }), }; - m_taskTreeRunner.start(group); } @@ -265,23 +267,6 @@ VcpkgManifest parseVcpkgManifest(const QByteArray &vcpkgManifestJsonData, bool * return result; } -VcpkgManifests vcpkgManifests(const FilePath &vcpkgRoot) -{ - const FilePath portsDir = vcpkgRoot / "ports"; - VcpkgManifests result; - const FilePaths manifestFiles = - portsDir.dirEntries({{"vcpkg.json"}, QDir::Files, QDirIterator::Subdirectories}); - for (const FilePath &manifestFile : manifestFiles) { - FileReader reader; - if (reader.fetch(manifestFile)) { - const QByteArray &manifestData = reader.data(); - const VcpkgManifest manifest = parseVcpkgManifest(manifestData); - result.append(manifest); - } - } - return result; -} - VcpkgManifest showVcpkgPackageSearchDialog(const VcpkgManifest &projectManifest, QWidget *parent) { QWidget *dlgParent = parent ? parent : Core::ICore::dialogParent(); diff --git a/src/plugins/vcpkg/vcpkgsearch.h b/src/plugins/vcpkg/vcpkgsearch.h index 912dff00b6a..52f3054d5d2 100644 --- a/src/plugins/vcpkg/vcpkgsearch.h +++ b/src/plugins/vcpkg/vcpkgsearch.h @@ -21,10 +21,7 @@ struct VcpkgManifest QUrl homepage; }; -using VcpkgManifests = QList; - VcpkgManifest parseVcpkgManifest(const QByteArray &vcpkgManifestJsonData, bool *ok = nullptr); -VcpkgManifests vcpkgManifests(const Utils::FilePath &vcpkgRoot); VcpkgManifest showVcpkgPackageSearchDialog(const VcpkgManifest &projectManifest, QWidget *parent = nullptr);