diff options
Diffstat (limited to 'src/lua_state.cc')
| -rw-r--r-- | src/lua_state.cc | 171 |
1 files changed, 101 insertions, 70 deletions
diff --git a/src/lua_state.cc b/src/lua_state.cc index 919a304..c9bcb12 100644 --- a/src/lua_state.cc +++ b/src/lua_state.cc @@ -13,19 +13,27 @@ std::unordered_map<std::string, std::string> cached_install_directories; std::unordered_map<std::string, repo> cached_repos; std::unordered_map<std::string, int> cached_build_systems; +void push_lua_path(lua_State *L, const char *new_path) { + lua_getglobal(L, "package"); + lua_getfield(L, -1, "path"); + const char *current_path = lua_tostring(L, -1); + if (!current_path) current_path = ""; + lua_pop(L, 1); + lua_pushfstring(L, "%s;%s", current_path, new_path); + lua_setfield(L, -2, "path"); + lua_pop(L, 1); +} + void init_lua_state() { if (L != nullptr) return; L = luaL_newstate(); luaL_openlibs(L); + push_lua_path(L, (config_dir + "/?.lua").c_str()); if (luaL_loadfile(L, config_file.c_str()) || lua_pcall(L, 0, 0, 0)) { std::cout << print_error << "cannot run configuration script: " << lua_tostring(L, -1) << "\n"; return; } - //if (luaL_loadfile(L, repo_file.c_str()) || lua_pcall(L, 0, 0, 0)) { - // std::cout << print_error << "cannot run repository script: " << lua_tostring(L, -1) << "\n"; - // return; - //} config_loaded = true; } @@ -35,7 +43,8 @@ void init_bldit() { B = luaL_newstate(); luaL_openlibs(B); if (luaL_loadfile(B, "bldit.lua") || lua_pcall(B, 0, 0, 0)) { - std::cout << print_warning << "cannot run bldit script: " << lua_tostring(B, -1) << "\n"; + std::cout << print_warning + << "cannot run bldit script: " << lua_tostring(B, -1) << "\n"; return; } bldit_loaded = true; @@ -57,7 +66,7 @@ void cache_install_directories() { } if (!lua_istable(L, -1)) { std::cout << print_error - << "lua variable 'install_directories' is not a table.\n"; + << "lua variable 'install_directories' is not a table.\n"; return; } lua_pushnil(L); @@ -74,42 +83,48 @@ bool repo_build(const char *repository) { lua_getglobal(L, "repositories"); if (!config_loaded || !lua_istable(L, -1)) { std::cout << print_warning - << "lua variable 'repositories' is not a table.\n"; + << "lua variable 'repositories' is not a table.\n"; lua_pop(L, 1); return false; } std::cout << print_pkgit - << "lua variable 'repositories' used successfully.\n"; + << "lua variable 'repositories' used successfully.\n"; lua_getfield(L, -1, repository); if (!lua_istable(L, -1)) { - std::cout << print_warning << "'repositories' lua variable '" << repository << "' is not a table.\n"; + std::cout << print_warning << "'repositories' lua variable '" << repository + << "' is not a table.\n"; lua_pop(L, 2); return false; } - std::cout << print_pkgit << "'repositories' lua variable '" << repository << "' used successfully.\n"; + std::cout << print_pkgit << "'repositories' lua variable '" << repository + << "' used successfully.\n"; lua_getfield(L, -1, "build"); if (!lua_isfunction(L, -1)) { - std::cout << print_warning << "'repositories' lua variable 'build' is not a function." << std::endl; + std::cout << print_warning + << "'repositories' lua variable 'build' is not a function." + << std::endl; lua_pop(L, 3); return false; } if (lua_pcall(L, 0, 0, 0) != LUA_OK) { - std::cout << "'repositories' build failed: " << lua_tostring(L, -1) << std::endl; + std::cout << "'repositories' build failed: " << lua_tostring(L, -1) + << std::endl; lua_pop(L, 1); lua_pop(L, 2); return false; } lua_pop(L, 2); std::cout << print_pkgit - << "'repositories' lua function 'build' ran successfully.\n"; + << "'repositories' lua function 'build' ran successfully.\n"; return true; } bool bldit(const char *target) { - lua_State* B = luaL_newstate(); + lua_State *B = luaL_newstate(); luaL_openlibs(B); if (luaL_loadfile(B, "bldit.lua") || lua_pcall(B, 0, 0, 0)) { - std::cout << print_warning << "cannot run bldit script: " << lua_tostring(B, -1) << "\n"; + std::cout << print_warning + << "cannot run bldit script: " << lua_tostring(B, -1) << "\n"; return false; } lua_getglobal(B, "targets"); @@ -120,14 +135,17 @@ bool bldit(const char *target) { std::cout << print_pkgit << "bldit variable 'targets' used successfully.\n"; lua_getfield(B, -1, target); if (!lua_istable(B, -1)) { - std::cout << print_warning << "bldit variable '" << target << "' is not a table.\n"; + std::cout << print_warning << "bldit variable '" << target + << "' is not a table.\n"; lua_pop(B, 2); return false; } - std::cout << print_pkgit << "bldit variable '" << target << "' used successfully.\n"; + std::cout << print_pkgit << "bldit variable '" << target + << "' used successfully.\n"; lua_getfield(B, -1, "build"); if (!lua_isfunction(B, -1)) { - std::cout << "'repositories' lua variable 'build' is not a function." << std::endl; + std::cout << "'repositories' lua variable 'build' is not a function." + << std::endl; lua_pop(B, 3); return false; } @@ -142,45 +160,48 @@ bool bldit(const char *target) { return true; } -//bool config_build(const char* path) { -// free_lua_state(); -// init_lua_state(); -// lua_State* L = get_lua_state(); -// std::unordered_map<std::string, int> build_files; -// lua_getglobal(L, "build_systems"); -// if (!config_loaded || !lua_istable(L, -1)) { -// lua_pop(L, 1); -// std::cout << print_warning << "config lua variable 'build_systems' is not -// a table.\n"; return false; -// } -// lua_pushnil(L); -// bool build_found = false; -// while (lua_next(L, -2) != 0) { -// const char *key = lua_tostring(L, -2); -// int value = lua_type(L, -1); -// if (lua_isfunction(L, -1) == 0) { -// std::cout << print_warning << "config build value " << key << " is not -// a function\n"; lua_pop(L, 1); continue; -// } -// build_files[key] = value; -// for (auto const& dir_entry : std::filesystem::directory_iterator(std::filesystem::current_path().string())) { -// std::string string_key = key; -// if (dir_entry.path().filename() != string_key) { continue; } -// build_found = true; -// lua_pushvalue(L, -1); -// lua_pushstring(L, std::filesystem::current_path().string().c_str()); -// std::cout << "calling config lua build function according to key filename '" -// << key << "'...\n"; -// if (lua_pcall(L, 1, 0, 0) != 0) { -// std::cout << print_warning << "config lua build function failed to run\n"; -// break; -// } -// if (build_found) { break; } -// } -// lua_pop(L, 1); -// } -// return build_found; -//} +// bool config_build(const char* path) { +// free_lua_state(); +// init_lua_state(); +// lua_State* L = get_lua_state(); +// std::unordered_map<std::string, int> build_files; +// lua_getglobal(L, "build_systems"); +// if (!config_loaded || !lua_istable(L, -1)) { +// lua_pop(L, 1); +// std::cout << print_warning << "config lua variable 'build_systems' is not +// a table.\n"; return false; +// } +// lua_pushnil(L); +// bool build_found = false; +// while (lua_next(L, -2) != 0) { +// const char *key = lua_tostring(L, -2); +// int value = lua_type(L, -1); +// if (lua_isfunction(L, -1) == 0) { +// std::cout << print_warning << "config build value " << key << " is not +// a function\n"; lua_pop(L, 1); continue; +// } +// build_files[key] = value; +// for (auto const& dir_entry : +// std::filesystem::directory_iterator(std::filesystem::current_path().string())) +// { +// std::string string_key = key; +// if (dir_entry.path().filename() != string_key) { continue; } +// build_found = true; +// lua_pushvalue(L, -1); +// lua_pushstring(L, std::filesystem::current_path().string().c_str()); +// std::cout << "calling config lua build function according to key +// filename '" +// << key << "'...\n"; +// if (lua_pcall(L, 1, 0, 0) != 0) { +// std::cout << print_warning << "config lua build function failed to +// run\n"; break; +// } +// if (build_found) { break; } +// } +// lua_pop(L, 1); +// } +// return build_found; +// } void cache_repos() { lua_getglobal(L, "repositories"); @@ -198,28 +219,31 @@ void cache_repos() { continue; } lua_getfield(L, -1, "url"); - const char* url = lua_tostring(L, -1); + const char *url = lua_tostring(L, -1); lua_pop(L, 1); cached_repos[repo_name].source.key = "url"; cached_repos[repo_name].source.value = url ? url : ""; lua_getfield(L, -1, "dependencies"); if (!lua_istable(L, -1)) { - //std::cout << print_warning << repo_name << ": \tlua variable 'dependencies' is not a table.\n"; + // std::cout << print_warning << repo_name << ": \tlua variable + // 'dependencies' is not a table.\n"; lua_pop(L, 2); continue; } lua_pushnil(L); while (lua_next(L, -2) != 0) { - const char* depname = lua_tostring(L, -2); + const char *depname = lua_tostring(L, -2); if (depname && lua_istable(L, -1)) { lua_getfield(L, -1, "url"); - const char* dep_url = lua_tostring(L, -1); + const char *dep_url = lua_tostring(L, -1); lua_pop(L, 1); lua_getfield(L, -1, "version"); - const char* dep_version = lua_tostring(L, -1); + const char *dep_version = lua_tostring(L, -1); lua_pop(L, 1); - cached_repos[repo_name].dependencies[depname].url = dep_url ? dep_url : ""; - cached_repos[repo_name].dependencies[depname].version = dep_version ? dep_version : ""; + cached_repos[repo_name].dependencies[depname].url = + dep_url ? dep_url : ""; + cached_repos[repo_name].dependencies[depname].version = + dep_version ? dep_version : ""; } lua_pop(L, 1); } @@ -236,7 +260,8 @@ void cache_build_systems() { lua_getglobal(L, "build_systems"); } if (!lua_istable(L, -1)) { - std::cout << print_error << "lua variable 'build_systems' is not a table.\n"; + std::cout << print_error + << "lua variable 'build_systems' is not a table.\n"; return; } lua_pushnil(L); @@ -256,11 +281,13 @@ void cache_build_systems() { bool config_build(const char *path) { lua_getglobal(L, "build_systems"); if (!config_loaded || !lua_istable(L, -1)) { - std::cout << print_warning << "lua variable 'build_systems' is not a table.\n"; + std::cout << print_warning + << "lua variable 'build_systems' is not a table.\n"; lua_pop(L, 1); return false; } - std::cout << print_pkgit << "lua variable 'build_systems' used successfully.\n"; + std::cout << print_pkgit + << "lua variable 'build_systems' used successfully.\n"; lua_pushnil(L); while (lua_next(L, -2) != 0) { const char *key = lua_tostring(L, -2); @@ -269,9 +296,12 @@ bool config_build(const char *path) { lua_pop(L, 1); continue; } - for (auto const& dir_entry : std::filesystem::directory_iterator(std::filesystem::current_path().string())) { + for (auto const &dir_entry : std::filesystem::directory_iterator( + std::filesystem::current_path().string())) { std::string string_key = key; - if (dir_entry.path().filename() != string_key) { continue; } + if (dir_entry.path().filename() != string_key) { + continue; + } lua_getfield(L, -1, "build"); if (lua_isfunction(L, -1) == 0) { std::cout << print_error << "no build function for " << key << "\n"; @@ -279,7 +309,8 @@ bool config_build(const char *path) { continue; } if (lua_pcall(L, 0, 0, 0) != LUA_OK) { - std::cout << "'" << key << "' build failed: " << lua_tostring(L, -1) << std::endl; + std::cout << "'" << key << "' build failed: " << lua_tostring(L, -1) + << std::endl; lua_pop(L, 1); continue; } |
