aboutsummaryrefslogtreecommitdiff
path: root/src/pkg_create.c
blob: cf5170f0c503cf969a54ca08fcb4400d4b51a393 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*

	pkgit - package_t it!

	Copyright (C) 2026 dacctal
	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <https://www.gnu.org/licenses/>. */

#include <string.h>
#include <unistd.h>

#include "pkg.h"

#include "files.h"
#include "globs.h"
#include "log.h"
#include "pkgit_lua.h"
#include "str.h"

static str get_destdir(str *cwd, str *arg) {
	str result;
	if (str_first(arg) == '.' && arg->len == 1) {
		result = str_format("%.*s/%.*s", str_fmt(&inst_dirs.src), str_fmt(cwd));
	} else {
		str name;
		if (str_find_char(arg, '/') != 0) name = str_from_after_delim(arg, '/');
		else str_copy_into(&name, arg);
		result = str_format("%.*s/%.*s", str_fmt(&inst_dirs.src), str_fmt(&name));
		str_free(&name);
	}
	return result;
}

static str get_pkgsrc(package_t pkg) {
	if (pkg.is_local == 1)
		return str_format("%.*s/%.*s", str_fmt(&inst_dirs.src), pkg.name);
	else
		return str_format("%.*s/%.*s/%.*s", str_fmt(&inst_dirs.src),
			str_fmt(&pkg.name), str_fmt(&pkg.version));
}

static void assign_pkg_version(package_t *pkg, str *new_arg_str) {
	if (str_find_char(new_arg_str, '@')) {
		str tmp_arg = str_from_after_delim(new_arg_str, '@');
		if (str_find_char(new_arg_str, ',')) {
			str tmp_trg = str_from_after_delim(&tmp_arg, ',');
			pkg->version = str_from_after_delim(new_arg_str, '@');
			pkg->version.len -= tmp_trg.len;
			str_free(&tmp_trg);
		} else {
			pkg->version = str_from_after_delim(new_arg_str, '@');
		}
		if (str_find_char(&pkg->version, ',')) pkg->version.len--;
		str_free(&tmp_arg);
		pkg->name.len -= pkg->version.len + 1;
	} else pkg->version = mstr("HEAD");
}

static void assign_pkg_target(package_t *pkg, str *new_arg_str) {
	if (str_find_char(new_arg_str, ',')) {
		str tmp_arg = str_from_after_delim(new_arg_str, ',');
		if (str_find_char(new_arg_str, '@')) {
			str tmp_ver = str_from_after_delim(&tmp_arg, '@');
			pkg->target = str_from_after_delim(new_arg_str, ',');
			pkg->target.len -= tmp_ver.len;
			str_free(&tmp_ver);
		} else {
			pkg->target = str_from_after_delim(new_arg_str, ',');
		}
		if (str_find_char(&pkg->target, '@')) pkg->target.len--;
		str_free(&tmp_arg);
		pkg->name.len -= pkg->target.len + 1;
	} else if (!flags.verbose) {
		pkg->target = mstr("quiet");
	} else pkg->target = mstr("default");
}

package_t pkg_create(str *arg) {
	package_t pkg = { .is_local = false, };

	assign_pkg_version(&pkg, arg);
	assign_pkg_target(&pkg, arg);
	if (str_find_char(arg, '@')) {
		str tmp = str_from_after_delim(arg, '@');
		arg->len -= tmp.len + 1;
		str_free(&tmp);
	} if (str_find_char(arg, ',')) {
		str tmp = str_from_after_delim(arg, ',');
		arg->len -= tmp.len + 1;
		str_free(&tmp);
	}

	char cwd[MAX_PATH_LEN];
	getcwd(cwd, MAX_PATH_LEN);
	str cwd_str = mstr(cwd);
	str dest_dir = get_destdir(&cwd_str, arg);
	bool is_installed_locally = is_directory(dest_dir.data);

	if (strncmp(arg->data, "http", 4) == 0 || strncmp(arg->data, "ssh", 3) == 0) {
		str_copy_into(&pkg.url, arg);
		pkg.name = str_from_after_delim(arg, '/');
	} else if (str_equal_cstr(arg, ".")) {
		str_copy_cstr_into(&pkg.url, "");
		pkg.name = str_from_after_delim(&cwd_str, '/');
		pkg.is_local = true;
	} else if (pkg_exists(arg)) {
		str_copy_into(&pkg.name, arg);
		pkg.url = pkg_get_url(&pkg.name);
	} else if (is_installed_locally) {
		str_copy_cstr_into(&pkg.url, "");
		pkg.name = str_from_after_delim(&dest_dir, '/');
		pkg.is_local = true;
	} else {
		log_error("'%.*s' is not a valid package", str_fmt(arg));
		str_free(&cwd_str);
		str_free(&dest_dir);
		str_free(arg);
		free_vars();
		exit(EXIT_FAILURE);
	}

	if (str_find_char(arg, ',')
		&& str_find_char(arg, '@')
	) pkg.name.len--;
	pkg.src = get_pkgsrc(pkg);

	str_free(&cwd_str);
	str_free(&dest_dir);
	if (pkg.is_local) str_free(arg);
	return pkg;
}