| 390 | } |
| 391 | |
| 392 | void cliRegisterLua(lua_State* L, int argc, char* argv[], const fs::path& scriptPath, int cliIndex, const std::optional<fs::path>& assetPath) { |
| 393 | luaL_openlibs(L); |
| 394 | luaL_requiref(L, "json", luaopen_colibc_json, 1); |
| 395 | lua_pop(L, 1); |
| 396 | luaL_requiref(L, "yue", luaopen_yue, 1); |
| 397 | lua_pop(L, 1); |
| 398 | lua_getglobal(L, "package"); |
| 399 | lua_getfield(L, -1, "path"); |
| 400 | auto packagePath = cliGetString(L, -1); |
| 401 | lua_pop(L, 1); |
| 402 | auto scriptDir = scriptPath.parent_path().string(); |
| 403 | auto assetsScriptDir = scriptPath.parent_path().parent_path().string(); |
| 404 | packagePath = scriptDir + "/?.lua;" + assetsScriptDir + "/?.lua;" + assetsScriptDir + "/?/init.lua;" + packagePath; |
| 405 | lua_pushlstring(L, packagePath.c_str(), packagePath.size()); |
| 406 | lua_setfield(L, -2, "path"); |
| 407 | lua_pop(L, 1); |
| 408 | |
| 409 | if (assetPath) { |
| 410 | auto path = assetPath->string(); |
| 411 | lua_newtable(L); // Content |
| 412 | lua_pushlstring(L, path.c_str(), path.size()); |
| 413 | lua_setfield(L, -2, "assetPath"); |
| 414 | lua_setglobal(L, "Content"); |
| 415 | } |
| 416 | |
| 417 | lua_newtable(L); // Dora |
| 418 | lua_newtable(L); // Dora.CLI |
| 419 | lua_newtable(L); // args |
| 420 | int argIndex = 1; |
| 421 | for (int i = cliIndex + 1; i < argc; i++) { |
| 422 | if (cliIsAssetArgument(argc, argv, i)) continue; |
| 423 | lua_pushstring(L, argv[i]); |
| 424 | lua_rawseti(L, -2, argIndex++); |
| 425 | } |
| 426 | lua_setfield(L, -2, "args"); |
| 427 | auto executablePath = cliAbsolutePath(argv[0]).string(); |
| 428 | lua_pushlstring(L, executablePath.c_str(), executablePath.size()); |
| 429 | lua_setfield(L, -2, "executablePath"); |
| 430 | auto path = scriptPath.string(); |
| 431 | lua_pushlstring(L, path.c_str(), path.size()); |
| 432 | lua_setfield(L, -2, "scriptPath"); |
| 433 | cliSetFunc(L, "cwd", cliLuaCwd); |
| 434 | cliSetFunc(L, "env", cliLuaEnv); |
| 435 | cliSetFunc(L, "absolute", cliLuaAbsolute); |
| 436 | cliSetFunc(L, "exists", cliLuaExists); |
| 437 | cliSetFunc(L, "isDir", cliLuaIsDir); |
| 438 | cliSetFunc(L, "isFile", cliLuaIsFile); |
| 439 | cliSetFunc(L, "mkdirs", cliLuaMkdirs); |
| 440 | cliSetFunc(L, "readFile", cliLuaReadFile); |
| 441 | cliSetFunc(L, "writeFile", cliLuaWriteFile); |
| 442 | cliSetFunc(L, "listDir", cliLuaListDir); |
| 443 | cliSetFunc(L, "mtime", cliLuaMTime); |
| 444 | cliSetFunc(L, "system", cliLuaSystem); |
| 445 | cliSetFunc(L, "http", cliLuaHttp); |
| 446 | lua_setfield(L, -2, "CLI"); |
| 447 | lua_setglobal(L, "Dora"); |
| 448 | } |
| 449 |
no test coverage detected