Meson: Merge ToolWrapper classes

Change-Id: I140088263059944b39c913acd1df07312b249953
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
This commit is contained in:
hjk
2024-07-22 17:27:50 +02:00
parent 36a6fa1664
commit ed10d7a449
9 changed files with 64 additions and 95 deletions

View File

@@ -10,23 +10,17 @@ using namespace Utils;
namespace MesonProjectManager { namespace MesonProjectManager {
namespace Internal { namespace Internal {
template<typename T> MesonTools::Tool_t tool(const Utils::Id &id,
inline bool is(const MesonTools::Tool_t &tool) const std::vector<MesonTools::Tool_t> &tools,
ToolType toolType)
{ {
return bool(std::dynamic_pointer_cast<T>(tool));
}
template<typename T>
std::shared_ptr<T> tool(const Utils::Id &id, const std::vector<MesonTools::Tool_t> &tools)
{
static_assert(std::is_base_of<ToolWrapper, T>::value, "Type must derive from ToolWrapper");
const auto tool = std::find_if(std::cbegin(tools), const auto tool = std::find_if(std::cbegin(tools),
std::cend(tools), std::cend(tools),
[&id](const MesonTools::Tool_t &tool) { [&id](const MesonTools::Tool_t &tool) {
return tool->id() == id; return tool->id() == id;
}); });
if (tool != std::cend(tools) && is<T>(*tool)) if (tool != std::cend(tools) && (*tool)->toolType() == toolType)
return std::dynamic_pointer_cast<T>(*tool); return *tool;
return nullptr; return nullptr;
} }
@@ -63,12 +57,12 @@ static void fixAutoDetected(std::vector<MesonTools::Tool_t> &tools, ToolType too
bool MesonTools::isMesonWrapper(const MesonTools::Tool_t &tool) bool MesonTools::isMesonWrapper(const MesonTools::Tool_t &tool)
{ {
return is<MesonWrapper>(tool); return tool->toolType() == ToolType::Meson;
} }
bool MesonTools::isNinjaWrapper(const MesonTools::Tool_t &tool) bool MesonTools::isNinjaWrapper(const MesonTools::Tool_t &tool)
{ {
return is<NinjaWrapper>(tool); return tool->toolType() == ToolType::Ninja;
} }
void MesonTools::setTools(std::vector<MesonTools::Tool_t> &&tools) void MesonTools::setTools(std::vector<MesonTools::Tool_t> &&tools)
@@ -79,25 +73,24 @@ void MesonTools::setTools(std::vector<MesonTools::Tool_t> &&tools)
fixAutoDetected(self->m_tools, ToolType::Ninja); fixAutoDetected(self->m_tools, ToolType::Ninja);
} }
std::shared_ptr<NinjaWrapper> MesonTools::ninjaWrapper(const Utils::Id &id) std::shared_ptr<ToolWrapper> MesonTools::ninjaWrapper(const Utils::Id &id)
{ {
return tool<NinjaWrapper>(id, MesonTools::instance()->m_tools); return tool(id, MesonTools::instance()->m_tools, ToolType::Ninja);
}
std::shared_ptr<MesonWrapper> MesonTools::mesonWrapper(const Utils::Id &id)
{
return tool<MesonWrapper>(id, MesonTools::instance()->m_tools);
} }
std::shared_ptr<NinjaWrapper> MesonTools::ninjaWrapper() std::shared_ptr<ToolWrapper> MesonTools::mesonWrapper(const Utils::Id &id)
{ {
return std::dynamic_pointer_cast<NinjaWrapper>( return tool(id, MesonTools::instance()->m_tools, ToolType::Meson);
autoDetected(MesonTools::instance()->m_tools, ToolType::Ninja));
} }
std::shared_ptr<MesonWrapper> MesonTools::mesonWrapper() std::shared_ptr<ToolWrapper> MesonTools::autoDetectedNinja()
{ {
return std::dynamic_pointer_cast<MesonWrapper>( return autoDetected(MesonTools::instance()->m_tools, ToolType::Ninja);
autoDetected(MesonTools::instance()->m_tools, ToolType::Meson)); }
std::shared_ptr<ToolWrapper> MesonTools::autoDetectedMeson()
{
return autoDetected(MesonTools::instance()->m_tools, ToolType::Meson);
} }
} // namespace Internal } // namespace Internal

View File

@@ -69,11 +69,11 @@ public:
emit self->toolRemoved(*item); emit self->toolRemoved(*item);
} }
static std::shared_ptr<NinjaWrapper> ninjaWrapper(const Utils::Id &id); static std::shared_ptr<ToolWrapper> ninjaWrapper(const Utils::Id &id);
static std::shared_ptr<MesonWrapper> mesonWrapper(const Utils::Id &id); static std::shared_ptr<ToolWrapper> mesonWrapper(const Utils::Id &id);
static std::shared_ptr<NinjaWrapper> ninjaWrapper(); static std::shared_ptr<ToolWrapper> autoDetectedNinja();
static std::shared_ptr<MesonWrapper> mesonWrapper(); static std::shared_ptr<ToolWrapper> autoDetectedMeson();
Q_SIGNAL void toolAdded(const Tool_t &tool); Q_SIGNAL void toolAdded(const Tool_t &tool);
Q_SIGNAL void toolRemoved(const Tool_t &tool); Q_SIGNAL void toolRemoved(const Tool_t &tool);

