From 536b651f4086fe01d0bd1f743f54ef650524b122 Mon Sep 17 00:00:00 2001 From: dacctal Date: Fri, 14 Aug 2026 07:37:03 +0000 Subject: i made the install work (kinda) --- src/cli.c | 6 +++++ src/files.c | 33 ++++++++++++++++++++++++ src/hooks.c | 59 +++++++++++++++++++++++++++++++++++++++---- src/lua_state.c | 2 ++ src/pkg.c | 19 +++++++++++--- src/recipes.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 188 insertions(+), 9 deletions(-) create mode 100644 src/files.c create mode 100644 src/recipes.c (limited to 'src') diff --git a/src/cli.c b/src/cli.c index 24bace0..d385865 100644 --- a/src/cli.c +++ b/src/cli.c @@ -26,6 +26,7 @@ #include "help.h" #include "log.h" #include "lua_state.h" +#include "pkg.h" #include "state.h" #include "str.h" @@ -67,6 +68,11 @@ void cmd_install(int argc, char **argv, int i) { if (argv[j][0] == '-') continue; str arg = mstr(argv[j]); + package_t pkg = pkg_create(&arg); + if (!pkg_install(&pkg)) { + log_error("failed to install"); + } + pkg_free(&pkg); if (str_is_valid(&arg)) str_free(&arg); } diff --git a/src/files.c b/src/files.c new file mode 100644 index 0000000..5eaa459 --- /dev/null +++ b/src/files.c @@ -0,0 +1,33 @@ +/* + + pkgit - package it! + + Copyright (C) 2026 dacctal + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +*/ + +#include +#include + +bool file_exists(const char *path) { + struct stat buffer; + return (stat(path, &buffer) == 0); +} + +bool is_directory(const char *path) { + struct stat statbuf; + if (stat(path, &statbuf) != 0) return false; + return S_ISDIR(statbuf.st_mode); +} \ No newline at end of file diff --git a/src/hooks.c b/src/hooks.c index f47d928..448c7a4 100644 --- a/src/hooks.c +++ b/src/hooks.c @@ -31,6 +31,7 @@ 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); return true; } @@ -38,22 +39,60 @@ 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); + if (!lua_isstring(L, -1)) return false; return true; } str pkg_get_url(package_t *pkg) { - str url = str_new(); - if (!pkg_hook_url(pkg)) return url; - url = mstr(lua_tostring(L, -1)); + str url = {0}; + if (!pkg_hook_url(pkg)) { + lua_pop(L, 3); + return url; + } + const char *url_cstr = lua_tostring(L, -1); + url = mstr(url_cstr); + lua_pop(L, 3); return url; } 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_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); + if (!lua_isnumber(L, -1) || lua_tonumber(L, -1) != 0) { + lua_pop(L, 4); + return false; + } + lua_pop(L, 4); + 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_pushstring(L, pkg->url.data); + lua_run_function("get_version", &config.init_path, 1, 1, 4); + if (!lua_isstring(L, -1)) { + lua_pop(L, 4); + return false; + } return true; } -str pkg_hook_fetch_latest_version(package_t *pkg) { - str version = {0}; +str pkg_get_latest_version(package_t *pkg) { + str version = str_new(); + if (!pkg_hook_fetch_latest_version(pkg)) + return version; + if (lua_isstring(L, -1)) + version = mstr(lua_tostring(L, -1)); + lua_pop(L, 4); return version; } @@ -62,6 +101,12 @@ bool pkg_hook_recipe(char *recipe, package_t *pkg) { 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); + if (!lua_isnumber(L, -1) || lua_tonumber(L, -1) != 0) { + lua_pop(L, 4); + return false; + } + lua_pop(L, 4); return true; } @@ -71,17 +116,21 @@ bool pkg_hook_setup(package_t *pkg) { } bool pkg_hook_build(package_t *pkg) { + pkg_hook_recipe("build", pkg); return true; } bool pkg_hook_install(package_t *pkg) { + pkg_hook_recipe("install", pkg); return true; } bool pkg_hook_remove(package_t *pkg) { + pkg_hook_recipe("remove", pkg); return true; } bool pkg_hook_on_update(package_t *pkg) { + pkg_hook_recipe("on_update", pkg); return true; } diff --git a/src/lua_state.c b/src/lua_state.c index 18040fc..297a218 100644 --- a/src/lua_state.c +++ b/src/lua_state.c @@ -114,6 +114,8 @@ static bool load_init_lua(void) { #undef X + lua_pop(L, 1); + return true; } diff --git a/src/pkg.c b/src/pkg.c index fe79b64..97417bc 100644 --- a/src/pkg.c +++ b/src/pkg.c @@ -32,8 +32,7 @@ static str get_destdir(str *cwd, str *arg) { str result = {0}; if (str_first(arg) == '.' && arg->len == 1) { result = str_format("%.*s/%.*s", str_fmt(&config.inst_dirs.src), str_fmt(cwd)); - } else { - str name = {0}; + } else { str name = {0}; if (str_find_char(arg, '/') != 0) name = str_from_after_delim(arg, '/'); else str_copy_into(&name, arg); result = str_format("%.*s/%.*s", str_fmt(&config.inst_dirs.src), str_fmt(&name)); @@ -54,7 +53,7 @@ static void assign_pkg_version(package_t *pkg, str *new_arg_str) { if (str_find_char(new_arg_str, '@')) { pkg->version = str_from_after_delim(new_arg_str, '@'); pkg->name.len -= pkg->version.len + 1; - } else str_copy_cstr_into(&pkg->version, "HEAD");; + } else str_copy_cstr_into(&pkg->version, "master");; } package_t pkg_create(str *arg) { @@ -67,7 +66,8 @@ package_t pkg_create(str *arg) { str cwd = mstr(cwd_cstr); str destdir = get_destdir(&cwd, arg); - if (strncmp(arg->data, "http", 4) == 0 || strncmp(arg->data, "ssh", 3) == 0) { + if (strncmp(arg->data, "http", 4) == 0 + || strncmp(arg->data, "ssh", 3) == 0) { str_copy_into(&pkg.url, arg); pkg.name = str_from_after_delim(arg, '/'); } else if (str_equal_cstr(arg, ".")) { @@ -89,5 +89,16 @@ package_t pkg_create(str *arg) { abort(); } + pkg.src = get_pkgsrc(&pkg); + str_free(&cwd); + str_free(&destdir); + return pkg; } + +void pkg_free(package_t *pkg) { + str_free(&pkg->name); + str_free(&pkg->version); + str_free(&pkg->url); + str_free(&pkg->src); +} diff --git a/src/recipes.c b/src/recipes.c new file mode 100644 index 0000000..5f387b4 --- /dev/null +++ b/src/recipes.c @@ -0,0 +1,78 @@ +/* + + pkgit - package it! + + Copyright (C) 2026 dacctal + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +*/ + +#include + +#include "common.h" +#include "files.h" +#include "log.h" +#include "pkg.h" + +static bool pkg_fetch_sources(package_t *pkg) { + if (!pkg_hook_fetch_sources(pkg)) { + log_error(GREEN "%.*s" COLOR_RESET " fetch failed", + str_fmt(&pkg->name)); + return false; + } + return true; +} + +static bool pkg_enter(package_t *pkg) { + if (!is_directory(pkg->src.data)) { + log_error(GREEN "%.*s" COLOR_RESET " is not a directory", + str_fmt(&pkg->src)); + return false; + } + chdir(pkg->src.data); + return true; +} + +bool pkg_setup(package_t *pkg) { + if (!pkg_enter(pkg)) return false; + if (!pkg_hook_setup(pkg)) return false; + return true; +} + +bool pkg_build(package_t *pkg) { + if (!pkg_enter(pkg)) return false; + if (!pkg_hook_build(pkg)) return false; + return true; +} + +bool pkg_install(package_t *pkg) { + if (!pkg_fetch_sources(pkg)) return false; + if (!pkg_enter(pkg)) return false; + if (!pkg_hook_setup(pkg)) return false; + if (!pkg_hook_build(pkg)) return false; + if (!pkg_hook_install(pkg)) return false; + return true; +} + +bool pkg_remove(package_t *pkg) { + if (!pkg_enter(pkg)) return false; + if (!pkg_hook_setup(pkg)) return false; + return true; +} + +bool pkg_update(package_t *pkg) { + if (!pkg_enter(pkg)) return false; + if (!pkg_hook_setup(pkg)) return false; + return true; +} -- cgit v1.2.3