-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.lua
More file actions
1280 lines (1176 loc) · 32.2 KB
/
Copy pathrun.lua
File metadata and controls
1280 lines (1176 loc) · 32.2 KB
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
plugin = {}
local PLUGIN_ID = "mcpm"
local PLUGIN_NAME = "MCPM"
local PLUGIN_VERSION = "0.1.0"
local REQUIRED_BINARY = "mcpm"
local PACKAGE_TYPE = "mcp-server"
local JSON_NULL = {}
local function trim(value)
return (tostring(value or ""):gsub("^%s+", ""):gsub("%s+$", ""))
end
local function lower(value)
return tostring(value or ""):lower()
end
local function non_empty(value)
local text = trim(value)
if text == "" then
return nil
end
return text
end
local function shell_quote(value)
return "'" .. tostring(value or ""):gsub("'", "'\\''") .. "'"
end
local function read_field(value, key)
if value == nil then
return nil
end
local ok, result = pcall(function()
return value[key]
end)
if ok then
return result
end
return nil
end
local function json_value(value)
if value == JSON_NULL then
return nil
end
return value
end
local function first_nonempty(...)
for index = 1, select("#", ...) do
local value = json_value(select(index, ...))
if value ~= nil then
if type(value) == "string" then
local text = trim(value)
if text ~= "" then
return text
end
else
return value
end
end
end
return nil
end
local function emit_event(context, name, payload)
if context == nil or context.events == nil then
return
end
local fn = context.events[name]
if type(fn) == "function" then
fn(payload)
end
end
local function begin_step(context, label)
if context == nil or context.tx == nil then
return
end
local fn = context.tx.begin_step
if type(fn) == "function" then
fn(label)
end
end
local function tx_success(context)
if context == nil or context.tx == nil then
return
end
local fn = context.tx.success
if type(fn) == "function" then
fn()
end
end
local function tx_failed(context, message)
if context == nil or context.tx == nil then
return
end
local fn = context.tx.failed
if type(fn) == "function" then
fn(message)
end
end
local function log_warn(context, message)
if context == nil or context.log == nil then
return
end
local fn = context.log.warn
if type(fn) == "function" then
fn(message)
end
end
local function exec_run(context, command)
if context ~= nil and context.exec ~= nil and type(context.exec.run) == "function" then
return context.exec.run(command)
end
if reqpack ~= nil and reqpack.exec ~= nil and type(reqpack.exec.run) == "function" then
return reqpack.exec.run(command)
end
return {
success = false,
stdout = "",
stderr = "missing ReqPack exec runner",
exitCode = 127,
}
end
local function command_exists(context, binary)
local result = exec_run(context, "command -v " .. shell_quote(binary) .. " >/dev/null 2>&1")
return result ~= nil and result.success == true
end
local function command_error(context, message, payload)
tx_failed(context, message)
if payload ~= nil then
emit_event(context, "unavailable", payload)
end
return false
end
local function ensure_available(context, fatal)
if command_exists(context, REQUIRED_BINARY) then
return true
end
local message = "mcpm command not available"
if fatal then
tx_failed(context, message)
emit_event(context, "unavailable", { reason = "mcpm-not-installed" })
else
log_warn(context, message)
end
return false
end
local function starts_with(value, prefix)
local text = tostring(value or "")
local expected = tostring(prefix or "")
return expected == "" or text:sub(1, #expected) == expected
end
local function join_path(...)
local parts = {}
for index = 1, select("#", ...) do
local raw = tostring(select(index, ...) or "")
if raw ~= "" then
raw = raw:gsub("/+$", "")
if #parts > 0 then
raw = raw:gsub("^/+", "")
end
parts[#parts + 1] = raw
end
end
if #parts == 0 then
return "."
end
return table.concat(parts, "/")
end
local function read_proc_environ_value(name)
if os ~= nil and type(os.getenv) == "function" then
local current = os.getenv(tostring(name or ""))
if current ~= nil and current ~= "" then
return trim(current)
end
end
local handle = io.open("/proc/self/environ", "rb")
if handle == nil then
return nil
end
local payload = handle:read("*a")
handle:close()
if payload == nil or payload == "" then
return nil
end
local prefix = tostring(name or "") .. "="
for entry in tostring(payload):gmatch("[^%z]+") do
if starts_with(entry, prefix) then
return trim(entry:sub(#prefix + 1))
end
end
return nil
end
local function runtime_expand(value)
local text = trim(value)
local fixture_root = non_empty(read_proc_environ_value("REQPACK_TEST_FIXTURE_ROOT"))
if fixture_root ~= nil then
text = text:gsub("%${fixtureRoot}", fixture_root)
end
return text
end
local function object_flags(value)
local raw = read_field(value, "flags")
if type(raw) ~= "table" and type(raw) ~= "userdata" then
return {}
end
local flags = {}
local ok = pcall(function()
for _, item in ipairs(raw) do
local text = runtime_expand(item)
if text ~= "" then
flags[#flags + 1] = text
end
end
end)
if not ok then
return {}
end
return flags
end
local function normalize_flags(flags)
local result = {}
if type(flags) == "table" then
for _, flag in ipairs(flags) do
local text = runtime_expand(flag)
if text ~= "" then
result[#result + 1] = text
end
end
elseif type(flags) == "string" then
local text = runtime_expand(flags)
if text ~= "" then
result[#result + 1] = text
end
end
return result
end
local function request_flags(context)
local flags = object_flags(context)
if #flags > 0 then
return flags
end
local request = read_field(context, "request") or {}
return normalize_flags(read_field(request, "flags"))
end
local function flag_value(flags, prefix)
local wanted = prefix .. "="
for _, flag in ipairs(flags or {}) do
if flag == prefix then
return true
end
if flag:sub(1, #wanted) == wanted then
return trim(flag:sub(#wanted + 1))
end
end
return nil
end
local function get_reqpack_path(name)
local paths = type(reqpack) == "table" and read_field(reqpack, "paths") or nil
if type(paths) ~= "table" then
return nil
end
return non_empty(read_field(paths, name))
end
local function xdg_root(kind)
if kind == "data" then
return non_empty(read_proc_environ_value("XDG_DATA_HOME")) or join_path(non_empty(read_proc_environ_value("HOME")) or "/home/test", ".local", "share")
end
if kind == "cache" then
return non_empty(read_proc_environ_value("XDG_CACHE_HOME")) or join_path(non_empty(read_proc_environ_value("HOME")) or "/home/test", ".cache")
end
return non_empty(read_proc_environ_value("XDG_CONFIG_HOME")) or join_path(non_empty(read_proc_environ_value("HOME")) or "/home/test", ".config")
end
local function reqpack_root(kind, context)
local override = nil
if kind == "data" then
override = flag_value(request_flags(context), "data-root")
elseif kind == "cache" then
override = flag_value(request_flags(context), "cache-root")
else
override = flag_value(request_flags(context), "config-root")
end
if type(override) == "string" and trim(override) ~= "" then
return trim(override)
end
local fixture_root = non_empty(read_proc_environ_value("REQPACK_TEST_FIXTURE_ROOT"))
if fixture_root ~= nil then
if kind == "data" then
return join_path(fixture_root, ".reqpack-data")
end
if kind == "cache" then
return join_path(fixture_root, ".reqpack-cache")
end
return join_path(fixture_root, ".reqpack-config")
end
if kind == "data" then
return get_reqpack_path("dataRoot") or join_path(xdg_root("data"), "reqpack")
end
if kind == "cache" then
return get_reqpack_path("cacheRoot") or join_path(xdg_root("cache"), "reqpack")
end
return get_reqpack_path("configRoot") or join_path(xdg_root("config"), "reqpack")
end
local function mcpm_roots(context)
local data_root = join_path(reqpack_root("data", context), PLUGIN_ID)
local cache_root = join_path(reqpack_root("cache", context), PLUGIN_ID)
local config_root = join_path(reqpack_root("config", context), PLUGIN_ID)
return {
dataRoot = data_root,
cacheRoot = cache_root,
configRoot = config_root,
mcpmDataDir = join_path(data_root, "data"),
mcpmCacheDir = join_path(cache_root, "cache"),
mcpmConfigDir = join_path(config_root, "config"),
}
end
local function env_prefix(context)
local roots = mcpm_roots(context)
return table.concat({
"NO_COLOR=1",
"LC_ALL=C",
"LANG=C",
"LANGUAGE=C",
"MCPM_NON_INTERACTIVE=true",
"MCPM_FORCE=true",
"MCPM_JSON_OUTPUT=true",
"MCPM_CONFIG_DIR=" .. shell_quote(roots.mcpmConfigDir),
"XDG_CACHE_HOME=" .. shell_quote(roots.mcpmCacheDir),
"XDG_DATA_HOME=" .. shell_quote(roots.mcpmDataDir),
}, " ")
end
local function with_env(context, command)
return env_prefix(context) .. " " .. command
end
local function append_arg(parts, value)
local text = trim(value)
if text ~= "" then
parts[#parts + 1] = shell_quote(text)
end
end
local function join_command(parts)
return table.concat(parts, " ")
end
local function package_name(package)
local name = non_empty(read_field(package, "name"))
if name ~= nil then
return name
end
return non_empty(read_field(package, "packageId"))
end
local function package_action(package)
return lower(first_nonempty(read_field(package, "action"), "install"))
end
local function codepoint_to_utf8(codepoint)
if codepoint <= 0x7F then
return string.char(codepoint)
end
if codepoint <= 0x7FF then
return string.char(0xC0 + math.floor(codepoint / 0x40), 0x80 + (codepoint % 0x40))
end
if codepoint <= 0xFFFF then
return string.char(
0xE0 + math.floor(codepoint / 0x1000),
0x80 + (math.floor(codepoint / 0x40) % 0x40),
0x80 + (codepoint % 0x40)
)
end
return string.char(
0xF0 + math.floor(codepoint / 0x40000),
0x80 + (math.floor(codepoint / 0x1000) % 0x40),
0x80 + (math.floor(codepoint / 0x40) % 0x40),
0x80 + (codepoint % 0x40)
)
end
local function decode_json_internal(raw)
local text = tostring(raw or "")
local length = #text
local index = 1
local parse_value
local function decode_error(message)
error(message .. " at byte " .. tostring(index), 0)
end
local function peek()
return text:sub(index, index)
end
local function skip_whitespace()
while index <= length do
local byte = text:byte(index)
if byte == 32 or byte == 9 or byte == 10 or byte == 13 then
index = index + 1
else
break
end
end
end
local function parse_string()
index = index + 1
local parts = {}
local segment_start = index
while index <= length do
local current = text:sub(index, index)
if current == '"' then
parts[#parts + 1] = text:sub(segment_start, index - 1)
index = index + 1
return table.concat(parts)
end
if current == "\\" then
parts[#parts + 1] = text:sub(segment_start, index - 1)
index = index + 1
if index > length then
decode_error("unterminated escape sequence")
end
local escape = text:sub(index, index)
if escape == '"' or escape == "\\" or escape == "/" then
parts[#parts + 1] = escape
index = index + 1
elseif escape == "b" then
parts[#parts + 1] = "\b"
index = index + 1
elseif escape == "f" then
parts[#parts + 1] = "\f"
index = index + 1
elseif escape == "n" then
parts[#parts + 1] = "\n"
index = index + 1
elseif escape == "r" then
parts[#parts + 1] = "\r"
index = index + 1
elseif escape == "t" then
parts[#parts + 1] = "\t"
index = index + 1
elseif escape == "u" then
local hex = text:sub(index + 1, index + 4)
if #hex ~= 4 or hex:match("^[0-9a-fA-F]+$") == nil then
decode_error("invalid unicode escape")
end
parts[#parts + 1] = codepoint_to_utf8(tonumber(hex, 16))
index = index + 5
else
decode_error("unsupported escape sequence")
end
segment_start = index
else
local byte = text:byte(index)
if byte ~= nil and byte < 32 then
decode_error("control character in string")
end
index = index + 1
end
end
decode_error("unterminated string")
end
local function parse_number()
local start_index = index
if peek() == "-" then
index = index + 1
end
local current = peek()
if current == "0" then
index = index + 1
elseif current:match("%d") then
repeat
index = index + 1
current = peek()
until current == "" or current:match("%d") == nil
else
decode_error("invalid number")
end
if peek() == "." then
index = index + 1
if peek():match("%d") == nil then
decode_error("invalid fraction")
end
repeat
index = index + 1
current = peek()
until current == "" or current:match("%d") == nil
end
current = peek()
if current == "e" or current == "E" then
index = index + 1
current = peek()
if current == "+" or current == "-" then
index = index + 1
end
if peek():match("%d") == nil then
decode_error("invalid exponent")
end
repeat
index = index + 1
current = peek()
until current == "" or current:match("%d") == nil
end
return tonumber(text:sub(start_index, index - 1))
end
local function parse_array()
index = index + 1
skip_whitespace()
local result = {}
if peek() == "]" then
index = index + 1
return result
end
while true do
result[#result + 1] = parse_value()
skip_whitespace()
local current = peek()
if current == "," then
index = index + 1
skip_whitespace()
elseif current == "]" then
index = index + 1
return result
else
decode_error("expected ',' or ']' in array")
end
end
end
local function parse_object()
index = index + 1
skip_whitespace()
local result = {}
if peek() == "}" then
index = index + 1
return result
end
while true do
if peek() ~= '"' then
decode_error("expected string key")
end
local key = parse_string()
skip_whitespace()
if peek() ~= ":" then
decode_error("expected ':' after key")
end
index = index + 1
local value = parse_value()
if value == nil then
result[key] = JSON_NULL
else
result[key] = value
end
skip_whitespace()
local current = peek()
if current == "," then
index = index + 1
skip_whitespace()
elseif current == "}" then
index = index + 1
return result
else
decode_error("expected ',' or '}' in object")
end
end
end
function parse_value()
skip_whitespace()
local current = peek()
if current == '"' then
return parse_string()
elseif current == "{" then
return parse_object()
elseif current == "[" then
return parse_array()
elseif current == "-" or current:match("%d") then
return parse_number()
elseif text:sub(index, index + 3) == "true" then
index = index + 4
return true
elseif text:sub(index, index + 4) == "false" then
index = index + 5
return false
elseif text:sub(index, index + 3) == "null" then
index = index + 4
return nil
end
decode_error("unexpected token")
end
local value = parse_value()
skip_whitespace()
if index <= length then
decode_error("unexpected trailing content")
end
return value
end
local function decode_json(raw)
local ok, value = pcall(decode_json_internal, raw)
if not ok then
return nil, value
end
return value, nil
end
local function is_array(value)
if type(value) ~= "table" then
return false
end
local count = 0
for key, _ in pairs(value) do
if type(key) ~= "number" then
return false
end
count = count + 1
end
return count == #value
end
local function is_record_like(value)
if type(value) ~= "table" then
return false
end
local keys = {
"name",
"server_name",
"serverName",
"id",
"description",
"summary",
"command",
"url",
"type",
"version",
"installed",
"source",
}
for _, key in ipairs(keys) do
if read_field(value, key) ~= nil then
return true
end
end
return false
end
local function collect_records(value, out, key_hint)
if value == nil or value == JSON_NULL then
return
end
local value_type = type(value)
if value_type == "string" then
out[#out + 1] = { name = value, __key = key_hint }
return
end
if value_type ~= "table" then
return
end
if is_array(value) then
for _, item in ipairs(value) do
collect_records(item, out, key_hint)
end
return
end
if is_record_like(value) then
value.__key = key_hint
out[#out + 1] = value
return
end
local preferred = { "servers", "installed", "items", "results", "data", "updates", "outdated", "available" }
local matched = false
for _, key in ipairs(preferred) do
local child = read_field(value, key)
if child ~= nil then
matched = true
collect_records(child, out, key_hint)
end
end
if matched then
return
end
for key, child in pairs(value) do
if type(child) == "table" then
collect_records(child, out, tostring(key))
elseif type(key) == "string" and (type(child) == "string" or type(child) == "boolean") then
out[#out + 1] = { name = key, status = tostring(child) }
end
end
end
local function raw_records(raw)
local parsed, err = decode_json(raw)
if parsed == nil then
return nil, err
end
local records = {}
collect_records(parsed, records, nil)
return records, nil
end
local function copy_extra(record)
local extra = {}
local keys = {
"source",
"profiles",
"clients",
"command",
"args",
"env",
"url",
"headers",
"alias",
"aliases",
"transport",
"registry",
"package",
"repository",
"homepage",
}
for _, key in ipairs(keys) do
local value = json_value(read_field(record, key))
if value ~= nil then
extra[key] = value
end
end
local key_hint = json_value(read_field(record, "__key"))
if key_hint ~= nil then
extra.key = key_hint
end
return extra
end
local function normalize_record(record, defaults)
defaults = defaults or {}
if type(record) == "string" then
record = { name = record }
end
if type(record) ~= "table" then
return nil
end
local name = first_nonempty(
read_field(record, "name"),
read_field(record, "server_name"),
read_field(record, "serverName"),
read_field(record, "id"),
read_field(record, "package"),
read_field(record, "title"),
read_field(record, "__key")
)
if name == nil then
return nil
end
local installed = read_field(record, "installed")
if installed == JSON_NULL or installed == nil then
installed = defaults.installed
end
local version = first_nonempty(
read_field(record, "version"),
read_field(record, "current_version"),
read_field(record, "currentVersion"),
read_field(record, "installed_version"),
read_field(record, "installedVersion"),
read_field(record, "current")
)
local latest = first_nonempty(
read_field(record, "latest_version"),
read_field(record, "latestVersion"),
read_field(record, "latest"),
read_field(record, "target_version"),
read_field(record, "targetVersion")
)
local description = first_nonempty(read_field(record, "summary"), read_field(record, "description"), defaults.description)
local status = first_nonempty(read_field(record, "status"), defaults.status)
if status == nil then
status = installed == true and "installed" or "available"
end
return {
name = name,
packageId = first_nonempty(read_field(record, "packageId"), read_field(record, "package_id"), name),
version = version,
latestVersion = latest,
packageType = PACKAGE_TYPE,
type = "package",
installed = installed,
status = status,
summary = description,
description = description,
extraFields = copy_extra(record),
}
end
local function normalize_records(records, defaults)
local items = {}
for _, record in ipairs(records or {}) do
local item = normalize_record(record, defaults)
if item ~= nil then
items[#items + 1] = item
end
end
table.sort(items, function(left, right)
return lower(left.name or "") < lower(right.name or "")
end)
return items
end
local function query_items(context, command, defaults)
local result = exec_run(context, with_env(context, command))
if result == nil or not result.success then
return nil, "mcpm query failed"
end
local records, err = raw_records(result.stdout or "")
if records == nil then
return nil, "mcpm JSON parse failed: " .. tostring(err)
end
return normalize_records(records, defaults), nil
end
local function query_list_items(context)
return query_items(context, "mcpm ls --verbose", {
installed = true,
status = "installed",
description = "Installed MCP server",
})
end
local function query_outdated_items(context)
return query_items(context, "mcpm update --check", {
installed = true,
status = "outdated",
description = "Outdated MCP server",
})
end
local function query_search_items(context, prompt)
local command = "mcpm search"
if non_empty(prompt) ~= nil then
command = command .. " " .. shell_quote(prompt)
end
return query_items(context, command, {
installed = false,
status = "available",
description = "Available MCP server",
})
end
local function query_info_item(context, name)
local items, err = query_items(context, "mcpm info " .. shell_quote(name), {
installed = false,
status = "available",
description = "MCP server",
})
if items == nil then
return nil, err
end
return items[1], nil
end
local function installed_index(items)
local installed = {}
for _, item in ipairs(items or {}) do
installed[lower(item.name or "")] = item
end
return installed
end
local function add_install_flags(parts, package)
local alias = nil
for _, flag in ipairs(object_flags(package)) do
if flag == "force" then
parts[#parts + 1] = "--force"
else
local key, value = flag:match("^([^=]+)=(.+)$")
if key == "alias" then
alias = value
end
end
end
if alias ~= nil and trim(alias) ~= "" then
parts[#parts + 1] = "--alias"
append_arg(parts, alias)
end
end
local function add_update_flags(parts, package)
for _, flag in ipairs(object_flags(package)) do
if flag == "rebase" then
parts[#parts + 1] = "--rebase"
elseif flag == "init" then
parts[#parts + 1] = "--init"
elseif flag == "force" then
parts[#parts + 1] = "--force"
end
end
end
local function make_requested_item(package, installed, status)
local name = package_name(package)
return {
name = name,
packageId = name,
version = non_empty(read_field(package, "version")),
packageType = PACKAGE_TYPE,
type = "package",
installed = installed,
status = status,
}
end
function plugin.getName()
return PLUGIN_NAME
end
function plugin.getVersion()
return PLUGIN_VERSION
end
function plugin.getRequirements()
return {}
end
function plugin.getCategories()
return { "Development", "Package Manager", "MCP" }
end
function plugin.getMissingPackages(packages)
if packages == nil or #packages == 0 then
return {}
end
if not ensure_available(nil, false) then
return packages
end
local list_items = select(1, query_list_items(nil))
if list_items == nil then
return packages
end
local installed = installed_index(list_items)
local outdated = nil
for _, package in ipairs(packages) do
if package_action(package) == "update" then
outdated = installed_index(select(1, query_outdated_items(nil)) or {})
break
end
end
local missing = {}
for _, package in ipairs(packages) do
local name = package_name(package)
if name ~= nil then
local action = package_action(package)
local has_installed = installed[lower(name)] ~= nil
if action == "remove" then
if has_installed then
missing[#missing + 1] = package
end
elseif action == "update" then
if outdated ~= nil and outdated[lower(name)] ~= nil then
missing[#missing + 1] = package
end
elseif not has_installed then
missing[#missing + 1] = package
end
end
end