From 2e3f39138e5f3fcb955869de5c0d41f687b5b3c5 Mon Sep 17 00:00:00 2001 From: taynpg Date: Sat, 15 Feb 2025 01:13:25 +0800 Subject: [PATCH] =?UTF-8?q?add=EF=BC=9A=E5=88=9D=E6=AD=A5=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E6=89=93=E5=8C=85lua=E8=84=9A=E6=9C=AC=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 ++- fs/lib.cxx | 38 +++++++++++++++++++++- fs/lib.h | 1 + pack/pack.lua | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++ pack/pack.nsi | 82 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 212 insertions(+), 2 deletions(-) create mode 100644 pack/pack.lua create mode 100644 pack/pack.nsi diff --git a/.gitignore b/.gitignore index 93a9432..28a99cc 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,6 @@ build cmake-* auto_build export -.xmake \ No newline at end of file +.xmake +*.exe +*.dll \ No newline at end of file diff --git a/fs/lib.cxx b/fs/lib.cxx index f701639..c7be35e 100644 --- a/fs/lib.cxx +++ b/fs/lib.cxx @@ -1,5 +1,5 @@ #include "lib.h" -#include +#include #ifdef USE_BOOST_FILESYSTEM #include @@ -22,6 +22,7 @@ static const luaL_Reg lua_fs[] = {{"exists", exists}, {"get_parent", get_parent}, {"append_path", append_path}, {"mkdir", mkdir}, + {"normalize", normalize}, {NULL, NULL}}; int luaopen_lua_fs(lua_State* L) @@ -30,6 +31,20 @@ int luaopen_lua_fs(lua_State* L) return 1; } +static std::string replace(const std::string& str, const std::string& from, const std::string& to) +{ + if (from.empty()) { + return str; + } + std::string result = str; + size_t startPos = 0; + while ((startPos = result.find(from, startPos)) != std::string::npos) { + result.replace(startPos, from.length(), to); + startPos += to.length(); + } + return result; +} + /// @brief 创建文件夹 /// @param path string 路径或者文件夹名称 /// @return boolean @@ -62,6 +77,27 @@ int get_parent(lua_State* L) return 1; } +/// @brief 规范化路径 +/// @param path string 路径 +/// @return string +int normalize(lua_State* L) +{ + const char* path = luaL_checkstring(L, 1); +#ifdef _WIN32 + std::string normalized = replace(path, "/", "\\"); + if (!normalized.empty() && normalized.back() == '\\') { + normalized.pop_back(); + } +#else + std::string normalized = replace(path, "\\", "/"); + if (!normalized.empty() && normalized.back() == '/') { + normalized.pop_back(); + } +#endif + lua_pushstring(L, normalized.c_str()); + return 1; +} + /// @brief 拼接路径 /// @param path string 基本路径 /// @param append string 添加路径 diff --git a/fs/lib.h b/fs/lib.h index d64b114..937a5b4 100644 --- a/fs/lib.h +++ b/fs/lib.h @@ -29,6 +29,7 @@ LIB_API int get_current_directory(lua_State* L); LIB_API int get_parent(lua_State* L); LIB_API int append_path(lua_State* L); LIB_API int mkdir(lua_State* L); +LIB_API int normalize(lua_State* L); #ifdef __cplusplus } diff --git a/pack/pack.lua b/pack/pack.lua new file mode 100644 index 0000000..0bf4bbc --- /dev/null +++ b/pack/pack.lua @@ -0,0 +1,89 @@ +local fs = require("lua_fs") + +local path1 = "C:\\Program Files\\NSIS\\makensis.exe" +local path2 = "C:\\Program Files (x86)\\NSIS\\makensis.exe" +local version = "v0.1" +local package_name = "mlua" +local extern = "" + +-- 确定使用哪个路径 +local makensis_path = nil + +if fs.exists(path2) then + makensis_path = path2 +elseif fs.exists(path1) then + makensis_path = path1 +else + print("Error: makensis.exe not found!") + return +end + +-- 执行 xmake f -v 命令并获取输出 +local handle = io.popen('cd .. && xmake f -v') -- 使用 io.popen 执行命令并获取输出句柄 +if not handle then + print("xmake f -v 命令执行失败") + return +end +local output = handle:read("*a") -- 读取命令输出 +handle:close() -- 关闭文件句柄 + +-- 匹配并提取 plat, arch 和 mode +local plat = string.match(output, "plat = (%S+)") +local arch = string.match(output, "arch = (%S+)") +local mode = string.match(output, "mode = (%S+)") + +-- 打印结果 +print("plat: " .. plat) +print("arch: " .. arch) +print("mode: " .. mode) + +-- 获取当前脚本所在的目录 +local cur = fs.get_current_directory() +local updir = fs.get_parent(cur) +local re_bin_dir = updir .. "/build/" .. plat .. "/" .. arch .. "/" .. mode +local bin_dir = fs.normalize(re_bin_dir) +print("bin_dir: " .. bin_dir) + +local files = "" +local filenames = {"mlua.exe", "mluac.exe", "lua_fs.dll"} +local function add_files_to_list(arg_bin_dir) + for _, filename in ipairs(filenames) do + files = files .. "File \"" .. fs.append_path(arg_bin_dir, filename) .. "\"\n " + end + return files +end + +add_files_to_list(bin_dir) + +-- 打开文件 +local file = io.open("pack.nsi", "r") +if not file then + print("文件打开失败") + return +end + +-- 读取文件内容 +local content = file:read("*all") +file:close() + +local new_str = string.gsub(content, "REP_NAME", package_name) +new_str = string.gsub(new_str, "REP_BIN_FILE", package_name .. "-" .. version .. "-" .. arch .. extern) +new_str = string.gsub(new_str, ";REP_FILES", files) + +print(new_str) +local f = io.open("tpack.nsi", "w") +if f then + f:write(new_str) + f:close() + print("success to save file tpack.nsi") +else + print("can't open file tpack.nsi") +end + +local command = "\"\"" .. makensis_path .. "\" tpack.nsi\"" +print("command: " .. command) + +local result = os.execute(command) +os.remove("tpack.nsi") +print("End...") + diff --git a/pack/pack.nsi b/pack/pack.nsi new file mode 100644 index 0000000..bab21b7 --- /dev/null +++ b/pack/pack.nsi @@ -0,0 +1,82 @@ +;NSIS Modern User Interface +;Welcome/Finish Page Example Script +;Written by Joost Verburg + +;-------------------------------- +;Include Modern UI + + !include "MUI2.nsh" + +;-------------------------------- +;General + + !define ProgramName "REP_NAME" + Name "${ProgramName}" + OutFile "REP_BIN_FILE.exe" + Unicode True + InstallDir "$LOCALAPPDATA\${ProgramName}" + RequestExecutionLevel user + +;-------------------------------- +;Interface Settings + + !define MUI_ABORTWARNING + +;-------------------------------- +;Pages + + !insertmacro MUI_PAGE_WELCOME + ;!insertmacro MUI_PAGE_LICENSE "REP_LICENSE" + !insertmacro MUI_PAGE_COMPONENTS + !insertmacro MUI_PAGE_DIRECTORY + !insertmacro MUI_PAGE_INSTFILES + !insertmacro MUI_PAGE_FINISH + + !insertmacro MUI_UNPAGE_WELCOME + !insertmacro MUI_UNPAGE_CONFIRM + !insertmacro MUI_UNPAGE_INSTFILES + !insertmacro MUI_UNPAGE_FINISH + +;-------------------------------- +;Languages + !insertmacro MUI_LANGUAGE "SimpChinese" + +;-------------------------------- +;Installer Sections + +Section "REP_NAME" All + + SetOutPath "$INSTDIR" + + ;ADD YOUR OWN FILES HERE... + ;REP_FILES + ;Create uninstaller + WriteUninstaller "$INSTDIR\Uninstall.exe" + +SectionEnd + +;-------------------------------- +;Descriptions + + ;Language strings + LangString DESC_All 2052 "install all main files." + + ;Assign language strings to sections + !insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN + !insertmacro MUI_DESCRIPTION_TEXT ${All} $(DESC_All) + !insertmacro MUI_FUNCTION_DESCRIPTION_END + +;-------------------------------- +;Uninstaller Section + +Section "Uninstall" + + ;ADD YOUR OWN FILES HERE... + + Delete "$INSTDIR\Uninstall.exe" + + RMDir "$INSTDIR" + + DeleteRegKey /ifempty HKCU "Software\${ProgramName}" + +SectionEnd