diff options
| author | dacctal <donotcontactmevia@email.invalid> | 2026-08-14 07:37:03 +0000 |
|---|---|---|
| committer | dacctal <donotcontactmevia@email.invalid> | 2026-08-14 07:37:03 +0000 |
| commit | 536b651f4086fe01d0bd1f743f54ef650524b122 (patch) | |
| tree | b63a9554ac3ca0d94dc2e9f7da583ad6ca0b358c | |
| parent | 4fa342bcfdb5ebcbe65cabe1fb9ac3a46ea4ddca (diff) | |
i made the install work (kinda)
| -rw-r--r-- | include/files.h | 29 | ||||
| -rw-r--r-- | include/lua_state.h | 19 | ||||
| -rw-r--r-- | include/pkg.h | 14 | ||||
| -rw-r--r-- | src/cli.c | 6 | ||||
| -rw-r--r-- | src/files.c | 33 | ||||
| -rw-r--r-- | src/hooks.c | 59 | ||||
| -rw-r--r-- | src/lua_state.c | 2 | ||||
| -rw-r--r-- | src/pkg.c | 19 | ||||
| -rw-r--r-- | src/recipes.c | 78 |
9 files changed, 246 insertions, 13 deletions
diff --git a/include/files.h b/include/files.h new file mode 100644 index 0000000..24712e9 --- /dev/null +++ b/include/files.h @@ -0,0 +1,29 @@ +/* + + 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 <https://www.gnu.org/licenses/>. + +*/ + +#ifndef PKGIT_FILES_H +#define PKGIT_FILES_H + +#include <stdbool.h> + +bool file_exists(const char *path); +bool is_directory(const char *path); + +#endif diff --git a/include/lua_state.h b/include/lua_state.h index db689fe..6c36f37 100644 --- a/include/lua_state.h +++ b/include/lua_state.h @@ -18,18 +18,31 @@ void free_lua_config(void); do { \ lua_getfield(L, -1, name); \ if (!lua_is##type(L, -1)) { \ - log_error("lua: expected type '" #type "' for %s in %.*s", \ + log_error("lua: expected type '" #type "' for '%s' in '%.*s'", \ name, str_fmt(file)); \ lua_pop(L, pop_num); \ return false; \ } \ } while (0) +#define lua_get_table(name, file, pop_num) \ + do { \ + lua_pushstring(L, name); \ + lua_gettable(L, -2); \ + lua_pop(L, 1); \ + if (!lua_istable(L, -1)) { \ + log_error("lua: expected type 'table' for '%s' in '%.*s'", \ + name, str_fmt(file)); \ + /*lua_pop(L, pop_num);*/ \ + return false; \ + } \ + } while (0) + #define lua_run_function(name, file, params, returns, pop_num) \ do { \ - if (lua_pcall(L, params, returns, 0) { \ + if (lua_pcall(L, params, returns, 0)) { \ log_error("lua: '%s' function borked in %.*s: %s", \ - name, str_fmt(file)), lua_tostring(L, -1); \ + name, str_fmt(file), lua_tostring(L, -1)); \ lua_pop(L, pop_num); \ return false; \ } \ diff --git a/include/pkg.h b/include/pkg.h index 15990ab..af720d5 100644 --- a/include/pkg.h +++ b/include/pkg.h @@ -9,6 +9,9 @@ typedef struct package_t { bool is_local; } package_t; +package_t pkg_create(str *arg); + +bool pkg_exists(str *name); bool pkg_hook_url(package_t *pkg); str pkg_get_url(package_t *pkg); bool pkg_hook_setup(package_t *pkg); @@ -17,6 +20,15 @@ bool pkg_hook_install(package_t *pkg); bool pkg_hook_remove(package_t *pkg); bool pkg_hook_on_update(package_t *pkg); bool pkg_hook_fetch_sources(package_t *pkg); -str pkg_hook_fetch_latest_version(package_t *pkg); +bool pkg_hook_fetch_latest_version(package_t *pkg); +str pkg_get_latest_version(package_t *pkg); + +bool pkg_setup(package_t *pkg); +bool pkg_build(package_t *pkg); +bool pkg_install(package_t *pkg); +bool pkg_remove(package_t *pkg); +bool pkg_update(package_t *pkg); + +void pkg_free(package_t *pkg); #endif @@ -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 <https://www.gnu.org/licenses/>. + +*/ + +#include <stdbool.h> +#include <sys/stat.h> + +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; } @@ -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 <https://www.gnu.org/licenses/>. + +*/ + +#include <unistd.h> + +#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; +} |
