diff options
| -rw-r--r-- | src/hooks.c | 22 | ||||
| -rw-r--r-- | src/main.c | 2 | ||||
| -rw-r--r-- | src/pkg.c | 23 | ||||
| -rw-r--r-- | src/recipes.c | 38 |
4 files changed, 57 insertions, 28 deletions
diff --git a/src/hooks.c b/src/hooks.c index 18ad31c..20923b7 100644 --- a/src/hooks.c +++ b/src/hooks.c @@ -90,8 +90,11 @@ 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)); + if (lua_isstring(L, -1)) { + str tmp_version = mstr(lua_tostring(L, -1)); + str_copy_into(&version, &tmp_version); + str_free(&tmp_version); + } lua_pop(L, 4); return version; } @@ -111,28 +114,23 @@ bool pkg_hook_recipe(char *recipe, package_t *pkg) { } bool pkg_hook_setup(package_t *pkg) { - pkg_hook_recipe("setup", pkg); - return true; + return pkg_hook_recipe("setup", pkg); } bool pkg_hook_build(package_t *pkg) { - pkg_hook_recipe("build", pkg); - return true; + return pkg_hook_recipe("build", pkg); } bool pkg_hook_install(package_t *pkg) { - pkg_hook_recipe("install", pkg); - return true; + return pkg_hook_recipe("install", pkg); } bool pkg_hook_remove(package_t *pkg) { - pkg_hook_recipe("remove", pkg); - return true; + return pkg_hook_recipe("remove", pkg); } bool pkg_hook_on_update(package_t *pkg) { - pkg_hook_recipe("on_update", pkg); - return true; + return pkg_hook_recipe("on_update", pkg); } bool config_hook_on_update(void) { @@ -25,9 +25,7 @@ int main(int argc, char **argv) { cli_result_t result = cli(argc, argv); if (!result) { retval = 1; - goto done; } -done: return retval; } @@ -23,6 +23,7 @@ #include <unistd.h> #include "common.h" +#include "files.h" #include "log.h" #include "pkg.h" #include "state.h" @@ -30,7 +31,9 @@ static str get_destdir(str *cwd, str *arg) { str result = {0}; - if (str_first(arg) == '.' && arg->len == 1) { + if (is_directory(arg->data)) { + result = str_format("%.*s", str_fmt(arg)); + } else 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}; @@ -44,10 +47,11 @@ static str get_destdir(str *cwd, str *arg) { return result; } -static void set_pkgsrc(package_t *pkg) { +static void set_pkgsrc(package_t *pkg, str *cwd, bool is_installed_locally) { pkg->root = str_format("%.*s/%.*s", str_fmt(&config.inst_dirs.src), str_fmt(&pkg->name)); - if (pkg->is_local) pkg->src = pkg->root; + if (is_installed_locally) str_copy_into(&pkg->src, cwd); + else if (pkg->is_local) pkg->src = pkg->root; else pkg->src = str_format("%.*s/%.*s/%.*s", str_fmt(&config.inst_dirs.src), str_fmt(&pkg->name), str_fmt(&pkg->version)); @@ -69,6 +73,7 @@ package_t pkg_create(str *arg) { getcwd(cwd_cstr, MAX_PATH_LEN); str cwd = mstr(cwd_cstr); str destdir = get_destdir(&cwd, arg); + bool is_installed_locally = is_directory(destdir.data); if (strncmp(arg->data, "http", 4) == 0 || strncmp(arg->data, "ssh", 3) == 0) { @@ -81,19 +86,19 @@ package_t pkg_create(str *arg) { } else if (pkg_exists(arg)) { str_copy_into(&pkg.name, arg); pkg.url = pkg_get_url(&pkg); -// } else if (is_installed_locally) { -// str_copy_cstr_into(&pkg.url, ""); -// pkg.name = str_from_after_delim(&destdir, '/'); -// pkg.is_local = true; + } else if (is_installed_locally) { + str_copy_cstr_into(&pkg.url, ""); + pkg.name = str_from_after_delim(&destdir, '/'); + pkg.is_local = true; } else { log_error("'%.*s' is not a valid package", str_fmt(arg)); str_free(&cwd); str_free(&destdir); str_free(arg); - abort(); + exit(1); } - set_pkgsrc(&pkg); + set_pkgsrc(&pkg, &cwd, is_installed_locally); str_free(&cwd); str_free(&destdir); diff --git a/src/recipes.c b/src/recipes.c index d94ee3c..41af109 100644 --- a/src/recipes.c +++ b/src/recipes.c @@ -58,8 +58,13 @@ static bool pkg_read_status(const char desired, package_t *pkg) { bool success = false; str pkgit_status = str_format("%.*s/.pkgit_status", str_fmt(&pkg->src)); + if (!file_exists(pkgit_status.data)) { + log_info("%.*s doesn't exist", str_fmt(&pkgit_status)); + str_free(&pkgit_status); + return false; + } str status = file_read(&pkgit_status); - success = str_find_char(&status, desired); + success = (str_find_char(&status, desired) == 0); str_free(&pkgit_status); str_free(&status); return success; @@ -119,8 +124,11 @@ 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'); + pkg_setup(pkg); + //try_pkg_fn(pkg_hook_setup, pkg, "setup", 's'); + pkg_build(pkg); + //try_pkg_fn(pkg_hook_build, pkg, "build", 'b'); + printf("test\n"); try_pkg_fn(pkg_hook_install, pkg, "install", 'i'); log_success("'%.*s' installed", str_fmt(&pkg->name)); return true; @@ -134,9 +142,29 @@ bool pkg_remove(package_t *pkg) { return true; } +static bool is_latest(package_t *pkg) { + bool result = false; + if (str_is_valid(&pkg->src) && pkg->src.len > 0 && + chdir(pkg->src.data) != 0) return result; + str latest_version = pkg_get_latest_version(pkg); + result = (strcmp(pkg->version.data, latest_version.data) != 0); + str_free(&latest_version); + return result; +} + bool pkg_update(package_t *pkg) { - pkg_install(pkg); - log_success("'%.*s' is up to date", str_fmt(&pkg->name)); + if (!is_latest(pkg)) { + if (pkg_install(pkg)) log_success( + "'%.*s' is up to date", str_fmt(&pkg->name) + ); + } else if (flags.force) { + log_warn("'%.*s' is already up to date", str_fmt(&pkg->name)); + if (pkg_install(pkg)) log_success( + "'%.*s' is up to date", str_fmt(&pkg->name) + ); + } else { + log_info("'%.*s' is already up to date", str_fmt(&pkg->name)); + } return true; } |
