diff options
| -rw-r--r-- | include/lua_state.h | 2 | ||||
| -rw-r--r-- | src/lua_state.c | 88 | ||||
| -rw-r--r-- | src/update_all.c | 1 | ||||
| -rw-r--r-- | src/update_pkg.c | 1 |
4 files changed, 89 insertions, 3 deletions
diff --git a/include/lua_state.h b/include/lua_state.h index e026ec4..1db7ccd 100644 --- a/include/lua_state.h +++ b/include/lua_state.h @@ -41,6 +41,8 @@ void cache_repos(void); void cache_build_systems(void); void cache_install_directories(void); void cache_prefix_directory(void); +bool on_update(lua_State *L); +bool target_loop_on_update(lua_State *L, const char* lua_file, const char *target); bool repo_build(const char *repository, const char *target); bool repo_install(const char *repository, const char *target); bool repo_uninstall(const char *repository, const char *target); diff --git a/src/lua_state.c b/src/lua_state.c index 10820f4..3c53668 100644 --- a/src/lua_state.c +++ b/src/lua_state.c @@ -164,6 +164,82 @@ void install_dependencies(lua_State *L) { } } +bool on_update(lua_State *L) { + const char* lua_file = "init.lua"; + lua_getglobal(L, "on_update"); + if (!lua_isfunction(L, -1)) { + if (is_verbose) printf( + "%s %s: 'on_update' is not a function.\n", + print_warning, lua_file + ); + lua_pop(L, 2); + return false; + } + if (lua_pcall(L, 0, 1, 0) != LUA_OK) { + printf( + "%s %s: 'on_update' function borked: %s\n", + print_error, lua_file, lua_tostring(L, -1) + ); + lua_pop(L, 2); + return false; + } + if ((int)lua_tonumber(L, -1) != 0) if (is_verbose) { + printf( + "%s %s: 'on_update' failed: %s\n", + print_error, lua_file, lua_tostring(L, -1) + ); + lua_pop(L, 2); + return false; + } + lua_pop(L, 2); + return true; +} + +bool target_loop_on_update( + lua_State *L, const char* lua_file, const char *target +) { + lua_getfield(L, -1, target); + if (!lua_istable(L, -1)) { + if (is_verbose) printf( + "%s %s: 'targets.%s' is not a table.\n", + print_warning, lua_file, target + ); + lua_pop(L, 1); + if (strcmp(target, "quiet") == 0) { + target_loop_on_update(L, lua_file, "default"); + lua_pop(L, 1); + } + return false; + } + lua_getfield(L, -1, "on_update"); + if (!lua_isfunction(L, -1)) { + if (is_verbose) printf( + "%s %s: 'targets.%s.on_update' is not a function.\n", + print_warning, lua_file, target + ); + lua_pop(L, 3); + return false; + } + if (lua_pcall(L, 0, 1, 0) != LUA_OK) { + printf( + "%s %s: 'targets.%s.on_update' function borked: %s\n", + print_error, lua_file, target, lua_tostring(L, -1) + ); + lua_pop(L, 3); + return false; + } + if ((int)lua_tonumber(L, -1) != 0) if (is_verbose) { + printf( + "%s %s: 'targets.%s.on_update' failed: %s\n", + print_error, lua_file, target, lua_tostring(L, -1) + ); + lua_pop(L, 3); + return false; + } + lua_pop(L, 3); + return true; +} + bool target_loop_build( lua_State *L, const char* lua_file, const char *target ) { @@ -174,8 +250,10 @@ bool target_loop_build( print_warning, lua_file, target ); lua_pop(L, 1); - if (strcmp(target, "quiet") == 0) + if (strcmp(target, "quiet") == 0) { target_loop_build(L, lua_file, "default"); + lua_pop(L, 1); + } return false; } lua_getfield(L, -1, "dependencies"); @@ -224,8 +302,10 @@ bool target_loop_install( print_warning, lua_file, target ); lua_pop(L, 1); - if (strcmp(target, "quiet") == 0) + if (strcmp(target, "quiet") == 0) { target_loop_install(L, lua_file, "default"); + lua_pop(L, 1); + } return false; } lua_getfield(L, -1, "pre_install"); @@ -304,8 +384,10 @@ bool target_loop_uninstall(lua_State *L, const char *lua_file, const char *targe print_warning, lua_file, target ); lua_pop(L, 1); - if (strcmp(target, "quiet") == 0) + if (strcmp(target, "quiet") == 0) { target_loop_uninstall(L, lua_file, "default"); + lua_pop(L, 1); + } return false; } lua_getfield(L, -1, "uninstall"); diff --git a/src/update_all.c b/src/update_all.c index 756f98f..4ff750d 100644 --- a/src/update_all.c +++ b/src/update_all.c @@ -55,4 +55,5 @@ void update_all(void) { } } closedir(dir_ptr); + on_update(get_lua_state()); }
\ No newline at end of file diff --git a/src/update_pkg.c b/src/update_pkg.c index ea6a20d..cbe7521 100644 --- a/src/update_pkg.c +++ b/src/update_pkg.c @@ -40,4 +40,5 @@ void update_pkg(Pkg pkg) { print_pkgit, green, pkg.name, color_reset ); install_pkg(pkg); + target_loop_on_update(get_lua_state(), "init.lua", pkg.target); }
\ No newline at end of file |