View File

@@ -36,7 +36,7 @@ static const QList<projectData> projectList{
_intro_file.open(); \ _intro_file.open(); \
const auto tool = findMesonTool(); \ const auto tool = findMesonTool(); \
QVERIFY(tool.has_value()); \ QVERIFY(tool.has_value()); \
const MesonWrapper _meson(ToolType::Meson, "name", *tool); \ const ToolWrapper _meson(ToolType::Meson, "name", *tool); \
run_meson(_meson.introspect(Utils::FilePath::fromString(_source_dir)), &_intro_file); \ run_meson(_meson.introspect(Utils::FilePath::fromString(_source_dir)), &_intro_file); \
__VA_ARGS__ \ __VA_ARGS__ \
} }
@@ -78,7 +78,7 @@ private slots:
FilePath buildDir = FilePath::fromString(build_dir.path()); FilePath buildDir = FilePath::fromString(build_dir.path());
const auto tool = findMesonTool(); const auto tool = findMesonTool();
QVERIFY(tool.has_value()); QVERIFY(tool.has_value());
MesonWrapper meson(ToolType::Meson, "name", *tool); ToolWrapper meson(ToolType::Meson, "name", *tool);
run_meson(meson.setup(FilePath::fromString(src_dir), buildDir)); run_meson(meson.setup(FilePath::fromString(src_dir), buildDir));
QVERIFY(isSetup(buildDir)); QVERIFY(isSetup(buildDir));

View File

