forked from vladimir-kotikov/clink-completions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scoop.lua
338 lines (286 loc) · 7.59 KB
/
scoop.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
-- -*- coding: utf-8 -*-
-- preamble: common routines
local JSON = require("JSON")
local matchers = require("matchers")
local path = require("path")
local w = require("tables").wrap
local concat = require("funclib").concat
local parser = clink.arg.new_parser
local function scoop_folder()
local folder = os.getenv("SCOOP")
if not folder then
folder = os.getenv("home") .. "\\scoop"
end
return folder
end
local function scoop_global_folder()
local folder = os.getenv("SCOOP_GLOBAL")
if not folder then
folder = os.getenv("ProgramData") .. "\\scoop"
end
return folder
end
local function scoop_load_config() -- luacheck: no unused args
local file = io.open(os.getenv("home") .. "\\.config\\scoop\\config.json")
-- If there is no such file, then close handle and return
if file == nil then
return w()
end
-- Read the whole file contents
local contents = file:read("*a")
file:close()
-- strip UTF-8-BOM
local utf8_len = contents:len()
local pat_start, _ = string.find(contents, "{")
contents = contents:sub(pat_start, utf8_len)
local data = JSON:decode(contents)
if data == nil then
return w()
end
return data
end
local function scoop_alias_list(token) -- luacheck: no unused args
local data = scoop_load_config()
return w(data.alias):keys()
end
local function scoop_config_list(token) -- luacheck: no unused args
local data = scoop_load_config()
return w(data):keys()
end
local function scoop_bucket_known_list(token) -- luacheck: no unused args
local file = io.open(scoop_folder() .. "\\apps\\scoop\\current\\buckets.json")
-- If there is no such file, then close handle and return
if file == nil then
return w()
end
-- Read the whole file contents
local contents = file:read("*a")
file:close()
local data = JSON:decode(contents)
return w(data):keys()
end
local function scoop_bucket_list(token)
local finder = matchers.create_files_matcher(scoop_folder() .. "\\buckets\\*")
local list = finder(token)
return list:filter(path.is_real_dir)
end
local function scoop_apps_list(token)
local folders = {scoop_folder(), scoop_global_folder()}
local list = w()
for _, folder in pairs(folders) do
local finder = matchers.create_files_matcher(folder .. "\\apps\\*")
local new_list = finder(token)
list = w(concat(list, new_list))
end
return list:filter(path.is_real_dir)
end
local function scoop_available_apps_list(token)
-- search in default bucket
local finder = matchers.create_files_matcher(scoop_folder() .. "\\apps\\scoop\\current\\bucket\\*.json")
local list = finder(token)
-- search in each installed bucket
local buckets = scoop_bucket_list("")
for _, bucket in pairs(buckets) do
local bucket_folder = scoop_folder() .. "\\buckets\\" .. bucket
-- check the bucket folder exists
if clink.is_dir(bucket_folder .. "\\bucket") then
bucket_folder = bucket_folder .. "\\bucket"
end
local b_finder = matchers.create_files_matcher(bucket_folder .. "\\*.json")
local b_list = b_finder(token)
list = w(concat(list, b_list))
end
-- remove ".json" of file name
for k, v in pairs(list) do
list[k] = v:gsub(".json", "")
end
return list
end
local function scoop_cache_apps_list(token)
local cache_folder = os.getenv("SCOOP_CACHE")
if not cache_folder then
cache_folder = scoop_folder() .. "\\cache"
end
local finder = matchers.create_files_matcher(cache_folder .. "\\*")
local list = finder(token)
list = w(list:filter(path.is_real_dir))
-- get name before "#" from cache list (name#version#url)
for k, v in pairs(list) do
list[k] = v:gsub("#.*$", "")
end
return list
end
local scoop_default_flags = {
"--help",
"-h"
}
local scoop_alias_parser =
parser(
{
"add",
"list" .. parser("-v", "--verbose"),
"rm" .. parser({scoop_alias_list})
}
)
local scoop_bucket_parser =
parser(
{
"add" .. parser({scoop_bucket_known_list}),
"list",
"known",
"rm" .. parser({scoop_bucket_list})
}
)
local scoop_cache_parser =
parser(
{
"show" .. parser({scoop_cache_apps_list, scoop_apps_list, "*"}),
"rm" .. parser({scoop_cache_apps_list, "*"})
}
)
local scoop_cleanup_parser =
parser(
{
scoop_apps_list,
"*"
},
"--global",
"-g",
"--cache",
"-k"
):loop(1)
local scoop_config_parser =
parser(
{
"rm" .. parser({scoop_config_list}),
scoop_config_list,
"aria2-enabled" .. parser({"true", "false"}),
"aria2-max-connection-per-server",
"aria2-min-split-size",
"aria2-options",
"aria2-retry-wait",
"aria2-split",
"debug" .. parser({"true", "false"}),
"proxy",
"show_update_log" .. parser({"true", "false"}),
"virustotal_api_key"
}
)
local scoop_uninstall_parser =
parser(
{
scoop_apps_list
},
"--global",
"-g",
"--purge",
"-p"
):loop(1)
local scoop_update_parser =
parser(
{
scoop_apps_list,
"*"
},
"--force",
"-f",
"--global",
"-g",
"--independent",
"-i",
"--no-cache",
"-k",
"--skip",
"-s",
"--quiet",
"-q"
):loop(1)
local scoop_install_parser =
parser(
{scoop_available_apps_list},
"--global",
"-g",
"--independent",
"-i",
"--no-cache",
"-k",
"--skip",
"-s",
"--arch" .. parser({"32bit", "64bit"}),
"-a" .. parser({"32bit", "64bit"})
):loop(1)
local scoop_help_parser =
parser(
{
"alias",
"bucket",
"cache",
"checkup",
"cleanup",
"config",
"create",
"depends",
"export",
"help",
"home",
"info",
"install",
"list",
"prefix",
"reset",
"search",
"status",
"uninstall",
"update",
"virustotal",
"which"
},
"/?",
"--help",
"-h",
"--version"
)
local scoop_parser = parser()
scoop_parser:set_flags(scoop_default_flags)
scoop_parser:set_arguments(
{
scoop_alias_list,
"alias" .. scoop_alias_parser,
"bucket" .. scoop_bucket_parser,
"cache" .. scoop_cache_parser,
"checkup",
"cleanup" .. scoop_cleanup_parser,
"config" .. scoop_config_parser,
"create",
"depends" ..
parser(
{scoop_available_apps_list, scoop_apps_list},
"--arch" .. parser({"32bit", "64bit"}),
"-a" .. parser({"32bit", "64bit"})
),
"export",
"help" .. scoop_help_parser,
"home" .. parser({scoop_available_apps_list, scoop_apps_list}),
"info" .. parser({scoop_available_apps_list, scoop_apps_list}),
"install" .. scoop_install_parser,
"list",
"prefix" .. parser({scoop_apps_list}),
"reset" .. parser({scoop_apps_list}):loop(1),
"search",
"status",
"uninstall" .. scoop_uninstall_parser,
"update" .. scoop_update_parser,
"virustotal" ..
parser(
{scoop_apps_list, "*"},
"--arch" .. parser({"32bit", "64bit"}),
"-a" .. parser({"32bit", "64bit"}),
"--scan",
"-s",
"--no-depends",
"-n"
):loop(1),
"which"
}
)
clink.arg.register_parser("scoop", scoop_parser)