diff options
| -rw-r--r-- | include/pkgit_lua.h | 3 | ||||
| -rw-r--r-- | src/build.c | 1 | ||||
| -rw-r--r-- | src/lua_globs.c | 8 | ||||
| -rw-r--r-- | src/parse_args.c | 4 | ||||
| -rw-r--r-- | src/pkg_install.c | 44 | ||||
| -rw-r--r-- | src/pkg_update.c | 54 |
6 files changed, 86 insertions, 28 deletions
diff --git a/include/pkgit_lua.h b/include/pkgit_lua.h index eee5160..a9a6adf 100644 --- a/include/pkgit_lua.h +++ b/include/pkgit_lua.h @@ -71,4 +71,7 @@ void pkg_remove(package_t *pkg); void pkg_update(package_t *pkg); void all_update(void); +// declare +void declare(void); + #endif diff --git a/src/build.c b/src/build.c index 740d739..769106a 100644 --- a/src/build.c +++ b/src/build.c @@ -35,6 +35,7 @@ bool target_build(lua_State *L, char* lua_file, str *target) { } lua_pop(L, 1); if (!lua_try_function(L, lua_file, "build")) return false; + lua_pop(L, 1); return true; } diff --git a/src/lua_globs.c b/src/lua_globs.c index 74a7faa..d531fb8 100644 --- a/src/lua_globs.c +++ b/src/lua_globs.c @@ -76,7 +76,7 @@ void init_bldit_state(void) { luaL_openlibs(B); if (luaL_loadfile(B, "bldit.lua") || lua_pcall(B, 0, 0, 0)) { if (flags.verbose) - if (flags.verbose) log_warn("cannot run bldit: %s", lua_tostring(B, -1)); + log_warn("cannot run bldit: %s", lua_tostring(B, -1)); return; } lua_pushfstring(B, "%s", inst_dirs.prefix.data); @@ -94,11 +94,11 @@ void free_lua_state(void) { } void free_bldit_state(void) { - if (L != NULL) { + if (B != NULL) { lua_close(L); - L = NULL; + B = NULL; } - config_loaded = false; + bldit_loaded = false; } lua_State *get_lua_state(void) { return L; } diff --git a/src/parse_args.c b/src/parse_args.c index 26d138f..2dfbef5 100644 --- a/src/parse_args.c +++ b/src/parse_args.c @@ -138,7 +138,7 @@ void flags_cmd(int argc, char **argv, int i) { case 'a': cmd_add(argv, i); break; case 'b': cmd_build(argc, argv, i); break; case 'c': check(); break; - case 'd': panic("not implemented"); printf("declare\n"); break; + case 'd': declare(); break; case 'i': cmd_install(argc, argv, i); break; case 'r': cmd_remove(argc, argv, i); break; case 'u': cmd_update(argc, argv, i); break; @@ -172,7 +172,7 @@ void parse_cmds(int argc, char **argv) { if (COMMAND("--install", "i")) { cmd_install(argc, argv, i); } if (COMMAND("--remove", "r")) { cmd_remove(argc, argv, i); } if (COMMAND("--update", "u")) { cmd_update(argc, argv, i); } - if (COMMAND("--declare", "d")) { panic("not implemented"); } + if (COMMAND("--declare", "d")) { declare(); } if (COMMAND("--list", "l")) { panic("not implemented"); } if (COMMAND("--search", "s")) { panic("not implemented"); } if (COMMAND("--version", "v")) { printf(VERSION "\n"); } diff --git a/src/pkg_install.c b/src/pkg_install.c index b911a71..6ee7e08 100644 --- a/src/pkg_install.c +++ b/src/pkg_install.c @@ -32,28 +32,28 @@ #include "pkg.h" void install_dependencies(lua_State *L) { - while (lua_next(L, -2) != 0) { - const char *depname = lua_tostring(L, -2); - if (depname && lua_istable(L, -1)) { - lua_getfield(L, -1, "url"); - str dep_url = mstr(lua_tostring(L, -1)); - lua_pop(L, 1); - package_t pkg = pkg_create(&dep_url); - str_free(&dep_url); - lua_getfield(L, -1, "version"); - pkg.version = mstr(lua_tostring(L, -1)); - lua_pop(L, 1); - const int top = lua_gettop(L); - char cwd[MAX_PATH_LEN]; - if (getcwd(cwd, sizeof(cwd)) != NULL) { - pkg_install(&pkg); - chdir(cwd); - } - lua_settop(L, top); - pkg_free(&pkg); - } - lua_pop(L, 1); - } + while (lua_next(L, -2) != 0) { + const char *depname = lua_tostring(L, -2); + if (depname && lua_istable(L, -1)) { + lua_getfield(L, -1, "url"); + str dep_url = mstr(lua_tostring(L, -1)); + lua_pop(L, 1); + package_t pkg = pkg_create(&dep_url); + str_free(&dep_url); + lua_getfield(L, -1, "version"); + pkg.version = mstr(lua_tostring(L, -1)); + lua_pop(L, 1); + const int top = lua_gettop(L); + char cwd[MAX_PATH_LEN]; + if (getcwd(cwd, sizeof(cwd)) != NULL) { + pkg_install(&pkg); + chdir(cwd); + } + lua_settop(L, top); + pkg_free(&pkg); + } + lua_pop(L, 1); + } } bool target_install(lua_State *L, char* lua_file, str *target) { diff --git a/src/pkg_update.c b/src/pkg_update.c index f778438..3441db0 100644 --- a/src/pkg_update.c +++ b/src/pkg_update.c @@ -17,11 +17,38 @@ pkgit - package it! #include <dirent.h> #include <string.h> +#include "files.h" #include "globs.h" #include "is_updated.h" #include "log.h" #include "pkgit_lua.h" +lua_State *U = NULL; + +void init_update_state(void) { + if (U != NULL) + return; + U = luaL_newstate(); + luaL_openlibs(U); + str lua_path = str_format("%.*s/?.lua", str_fmt(&cfg.dir)); + push_lua_path(U, lua_path.data); + if (luaL_loadfile(U, cfg.name.data) || lua_pcall(U, 0, 0, 0)) { + log_error("cannot run configuration script: %s", lua_tostring(U, -1)); + log_pkgit("to generate a configuration file, head into the"); + log_pkgit( + "root directory of the pkgit source and run `make defconfig`"); + exit(EXIT_FAILURE); + } + if (file_exists(cfg.repos.data)) { + if (luaL_loadfile(U, cfg.repos.data) || lua_pcall(U, 0, 0, 0)) { + if (flags.verbose) log_warn("cannot load repository file: %s", lua_tostring(U, -1)); + lua_pop(U, 1); + } + } + str_free(&lua_path); + config_loaded = true; +} + bool on_pkg_update(lua_State *L, package_t *pkg) { lua_getglobal(L, "repositories"); if (!lua_istable(L, -1)) { @@ -118,3 +145,30 @@ void all_update(void) { if (!on_all_update(L)) log_warn("init.lua: 'on_update' function failed"); } + +void declare(void) { + init_update_state(); + lua_getglobal(U, "repositories"); + if (!lua_istable(U, -1)) { + lua_isnt_type("repositories", "table"); + lua_pop(U, 1); + return; + } + lua_pushnil(U); + while (lua_next(U, -2) != 0) { + str key = mstr(lua_tostring(U, -2)); + if (!lua_istable(U, -1)) { + lua_pop(U, 1); + str_free(&key); + continue; + } + package_t pkg = pkg_create(&key); + pkg_install(&pkg); + pkg_free(&pkg); + if (str_is_valid(&key)) str_free(&key); + lua_pop(U, 1); + } + if (!on_all_update(L)) + log_warn("init.lua: 'on_update' function failed"); + lua_pop(U, 1); +} |
