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
|
/*
pkgit - package 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 <stdio.h>
#include <string.h>
#include "common.h"
#include "log.h"
#include "lua_state.h"
#include "pkg.h"
#include "str.h"
#include "state.h"
bool pkg_exists(str *name) {
lua_get_field_type("pkgs", table, &config.init_path, 1);
lua_get_field_type(name->data, table, &config.init_path, 2);
lua_pop(L, 2);
return true;
}
bool pkg_hook_url(package_t *pkg) {
lua_get_field_type("pkgs", table, &config.init_path, 1);
lua_get_field_type(pkg->name.data, table, &config.init_path, 2);
lua_get_field_type("url", string, &config.init_path, 3);
if (!lua_isstring(L, -1)) return false;
return true;
}
str pkg_get_url(package_t *pkg) {
str url = {0};
if (!pkg_hook_url(pkg)) {
lua_pop(L, 3);
return url;
}
const char *url_cstr = lua_tostring(L, -1);
url = mstr(url_cstr);
lua_pop(L, 3);
return url;
}
bool pkg_hook_fetch_sources(package_t *pkg) {
lua_get_field_type("pkgs", table, &config.init_path, 1);
lua_get_field_type(pkg->name.data, table, &config.init_path, 2);
lua_get_field_type("fetcher", table, &config.init_path, 3);
lua_get_field_type("get_source", function, &config.init_path, 4);
lua_pushstring(L, pkg->url.data);
lua_pushstring(L, pkg->version.data);
lua_pushstring(L, pkg->name.data);
lua_run_function("get_source", &config.init_path, 3, 1, 4);
if (!lua_isnumber(L, -1) || lua_tonumber(L, -1) != 0) {
lua_pop(L, 4);
return false;
}
lua_pop(L, 4);
return true;
}
bool pkg_hook_fetch_latest_version(package_t *pkg) {
lua_get_field_type("pkgs", table, &config.init_path, 1);
lua_get_field_type(pkg->name.data, table, &config.init_path, 2);
lua_get_field_type("fetcher", table, &config.init_path, 3);
lua_get_field_type("get_version", function, &config.init_path, 4);
lua_pushstring(L, pkg->url.data);
lua_run_function("get_version", &config.init_path, 1, 1, 4);
if (!lua_isstring(L, -1)) {
lua_pop(L, 4);
return false;
}
return true;
}
str pkg_get_latest_version(package_t *pkg) {
str version = str_new();
if (!pkg_hook_fetch_latest_version(pkg))
return version;
if (lua_isstring(L, -1))
version = mstr(lua_tostring(L, -1));
lua_pop(L, 4);
return version;
}
bool pkg_hook_recipe(char *recipe, package_t *pkg) {
lua_get_field_type("pkgs", table, &config.init_path, 1);
lua_get_field_type(pkg->name.data, table, &config.init_path, 2);
lua_get_field_type("recipe", table, &config.init_path, 3);
lua_get_field_type(recipe, function, &config.init_path, 4);
lua_run_function(recipe, &config.init_path, 0, 1, 4);
if (!lua_isnumber(L, -1) || lua_tonumber(L, -1) != 0) {
lua_pop(L, 4);
return false;
}
lua_pop(L, 4);
return true;
}
bool pkg_hook_setup(package_t *pkg) {
pkg_hook_recipe("setup", pkg);
return true;
}
bool pkg_hook_build(package_t *pkg) {
pkg_hook_recipe("build", pkg);
return true;
}
bool pkg_hook_install(package_t *pkg) {
pkg_hook_recipe("install", pkg);
return true;
}
bool pkg_hook_remove(package_t *pkg) {
pkg_hook_recipe("remove", pkg);
return true;
}
bool pkg_hook_on_update(package_t *pkg) {
pkg_hook_recipe("on_update", pkg);
return true;
}
|