-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
212 lines (174 loc) · 7.6 KB
/
init.lua
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
-- mod-version:3
-----------------------------------------------------------------------
local core = require("core")
local command = require("core.command")
local os = require("os")
local utils = require("lixling/utils")
local lixlog = require("lixling/lixlog")
local download = require("lixling/download")
local update = require("lixling/update")
local USERDIR = USERDIR or {}
-----------------------------------------------------------------------
local plugins_list = {} -- list of the plugins found in the init.lua
local plugins_path = nil
local function plugins(...)
local param_num = select("#", ...)
-- single parameter
if param_num == 1 then
local param1 = ...
if type(param1) ~= "table" then
lixlog.err("Invalid parameter: expected table.")
return
end
plugins_path = USERDIR .. "/plugins/"
plugins_list = param1
-- two parameters
elseif param_num == 2 then
local param1, param2 = ...
if type(param1) ~= "string" or type(param2) ~= "table" then
lixlog.err("Invalid parameters: expected (string, table).")
return
end
plugins_path = param1
plugins_list = param2
else
lixlog.err("Invalid number of parameters: expected 1 or 2.")
end
end
-- Looks for unlisted files in the plugins dir
local function clear_plugins()
lixlog.clear("Running the clearing process. Please wait.")
local dir_plugs = utils.dir_lookup(plugins_path)
local clear_list = {}
local clear_size = 0
for f in ipairs(dir_plugs) do
-- If plugin ends with `.lua` AND is in list
if utils.string_ends_with(dir_plugs[f], ".lua") and plugins_list[dir_plugs[f]:sub(1, -5)] == nil then
table.insert(clear_list, dir_plugs[f])
clear_size = clear_size + 1
lixlog.clear("File '" .. dir_plugs[f] .. "' added to exile list.")
-- If plugin ends with `/` AND is in list
elseif utils.string_ends_with(dir_plugs[f], "/") and plugins_list[dir_plugs[f]:sub(1, -2)] == nil then
table.insert(clear_list, dir_plugs[f])
clear_size = clear_size + 1
lixlog.clear("Folder '" .. dir_plugs[f] .. "' added to exile list.")
end
end
-- Handles clearing user input
if clear_size > 0 then
core.command_view:enter(
"LIXLING CLEAR: Found " .. clear_size .. " unlisted plugins, exile them? ([y]es/[N]o/[d]elete)",
{
submit = function(input)
if string.lower(input) == "y" or string.lower(input) == "yes" then
for plug in ipairs(clear_list) do
lixlog.clear("Moving: '" .. clear_list[plug] .. "'.")
utils.mkdir(USERDIR .. "/lixling/exiled")
os.rename(
plugins_path .. clear_list[plug],
USERDIR .. "/lixling/exiled/" .. clear_list[plug]
)
end
lixlog.clear(
clear_size .. " plugins exiled. You can find them in '" .. USERDIR .. "/lixling/exiled'."
)
elseif string.lower(input) == "d" or string.lower(input) == "delete" then
core.command_view:enter("LIXLING CLEAR: Are you sure? Deleting can't be undone. ([y]es/[N]o)", {
submit = function(delete)
if string.lower(delete) == "y" or string.lower(delete) == "yes" then
for plug in ipairs(clear_list) do
lixlog.clear("Deleting: '" .. clear_list[plug] .. "'.")
--os.remove(plugins_path .. clear_list[plug])
utils.rmrf(plugins_path .. clear_list[plug])
end
end
lixlog.clear(clear_size .. " plugins deleted. They can't be retrieved.")
end,
})
end
end,
}
)
return 0
end
lixlog.clear("No unlisted plugins found.")
end
-----------------------------------------------------------------------
-- DOWNLOADING
-----------------------------------------------------------------------
local function download_plugins()
lixlog.install("", "Running the install process. Please wait.")
local dir_plugs = utils.dir_lookup(plugins_path)
utils.mkdir(plugins_path)
core.add_thread(function()
download.set_plugins_path(plugins_path)
for plug in pairs(plugins_list) do
-- single file + default git branch
if #plugins_list[plug] == 1 then
download.raw(dir_plugs, plugins_list, plug)
download.repo(plugins_list, plug)
-- git branch handling
elseif #plugins_list[plug] == 2 then
download.repo(plugins_list, plug, plugins_list[plug][2])
-- git branch + hook handle
elseif (#plugins_list[plug] == 3) and (#plugins_list[plug][2] ~= 0) then
download.repo(plugins_list, plug, plugins_list[plug][2])
-- if in old srting format
elseif type(plugins_list[plug]) == "string" then
local dummy = { [plug] = { plugins_list[plug] } }
download.raw(dir_plugs, dummy, plug)
download.repo(dummy, plug)
end
end
lixlog.install("", "All plugins have been downloaded.")
end)
end
-----------------------------------------------------------------------
-- UPDATING
-----------------------------------------------------------------------
local function update_plugins()
lixlog.update("", "Running the update process. Please wait.")
local dir_plugs = utils.dir_lookup(plugins_path)
core.add_thread(function()
for plug in pairs(plugins_list) do
-- single file + default git branch
if #plugins_list[plug] == 1 then
update.raw(dir_plugs, plugins_list, plug)
update.repo(plugins_list, plug)
-- git branch handling
elseif #plugins_list[plug] == 2 then
update.repo(plugins_list, plug, plugins_list[plug] == 2)
-- git branch + hook handle
elseif (#plugins_list[plug] == 3) and (#plugins_list[plug][2] ~= 0) then
update.repo(plugins_list, plug, plugins_list[plug][2])
-- if in old srting format
elseif type(plugins_list[plug]) == "string" then
local dummy = { [plug] = { plugins_list[plug] } }
update.raw(dir_plugs, dummy, plug)
update.repo(dummy, plug)
end
end
lixlog.update("", "All plugins are up to date.")
end)
end
-- Upgrades self
local function upgrade_self()
lixlog.upgrade("Running the upgrade process. Please wait.")
core.add_thread(function()
local status = utils.git_pull(USERDIR .. "/lixling/")
--core.log(status)
if status ~= "Already up to date." then
lixlog.upgrade("Upgraded to the latest version.")
return 0
end
lixlog.upgrade("Already up to date.")
end)
end
-----------------------------------------------------------------------
command.add(nil, {
["lixling:install"] = download_plugins,
["lixling:update"] = update_plugins,
["lixling:upgrade"] = upgrade_self,
["lixling:clear"] = clear_plugins,
})
return { plugins = plugins }