forked from qt-creator/qt-creator
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 <cristian.adam@qt.io>
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
|
||||
#include "vcpkg_test.h"
|
||||
|
||||
#include "vcpkgmanifesteditor.h"
|
||||
#include "vcpkgsearch.h"
|
||||
|
||||
#include <QTest>
|
||||
@@ -114,4 +115,67 @@ void VcpkgSearchTest::testVcpkgJsonParser()
|
||||
QCOMPARE(ok, success);
|
||||
}
|
||||
|
||||
void VcpkgSearchTest::testAddDependency_data()
|
||||
{
|
||||
QTest::addColumn<QString>("originalVcpkgManifestJsonData");
|
||||
QTest::addColumn<QString>("addedPackage");
|
||||
QTest::addColumn<QString>("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
|
||||
|
@@ -19,6 +19,8 @@ public:
|
||||
private slots:
|
||||
void testVcpkgJsonParser_data();
|
||||
void testVcpkgJsonParser();
|
||||
void testAddDependency_data();
|
||||
void testAddDependency();
|
||||
};
|
||||
|
||||
} // namespace Vcpkg::Internal
|
||||
|
@@ -20,6 +20,9 @@
|
||||
#include <texteditor/texteditorsettings.h>
|
||||
|
||||
#include <QDialogButtonBox>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QPlainTextEdit>
|
||||
#include <QToolBar>
|
||||
|
||||
@@ -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
|
||||
|
@@ -13,4 +13,6 @@ public:
|
||||
VcpkgManifestEditorFactory();
|
||||
};
|
||||
|
||||
QByteArray addDependencyToManifest(const QByteArray &manifest, const QString &package);
|
||||
|
||||
} // namespace Vcpkg::Internal
|
||||
|
Reference in New Issue
Block a user