Skip to content

Commit

Permalink
✨ feat(xmake, project): use json parse
Browse files Browse the repository at this point in the history
  • Loading branch information
xqyjlj committed Dec 5, 2023
1 parent efd3002 commit 0408c6d
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 197 deletions.
78 changes: 22 additions & 56 deletions common/core/inc/xmake.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

#include "os.h"
#include "qtjson.h"
#include "qtyaml.h"

class xmake final
{
Expand Down Expand Up @@ -75,18 +74,20 @@ class xmake final

/**
* @brief get package configuration from file
* @param packages: packages ptr
* @param file: json file path
* @return xmake:packages_t
* @return void
*/
static packages_t load_packages_byfile(const QString &file);
static void load_packages_byfile(packages_t *packages, const QString &file);

/**
* @brief get package configuration from csp repo
* @param packages: packages ptr
* @param program: xmake exe path
* @param workdir: xmake workdir
* @return xmake:packages_t
* @return void
*/
static packages_t load_packages(const QString &program = "xmake", const QString &workdir = "");
static void load_packages(packages_t *packages, const QString &program = "xmake", const QString &workdir = "");

private:
xmake() = default;
Expand All @@ -95,61 +96,26 @@ class xmake final
Q_DISABLE_COPY_MOVE(xmake)
};

namespace YAML
{
template <> struct convert<xmake::info_t>
{
static Node encode(const xmake::info_t &rhs)
{
Node node;
node.force_insert("versions", rhs.versions);
node.force_insert("urls", rhs.urls);
node.force_insert("homepage", rhs.homepage);
node.force_insert("description", rhs.description);
node.force_insert("license", rhs.license);
return node;
}

static bool decode(const Node &node, xmake::info_t &rhs)
{
if (!node.IsMap() || node.size() < 2)
return false;

rhs.versions = node["versions"].as<QMap<QString, QString>>();
rhs.urls = node["urls"].as<QStringList>();
rhs.homepage = node["homepage"].as<QString>();
rhs.description = node["description"].as<QString>();
rhs.license = node["license"].as<QString>();
return true;
}
};

template <> struct convert<xmake::packages_t>
{
static Node encode(const xmake::packages_t &rhs)
{
Node node;
node.force_insert("toolchain", rhs.toolchain);
node.force_insert("library", rhs.library);
return node;
}

static bool decode(const Node &node, xmake::packages_t &rhs)
{
if (!node.IsMap() || node.size() < 2)
return false;

rhs.toolchain = node["toolchain"].as<xmake::package_t>();
rhs.library = node["library"].as<xmake::package_t>();
return true;
}
};
} // namespace YAML

namespace nlohmann
{
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(xmake::info_struct, versions, urls, homepage, description, license)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(xmake::packages_struct, toolchain, library)
} // namespace nlohmann

#include <QDebug>

inline QDebug operator<<(QDebug debug, const xmake::info_t &rhs)
{
const nlohmann::json j = rhs;
debug << QString::fromStdString(j.dump(2));
return debug;
}

inline QDebug operator<<(QDebug debug, const xmake::packages_t &rhs)
{
const nlohmann::json j = rhs;
debug << QString::fromStdString(j.dump(2));
return debug;
}

#endif // CSP_COMMON_CORE_XMAKE_H
28 changes: 10 additions & 18 deletions common/core/src/xmake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,19 @@ QString xmake::lua(const QString &lua_path, const QStringList &args, const QStri
return output;
}

xmake::packages_t xmake::load_packages_byfile(const QString &file)
void xmake::load_packages_byfile(packages_t *packages, const QString &file)
{
Q_ASSERT(packages != nullptr);
Q_ASSERT(!file.isEmpty());
Q_ASSERT(os::isfile(file));

try
{
const std::string buffer = os::readfile(file).toStdString();
const YAML::Node yaml_data = YAML::Load(buffer);
return yaml_data.as<xmake::packages_t>();
const nlohmann::json json = nlohmann::json::parse(buffer);
json.get_to(*packages);
}
catch (YAML::BadFile &e)
{
os::show_error_and_exit(e.what());
throw;
}
catch (YAML::BadConversion &e)
catch (const nlohmann::json::exception &e)
{
os::show_error_and_exit(e.what());
throw;
Expand All @@ -103,11 +99,12 @@ xmake::packages_t xmake::load_packages_byfile(const QString &file)
}
}

