fix:优化自动生成脚本。

This commit is contained in:
taynpg 2025-02-13 11:21:05 +08:00
parent bddec5b850
commit f709f3a2a1
6 changed files with 35 additions and 22 deletions

View File

@ -1,3 +1,3 @@
{
"workspace.library": ["./build/bin/Debug/def", "./build/bin/Release/def"]
"workspace.library": ["./build/defs"]
}

View File

@ -1,2 +0,0 @@
@echo off
cmd /k "set PATH=%PATH%;%~dp0..\build\bin\Debug"

2
fs/env.bat Normal file
View File

@ -0,0 +1,2 @@
@echo off
cmd /k "set PATH=%PATH%;%~dp0..\build\bin\"

View File

@ -1,7 +1,7 @@
-- 加载模块
local fs = require("lua_fs")
print(fs.file_exists("D:\\download"))
local files = fs.get_dir_files("D:\\download")
local files = fs.get_dir_files("D:\\")
if files then
for i, filename in ipairs(files) do
print(i, filename) -- 输出索引和文件名

View File

@ -8,6 +8,15 @@ static const luaL_Reg lua_fs[] = {
{"file_exists", file_exists}, {"is_regular_file", is_regular_file}, {"copy_file", copy_file},
{"delete_file", delete_file}, {"get_dir_files", get_dir_files}, {NULL, NULL}};
int luaopen_lua_fs(lua_State* L)
{
luaL_newlib(L, lua_fs);
return 1;
}
/// @brief 文件是否存在
/// @param path string 文件路径
/// @return boolean
int file_exists(lua_State* L)
{
const char* path = luaL_checkstring(L, 1);
@ -17,8 +26,8 @@ int file_exists(lua_State* L)
}
/// @brief 文件是否是一个常规文件
/// @param path 文件路径
/// @return
/// @param path string 文件路径
/// @return boolean
int is_regular_file(lua_State* L)
{
const char* path = luaL_checkstring(L, 1);
@ -28,9 +37,9 @@ int is_regular_file(lua_State* L)
}
/// @brief 复制文件
/// @param source 源文件(string)
/// @param destination 目标文件(string)
/// @return bool
/// @param source string 源文件
/// @param destination string 目标文件
/// @return boolean
int copy_file(lua_State* L)
{
const char* source = luaL_checkstring(L, 1);
@ -40,6 +49,9 @@ int copy_file(lua_State* L)
return 1;
}
/// @brief 删除文件
/// @param path 文件路径(string)
/// @return boolean
int delete_file(lua_State* L)
{
const char* path = luaL_checkstring(L, 1);
@ -48,12 +60,9 @@ int delete_file(lua_State* L)
return 1;
}
int luaopen_lua_fs(lua_State* L)
{
luaL_newlib(L, lua_fs);
return 1;
}
/// @brief 获取一个目录下所有文件(不递归)
/// @param dir_path string 目录
/// @return table
int get_dir_files(lua_State* L)
{
// 获取第一个参数:目录路径

View File

@ -24,7 +24,8 @@ std::string to_full(const std::string& path)
struct LuaFunction {
std::string name;
std::string brief;
std::vector<std::pair<std::string, std::string>> params;
std::vector<std::tuple<std::string, std::string, std::string>>
params; // (name, type, description)
std::string returnType;
};
@ -33,7 +34,7 @@ std::vector<LuaFunction> parse_cpp(const std::string& cpp_code)
{
std::vector<LuaFunction> functions;
std::regex func_regex(R"(///\s*@brief\s*(.*))");
std::regex param_regex(R"(///\s*@param\s*(\w+)\s*(.*))");
std::regex param_regex(R"(///\s*@param\s*(\w+)\s*(\w+)\s*(.*))");
std::regex return_regex(R"(///\s*@return\s*(.*))");
std::regex name_regex(R"(\bint\s+(\w+)\s*\()");
@ -49,7 +50,8 @@ std::vector<LuaFunction> parse_cpp(const std::string& cpp_code)
current.brief = match[1].str();
inFunction = true;
} else if (std::regex_search(line, match, param_regex)) {
current.params.emplace_back(match[1].str(), match[2].str());
// 将参数的name, type和description存储到params中
current.params.emplace_back(match[1].str(), match[2].str(), match[3].str());
} else if (std::regex_search(line, match, return_regex)) {
current.returnType = match[1].str().empty() ? "boolean" : match[1].str();
} else if (std::regex_search(line, match, name_regex)) {
@ -64,7 +66,8 @@ std::vector<LuaFunction> parse_cpp(const std::string& cpp_code)
}
// 生成 Lua 绑定文件
void generate_lua_file(const std::vector<LuaFunction>& functions, const std::string& filename, const std::string& mod_name)
void generate_lua_file(const std::vector<LuaFunction>& functions, const std::string& filename,
const std::string& mod_name)
{
std::ofstream out(filename);
if (!out) {
@ -82,18 +85,19 @@ void generate_lua_file(const std::vector<LuaFunction>& functions, const std::str
for (const auto& func : functions) {
out << "--- " << func.brief << "\n";
for (const auto& param : func.params) {
out << "--- @param " << param.first << " " << param.second << "\n";
out << "--- @param " << std::get<0>(param) << " " << std::get<1>(param) << " "
<< std::get<2>(param) << "\n";
}
out << "--- @return " << func.returnType << "\n";
out << "function M." << func.name << "(";
for (size_t i = 0; i < func.params.size(); ++i) {
out << func.params[i].first;
out << std::get<0>(func.params[i]);
if (i < func.params.size() - 1)
out << ", ";
}
out << ")\n return " << mod_call_p << func.name << "(";
for (size_t i = 0; i < func.params.size(); ++i) {
out << func.params[i].first;
out << std::get<0>(func.params[i]);
if (i < func.params.size() - 1)
out << ", ";
}