aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cli.c2
-rw-r--r--src/hooks.c107
-rw-r--r--src/lua_state.c19
-rw-r--r--src/pkg.c3
-rw-r--r--src/recipes.c44
5 files changed, 127 insertions, 48 deletions
diff --git a/src/cli.c b/src/cli.c
index 3017a80..fc3edfd 100644
--- a/src/cli.c
+++ b/src/cli.c
@@ -73,7 +73,7 @@ void cmd_install(int argc, char **argv, int i) {
continue;
str arg = mstr(argv[j]);
package_t pkg = pkg_create(&arg);
- if (!pkg_install(&pkg)) log_error(
+ if (!pkg_full_install(&pkg)) log_error(
"failed to install '%.*s'", str_fmt(&pkg.name));
pkg_free(&pkg);
if (str_is_valid(&arg))
diff --git a/src/hooks.c b/src/hooks.c
index 20923b7..6053575 100644
--- a/src/hooks.c
+++ b/src/hooks.c
@@ -21,7 +21,8 @@
#include <stdio.h>
#include <string.h>
-#include "common.h"
+#include "bldit.h"
+#include "files.h"
#include "log.h"
#include "lua_state.h"
#include "pkg.h"
@@ -29,16 +30,18 @@
#include "state.h"
bool pkg_exists(str *name) {
- lua_get_field_type("pkgs", table, &config.init_path, 1);
- lua_get_field_type(name->data, table, &config.init_path, 2);
- lua_pop(L, 2);
+ lua_getglobal(L, "__pkgit_user_config");
+ lua_get_field_type("pkgs", table, &config.init_path, 2);
+ lua_get_field_type(name->data, table, &config.init_path, 3);
+ lua_pop(L, 3);
return true;
}
bool pkg_hook_url(package_t *pkg) {
- lua_get_field_type("pkgs", table, &config.init_path, 1);
- lua_get_field_type(pkg->name.data, table, &config.init_path, 2);
- lua_get_field_type("url", string, &config.init_path, 3);
+ lua_getglobal(L, "__pkgit_user_config");
+ lua_get_field_type("pkgs", table, &config.init_path, 2);
+ lua_get_field_type(pkg->name.data, table, &config.init_path, 3);
+ lua_get_field_type("url", string, &config.init_path, 4);
if (!lua_isstring(L, -1)) return false;
return true;
}
@@ -56,33 +59,36 @@ str pkg_get_url(package_t *pkg) {
}
bool pkg_hook_fetch_sources(package_t *pkg) {
- lua_get_field_type("pkgs", table, &config.init_path, 1);
- lua_get_field_type(pkg->name.data, table, &config.init_path, 2);
- lua_get_field_type("fetcher", table, &config.init_path, 3);
- lua_get_field_type("get_source", function, &config.init_path, 4);
+ lua_getglobal(L, "__pkgit_user_config");
+ lua_get_field_type("pkgs", table, &config.init_path, 2);
+ lua_get_field_type(pkg->name.data, table, &config.init_path, 3);
+ lua_get_field_type("fetcher", table, &config.init_path, 4);
+ lua_get_field_type("get_source", function, &config.init_path, 5);
lua_pushstring(L, pkg->url.data);
lua_pushstring(L, pkg->version.data);
lua_pushstring(L, pkg->name.data);
- lua_run_function("get_source", &config.init_path, 3, 1, 4);
+ lua_run_function("get_source", &config.init_path, 3, 1, 5);
if (!lua_isnumber(L, -1) || lua_tonumber(L, -1) != 0) {
- lua_pop(L, 4);
+ lua_pop(L, 5);
return false;
}
- lua_pop(L, 4);
+ lua_pop(L, 5);
return true;
}
bool pkg_hook_fetch_latest_version(package_t *pkg) {
- lua_get_field_type("pkgs", table, &config.init_path, 1);
- lua_get_field_type(pkg->name.data, table, &config.init_path, 2);
- lua_get_field_type("fetcher", table, &config.init_path, 3);
- lua_get_field_type("get_version", function, &config.init_path, 4);
+ lua_getglobal(L, "__pkgit_user_config");
+ lua_get_field_type("pkgs", table, &config.init_path, 2);
+ lua_get_field_type(pkg->name.data, table, &config.init_path, 3);
+ lua_get_field_type("fetcher", table, &config.init_path, 4);
+ lua_get_field_type("get_version", function, &config.init_path, 5);
lua_pushstring(L, pkg->url.data);
- lua_run_function("get_version", &config.init_path, 1, 1, 4);
+ lua_run_function("get_version", &config.init_path, 1, 1, 5);
if (!lua_isstring(L, -1)) {
- lua_pop(L, 4);
+ lua_pop(L, 5);
return false;
}
+ lua_pop(L, 5);
return true;
}
@@ -95,21 +101,22 @@ str pkg_get_latest_version(package_t *pkg) {
str_copy_into(&version, &tmp_version);
str_free(&tmp_version);
}
- lua_pop(L, 4);
+ lua_pop(L, 5);
return version;
}
bool pkg_hook_recipe(char *recipe, package_t *pkg) {
- lua_get_field_type("pkgs", table, &config.init_path, 1);
- lua_get_field_type(pkg->name.data, table, &config.init_path, 2);
- lua_get_field_type("recipe", table, &config.init_path, 3);
- lua_get_field_type(recipe, function, &config.init_path, 4);
- lua_run_function(recipe, &config.init_path, 0, 1, 4);
+ lua_getglobal(L, "__pkgit_user_config");
+ lua_get_field_type("pkgs", table, &config.init_path, 2);
+ lua_get_field_type(pkg->name.data, table, &config.init_path, 3);
+ lua_get_field_type("recipe", table, &config.init_path, 4);
+ lua_get_field_type(recipe, function, &config.init_path, 5);
+ lua_run_function(recipe, &config.init_path, 0, 1, 5);
if (!lua_isnumber(L, -1) || lua_tonumber(L, -1) != 0) {
- lua_pop(L, 4);
+ lua_pop(L, 5);
return false;
}
- lua_pop(L, 4);
+ lua_pop(L, 5);
return true;
}
@@ -134,12 +141,52 @@ bool pkg_hook_on_update(package_t *pkg) {
}
bool config_hook_on_update(void) {
- lua_get_field_type("on_update", function, &config.init_path, 1);
- lua_run_function("on_update", &config.init_path, 0, 1, 1);
+ lua_getglobal(L, "__pkgit_user_config");
+ lua_get_field_type("on_update", function, &config.init_path, 2);
+ lua_run_function("on_update", &config.init_path, 0, 1, 2);
if (!lua_isnumber(L, -1) || lua_tonumber(L, -1) != 0) {
+ lua_pop(L, 2);
+ return false;
+ }
+ lua_pop(L, 2);
+ return true;
+}
+
+void bldit_file_set(package_t *pkg) {
+ pkg->bldit = str_format("%.*s/bldit.lua", str_fmt(&pkg->src));
+}
+
+bool bldit_file_exists(package_t *pkg) {
+ if (file_exists(pkg->bldit.data)) return true;
+ return false;
+}
+
+bool bldit_hook_set(package_t *pkg) {
+ if (luaL_loadfile(L, pkg->bldit.data) || lua_pcall(L, 0, 1, 0)) {
+ log_warn("bldit brokie");
lua_pop(L, 1);
return false;
}
+ lua_setglobal(L, "__pkgit_bldit_config");
lua_pop(L, 1);
return true;
}
+
+bool bldit_hook_recipe(package_t *pkg, const char *recipe) {
+// luaL_dofile(L, pkg->bldit.data);
+// lua_setglobal(L, "__pkgit_bldit_config");
+ lua_getglobal(L, "__pkgit_bldit_config");
+ printf("\n%s\n\n", lua_tostring(L, -1));
+ log_info("entered bldit config");
+ lua_get_field_type("recipe", table, &pkg->bldit, 2);
+ log_info("entered bldit recipe");
+ lua_get_field_type(recipe, table, &pkg->bldit, 3);
+ log_info("entered bldit recipe function: %s", recipe);
+ lua_run_function(recipe, &pkg->bldit, 0, 1, 3);
+ if (!lua_isnumber(L, -1) || lua_tonumber(L, -1) != 0) {
+ lua_pop(L, 3);
+ return false;
+ }
+ lua_pop(L, 3);
+ return true;
+}
diff --git a/src/lua_state.c b/src/lua_state.c
index 297a218..cf46760 100644
--- a/src/lua_state.c
+++ b/src/lua_state.c
@@ -62,6 +62,8 @@ bool init_lua_state(void) {
}
str_free(&lua_path);
+ lua_setglobal(L, "__pkgit_user_config");
+ lua_pop(L, 1);
L_is_loaded = true;
return true;
}
@@ -94,10 +96,8 @@ static void setup_base_dirs(void) {
}
static bool load_init_lua(void) {
- lua_setglobal(L, "__PkgitUserConfig");
- lua_getglobal(L, "__PkgitUserConfig");
-
- lua_get_field_type("dirs", table, &config.init_path, 1);
+ lua_getglobal(L, "__pkgit_user_config");
+ lua_get_field_type("dirs", table, &config.init_path, 2);
#define X(field) \
do { \
@@ -114,20 +114,15 @@ static bool load_init_lua(void) {
#undef X
- lua_pop(L, 1);
+ lua_pop(L, 2);
return true;
}
bool init_lua_config(void) {
setup_base_dirs();
-
- if (!init_lua_state())
- return false;
-
- if (!load_init_lua())
- return false;
-
+ if (!init_lua_state()) return false;
+ if (!load_init_lua()) return false;
return true;
}
diff --git a/src/pkg.c b/src/pkg.c
index 852f555..04cc95c 100644
--- a/src/pkg.c
+++ b/src/pkg.c
@@ -102,6 +102,8 @@ package_t pkg_create(str *arg) {
str_free(&cwd);
str_free(&destdir);
+ bldit_file_set(&pkg);
+ bldit_hook_set(&pkg);
return pkg;
}
@@ -111,4 +113,5 @@ void pkg_free(package_t *pkg) {
str_free(&pkg->url);
str_free(&pkg->src);
str_free(&pkg->root);
+ str_free(&pkg->bldit);
}
diff --git a/src/recipes.c b/src/recipes.c
index 246224e..a4a08e8 100644
--- a/src/recipes.c
+++ b/src/recipes.c
@@ -44,6 +44,21 @@
log_info("'%.*s' %s done", str_fmt(&(pkg)->name), operation); \
} while (0)
+#define try_bldit_fn(fn, pkg, operation, smol) \
+ do { \
+ if (pkg_read_status(smol, pkg)) return true; \
+ if (!flags.quiet) log_info("'%.*s' %s in progress...", \
+ str_fmt(&(pkg)->name), operation); \
+ if (!(fn((pkg),(operation)))) { \
+ log_error("%s failed", operation); \
+ return false; \
+ } \
+ char big[2] = "\0"; \
+ big[0] = smol; \
+ if (!pkg_write_status(big, pkg)) return false; \
+ log_info("'%.*s' %s done", str_fmt(&(pkg)->name), operation); \
+ } while (0)
+
static bool pkg_write_status(const char *status, package_t *pkg) {
bool success = false;
str status_str = mstr(status);
@@ -105,7 +120,13 @@ static bool pkg_enter(package_t *pkg) {
bool pkg_setup(package_t *pkg) {
if (pkg_read_status('s', pkg)) return true;
if (!pkg_enter(pkg)) return false;
- try_pkg_fn(pkg_hook_setup, pkg, "setup", 's');
+ if (bldit_file_exists(pkg)) {
+ log_info("using bldit.lua");
+ try_bldit_fn(bldit_hook_recipe, pkg, "setup", 's');
+ } else {
+ log_info("using your config");
+ try_pkg_fn(pkg_hook_setup, pkg, "setup", 's');
+ }
log_success("'%.*s' set up", str_fmt(&pkg->name));
return true;
}
@@ -113,22 +134,35 @@ bool pkg_setup(package_t *pkg) {
bool pkg_build(package_t *pkg) {
if (pkg_read_status('b', pkg)) return true;
if (!pkg_enter(pkg)) return false;
- try_pkg_fn(pkg_hook_build, pkg, "build", 'b');
+ if (bldit_file_exists(pkg)) {
+ log_info("using bldit.lua");
+ try_bldit_fn(bldit_hook_recipe, pkg, "build", 'b');
+ } else {
+ log_info("using your config");
+ try_pkg_fn(pkg_hook_build, pkg, "build", 'b');
+ }
log_success("'%.*s' built", str_fmt(&pkg->name));
return true;
}
bool pkg_install(package_t *pkg) {
if (is_pkg_installed(pkg) && !flags.force) return true;
- try_pkg_fn(pkg_fetch_sources, pkg, "fetch", 'f');
if (!pkg_enter(pkg)) return false;
- try_pkg_fn(pkg_hook_setup, pkg, "setup", 's');
- try_pkg_fn(pkg_hook_build, pkg, "build", 'b');
try_pkg_fn(pkg_hook_install, pkg, "install", 'i');
log_success("'%.*s' installed", str_fmt(&pkg->name));
return true;
}
+bool pkg_full_install(package_t *pkg) {
+ if (is_pkg_installed(pkg) && !flags.force) return true;
+ try_pkg_fn(pkg_fetch_sources, pkg, "fetch", 'f');
+ if (!pkg_enter(pkg)) return false;
+ pkg_setup(pkg);
+ pkg_build(pkg);
+ pkg_install(pkg);
+ return true;
+}
+
bool pkg_remove(package_t *pkg) {
if (!pkg_enter(pkg)) return false;
if (!pkg_hook_remove(pkg)) return false;