-
Notifications
You must be signed in to change notification settings - Fork 83
/
scoop.lua
493 lines (430 loc) · 9.77 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
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 get_home_dir()
return os.getenv("home") or os.getenv("USERPROFILE")
end
local function scoop_load_config() -- luacheck: no unused args
local file = io.open(get_home_dir() .. "\\.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_folder()
local folder = os.getenv("SCOOP")
if not folder then
local config = scoop_load_config()
if config and config.root_path then
folder = config.root_path
else
folder = get_home_dir() .. "\\scoop"
end
end
return folder
end
local function scoop_global_folder()
local folder = os.getenv("SCOOP_GLOBAL")
if not folder then
local config = scoop_load_config()
if config and config.global_path then
folder = config.global_path
else
folder = os.getenv("ProgramData") .. "\\scoop"
end
end
return folder
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 arch_parser =
parser(
{
"32bit",
"64bit",
"arm64",
}
)
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, "*"}, "--all", "-a")
}
)
local scoop_cat_parser =
parser(
{
scoop_available_apps_list,
scoop_apps_list
}
)
local scoop_cleanup_parser =
parser(
{
scoop_apps_list,
"*"
},
"--all",
"-a",
"--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_depends_parser =
parser(
{
scoop_available_apps_list,
scoop_apps_list
},
"--arch" .. arch_parser,
"-a" .. arch_parser
)
local scoop_download_parser =
parser(
{
scoop_available_apps_list
},
"--arch" .. arch_parser,
"-a" .. arch_parser,
"--force",
"-f",
"--no-hash-check",
"-h",
"--no-update-scoop",
"-u"
)
local scoop_export_parser =
parser(
"--config",
"-c"
):nofiles()
local scoop_hold_parser =
parser(
{
scoop_apps_list
},
"--global",
"-g"
)
local scoop_home_parser =
parser(
{
scoop_available_apps_list,
scoop_apps_list
}
)
local scoop_info_parser =
parser(
{
scoop_available_apps_list,
scoop_apps_list
},
"--verbose",
"-v"
)
local scoop_install_parser =
parser(
{
scoop_available_apps_list
},
"--global",
"-g",
"--independent",
"-i",
"--no-cache",
"-k",
"--no-update-scoop",
"-u",
"--skip",
"-s",
"--arch" .. arch_parser,
"-a" .. arch_parser
):loop(1)
local scoop_prefix_parser =
parser(
{
scoop_apps_list
}
)
local scoop_reset_parser =
parser(
{
scoop_apps_list,
"*",
},
"--all",
"-a"
):loop(1)
local shim_subcommand_parser =
parser(
"--global",
"-g"
)
local scoop_shim_parser =
parser(
{
"add" .. shim_subcommand_parser,
"rm" .. shim_subcommand_parser,
"list" .. shim_subcommand_parser,
"info" .. shim_subcommand_parser,
"alter" .. shim_subcommand_parser,
}
)
local scoop_status_parser =
parser(
"--local",
"-l"
)
local scoop_unhold_parser =
parser(
{
scoop_apps_list
},
"--global",
"-g"
)
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",
"--all",
"-a"
):loop(1)
local scoop_virustotal_parser =
parser(
{
scoop_apps_list,
"*"
},
"--all",
"-a",
"--scan",
"-s",
"--no-depends",
"-n",
"--no-update-scoop",
"-u",
"--passthru",
"-p"
):loop(1)
local scoop_help_parser =
parser(
{
"alias",
"bucket",
"cache",
"cat",
"checkup",
"cleanup",
"config",
"create",
"depends",
"download",
"export",
"help",
"home",
"hold",
"import",
"info",
"install",
"list",
"prefix",
"reset",
"search",
"shim",
"status",
"unhold",
"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,
"cat" .. scoop_cat_parser,
"checkup",
"cleanup" .. scoop_cleanup_parser,
"config" .. scoop_config_parser,
"create",
"depends" .. scoop_depends_parser,
"download" .. scoop_download_parser,
"export" .. scoop_export_parser,
"help" .. scoop_help_parser,
"hold" .. scoop_hold_parser,
"home" .. scoop_home_parser,
"import",
"info" .. scoop_info_parser,
"install" .. scoop_install_parser,
"list",
"prefix" .. scoop_prefix_parser,
"reset" .. scoop_reset_parser,
"search",
"shim" .. scoop_shim_parser,
"status" .. scoop_status_parser,
"unhold" .. scoop_unhold_parser,
"uninstall" .. scoop_uninstall_parser,
"update" .. scoop_update_parser,
"virustotal" .. scoop_virustotal_parser,
"which"
}
)
clink.arg.register_parser("scoop", scoop_parser)