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
|
/*
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 <dirent.h>
#include <string.h>
#include "globs.h"
#include "is_updated.h"
#include "log.h"
#include "pkgit_lua.h"
bool on_pkg_update(lua_State *L, package_t *pkg) {
lua_getglobal(L, "repositories");
if (!lua_istable(L, -1)) {
lua_pop(L, 1);
return false;
}
if (!lua_try_table(L, "init.lua", pkg->name.data)) {
lua_pop(L, 2);
return false;
}
if (!lua_try_table(L, "init.lua", "targets")) {
lua_pop(L, 3);
return false;
}
if (!lua_try_table(L, "init.lua", pkg->target.data)) {
lua_pop(L, 4);
return false;
}
if (!lua_try_function(L, "init.lua", "on_update")) {
lua_pop(L, 4);
return false;
}
lua_pop(L, 4);
return true;
}
void pkg_update(package_t *pkg) {
if (is_updated(&pkg->src)) {
if (flags.verbose) log_info(
"%.*s is already up to date.",
str_fmt(&pkg->name)
);
return;
}
flags.force = true;
str_copy_cstr_into(&pkg->version, "HEAD");
pkg_install(pkg);
if (!on_pkg_update(L, pkg)) log_warn(
"init.lua: 'repositories.%.*s.%.*s.on_update' function failed",
str_fmt(&pkg->name), str_fmt(&pkg->target)
);
pkg_free(pkg);
}
bool on_all_update(lua_State *L) {
char* fname = "on_update";
char* lua_file = "init.lua";
lua_getglobal(L, fname);
if (!lua_isfunction(L, -1)) {
if (flags.verbose) lua_isnt_type(fname, "function");
lua_pop(L, 1);
} else if (lua_pcall(L, 0, 1, 0) != LUA_OK) {
if (flags.verbose) log_warn(
"%s: '%s' function borked: %s",
lua_file, fname, lua_tostring(L, -1)
);
lua_pop(L, 1);
return false;
}
if (!lua_isnumber(L, -1) || lua_tonumber(L, -1) != 0) {
if (flags.verbose) log_warn(
"%s: '%s' failed: %s",
lua_file, fname, lua_tostring(L, -1)
);
lua_pop(L, 1);
return false;
}
lua_pop(L, 1);
return true;
}
void all_update(void) {
struct dirent* dirent_ptr;
DIR* dir_ptr;
if ((dir_ptr = opendir(inst_dirs.src.data)) == NULL) {
log_error("could not open %s", str_fmt(&inst_dirs.src));
}
while ((dirent_ptr = readdir(dir_ptr)) != NULL) {
if (
strcmp(dirent_ptr->d_name, "..") == 0 ||
strcmp(dirent_ptr->d_name, ".") == 0
) continue;
str pkg_name = mstr(dirent_ptr->d_name);
if (!pkg_exists(&pkg_name)) {
str_free(&pkg_name);
continue;
}
package_t pkg = pkg_create(&pkg_name);
pkg_update(&pkg);
pkg_free(&pkg);
if (str_is_valid(&pkg_name)) str_free(&pkg_name);
}
closedir(dir_ptr);
if (!on_all_update(L))
log_warn("init.lua: 'on_update' function failed");
}
|