aboutsummaryrefslogtreecommitdiff
path: root/src/create_pkg.c
diff options
context:
space:
mode:
authordacctal <dacctal@symlinx.net>2026-06-21 01:33:58 +0000
committerdacctal <dacctal@symlinx.net>2026-06-21 01:33:58 +0000
commit7cde9aeb5dccce578c1eed13cf1ed2c61e446800 (patch)
tree3323e34bbec0a87a4bb659a90816b28a6a241f5a /src/create_pkg.c
parent6482da05e96630d622f7deea02e31cb0ff07c89e (diff)
huge code cleanup
Diffstat (limited to 'src/create_pkg.c')
-rw-r--r--src/create_pkg.c117
1 files changed, 69 insertions, 48 deletions
diff --git a/src/create_pkg.c b/src/create_pkg.c
index cfb388f..2ce3588 100644
--- a/src/create_pkg.c
+++ b/src/create_pkg.c
@@ -4,66 +4,100 @@
#include <unistd.h>
#include "create_pkg.h"
-#include "files.h"
#include "lua_state.h"
#include "name_from_url.h"
#include "vars.h"
+bool is_in_repos(char *arg) {
+ for (size_t i = 0; i < cached_repos_count; i++)
+ if (strcmp(arg, cached_repos[i].source_key) == 0) return true;
+ return false;
+}
+
+Pkg pkg_from_repo(char* arg) {
+ Pkg pkg = {0};
+ for (size_t i = 0; i < cached_repos_count; i++) {
+ if (strcmp(arg, cached_repos[i].source_key) == 0) {
+ pkg.url = strdup(cached_repos[i].source_value);
+ break;
+ }
+ }
+ pkg.name = arg;
+ return pkg;
+}
+
+char* get_destdir(char* cwd, char* arg) {
+ char dest_dir[MAX_PATH_LEN];
+ if (arg[0] == '.') {
+ snprintf(dest_dir, sizeof(dest_dir), "%s/%s",
+ get_install_dir("src"), name_from_url(cwd));
+ } else {
+ snprintf(dest_dir, sizeof(dest_dir), "%s/%s",
+ get_install_dir("src"), arg);
+ }
+ return strdup(dest_dir);
+}
+
+void rmdotgit(Pkg pkg) {
+ if (strlen(pkg.name) > 4 &&
+ strncmp(pkg.name + strlen(pkg.name) - 4, ".git", 4) == 0)
+ pkg.name[strlen(pkg.name) - 4] = '\0';
+}
+
+char* get_pkgsrc(Pkg pkg) {
+ char src_dir[MAX_PATH_LEN];
+ if (pkg.is_local) {
+ snprintf(src_dir, sizeof(src_dir), "%s/%s",
+ get_install_dir("src"), pkg.name);
+ } else {
+ snprintf(src_dir, sizeof(src_dir), "%s/%s/%s",
+ get_install_dir("src"), pkg.name, pkg.ver);
+ }
+ return strdup(src_dir);
+}
+
Pkg create_pkg(const char *arg) {
Pkg pkg = {0};
pkg.ver = "HEAD";
pkg.is_local = false;
+ char cwd[MAX_PATH_LEN];
+ getcwd(cwd, MAX_PATH_LEN);
+ char *new_arg = strdup(arg);
+ if (!new_arg) exit(EXIT_FAILURE);
+ char* dest_dir = get_destdir(cwd, new_arg);
+ bool is_installed_locally = false;
+ if (is_directory(dest_dir)) is_installed_locally = true;
+ char *verptr = strchr(new_arg, '@');
+ char *trgptr = strchr(new_arg, ',');
for (int i = 0; i < cached_repos_count; i++) {
- if (strcmp(cached_repos[i].source_key, arg) == 0) {
- pkg.ver = cached_repos[i].version;
- }
+ if (strcmp(cached_repos[i].source_key, new_arg) != 0) continue;
+ pkg.ver = cached_repos[i].version;
}
-
- char *new_arg = strdup(arg);
- if (!new_arg) exit(EXIT_FAILURE);
- char *argver = strchr(new_arg, '@');
- char *argtrg = strchr(new_arg, ',');
- if (argver) {
- pkg.ver = argver + 1;
- *argver = '\0';
+ if (verptr) {
+ pkg.ver = verptr + 1;
+ *verptr = '\0';
}
- if (argtrg) {
- pkg.target = argtrg + 1;
- *argtrg = '\0';
+ if (trgptr) {
+ pkg.target = trgptr + 1;
+ *trgptr = '\0';
} else {
pkg.target = "default";
}
bool is_in_repos = false;
for (size_t i = 0; i < cached_repos_count; i++) {
- if (strcmp(new_arg, cached_repos[i].source_key) == 0) {
+ if (strcmp(arg, cached_repos[i].source_key) == 0) {
is_in_repos = true;
break;
}
}
- cache_install_directories();
- bool is_installed_locally = false;
- char dest_dir[MAX_PATH_LEN];
- if (new_arg[0] == '.') {
- char cwd[MAX_PATH_LEN];
- getcwd(cwd, MAX_PATH_LEN);
- snprintf(dest_dir, sizeof(dest_dir), "%s/%s",
- get_install_dir("src"), name_from_url(cwd));
- } else {
- snprintf(dest_dir, sizeof(dest_dir), "%s/%s",
- get_install_dir("src"), new_arg);
- }
- if (is_directory(dest_dir)) is_installed_locally = true;
-
if (strncmp(new_arg, "http", 4) == 0) {
pkg.url = strdup(new_arg);
pkg.name = name_from_url(new_arg);
} else if (strcmp(new_arg, ".") == 0) {
pkg.url = "";
- char cwd[MAX_PATH_LEN];
- getcwd(cwd, MAX_PATH_LEN);
pkg.name = name_from_url(cwd);
pkg.is_local = true;
} else if (is_in_repos) {
@@ -73,7 +107,7 @@ Pkg create_pkg(const char *arg) {
break;
}
}
- pkg.name = strdup(new_arg);
+ pkg.name = new_arg;
} else if (is_installed_locally) {
pkg.url = "";
pkg.name = name_from_url(dest_dir);
@@ -82,20 +116,7 @@ Pkg create_pkg(const char *arg) {
printf("%s '%s' is not a valid package\n", print_error, new_arg);
exit(EXIT_FAILURE);
}
-
- if (strlen(pkg.name) > 4 &&
- strncmp(pkg.name + strlen(pkg.name) - 4, ".git", 4) == 0)
- pkg.name[strlen(pkg.name) - 4] = '\0';
-
- char src_dir[MAX_PATH_LEN];
- if (pkg.is_local) {
- snprintf(src_dir, sizeof(src_dir), "%s/%s",
- get_install_dir("src"), pkg.name);
- } else {
- snprintf(src_dir, sizeof(src_dir), "%s/%s/%s",
- get_install_dir("src"), pkg.name, pkg.ver);
- }
- snprintf(pkg.src, MAX_PATH_LEN, "%s", src_dir);
-
+ rmdotgit(pkg);
+ snprintf(pkg.src, MAX_PATH_LEN, "%s", get_pkgsrc(pkg));
return pkg;
} \ No newline at end of file