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 Internal {
template<typename T>
inline bool is(const MesonTools::Tool_t &tool)
MesonTools::Tool_t tool(const Utils::Id &id,
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),
std::cend(tools),
[&id](const MesonTools::Tool_t &tool) {
return tool->id() == id;
});
if (tool != std::cend(tools) && is<T>(*tool))
return std::dynamic_pointer_cast<T>(*tool);
if (tool != std::cend(tools) && (*tool)->toolType() == toolType)
return *tool;
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)
{
return is<MesonWrapper>(tool);
return tool->toolType() == ToolType::Meson;
}
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)
@@ -79,25 +73,24 @@ void MesonTools::setTools(std::vector<MesonTools::Tool_t> &&tools)
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);
}
std::shared_ptr<MesonWrapper> MesonTools::mesonWrapper(const Utils::Id &id)
{
return tool<MesonWrapper>(id, MesonTools::instance()->m_tools);
return tool(id, MesonTools::instance()->m_tools, ToolType::Ninja);
}
std::shared_ptr<NinjaWrapper> MesonTools::ninjaWrapper()
std::shared_ptr<ToolWrapper> MesonTools::mesonWrapper(const Utils::Id &id)
{
return std::dynamic_pointer_cast<NinjaWrapper>(
autoDetected(MesonTools::instance()->m_tools, ToolType::Ninja));
return tool(id, MesonTools::instance()->m_tools, ToolType::Meson);
}
std::shared_ptr<MesonWrapper> MesonTools::mesonWrapper()
std::shared_ptr<ToolWrapper> MesonTools::autoDetectedNinja()
{
return std::dynamic_pointer_cast<MesonWrapper>(
autoDetected(MesonTools::instance()->m_tools, ToolType::Meson));
return autoDetected(MesonTools::instance()->m_tools, ToolType::Ninja);
}
std::shared_ptr<ToolWrapper> MesonTools::autoDetectedMeson()
{
return autoDetected(MesonTools::instance()->m_tools, ToolType::Meson);
}
} // namespace Internal

View File

@@ -69,11 +69,11 @@ public:
emit self->toolRemoved(*item);
}
static std::shared_ptr<NinjaWrapper> ninjaWrapper(const Utils::Id &id);
static std::shared_ptr<MesonWrapper> mesonWrapper(const Utils::Id &id);
static std::shared_ptr<ToolWrapper> ninjaWrapper(const Utils::Id &id);
static std::shared_ptr<ToolWrapper> mesonWrapper(const Utils::Id &id);
static std::shared_ptr<NinjaWrapper> ninjaWrapper();
static std::shared_ptr<MesonWrapper> mesonWrapper();
static std::shared_ptr<ToolWrapper> autoDetectedNinja();
static std::shared_ptr<ToolWrapper> autoDetectedMeson();
Q_SIGNAL void toolAdded(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(); \
const auto tool = findMesonTool(); \
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); \
__VA_ARGS__ \
}
@@ -78,7 +78,7 @@ private slots:
FilePath buildDir = FilePath::fromString(build_dir.path());
const auto tool = findMesonTool();
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));
QVERIFY(isSetup(buildDir));

View File

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

View File

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

View File

@@ -17,7 +17,7 @@ public:
static Utils::Id mesonToolId(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
@@ -27,7 +27,7 @@ public:
static Utils::Id ninjaToolId(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

View File

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

View File

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

View File

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