aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/add_repo.cc3
-rw-r--r--src/build.cc19
-rw-r--r--src/cla_parse.cc8
-rw-r--r--src/lua_build.cc21
-rw-r--r--src/lua_state.cc109
-rw-r--r--src/set_install_directories.cc19
-rw-r--r--src/setup_repo.cc18
-rw-r--r--src/vars.cc6
8 files changed, 148 insertions, 55 deletions
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 <fstream>
+#include <iostream>
#include <string>
#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 <iostream>
#include <filesystem>
-#include <map>
-extern "C" {
-#include <luajit-2.1/lua.h>
-#include <luajit-2.1/lauxlib.h>
-#include <luajit-2.1/lualib.h>
-}
-
+#include <unordered_map>
+#include "lua_state.hh"
#include "lua_build.hh"
#include "vars.hh"
-std::map<std::string, int> build_files;
+std::unordered_map<std::string, int> 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 <iostream>
+#include <unordered_map>
+#include "lua_state.hh"
+#include "vars.hh"
+
+static lua_State *L = nullptr;
+static bool config_loaded = false;
+
+std::unordered_map<std::string, std::string> cached_install_directories;
+std::unordered_map<std::string, std::string> cached_repos;
+std::unordered_map<std::string, int> 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 <iostream>
-
-extern "C" {
-#include <luajit-2.1/lua.h>
-#include <luajit-2.1/lauxlib.h>
-#include <luajit-2.1/lualib.h>
-}
-
+#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 <iostream>
-extern "C" {
-#include <luajit-2.1/lua.h>
-#include <luajit-2.1/lauxlib.h>
-#include <luajit-2.1/lualib.h>
-}
-
+#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 <filesystem>
-#include <map>
+#include <unordered_map>
#include <string>
#include "vars.hh"
-std::map<std::string, std::string> repos;
-std::map<std::string, std::string> install_directories;
+std::unordered_map<std::string, std::string> repos;
+std::unordered_map<std::string, std::string> install_directories;
bool is_symlink_install = false;
bool is_verbose = false;