Meson: Dismantle classes in favor of static methods

Change-Id: Ifefd2847feb57c613a4089ed9d12b0d07d91d30c
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Jarek Kobus
2024-07-25 16:29:24 +02:00
parent 3188c8785b
commit d0621a7d54

View File

@@ -20,206 +20,179 @@ using namespace Utils;
namespace MesonProjectManager::Internal::MesonInfoParser { namespace MesonProjectManager::Internal::MesonInfoParser {
class BuildOptionsParser static std::unique_ptr<BuildOption> loadOption(const QJsonObject &option)
{ {
static inline std::unique_ptr<BuildOption> load_option(const QJsonObject &option) const auto type = option["type"].toString();
{ if (type == "string")
const auto type = option["type"].toString(); return std::make_unique<StringBuildOption>(option["name"].toString(),
if (type == "string") option["section"].toString(),
return std::make_unique<StringBuildOption>(option["name"].toString(), option["description"].toString(),
option["section"].toString(), option["value"]);
option["description"].toString(), if (type == "boolean")
option["value"]); return std::make_unique<BooleanBuildOption>(option["name"].toString(),
if (type == "boolean")
return std::make_unique<BooleanBuildOption>(option["name"].toString(),
option["section"].toString(),
option["description"].toString(),
option["value"]);
if (type == "combo")
return std::make_unique<ComboBuildOption>(option["name"].toString(),
option["section"].toString(),
option["description"].toString(),
option["choices"].toVariant().toStringList(),
option["value"]);
if (type == "integer")
return std::make_unique<IntegerBuildOption>(option["name"].toString(),
option["section"].toString(),
option["description"].toString(),
option["value"]);
if (type == "array")
return std::make_unique<ArrayBuildOption>(option["name"].toString(),
option["section"].toString(),
option["description"].toString(),
option["value"].toVariant());
if (type == "feature")
return std::make_unique<FeatureBuildOption>(option["name"].toString(),
option["section"].toString(),
option["description"].toString(),
option["value"]);
return std::make_unique<UnknownBuildOption>(option["name"].toString(),
option["section"].toString(), option["section"].toString(),
option["description"].toString()); option["description"].toString(),
} option["value"]);
if (type == "combo")
return std::make_unique<ComboBuildOption>(option["name"].toString(),
option["section"].toString(),
option["description"].toString(),
option["choices"].toVariant().toStringList(),
option["value"]);
if (type == "integer")
return std::make_unique<IntegerBuildOption>(option["name"].toString(),
option["section"].toString(),
option["description"].toString(),
option["value"]);
if (type == "array")
return std::make_unique<ArrayBuildOption>(option["name"].toString(),
option["section"].toString(),
option["description"].toString(),
option["value"].toVariant());
if (type == "feature")
return std::make_unique<FeatureBuildOption>(option["name"].toString(),
option["section"].toString(),
option["description"].toString(),
option["value"]);
return std::make_unique<UnknownBuildOption>(option["name"].toString(),
option["section"].toString(),
option["description"].toString());
}
static inline std::vector<std::unique_ptr<BuildOption>> load_options(const QJsonArray &arr) static std::vector<std::unique_ptr<BuildOption>> loadOptions(const QJsonArray &arr)
{
std::vector<std::unique_ptr<BuildOption>> buildOptions;
std::transform(std::cbegin(arr),
std::cend(arr),
std::back_inserter(buildOptions),
[](const auto &option) { return load_option(option.toObject()); });
return buildOptions;
}
std::vector<std::unique_ptr<BuildOption>> m_buildOptions;
public:
BuildOptionsParser(const FilePath &buildDir)
{
FilePath path = buildDir / Constants::MESON_INFO_DIR / Constants::MESON_INTRO_BUIDOPTIONS;
auto arr = load<QJsonArray>(path.toFSPathString());
if (arr)
m_buildOptions = load_options(*arr);
}
BuildOptionsParser(const QJsonDocument &js)
{
auto obj = get<QJsonArray>(js.object(), "buildoptions");
if (obj)
m_buildOptions = load_options(*obj);
}
inline std::vector<std::unique_ptr<BuildOption>> takeBuildOptions()
{
return std::move(m_buildOptions);
}
};
class TargetParser
{ {
static inline Target::SourceGroup extract_source(const QJsonValue &source) std::vector<std::unique_ptr<BuildOption>> buildOptions;
{ std::transform(std::cbegin(arr),
const auto srcObj = source.toObject(); std::cend(arr),
return {srcObj["language"].toString(), std::back_inserter(buildOptions),
srcObj["compiler"].toVariant().toStringList(), [](const auto &option) { return loadOption(option.toObject()); });
srcObj["parameters"].toVariant().toStringList(), return buildOptions;
srcObj["sources"].toVariant().toStringList(), }
srcObj["generated_sources"].toVariant().toStringList()};
}
static inline Target::SourceGroupList extract_sources(const QJsonArray &sources) static std::vector<std::unique_ptr<BuildOption>> buildOptionsList(const FilePath &buildDir)
{
Target::SourceGroupList res;
std::transform(std::cbegin(sources),
std::cend(sources),
std::back_inserter(res),
extract_source);
return res;
}
static inline Target extract_target(const QJsonValue &target)
{
auto targetObj = target.toObject();
Target t{targetObj["type"].toString(),
targetObj["name"].toString(),
targetObj["id"].toString(),
targetObj["defined_in"].toString(),
targetObj["filename"].toVariant().toStringList(),
targetObj["extra_files"].toVariant().toStringList(),
targetObj["subproject"].toString(),
extract_sources(targetObj["target_sources"].toArray())};
return t;
}
static inline TargetsList load_targets(const QJsonArray &arr)
{
TargetsList targets;
std::transform(std::cbegin(arr),
std::cend(arr),
std::back_inserter(targets),
extract_target);
return targets;
}
public:
static TargetsList targetList(const QJsonDocument &js)
{
if (auto obj = get<QJsonArray>(js.object(), "targets"))
return load_targets(*obj);
return {};
}
static TargetsList targetList(const FilePath &buildDir)
{
const FilePath path = buildDir / Constants::MESON_INFO_DIR / Constants::MESON_INTRO_TARGETS;
if (auto arr = load<QJsonArray>(path.toFSPathString()))
return load_targets(*arr);
return {};
}
};
class BuildSystemFilesParser
{ {
static void appendFiles(const std::optional<QJsonArray> &arr, FilePaths &dest) FilePath path = buildDir / Constants::MESON_INFO_DIR / Constants::MESON_INTRO_BUIDOPTIONS;
{ auto arr = load<QJsonArray>(path.toFSPathString());
if (arr) if (arr)
std::transform(std::cbegin(*arr), return loadOptions(*arr);
std::cend(*arr), return {};
std::back_inserter(dest), }
[](const auto &file) {
return FilePath::fromString(file.toString());
});
}
public: static std::vector<std::unique_ptr<BuildOption>> buildOptionsList(const QJsonDocument &js)
static inline FilePaths files(const FilePath &buildDir) {
{ auto obj = get<QJsonArray>(js.object(), "buildoptions");
FilePaths files; if (obj)
FilePath path = buildDir / Constants::MESON_INFO_DIR / Constants::MESON_INTRO_BUILDSYSTEM_FILES; return loadOptions(*obj);
auto arr = load<QJsonArray>(path.toFSPathString()); return {};
appendFiles(arr, files); }
return files;
}
static inline FilePaths files(const QJsonDocument &js) static Target::SourceGroup extract_source(const QJsonValue &source)
{ {
FilePaths files; const auto srcObj = source.toObject();
auto arr = get<QJsonArray>(js.object(), "projectinfo", "buildsystem_files"); return {
srcObj["language"].toString(),
srcObj["compiler"].toVariant().toStringList(),
srcObj["parameters"].toVariant().toStringList(),
srcObj["sources"].toVariant().toStringList(),
srcObj["generated_sources"].toVariant().toStringList()
};
}
static Target::SourceGroupList extract_sources(const QJsonArray &sources)
{
Target::SourceGroupList res;
std::transform(std::cbegin(sources), std::cend(sources), std::back_inserter(res), extract_source);
return res;
}
static Target extract_target(const QJsonValue &target)
{
auto targetObj = target.toObject();
return {
targetObj["type"].toString(),
targetObj["name"].toString(),
targetObj["id"].toString(),
targetObj["defined_in"].toString(),
targetObj["filename"].toVariant().toStringList(),
targetObj["extra_files"].toVariant().toStringList(),
targetObj["subproject"].toString(),
extract_sources(targetObj["target_sources"].toArray())
};
}
static TargetsList load_targets(const QJsonArray &arr)
{
TargetsList targets;
std::transform(std::cbegin(arr), std::cend(arr), std::back_inserter(targets), extract_target);
return targets;
}
static TargetsList targetsList(const QJsonDocument &js)
{
if (auto obj = get<QJsonArray>(js.object(), "targets"))
return load_targets(*obj);
return {};
}
static TargetsList targetsList(const FilePath &buildDir)
{
const FilePath path = buildDir / Constants::MESON_INFO_DIR / Constants::MESON_INTRO_TARGETS;
if (auto arr = load<QJsonArray>(path.toFSPathString()))
return load_targets(*arr);
return {};
}
static void appendFiles(const std::optional<QJsonArray> &arr, FilePaths &dest)
{
if (!arr)
return;
std::transform(std::cbegin(*arr), std::cend(*arr), std::back_inserter(dest), [](const auto &file) {
return FilePath::fromString(file.toString());
});
}
static FilePaths files(const FilePath &buildDir)
{
FilePaths files;
FilePath path = buildDir / Constants::MESON_INFO_DIR / Constants::MESON_INTRO_BUILDSYSTEM_FILES;
auto arr = load<QJsonArray>(path.toFSPathString());
appendFiles(arr, files);
return files;
}
static FilePaths files(const QJsonDocument &js)
{
FilePaths files;
auto arr = get<QJsonArray>(js.object(), "projectinfo", "buildsystem_files");
appendFiles(arr, files);
const auto subprojects = get<QJsonArray>(js.object(), "projectinfo", "subprojects");
for (const auto &subproject : *subprojects) {
auto arr = get<QJsonArray>(subproject.toObject(), "buildsystem_files");
appendFiles(arr, files); appendFiles(arr, files);
const auto subprojects = get<QJsonArray>(js.object(), "projectinfo", "subprojects");
for (const auto &subproject : *subprojects) {
auto arr = get<QJsonArray>(subproject.toObject(), "buildsystem_files");
appendFiles(arr, files);
}
return files;
} }
}; return files;
}
Result parse(const FilePath &buildDir) Result parse(const FilePath &buildDir)
{ {
return {TargetParser::targetList(buildDir), return {targetsList(buildDir), buildOptionsList(buildDir), files(buildDir)};
BuildOptionsParser{buildDir}.takeBuildOptions(),
BuildSystemFilesParser::files(buildDir)};
} }
Result parse(const QByteArray &data) Result parse(const QByteArray &data)
{ {
auto json = QJsonDocument::fromJson(data); const auto json = QJsonDocument::fromJson(data);
return {TargetParser::targetList(json), return {targetsList(json), buildOptionsList(json), files(json)};
BuildOptionsParser{json}.takeBuildOptions(),
BuildSystemFilesParser::files(json)};
} }
Result parse(QIODevice *introFile) Result parse(QIODevice *introFile)
{ {
if (introFile) { if (!introFile)
if (!introFile->isOpen()) return {};
introFile->open(QIODevice::ReadOnly | QIODevice::Text); if (!introFile->isOpen())
introFile->seek(0); introFile->open(QIODevice::ReadOnly | QIODevice::Text);
auto data = introFile->readAll(); introFile->seek(0);
return parse(data); auto data = introFile->readAll();
} return parse(data);
return {};
} }
} // namespace MesonProjectManager::Internal::MesonInfoParser } // namespace MesonProjectManager::Internal::MesonInfoParser