@@ -14,13 +14,9 @@
#include <QTemporaryDir> #include <QTemporaryDir>
#include <QtTest/QtTest> #include <QtTest/QtTest>
#include <iostream>
using namespace MesonProjectManager::Internal; using namespace MesonProjectManager::Internal;
namespace { static const QPair<const char *, QString> projectList[] = {{"Simple C Project", "simplecproject"}};
static const QList<QPair<const char *, QString>> projectList{{"Simple C Project", "simplecproject"}};
} // namespace
class AMesonWrapper : public QObject class AMesonWrapper : public QObject
{ {
@@ -69,7 +65,7 @@ private slots:
{ {
QFETCH(QString, src_dir); QFETCH(QString, src_dir);
QTemporaryDir build_dir{"test-meson"}; QTemporaryDir build_dir{"test-meson"};
const MesonWrapper meson(ToolType::Meson, "name", *findMesonTool()); const ToolWrapper meson(ToolType::Meson, "name", *findMesonTool());
QVERIFY(run_meson(meson.setup(Utils::FilePath::fromString(src_dir), QVERIFY(run_meson(meson.setup(Utils::FilePath::fromString(src_dir),
Utils::FilePath::fromString(build_dir.path())))); Utils::FilePath::fromString(build_dir.path()))));
QVERIFY( QVERIFY(
@@ -90,7 +86,7 @@ private slots:
{ {
QFETCH(QString, src_dir); QFETCH(QString, src_dir);
QTemporaryDir build_dir{"test-meson"}; QTemporaryDir build_dir{"test-meson"};
const MesonWrapper meson(ToolType::Meson, "name", *findMesonTool()); const ToolWrapper meson(ToolType::Meson, "name", *findMesonTool());
QVERIFY(run_meson(meson.setup(Utils::FilePath::fromString(src_dir), QVERIFY(run_meson(meson.setup(Utils::FilePath::fromString(src_dir),
Utils::FilePath::fromString(build_dir.path())))); Utils::FilePath::fromString(build_dir.path()))));
QVERIFY(run_meson(meson.configure(Utils::FilePath::fromString(src_dir), QVERIFY(run_meson(meson.configure(Utils::FilePath::fromString(src_dir),
@@ -101,8 +97,6 @@ private slots:
{ {
Utils::Singleton::deleteAll(); Utils::Singleton::deleteAll();
} }
private:
}; };
QTEST_GUILESS_MAIN(AMesonWrapper) QTEST_GUILESS_MAIN(AMesonWrapper)

View File

@@ -142,8 +142,8 @@ void MesonToolKitAspectImpl::setToDefault()
{ {
const MesonTools::Tool_t autoDetected = [this] { const MesonTools::Tool_t autoDetected = [this] {
if (m_type == ToolType::Meson) if (m_type == ToolType::Meson)
return std::dynamic_pointer_cast<ToolWrapper>(MesonTools::mesonWrapper()); return MesonTools::autoDetectedMeson();
return std::dynamic_pointer_cast<ToolWrapper>(MesonTools::ninjaWrapper()); return MesonTools::autoDetectedNinja();
}(); }();
if (autoDetected) { if (autoDetected) {
@@ -172,7 +172,7 @@ Id MesonToolKitAspect::mesonToolId(const Kit *kit)
return Id::fromSetting(kit->value(MESON_TOOL_ID)); return Id::fromSetting(kit->value(MESON_TOOL_ID));
} }
std::shared_ptr<MesonWrapper> MesonToolKitAspect::mesonTool(const Kit *kit) std::shared_ptr<ToolWrapper> MesonToolKitAspect::mesonTool(const Kit *kit)
{ {
return MesonTools::mesonWrapper(MesonToolKitAspect::mesonToolId(kit)); return MesonTools::mesonWrapper(MesonToolKitAspect::mesonToolId(kit));
} }
@@ -210,7 +210,7 @@ public:
{ {
const auto tool = MesonToolKitAspect::mesonTool(k); const auto tool = MesonToolKitAspect::mesonTool(k);
if (!tool) { if (!tool) {
const auto autoDetected = MesonTools::mesonWrapper(); const auto autoDetected = MesonTools::autoDetectedMeson();
if (autoDetected) if (autoDetected)
MesonToolKitAspect::setMesonTool(k, autoDetected->id()); MesonToolKitAspect::setMesonTool(k, autoDetected->id());
} }
@@ -253,7 +253,7 @@ Id NinjaToolKitAspect::ninjaToolId(const Kit *kit)
return Id::fromSetting(kit->value(NINJA_TOOL_ID)); return Id::fromSetting(kit->value(NINJA_TOOL_ID));
} }
std::shared_ptr<NinjaWrapper> NinjaToolKitAspect::ninjaTool(const Kit *kit) std::shared_ptr<ToolWrapper> NinjaToolKitAspect::ninjaTool(const Kit *kit)
{ {
return MesonTools::ninjaWrapper(NinjaToolKitAspect::ninjaToolId(kit)); return MesonTools::ninjaWrapper(NinjaToolKitAspect::ninjaToolId(kit));
} }
@@ -291,7 +291,7 @@ public:
{ {
const auto tool = NinjaToolKitAspect::ninjaTool(k); const auto tool = NinjaToolKitAspect::ninjaTool(k);
if (!tool) { if (!tool) {
const auto autoDetected = MesonTools::ninjaWrapper(); const auto autoDetected = MesonTools::autoDetectedNinja();
if (autoDetected) if (autoDetected)
NinjaToolKitAspect::setNinjaTool(k, autoDetected->id()); NinjaToolKitAspect::setNinjaTool(k, autoDetected->id());
} }

View File

@@ -17,7 +17,7 @@ public:
static Utils::Id mesonToolId(const ProjectExplorer::Kit *kit); static Utils::Id mesonToolId(const ProjectExplorer::Kit *kit);
static bool isValid(const ProjectExplorer::Kit *kit); static bool isValid(const ProjectExplorer::Kit *kit);
static std::shared_ptr<MesonWrapper> mesonTool(const ProjectExplorer::Kit *kit); static std::shared_ptr<ToolWrapper> mesonTool(const ProjectExplorer::Kit *kit);
}; };
class NinjaToolKitAspect final class NinjaToolKitAspect final
@@ -27,7 +27,7 @@ public:
static Utils::Id ninjaToolId(const ProjectExplorer::Kit *kit); static Utils::Id ninjaToolId(const ProjectExplorer::Kit *kit);
static bool isValid(const ProjectExplorer::Kit *kit); static bool isValid(const ProjectExplorer::Kit *kit);
static std::shared_ptr<NinjaWrapper> ninjaTool(const ProjectExplorer::Kit *kit); static std::shared_ptr<ToolWrapper> ninjaTool(const ProjectExplorer::Kit *kit);
}; };
} // MesonProjectManager::Internal } // MesonProjectManager::Internal

View File

@@ -108,7 +108,7 @@ void ToolTreeItem::update_tooltip(const Version &version)
void ToolTreeItem::update_tooltip() void ToolTreeItem::update_tooltip()
{ {
update_tooltip(MesonWrapper::read_version(m_executable)); update_tooltip(ToolWrapper::read_version(m_executable));
} }
} // namespace Internal } // namespace Internal

View File

@@ -3,6 +3,8 @@
#include "toolwrapper.h" #include "toolwrapper.h"
#include "mesonpluginconstants.h"
#include <utils/algorithm.h> #include <utils/algorithm.h>
#include <utils/environment.h> #include <utils/environment.h>
#include <utils/fileutils.h> #include <utils/fileutils.h>
@@ -18,8 +20,6 @@ using namespace Utils;
namespace MesonProjectManager { namespace MesonProjectManager {
namespace Internal { namespace Internal {
// ToolWrapper base
ToolWrapper::ToolWrapper(ToolType toolType, ToolWrapper::ToolWrapper(ToolType toolType,
const QString &name, const QString &name,
const FilePath &path, const FilePath &path,
@@ -49,6 +49,8 @@ ToolWrapper::ToolWrapper(ToolType toolType,
QTC_ASSERT(m_id.isValid(), m_id = Utils::Id::generate()); QTC_ASSERT(m_id.isValid(), m_id = Utils::Id::generate());
} }
ToolWrapper::~ToolWrapper() = default;
void ToolWrapper::setExe(const Utils::FilePath &newExe) void ToolWrapper::setExe(const Utils::FilePath &newExe)
{ {
m_exe = newExe; m_exe = newExe;
@@ -101,8 +103,6 @@ static std::optional<FilePath> findTool(const QStringList &exeNames)
return std::nullopt; return std::nullopt;
} }
// MesonWrapper
template<typename First> template<typename First>
void impl_option_cat(QStringList &list, const First &first) void impl_option_cat(QStringList &list, const First &first)
{ {
@@ -124,7 +124,7 @@ QStringList options_cat(const T &...args)
return result; return result;
} }
Command MesonWrapper::setup(const FilePath &sourceDirectory, Command ToolWrapper::setup(const FilePath &sourceDirectory,
const FilePath &buildDirectory, const FilePath &buildDirectory,
const QStringList &options) const const QStringList &options) const
{ {
@@ -132,7 +132,7 @@ Command MesonWrapper::setup(const FilePath &sourceDirectory,
sourceDirectory}; sourceDirectory};
} }
Command MesonWrapper::configure(const FilePath &sourceDirectory, Command ToolWrapper::configure(const FilePath &sourceDirectory,
const FilePath &buildDirectory, const FilePath &buildDirectory,
const QStringList &options) const const QStringList &options) const
{ {
@@ -142,7 +142,7 @@ Command MesonWrapper::configure(const FilePath &sourceDirectory,
buildDirectory}; buildDirectory};
} }
Command MesonWrapper::regenerate(const FilePath &sourceDirectory, Command ToolWrapper::regenerate(const FilePath &sourceDirectory,
const FilePath &buildDirectory) const const FilePath &buildDirectory) const
{ {
return {{m_exe, return {{m_exe,
@@ -155,7 +155,7 @@ Command MesonWrapper::regenerate(const FilePath &sourceDirectory,
buildDirectory}; buildDirectory};
} }
Command MesonWrapper::introspect(const Utils::FilePath &sourceDirectory) const Command ToolWrapper::introspect(const Utils::FilePath &sourceDirectory) const
{ {
return {{m_exe, return {{m_exe,
{"introspect", "--all", QString("%1/meson.build").arg(sourceDirectory.path())}}, {"introspect", "--all", QString("%1/meson.build").arg(sourceDirectory.path())}},

View File

@@ -3,7 +3,6 @@
#pragma once #pragma once
#include "mesonpluginconstants.h"
#include "versionhelper.h" #include "versionhelper.h"
#include <utils/commandline.h> #include <utils/commandline.h>
@@ -24,10 +23,9 @@ public:
Utils::FilePath workDir; Utils::FilePath workDir;
}; };
class ToolWrapper class ToolWrapper final
{ {
public: public:
virtual ~ToolWrapper() {}
ToolWrapper() = delete; ToolWrapper() = delete;
ToolWrapper(ToolType toolType, ToolWrapper(ToolType toolType,
const QString &name, const QString &name,
@@ -38,10 +36,8 @@ public:
const Utils::FilePath &path, const Utils::FilePath &path,
const Utils::Id &id, const Utils::Id &id,
bool autoDetected = false); bool autoDetected = false);
ToolWrapper(const ToolWrapper &other) = default;
ToolWrapper(ToolWrapper &&other) = default; ~ToolWrapper();
ToolWrapper &operator=(const ToolWrapper &other) = default;
ToolWrapper &operator=(ToolWrapper &&other) = default;
const Version &version() const noexcept { return m_version; } const Version &version() const noexcept { return m_version; }
bool isValid() const noexcept { return m_isValid; } bool isValid() const noexcept { return m_isValid; }
@@ -50,8 +46,8 @@ public:
Utils::FilePath exe() const noexcept { return m_exe; } Utils::FilePath exe() const noexcept { return m_exe; }
QString name() const noexcept { return m_name; } QString name() const noexcept { return m_name; }
inline void setName(const QString &newName) { m_name = newName; } void setName(const QString &newName) { m_name = newName; }
virtual void setExe(const Utils::FilePath &newExe); void setExe(const Utils::FilePath &newExe);
static Version read_version(const Utils::FilePath &toolPath); static Version read_version(const Utils::FilePath &toolPath);
@@ -61,6 +57,16 @@ public:
ToolType toolType() const { return m_toolType; } ToolType toolType() const { return m_toolType; }
void setToolType(ToolType newToolType) { m_toolType = newToolType; } void setToolType(ToolType newToolType) { m_toolType = newToolType; }
Command setup(const Utils::FilePath &sourceDirectory,
const Utils::FilePath &buildDirectory,
const QStringList &options = {}) const;
Command configure(const Utils::FilePath &sourceDirectory,
const Utils::FilePath &buildDirectory,
const QStringList &options = {}) const;
Command regenerate(const Utils::FilePath &sourceDirectory,
const Utils::FilePath &buildDirectory) const;
Command introspect(const Utils::FilePath &sourceDirectory) const;
protected: protected:
ToolType m_toolType; ToolType m_toolType;
Version m_version; Version m_version;
@@ -75,30 +81,6 @@ bool run_meson(const Command &command, QIODevice *output = nullptr);
bool isSetup(const Utils::FilePath &buildPath); bool isSetup(const Utils::FilePath &buildPath);
class MesonWrapper final : public ToolWrapper
{
public:
using ToolWrapper::ToolWrapper;
Command setup(const Utils::FilePath &sourceDirectory,
const Utils::FilePath &buildDirectory,
const QStringList &options = {}) const;
Command configure(const Utils::FilePath &sourceDirectory,
const Utils::FilePath &buildDirectory,
const QStringList &options = {}) const;
Command regenerate(const Utils::FilePath &sourceDirectory,
const Utils::FilePath &buildDirectory) const;
Command introspect(const Utils::FilePath &sourceDirectory) const;
};
class NinjaWrapper final : public ToolWrapper
{
public:
using ToolWrapper::ToolWrapper;
};
std::optional<Utils::FilePath> findMesonTool(); std::optional<Utils::FilePath> findMesonTool();
std::optional<Utils::FilePath> findNinjaTool(); std::optional<Utils::FilePath> findNinjaTool();