fix:优化自动生成脚本。
This commit is contained in:
parent
bddec5b850
commit
f709f3a2a1
@ -1,3 +1,3 @@
|
|||||||
{
|
{
|
||||||
"workspace.library": ["./build/bin/Debug/def", "./build/bin/Release/def"]
|
"workspace.library": ["./build/defs"]
|
||||||
}
|
}
|
@ -1,2 +0,0 @@
|
|||||||
@echo off
|
|
||||||
cmd /k "set PATH=%PATH%;%~dp0..\build\bin\Debug"
|
|
2
fs/env.bat
Normal file
2
fs/env.bat
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
@echo off
|
||||||
|
cmd /k "set PATH=%PATH%;%~dp0..\build\bin\"
|
@ -1,7 +1,7 @@
|
|||||||
-- 加载模块
|
-- 加载模块
|
||||||
local fs = require("lua_fs")
|
local fs = require("lua_fs")
|
||||||
print(fs.file_exists("D:\\download"))
|
print(fs.file_exists("D:\\download"))
|
||||||
local files = fs.get_dir_files("D:\\download")
|
local files = fs.get_dir_files("D:\\")
|
||||||
if files then
|
if files then
|
||||||
for i, filename in ipairs(files) do
|
for i, filename in ipairs(files) do
|
||||||
print(i, filename) -- 输出索引和文件名
|
print(i, filename) -- 输出索引和文件名
|
||||||
|
31
fs/lib.cxx
31
fs/lib.cxx
@ -8,6 +8,15 @@ static const luaL_Reg lua_fs[] = {
|
|||||||
{"file_exists", file_exists}, {"is_regular_file", is_regular_file}, {"copy_file", copy_file},
|
{"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}};
|
{"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)
|
int file_exists(lua_State* L)
|
||||||
{
|
{
|
||||||
const char* path = luaL_checkstring(L, 1);
|
const char* path = luaL_checkstring(L, 1);
|
||||||
@ -17,8 +26,8 @@ int file_exists(lua_State* L)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// @brief 文件是否是一个常规文件
|
/// @brief 文件是否是一个常规文件
|
||||||
/// @param path 文件路径
|
/// @param path string 文件路径
|
||||||
/// @return
|
/// @return boolean
|
||||||
int is_regular_file(lua_State* L)
|
int is_regular_file(lua_State* L)
|
||||||
{
|
{
|
||||||
const char* path = luaL_checkstring(L, 1);
|
const char* path = luaL_checkstring(L, 1);
|
||||||
@ -28,9 +37,9 @@ int is_regular_file(lua_State* L)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// @brief 复制文件
|
/// @brief 复制文件
|
||||||
/// @param source 源文件(string)
|
/// @param source string 源文件
|
||||||
/// @param destination 目标文件(string)
|
/// @param destination string 目标文件
|
||||||
/// @return bool
|
/// @return boolean
|
||||||
int copy_file(lua_State* L)
|
int copy_file(lua_State* L)
|
||||||
{
|
{
|
||||||
const char* source = luaL_checkstring(L, 1);
|
const char* source = luaL_checkstring(L, 1);
|
||||||
@ -40,6 +49,9 @@ int copy_file(lua_State* L)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// @brief 删除文件
|
||||||
|
/// @param path 文件路径(string)
|
||||||
|
/// @return boolean
|
||||||
int delete_file(lua_State* L)
|
int delete_file(lua_State* L)
|
||||||
{
|
{
|
||||||
const char* path = luaL_checkstring(L, 1);
|
const char* path = luaL_checkstring(L, 1);
|
||||||
@ -48,12 +60,9 @@ int delete_file(lua_State* L)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int luaopen_lua_fs(lua_State* L)
|
/// @brief 获取一个目录下所有文件(不递归)
|
||||||
{
|
/// @param dir_path string 目录
|
||||||
luaL_newlib(L, lua_fs);
|
/// @return table
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int get_dir_files(lua_State* L)
|
int get_dir_files(lua_State* L)
|
||||||
{
|
{
|
||||||
// 获取第一个参数:目录路径
|
// 获取第一个参数:目录路径
|
||||||
|
@ -24,7 +24,8 @@ std::string to_full(const std::string& path)
|
|||||||
struct LuaFunction {
|
struct LuaFunction {
|
||||||
std::string name;
|
std::string name;
|
||||||
std::string brief;
|
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;
|
std::string returnType;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -33,7 +34,7 @@ std::vector<LuaFunction> parse_cpp(const std::string& cpp_code)
|
|||||||
{
|
{
|
||||||
std::vector<LuaFunction> functions;
|
std::vector<LuaFunction> functions;
|
||||||
std::regex func_regex(R"(///\s*@brief\s*(.*))");
|
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 return_regex(R"(///\s*@return\s*(.*))");
|
||||||
std::regex name_regex(R"(\bint\s+(\w+)\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();
|
current.brief = match[1].str();
|
||||||
inFunction = true;
|
inFunction = true;
|
||||||
} else if (std::regex_search(line, match, param_regex)) {
|
} 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)) {
|
} else if (std::regex_search(line, match, return_regex)) {
|
||||||
current.returnType = match[1].str().empty() ? "boolean" : match[1].str();
|
current.returnType = match[1].str().empty() ? "boolean" : match[1].str();
|
||||||
} else if (std::regex_search(line, match, name_regex)) {
|
} else if (std::regex_search(line, match, name_regex)) {
|
||||||
@ -64,7 +66,8 @@ std::vector<LuaFunction> parse_cpp(const std::string& cpp_code)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 生成 Lua 绑定文件
|
// 生成 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);
|
std::ofstream out(filename);
|
||||||
if (!out) {
|
if (!out) {
|
||||||
@ -82,18 +85,19 @@ void generate_lua_file(const std::vector<LuaFunction>& functions, const std::str
|
|||||||
for (const auto& func : functions) {
|
for (const auto& func : functions) {
|
||||||
out << "--- " << func.brief << "\n";
|
out << "--- " << func.brief << "\n";
|
||||||
for (const auto& param : func.params) {
|
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 << "--- @return " << func.returnType << "\n";
|
||||||
out << "function M." << func.name << "(";
|
out << "function M." << func.name << "(";
|
||||||
for (size_t i = 0; i < func.params.size(); ++i) {
|
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)
|
if (i < func.params.size() - 1)
|
||||||
out << ", ";
|
out << ", ";
|
||||||
}
|
}
|
||||||
out << ")\n return " << mod_call_p << func.name << "(";
|
out << ")\n return " << mod_call_p << func.name << "(";
|
||||||
for (size_t i = 0; i < func.params.size(); ++i) {
|
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)
|
if (i < func.params.size() - 1)
|
||||||
out << ", ";
|
out << ", ";
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user