xmake::packages_t xmake::load_packages(const QString &program, const QString &workdir)
void xmake::load_packages(packages_t *packages, const QString &program, const QString &workdir)
{
const QString repodir = config::repodir();
const QString script_path = QString("%1/tools/csp/dump_package.lua").arg(repodir);

Q_ASSERT(packages != nullptr);
Q_ASSERT(!script_path.isEmpty());
Q_ASSERT(os::isfile(script_path));
Q_ASSERT(!program.isEmpty());
Expand All @@ -116,15 +113,10 @@ xmake::packages_t xmake::load_packages(const QString &program, const QString &wo
try
{
const std::string buffer = yml.toStdString();
const YAML::Node yaml_data = YAML::Load(buffer);
return yaml_data.as<xmake::packages_t>();
}
catch (YAML::BadFile &e)
{
os::show_error_and_exit(e.what());
throw;
const nlohmann::json json = nlohmann::json::parse(buffer);
json.get_to(*packages);
}
catch (YAML::BadConversion &e)
catch (const nlohmann::json::exception &e)
{
os::show_error_and_exit(e.what());
throw;
Expand Down
14 changes: 8 additions & 6 deletions common/core/test/testcase_xmake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,18 @@ class testcase_xmake final : public QObject

static void load_packages_byfile()
{
const auto result = xmake::load_packages_byfile(":/packages.json");
QVERIFY(!result.toolchain.isEmpty());
QVERIFY(!result.library.isEmpty());
xmake::packages_t packages;
xmake::load_packages_byfile(&packages, ":/packages.json");
QVERIFY(!packages.toolchain.isEmpty());
QVERIFY(!packages.library.isEmpty());
}

static void load_packages()
{
const auto result = xmake::load_packages();
QVERIFY(!result.toolchain.isEmpty());
QVERIFY(!result.library.isEmpty());
xmake::packages_t packages;
xmake::load_packages(&packages);
QVERIFY(!packages.toolchain.isEmpty());
QVERIFY(!packages.library.isEmpty());
}

static void cleanupTestCase()
Expand Down
108 changes: 14 additions & 94 deletions common/project/inc/project_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
#define COMMON_PROJECT_CSP_PROJECT_TABLE_H

#include "qtjson.h"
#include "qtyaml.h"

class project_table
{
Expand All @@ -49,13 +48,14 @@ class project_table

typedef struct core_struct
{
QString name; // name
QString hal; // hal
QString hal_name; // hal.name
QString package; // package
QString company; // company
QString type; // type
QString toolchains; // toolchains
QString name; // name
QString hal; // hal
QString hal_name; // hal.name
QString package; // package
QString company; // company
QString type; // type
QString toolchains; // toolchains
QStringList modules; // modules
} core_t;

typedef struct project_struct
Expand All @@ -67,10 +67,11 @@ class project_table
public:
/**
* @brief load project from json file
* @param project: project ptr
* @param path: project file path
* @return project
* @return void
*/
static project_t load_project(const QString &path);
static void load_project(project_t *project, const QString &path);

/**
* @brief save project to json file
Expand All @@ -91,93 +92,12 @@ class project_table
~project_table();
};

namespace YAML
{
template <> struct convert<project_table::project_t>
{
static Node encode(const project_table::project_t &rhs)
{
Node node;
node.force_insert("core", rhs.core);
node.force_insert("pin_configs", rhs.pin_configs);
return node;
}

static bool decode(const Node &node, project_table::project_t &rhs)
{
if (!node.IsMap() || node.size() < 1)
return false;

rhs.core = node["core"].as<project_table::core_t>();
if (node["pin_configs"].IsDefined())
rhs.pin_configs = node["pin_configs"].as<QMap<QString, project_table::pin_config_t>>();
return true;
}
};

template <> struct convert<project_table::pin_config_t>
{
static Node encode(const project_table::pin_config_t &rhs)
{
Node node;
node.force_insert("function", rhs.function);
node.force_insert("comment", rhs.comment);
node.force_insert("locked", rhs.locked);
node.force_insert("function_property", rhs.function_property);
return node;
}

static bool decode(const Node &node, project_table::pin_config_t &rhs)
{
if (!node.IsMap() || node.size() < 3)
return false;

rhs.function = node["function"].as<QString>();
rhs.comment = node["comment"].as<QString>();
rhs.locked = node["locked"].as<bool>();
if (node["function_property"].IsDefined())
rhs.function_property = node["function_property"].as<project_table::pin_function_properties_t>();
return true;
}
};

template <> struct convert<project_table::core_t>
{
static Node encode(const project_table::core_t &rhs)
{
Node node;
node.force_insert("name", rhs.name);
node.force_insert("hal", rhs.hal);
node.force_insert("hal_name", rhs.hal_name);
node.force_insert("package", rhs.package);
node.force_insert("company", rhs.company);
node.force_insert("type", rhs.type);
node.force_insert("toolchains", rhs.toolchains);
return node;
}

static bool decode(const Node &node, project_table::core_t &rhs)
{
if (!node.IsMap() || node.size() < 7)
return false;

rhs.name = node["name"].as<QString>();
rhs.hal = node["hal"].as<QString>();
rhs.hal_name = node["hal_name"].as<QString>();
rhs.package = node["package"].as<QString>();
rhs.company = node["company"].as<QString>();
rhs.type = node["type"].as<QString>();
rhs.toolchains = node["toolchains"].as<QString>();
return true;
}
};
} // namespace YAML

namespace nlohmann
{
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(project_table::pin_config_struct, function, comment, locked, function_property)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(project_table::core_struct, name, hal, hal_name, package, company, type, toolchains)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(project_table::project_struct, core)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(project_table::core_struct, name, hal, hal_name, package, company, type, toolchains,
modules)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(project_table::project_struct, core, pin_configs)
} // namespace nlohmann

#include <QDebug>
Expand Down
13 changes: 8 additions & 5 deletions common/project/src/project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ void project::set_pin_config_fp(const QString &key, const QString &module, const
Q_ASSERT(!property.isEmpty());
Q_ASSERT(!value.isEmpty());

emit signals_pin_function_property_changed(module, property, key, _project.pin_configs[key].function_property[module][property],
value);
emit signals_pin_function_property_changed(module, property, key,
_project.pin_configs[key].function_property[module][property], value);
_project.pin_configs[key].function_property[module][property] = value;
}

Expand All @@ -216,8 +216,8 @@ void project::clear_pin_config_fp(const QString &key, const QString &module, con
{
if (_project.pin_configs[key].function_property[module].contains(property))
{
emit signals_pin_function_property_changed(module, property, key,
_project.pin_configs[key].function_property[module][property], "");
emit signals_pin_function_property_changed(
module, property, key, _project.pin_configs[key].function_property[module][property], "");
_project.pin_configs[key].function_property[module].remove(property);
}
}
Expand Down Expand Up @@ -255,7 +255,10 @@ QString &project::get_pin_config_fp(const QString &key, const QString &module, c

void project::load_project(const QString &path)
{
_project = project_table::load_project(path);
Q_ASSERT(!path.isEmpty());
Q_ASSERT(os::isfile(path));

project_table::load_project(&_project, path);
_path = path;

load_maps(_project.core.hal);
Expand Down
Loading

0 comments on commit 0408c6d

Please sign in to comment.