aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordacctal <dacctal@symlinx.net>2026-05-02 16:47:08 +0000
committerdacctal <dacctal@symlinx.net>2026-05-02 16:47:08 +0000
commitd80025b93c65ab801b5d29499691c545e8baf186 (patch)
treeef2783cff555ede62798d68c06e355fb31898f3f
parent7322d59fef7958cc80604309804bc051243b9199 (diff)
can now require config files from config directory implicitly
-rw-r--r--include/lua_state.hh1
-rw-r--r--src/add_repo.cc2
-rw-r--r--src/lua_state.cc171
-rw-r--r--src/main.cc15
4 files changed, 103 insertions, 86 deletions
diff --git a/include/lua_state.hh b/include/lua_state.hh
index 5965093..0261e13 100644
--- a/include/lua_state.hh
+++ b/include/lua_state.hh
@@ -33,6 +33,7 @@ extern std::unordered_map<std::string, repo> cached_repos;
extern std::unordered_map<std::string, int> cached_build_systems;
+void push_lua_path(lua_State *L, const char *new_path);
void init_lua_state();
void free_lua_state();
lua_State* get_lua_state();
diff --git a/src/add_repo.cc b/src/add_repo.cc
index a0ab07d..1c66c94 100644
--- a/src/add_repo.cc
+++ b/src/add_repo.cc
@@ -22,7 +22,7 @@ void add_repo(std::string repo, std::string repo_name) {
std::ofstream wfile;
wfile.open(repo_file);
- wfile << previous_repos << "repos." << repo_name << ".url = \"" << repo << "\"" << std::endl;
+ wfile << previous_repos << "repositories." << repo_name << " = { url = \"" << repo << "\" }" << std::endl;
wfile.close();
std::cout << print_pkgit << green << "Added " << repo_name << color_reset << std::endl;
diff --git a/src/lua_state.cc b/src/lua_state.cc
index 919a304..c9bcb12 100644
--- a/src/lua_state.cc
+++ b/src/lua_state.cc
@@ -13,19 +13,27 @@ std::unordered_map<std::string, std::string> cached_install_directories;
std::unordered_map<std::string, repo> cached_repos;
std::unordered_map<std::string, int> cached_build_systems;
+void push_lua_path(lua_State *L, const char *new_path) {
+ lua_getglobal(L, "package");
+ lua_getfield(L, -1, "path");
+ const char *current_path = lua_tostring(L, -1);
+ if (!current_path) current_path = "";
+ lua_pop(L, 1);
+ lua_pushfstring(L, "%s;%s", current_path, new_path);
+ lua_setfield(L, -2, "path");
+ lua_pop(L, 1);
+}
+
void init_lua_state() {
if (L != nullptr)
return;
L = luaL_newstate();
luaL_openlibs(L);
+ push_lua_path(L, (config_dir + "/?.lua").c_str());
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;
}
@@ -35,7 +43,8 @@ void init_bldit() {
B = luaL_newstate();
luaL_openlibs(B);
if (luaL_loadfile(B, "bldit.lua") || lua_pcall(B, 0, 0, 0)) {
- std::cout << print_warning << "cannot run bldit script: " << lua_tostring(B, -1) << "\n";
+ std::cout << print_warning
+ << "cannot run bldit script: " << lua_tostring(B, -1) << "\n";
return;
}
bldit_loaded = true;
@@ -57,7 +66,7 @@ void cache_install_directories() {
}
if (!lua_istable(L, -1)) {
std::cout << print_error
- << "lua variable 'install_directories' is not a table.\n";
+ << "lua variable 'install_directories' is not a table.\n";
return;
}
lua_pushnil(L);
@@ -74,42 +83,48 @@ bool repo_build(const char *repository) {
lua_getglobal(L, "repositories");
if (!config_loaded || !lua_istable(L, -1)) {
std::cout << print_warning
- << "lua variable 'repositories' is not a table.\n";
+ << "lua variable 'repositories' is not a table.\n";
lua_pop(L, 1);
return false;
}
std::cout << print_pkgit
- << "lua variable 'repositories' used successfully.\n";
+ << "lua variable 'repositories' used successfully.\n";
lua_getfield(L, -1, repository);
if (!lua_istable(L, -1)) {
- std::cout << print_warning << "'repositories' lua variable '" << repository << "' is not a table.\n";
+ std::cout << print_warning << "'repositories' lua variable '" << repository
+ << "' is not a table.\n";
lua_pop(L, 2);
return false;
}
- std::cout << print_pkgit << "'repositories' lua variable '" << repository << "' used successfully.\n";
+ std::cout << print_pkgit << "'repositories' lua variable '" << repository
+ << "' used successfully.\n";
lua_getfield(L, -1, "build");
if (!lua_isfunction(L, -1)) {
- std::cout << print_warning << "'repositories' lua variable 'build' is not a function." << std::endl;
+ std::cout << print_warning
+ << "'repositories' lua variable 'build' is not a function."
+ << std::endl;
lua_pop(L, 3);
return false;
}
if (lua_pcall(L, 0, 0, 0) != LUA_OK) {
- std::cout << "'repositories' build failed: " << lua_tostring(L, -1) << std::endl;
+ std::cout << "'repositories' build failed: " << lua_tostring(L, -1)
+ << std::endl;
lua_pop(L, 1);
lua_pop(L, 2);
return false;
}
lua_pop(L, 2);
std::cout << print_pkgit
- << "'repositories' lua function 'build' ran successfully.\n";
+ << "'repositories' lua function 'build' ran successfully.\n";
return true;
}
bool bldit(const char *target) {
- lua_State* B = luaL_newstate();
+ lua_State *B = luaL_newstate();
luaL_openlibs(B);
if (luaL_loadfile(B, "bldit.lua") || lua_pcall(B, 0, 0, 0)) {
- std::cout << print_warning << "cannot run bldit script: " << lua_tostring(B, -1) << "\n";
+ std::cout << print_warning
+ << "cannot run bldit script: " << lua_tostring(B, -1) << "\n";
return false;
}
lua_getglobal(B, "targets");
@@ -120,14 +135,17 @@ bool bldit(const char *target) {
std::cout << print_pkgit << "bldit variable 'targets' used successfully.\n";
lua_getfield(B, -1, target);
if (!lua_istable(B, -1)) {
- std::cout << print_warning << "bldit variable '" << target << "' is not a table.\n";
+ std::cout << print_warning << "bldit variable '" << target
+ << "' is not a table.\n";
lua_pop(B, 2);
return false;
}
- std::cout << print_pkgit << "bldit variable '" << target << "' used successfully.\n";
+ std::cout << print_pkgit << "bldit variable '" << target
+ << "' used successfully.\n";
lua_getfield(B, -1, "build");
if (!lua_isfunction(B, -1)) {
- std::cout << "'repositories' lua variable 'build' is not a function." << std::endl;
+ std::cout << "'repositories' lua variable 'build' is not a function."
+ << std::endl;
lua_pop(B, 3);
return false;
}
@@ -142,45 +160,48 @@ bool bldit(const char *target) {
return true;
}
-//bool config_build(const char* path) {
-// free_lua_state();
-// init_lua_state();
-// lua_State* L = get_lua_state();
-// std::unordered_map<std::string, int> build_files;
-// lua_getglobal(L, "build_systems");
-// if (!config_loaded || !lua_istable(L, -1)) {
-// lua_pop(L, 1);
-// std::cout << print_warning << "config lua variable 'build_systems' is not
-// a table.\n"; return false;
-// }
-// lua_pushnil(L);
-// bool build_found = false;
-// while (lua_next(L, -2) != 0) {
-// const char *key = lua_tostring(L, -2);
-// int value = lua_type(L, -1);
-// if (lua_isfunction(L, -1) == 0) {
-// std::cout << print_warning << "config build value " << key << " is not
-// a function\n"; lua_pop(L, 1); continue;
-// }
-// build_files[key] = value;
-// for (auto const& dir_entry : std::filesystem::directory_iterator(std::filesystem::current_path().string())) {
-// std::string string_key = key;
-// if (dir_entry.path().filename() != string_key) { continue; }
-// build_found = true;
-// lua_pushvalue(L, -1);
-// lua_pushstring(L, std::filesystem::current_path().string().c_str());
-// std::cout << "calling config lua build function according to key filename '"
-// << key << "'...\n";
-// if (lua_pcall(L, 1, 0, 0) != 0) {
-// std::cout << print_warning << "config lua build function failed to run\n";
-// break;
-// }
-// if (build_found) { break; }
-// }
-// lua_pop(L, 1);
-// }
-// return build_found;
-//}
+// bool config_build(const char* path) {
+// free_lua_state();
+// init_lua_state();
+// lua_State* L = get_lua_state();
+// std::unordered_map<std::string, int> build_files;
+// lua_getglobal(L, "build_systems");
+// if (!config_loaded || !lua_istable(L, -1)) {
+// lua_pop(L, 1);
+// std::cout << print_warning << "config lua variable 'build_systems' is not
+// a table.\n"; return false;
+// }
+// lua_pushnil(L);
+// bool build_found = false;
+// while (lua_next(L, -2) != 0) {
+// const char *key = lua_tostring(L, -2);
+// int value = lua_type(L, -1);
+// if (lua_isfunction(L, -1) == 0) {
+// std::cout << print_warning << "config build value " << key << " is not
+// a function\n"; lua_pop(L, 1); continue;
+// }
+// build_files[key] = value;
+// for (auto const& dir_entry :
+// std::filesystem::directory_iterator(std::filesystem::current_path().string()))
+// {
+// std::string string_key = key;
+// if (dir_entry.path().filename() != string_key) { continue; }
+// build_found = true;
+// lua_pushvalue(L, -1);
+// lua_pushstring(L, std::filesystem::current_path().string().c_str());
+// std::cout << "calling config lua build function according to key
+// filename '"
+// << key << "'...\n";
+// if (lua_pcall(L, 1, 0, 0) != 0) {
+// std::cout << print_warning << "config lua build function failed to
+// run\n"; break;
+// }
+// if (build_found) { break; }
+// }
+// lua_pop(L, 1);
+// }
+// return build_found;
+// }
void cache_repos() {
lua_getglobal(L, "repositories");
@@ -198,28 +219,31 @@ void cache_repos() {
continue;
}
lua_getfield(L, -1, "url");
- const char* url = lua_tostring(L, -1);
+ const char *url = lua_tostring(L, -1);
lua_pop(L, 1);
cached_repos[repo_name].source.key = "url";
cached_repos[repo_name].source.value = url ? url : "";
lua_getfield(L, -1, "dependencies");
if (!lua_istable(L, -1)) {
- //std::cout << print_warning << repo_name << ": \tlua variable 'dependencies' is not a table.\n";
+ // std::cout << print_warning << repo_name << ": \tlua variable
+ // 'dependencies' is not a table.\n";
lua_pop(L, 2);
continue;
}
lua_pushnil(L);
while (lua_next(L, -2) != 0) {
- const char* depname = lua_tostring(L, -2);
+ const char *depname = lua_tostring(L, -2);
if (depname && lua_istable(L, -1)) {
lua_getfield(L, -1, "url");
- const char* dep_url = lua_tostring(L, -1);
+ const char *dep_url = lua_tostring(L, -1);
lua_pop(L, 1);
lua_getfield(L, -1, "version");
- const char* dep_version = lua_tostring(L, -1);
+ const char *dep_version = lua_tostring(L, -1);
lua_pop(L, 1);
- cached_repos[repo_name].dependencies[depname].url = dep_url ? dep_url : "";
- cached_repos[repo_name].dependencies[depname].version = dep_version ? dep_version : "";
+ cached_repos[repo_name].dependencies[depname].url =
+ dep_url ? dep_url : "";
+ cached_repos[repo_name].dependencies[depname].version =
+ dep_version ? dep_version : "";
}
lua_pop(L, 1);
}
@@ -236,7 +260,8 @@ void cache_build_systems() {
lua_getglobal(L, "build_systems");
}
if (!lua_istable(L, -1)) {
- std::cout << print_error << "lua variable 'build_systems' is not a table.\n";
+ std::cout << print_error
+ << "lua variable 'build_systems' is not a table.\n";
return;
}
lua_pushnil(L);
@@ -256,11 +281,13 @@ void cache_build_systems() {
bool config_build(const char *path) {
lua_getglobal(L, "build_systems");
if (!config_loaded || !lua_istable(L, -1)) {
- std::cout << print_warning << "lua variable 'build_systems' is not a table.\n";
+ std::cout << print_warning
+ << "lua variable 'build_systems' is not a table.\n";
lua_pop(L, 1);
return false;
}
- std::cout << print_pkgit << "lua variable 'build_systems' used successfully.\n";
+ std::cout << print_pkgit
+ << "lua variable 'build_systems' used successfully.\n";
lua_pushnil(L);
while (lua_next(L, -2) != 0) {
const char *key = lua_tostring(L, -2);
@@ -269,9 +296,12 @@ bool config_build(const char *path) {
lua_pop(L, 1);
continue;
}
- for (auto const& dir_entry : std::filesystem::directory_iterator(std::filesystem::current_path().string())) {
+ for (auto const &dir_entry : std::filesystem::directory_iterator(
+ std::filesystem::current_path().string())) {
std::string string_key = key;
- if (dir_entry.path().filename() != string_key) { continue; }
+ if (dir_entry.path().filename() != string_key) {
+ continue;
+ }
lua_getfield(L, -1, "build");
if (lua_isfunction(L, -1) == 0) {
std::cout << print_error << "no build function for " << key << "\n";
@@ -279,7 +309,8 @@ bool config_build(const char *path) {
continue;
}
if (lua_pcall(L, 0, 0, 0) != LUA_OK) {
- std::cout << "'" << key << "' build failed: " << lua_tostring(L, -1) << std::endl;
+ std::cout << "'" << key << "' build failed: " << lua_tostring(L, -1)
+ << std::endl;
lua_pop(L, 1);
continue;
}
diff --git a/src/main.cc b/src/main.cc
index 4ec16c9..e69de29 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -1,15 +0,0 @@
-#include <iostream>
-#include <unordered_map>
-
-#include "cla_parse.hh"
-#include "setup_pkgit.hh"
-#include "lua_state.hh"
-
-int main(int argc, char *argv[]) {
- setup_pkgit();
- //init_lua_state();
- //repo_build("omnisearch");
- cla_parse(argc, argv);
- free_lua_state();
- return 0;
-}