From f1d7a1903e49c75f9965693247e8d102a96b482c Mon Sep 17 00:00:00 2001 From: frosty Date: Wed, 22 Apr 2026 22:30:21 -0400 Subject: misc optimisations and QoL improvements --- .gitignore | 3 +- Makefile | 23 ++++++--- config/init.lua | 3 +- config/install_directories.lua | 13 +++-- include/lua_state.hh | 18 +++++++ include/vars.hh | 6 +-- src/add_repo.cc | 3 ++ src/build.cc | 19 +++++-- src/cla_parse.cc | 8 +-- src/lua_build.cc | 21 +++----- src/lua_state.cc | 109 +++++++++++++++++++++++++++++++++++++++++ src/set_install_directories.cc | 19 ++----- src/setup_repo.cc | 18 ++----- src/vars.cc | 6 +-- 14 files changed, 197 insertions(+), 72 deletions(-) create mode 100644 include/lua_state.hh create mode 100644 src/lua_state.cc diff --git a/.gitignore b/.gitignore index 618a3f6..03b7c50 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -pkgit \ No newline at end of file +pkgit +obj/ \ No newline at end of file diff --git a/Makefile b/Makefile index 2ac1d8f..7fa9481 100644 --- a/Makefile +++ b/Makefile @@ -1,15 +1,24 @@ CC = clang++ RM = rm -f PREFIX ?= /usr/local -CXXFLAGS += $(shell pkg-config --cflags --libs luajit ) -I./include -L/usr/lib +OBJDIR = obj +SRCS = $(wildcard src/*.cc) +OBJS = $(SRCS:src/%.cc=$(OBJDIR)/%.o) +CXXFLAGS += $(shell pkg-config --cflags luajit ) -I./include default: pkgit -pkgit: src/*.cc - ${CC} -o pkgit src/*.cc ${CXXFLAGS} +pkgit: $(OBJS) + ${CC} -o pkgit $^ $(shell pkg-config --libs luajit) + +$(OBJDIR): + @mkdir -p $(OBJDIR) + +$(OBJDIR)/%.o: src/%.cc | $(OBJDIR) + ${CC} $(CXXFLAGS) -c -o $@ $< debug: src/*.cc - ${CC} -o pkgit src/*.cc ${CXXFLAGS} -g -O0 + ${CC} -o pkgit src/*.cc ${CXXFLAGS} ${LDFLAGS} -g -O0 install: pkgit install -d ${DESTDIR}${PREFIX}/bin @@ -18,6 +27,8 @@ install: pkgit defconfig: @echo "Installing default config to ~/.config/pkgit ..." @mkdir -p ~/.config/pkgit - @cp -r config/pkgit/* ~/.config/pkgit - @sed 's|\[placeholder\]|$HOME|g' config/pkgit/dirs.lua > ~/.config/pkgit/dirs.lua + @cp -r config/* ~/.config/pkgit @echo "default config installed" + +clean: + ${RM} -r $(OBJDIR) pkgit \ No newline at end of file diff --git a/config/init.lua b/config/init.lua index 3cbcc06..cb1919a 100644 --- a/config/init.lua +++ b/config/init.lua @@ -1,8 +1,7 @@ -- initializing the path for configuration -- DO NOT CHANGE home_dir = os.getenv("HOME") -package.path = package.path .. ";" .. home_dir .. "/.config/pkgit/?.lua" - +package.path = string.format("%s;%s/.config/pkgit/?.lua", package.path, home_dir) -- require other lua scripts require "build_systems" require "repositories" diff --git a/config/install_directories.lua b/config/install_directories.lua index f3e2ea8..d87bc29 100644 --- a/config/install_directories.lua +++ b/config/install_directories.lua @@ -1,5 +1,8 @@ -install_directories = {} -install_directories.bin = "/home/dacc/pkgit/bin" -install_directories.lib = "/home/dacc/pkgit/lib" -install_directories.include = "/home/dacc/pkgit/include" -install_directories.pkgblds = "/home/dacc/pkgit/src" +local base_dir = home_dir .. "/pkgit" + +install_directories = { + bin = base_dir .. "/bin", + lib = base_dir .. "/lib", + include = base_dir .. "/include", + pkgblds = base_dir .. "/src" +} diff --git a/include/lua_state.hh b/include/lua_state.hh new file mode 100644 index 0000000..a8dfed8 --- /dev/null +++ b/include/lua_state.hh @@ -0,0 +1,18 @@ +#ifndef LUA_STATE +#define LUA_STATE + +extern "C" { +#include +#include +#include +} + +void init_lua_state(); +void free_lua_state(); +lua_State* get_lua_state(); + +void cache_install_directories(); +void cache_repos(); +void cache_build_systems(); + +#endif \ No newline at end of file diff --git a/include/vars.hh b/include/vars.hh index 71045ee..b70615b 100644 --- a/include/vars.hh +++ b/include/vars.hh @@ -3,10 +3,10 @@ #include #include -#include +#include -extern std::map repos; -extern std::map install_directories; +extern std::unordered_map repos; +extern std::unordered_map install_directories; extern bool is_symlink_install; extern bool is_verbose; diff --git a/src/add_repo.cc b/src/add_repo.cc index f2a4910..c302b94 100644 --- a/src/add_repo.cc +++ b/src/add_repo.cc @@ -1,4 +1,5 @@ #include +#include #include #include "add_repo.hh" @@ -23,4 +24,6 @@ void add_repo(std::string repo, std::string repo_name) { wfile.open(repo_file); wfile << previous_repos << "repos[\"" << repo_name << "\"] = \"" << repo << "\"" << std::endl; wfile.close(); + + std::cout << print_pkgit << green << "Added " << repo_name << color_reset << std::endl; } diff --git a/src/build.cc b/src/build.cc index adbfec5..88b40c7 100644 --- a/src/build.cc +++ b/src/build.cc @@ -9,11 +9,22 @@ void build(std::filesystem::path build_dir) { if (build_dir != std::filesystem::current_path().string()) { std::filesystem::current_path(build_dir); } + + bool bldit_found = false; + bool build_found = false; + for (auto const &dir_entry : std::filesystem::directory_iterator(std::filesystem::current_path().string())) { - if (dir_entry.path().filename() == "bldit") { system("./bldit"); return; } + if (dir_entry.path().filename() == "bldit") { + bldit_found = true; + break; + } } - for (auto const &dir_entry : std::filesystem::directory_iterator(std::filesystem::current_path().string())) { - if (lua_build(build_dir.c_str())) { return; } + + if (bldit_found) { + system("./bldit"); + } else if (lua_build(build_dir.c_str())) { + return; + } else { + std::cout << print_error << "no usable build system was found\n"; } - std::cout << print_error << "no usable build system was found\n"; } \ No newline at end of file diff --git a/src/cla_parse.cc b/src/cla_parse.cc index 2813ded..42d2f65 100644 --- a/src/cla_parse.cc +++ b/src/cla_parse.cc @@ -28,7 +28,7 @@ void cla_parse(int argc, char** argv) { } if (strcmp(argv[i], "add") == 0 || strcmp(argv[i], "a") == 0) { - for (int j = i+1; i < argc; j++) { + for (int j = i+1; j < argc; j++) { if (argv[j]) { add_repo(argv[j], name_from_url(argv[j])); return; @@ -39,7 +39,7 @@ void cla_parse(int argc, char** argv) { } } else if (strcmp(argv[i], "build") == 0 || strcmp(argv[i], "b") == 0) { - for (int j = i+1; i < argc; j++) { + for (int j = i+1; j < argc; j++) { if (argv[j]) { build(argv[j]); return; @@ -50,7 +50,7 @@ void cla_parse(int argc, char** argv) { } } else if (strcmp(argv[i], "install") == 0 || strcmp(argv[i], "i") == 0) { - for (int j = i+1; i < argc; j++) { + for (int j = i+1; j < argc; j++) { pkg = create_pkg(argv[j]); if (argv[j]) { install_pkg(pkg); @@ -62,7 +62,7 @@ void cla_parse(int argc, char** argv) { } } else if (strcmp(argv[i], "remove") == 0 || strcmp(argv[i], "r") == 0) { - for (int j = i+1; i < argc; j++) { + for (int j = i+1; j < argc; j++) { pkg = create_pkg(argv[j]); if (argv[j]) { remove_pkg(pkg); diff --git a/src/lua_build.cc b/src/lua_build.cc index bf21f66..75a626a 100644 --- a/src/lua_build.cc +++ b/src/lua_build.cc @@ -1,24 +1,15 @@ #include #include -#include -extern "C" { -#include -#include -#include -} - +#include +#include "lua_state.hh" #include "lua_build.hh" #include "vars.hh" -std::map build_files; +std::unordered_map build_files; bool lua_build(const char *path) { - lua_State *L = lua_open(); - luaL_openlibs(L); - - if (luaL_loadfile(L, config_file.c_str()) || lua_pcall(L, 0, 0, 0)){ - std::cout << print_error << "cannot run configuration script: " << lua_tostring(L, -1) << "\n"; - } + init_lua_state(); + lua_State *L = get_lua_state(); lua_getglobal(L, "build_systems"); @@ -57,4 +48,4 @@ bool lua_build(const char *path) { lua_pop(L, 1); } return build_found; -} +} \ No newline at end of file diff --git a/src/lua_state.cc b/src/lua_state.cc new file mode 100644 index 0000000..81f8d13 --- /dev/null +++ b/src/lua_state.cc @@ -0,0 +1,109 @@ +#include +#include +#include "lua_state.hh" +#include "vars.hh" + +static lua_State *L = nullptr; +static bool config_loaded = false; + +std::unordered_map cached_install_directories; +std::unordered_map cached_repos; +std::unordered_map cached_build_systems; + +void init_lua_state() { + if (L != nullptr) return; + L = luaL_newstate(); + luaL_openlibs(L); + + if (luaL_loadfile(L, config_file.c_str()) || lua_pcall(L, 0, 0, 0)) { + std::cout << print_error << "cannot run configuration script: " << lua_tostring(L, -1) << "\n"; + return; + } + + if (luaL_loadfile(L, repo_file.c_str()) || lua_pcall(L, 0, 0, 0)) { + std::cout << print_error << "cannot run repository script: " << lua_tostring(L, -1) << "\n"; + return; + } + + config_loaded = true; +} + +void free_lua_state() { + if (L != nullptr) { + lua_close(L); + L = nullptr; + } + config_loaded = false; +} + +lua_State* get_lua_state() { + return L; +} + +void cache_install_directories() { + if (!config_loaded || !lua_istable(L, -1)) { + lua_getglobal(L, "install_directories"); + } + + if (!lua_istable(L, -1)) { + std::cout << print_error << "lua variable 'install_directories' is not a table.\n"; + return; + } + + lua_pushnil(L); + while (lua_next(L, -2) != 0) { + const char *key = lua_tostring(L, -2); + const char *value = lua_tostring(L, -1); + cached_install_directories[key] = value; + lua_pop(L, 1); + } + lua_pop(L, 1); +} + +void cache_repos() { + if (!config_loaded) { + lua_getglobal(L, "repos"); + } else if (!lua_istable(L, -1)) { + lua_getglobal(L, "repos"); + } + + if (!lua_istable(L, -1)) { + std::cout << print_error << "lua variable 'repos' is not a table.\n"; + return; + } + + lua_pushnil(L); + while (lua_next(L, -2) != 0) { + const char *key = lua_tostring(L, -2); + const char *value = lua_tostring(L, -1); + cached_repos[key] = value; + lua_pop(L, 1); + } + lua_pop(L, 1); +} + +void cache_build_systems() { + if (!config_loaded) { + lua_getglobal(L, "build_systems"); + } else if (!lua_istable(L, -1)) { + lua_getglobal(L, "build_systems"); + } + + if (!lua_istable(L, -1)) { + std::cout << print_error << "lua variable 'build_systems' is not a table.\n"; + return; + } + + lua_pushnil(L); + while (lua_next(L, -2) != 0) { + const char *key = lua_tostring(L, -2); + if (lua_isfunction(L, -1) == 0) { + std::cout << print_error << "build value is not a function\n"; + lua_pop(L, 1); + continue; + } + cached_build_systems[key] = lua_gettop(L); + lua_pop(L, 1); + } + lua_pop(L, 1); +} diff --git a/src/set_install_directories.cc b/src/set_install_directories.cc index 4eb712e..9163480 100644 --- a/src/set_install_directories.cc +++ b/src/set_install_directories.cc @@ -1,22 +1,11 @@ #include - -extern "C" { -#include -#include -#include -} - +#include "lua_state.hh" #include "set_install_directories.hh" #include "vars.hh" void set_install_directories() { - lua_State *L = lua_open(); - luaL_openlibs(L); - - if (luaL_loadfile(L, config_file.c_str()) || lua_pcall(L, 0, 0, 0)){ - std::cout << print_error << "cannot run configuration script: " << lua_tostring(L, -1) << "\n"; - return; - } + init_lua_state(); + lua_State *L = get_lua_state(); lua_getglobal(L, "install_directories"); @@ -34,4 +23,4 @@ void set_install_directories() { lua_pop(L, 1); } -} +} \ No newline at end of file diff --git a/src/setup_repo.cc b/src/setup_repo.cc index 99ca451..bc15d1f 100644 --- a/src/setup_repo.cc +++ b/src/setup_repo.cc @@ -1,10 +1,5 @@ #include -extern "C" { -#include -#include -#include -} - +#include "lua_state.hh" #include "setup_repo.hh" #include "ensure_repo.hh" #include "vars.hh" @@ -12,13 +7,8 @@ extern "C" { void setup_repo() { ensure_repo(); - lua_State *L = lua_open(); - luaL_openlibs(L); - - if (luaL_loadfile(L, config_file.c_str()) || lua_pcall(L, 0, 0, 0)){ - std::cout << print_error << "cannot run configuration script: " << lua_tostring(L, -1) << "\n"; - return; - } + init_lua_state(); + lua_State *L = get_lua_state(); lua_getglobal(L, "repos"); @@ -36,4 +26,4 @@ void setup_repo() { lua_pop(L, 1); } -} +} \ No newline at end of file diff --git a/src/vars.cc b/src/vars.cc index c288be9..12c7e7e 100644 --- a/src/vars.cc +++ b/src/vars.cc @@ -1,11 +1,11 @@ #include -#include +#include #include #include "vars.hh" -std::map repos; -std::map install_directories; +std::unordered_map repos; +std::unordered_map install_directories; bool is_symlink_install = false; bool is_verbose = false; -- cgit v1.2.3