From d9680baf2ab7f90db27c0aec7399da654e22e3e2 Mon Sep 17 00:00:00 2001 From: Alessandro Portale Date: Fri, 7 Jul 2023 18:44:00 +0200 Subject: [PATCH] Vcpkg: Implement adding of dependencies to vcpkg.json Instead of just inserting the package name at the cursor position, Qt Creator now attempts to append the package name in the dependencies array. With auto tests. The "interesting" effect of this is that the order of values inside a Json object may change. Qt seems to enforce an alphabetical order on the key names. Also the indentation becomes 4 after the modification. Task-number: QTCREATORBUG-29333 Change-Id: I803ca6a19ee32aeba95640726ac8d8e477750bc0 Reviewed-by: Cristian Adam --- src/plugins/vcpkg/vcpkg_test.cpp | 64 +++++++++++++++++++++++ src/plugins/vcpkg/vcpkg_test.h | 2 + src/plugins/vcpkg/vcpkgmanifesteditor.cpp | 20 ++++++- src/plugins/vcpkg/vcpkgmanifesteditor.h | 2 + 4 files changed, 86 insertions(+), 2 deletions(-) diff --git a/src/plugins/vcpkg/vcpkg_test.cpp b/src/plugins/vcpkg/vcpkg_test.cpp index c48e6ec3881..71e3d50a848 100644 --- a/src/plugins/vcpkg/vcpkg_test.cpp +++ b/src/plugins/vcpkg/vcpkg_test.cpp @@ -3,6 +3,7 @@ #include "vcpkg_test.h" +#include "vcpkgmanifesteditor.h" #include "vcpkgsearch.h" #include @@ -114,4 +115,67 @@ void VcpkgSearchTest::testVcpkgJsonParser() QCOMPARE(ok, success); } +void VcpkgSearchTest::testAddDependency_data() +{ + QTest::addColumn("originalVcpkgManifestJsonData"); + QTest::addColumn("addedPackage"); + QTest::addColumn("modifiedVcpkgManifestJsonData"); + + QTest::newRow("Existing dependencies") + << +R"({ + "name": "foo", + "dependencies": [ + "fmt", + { + "name": "vcpkg-cmake", + "host": true + } + ] +} +)" + << "7zip" + << +R"({ + "dependencies": [ + "fmt", + { + "host": true, + "name": "vcpkg-cmake" + }, + "7zip" + ], + "name": "foo" +} +)"; + + QTest::newRow("Without dependencies") + << +R"({ + "name": "foo" +} +)" + << "7zip" + << +R"({ + "dependencies": [ + "7zip" + ], + "name": "foo" +} +)"; +} + +void VcpkgSearchTest::testAddDependency() +{ + QFETCH(QString, originalVcpkgManifestJsonData); + QFETCH(QString, addedPackage); + QFETCH(QString, modifiedVcpkgManifestJsonData); + + const QByteArray result = addDependencyToManifest(originalVcpkgManifestJsonData.toUtf8(), + addedPackage); + + QCOMPARE(QString::fromUtf8(result), modifiedVcpkgManifestJsonData); +} + } // namespace Vcpkg::Internal diff --git a/src/plugins/vcpkg/vcpkg_test.h b/src/plugins/vcpkg/vcpkg_test.h index 8175e28d594..2bc386e7aa1 100644 --- a/src/plugins/vcpkg/vcpkg_test.h +++ b/src/plugins/vcpkg/vcpkg_test.h @@ -19,6 +19,8 @@ public: private slots: void testVcpkgJsonParser_data(); void testVcpkgJsonParser(); + void testAddDependency_data(); + void testAddDependency(); }; } // namespace Vcpkg::Internal diff --git a/src/plugins/vcpkg/vcpkgmanifesteditor.cpp b/src/plugins/vcpkg/vcpkgmanifesteditor.cpp index 6b5364463d1..0335f39af71 100644 --- a/src/plugins/vcpkg/vcpkgmanifesteditor.cpp +++ b/src/plugins/vcpkg/vcpkgmanifesteditor.cpp @@ -20,6 +20,9 @@ #include #include +#include +#include +#include #include #include @@ -97,8 +100,11 @@ public: m_searchPkgAction = toolBar()->addAction(vcpkgIcon, Tr::tr("Add vcpkg package...")); connect(m_searchPkgAction, &QAction::triggered, this, [this] { const Search::VcpkgManifest package = Search::showVcpkgPackageSearchDialog(); - if (!package.name.isEmpty()) - textCursor().insertText(package.name); + if (!package.name.isEmpty()) { + const QByteArray modifiedDocument = + addDependencyToManifest(textDocument()->contents(), package.name); + textDocument()->setContents(modifiedDocument); + } }); const QIcon cmakeIcon = Utils::Icon({{":/vcpkg/images/cmakeicon.png", @@ -153,4 +159,14 @@ VcpkgManifestEditorFactory::VcpkgManifestEditorFactory() setUseGenericHighlighter(true); } +QByteArray addDependencyToManifest(const QByteArray &manifest, const QString &package) +{ + constexpr char dependenciesKey[] = "dependencies"; + QJsonObject jsonObject = QJsonDocument::fromJson(manifest).object(); + QJsonArray dependencies = jsonObject.value(dependenciesKey).toArray(); + dependencies.append(package); + jsonObject.insert(dependenciesKey, dependencies); + return QJsonDocument(jsonObject).toJson(); +} + } // namespace Vcpkg::Internal diff --git a/src/plugins/vcpkg/vcpkgmanifesteditor.h b/src/plugins/vcpkg/vcpkgmanifesteditor.h index c7762d69df9..3d88ba79655 100644 --- a/src/plugins/vcpkg/vcpkgmanifesteditor.h +++ b/src/plugins/vcpkg/vcpkgmanifesteditor.h @@ -13,4 +13,6 @@ public: VcpkgManifestEditorFactory(); }; +QByteArray addDependencyToManifest(const QByteArray &manifest, const QString &package); + } // namespace Vcpkg::Internal