-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.lua
More file actions
1147 lines (1019 loc) · 33.6 KB
/
Copy pathrun.lua
File metadata and controls
1147 lines (1019 loc) · 33.6 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 = "spago"
local PLUGIN_NAME = "Spago"
local PLUGIN_VERSION = "0.1.0"
local REQUIRED_BINARY = "spago"
local JSON_NULL = {}
local cached_json_decoder = nil
local function trim(value)
return (tostring(value or ""):gsub("^%s+", ""):gsub("%s+$", ""))
end
local function lower(value)
return string.lower(tostring(value or ""))
end
local function shell_quote(value)
return "'" .. tostring(value or ""):gsub("'", "'\\''") .. "'"
end
local function is_safe_token(value)
return tostring(value or ""):match("^[A-Za-z0-9_./:@%%+=,-]+$") ~= nil
end
local function shell_arg(value)
local text = tostring(value or "")
if is_safe_token(text) then
return text
end
return shell_quote(text)
end
local function split_lines(value)
local lines = {}
for line in tostring(value or ""):gmatch("[^\r\n]+") do
lines[#lines + 1] = line
end
return lines
end
local function shallow_copy(value)
local copy = {}
for key, item in pairs(value or {}) do
copy[key] = item
end
return copy
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 merge_extra_fields(existing, additions)
local merged = shallow_copy(type(existing) == "table" and existing or {})
for key, value in pairs(additions or {}) do
merged[key] = value
end
if next(merged) == nil then
return nil
end
return merged
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 = select(index, ...)
if value ~= nil and value ~= JSON_NULL 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 as_string_array(value)
local items = {}
if type(value) ~= "table" then
return items
end
for _, item in ipairs(value) do
local text = first_nonempty(item)
if text ~= nil then
items[#items + 1] = tostring(text)
end
end
return items
end
local function sorted_keys(value)
local keys = {}
for key in pairs(value or {}) do
keys[#keys + 1] = key
end
table.sort(keys, function(left, right)
return tostring(left) < tostring(right)
end)
return keys
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 call_logger(context, level, message)
if context == nil or context.log == nil then
return
end
local fn = context.log[level]
if type(fn) == "function" then
fn(message)
end
end
local function run_global(command)
if reqpack == nil or reqpack.exec == nil or type(reqpack.exec.run) ~= "function" then
return {
success = false,
stdout = "",
stderr = "reqpack.exec.run unavailable",
}
end
return reqpack.exec.run(command)
end
local function make_exec_runner(context)
if context ~= nil and context.exec ~= nil and type(context.exec.run) == "function" then
return function(command)
return context.exec.run(command)
end
end
return run_global
end
local function command_exists(context, binary)
local exec_runner = make_exec_runner(context)
local result = exec_runner("command -v " .. shell_quote(binary) .. " >/dev/null 2>&1")
return result ~= nil and result.success == true
end
local function build_command(parts)
local args = {}
for _, part in ipairs(parts or {}) do
args[#args + 1] = shell_arg(part)
end
return table.concat(args, " ")
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 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 parse_value
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
local current = peek()
if current == "-" then
index = index + 1
end
current = peek()
if current == "0" then
index = index + 1
elseif current:match("%d") ~= nil then
while peek():match("%d") ~= nil do
index = index + 1
end
else
decode_error("invalid number")
end
current = peek()
if current == "." then
index = index + 1
if peek():match("%d") == nil then
decode_error("invalid decimal number")
end
while peek():match("%d") ~= nil do
index = index + 1
end
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
while peek():match("%d") ~= nil do
index = index + 1
end
end
local number = tonumber(text:sub(start_index, index - 1))
if number == nil then
decode_error("invalid numeric value")
end
return number
end
local function parse_array()
local items = {}
index = index + 1
skip_whitespace()
if peek() == "]" then
index = index + 1
return items
end
while true do
items[#items + 1] = parse_value()
skip_whitespace()
local current = peek()
if current == "," then
index = index + 1
skip_whitespace()
elseif current == "]" then
index = index + 1
return items
else
decode_error("expected ',' or ']' in array")
end
end
end
local function parse_object()
local object = {}
index = index + 1
skip_whitespace()
if peek() == "}" then
index = index + 1
return object
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
skip_whitespace()
object[key] = parse_value()
skip_whitespace()
local current = peek()
if current == "," then
index = index + 1
skip_whitespace()
elseif current == "}" then
index = index + 1
return object
else
decode_error("expected ',' or '}' in object")
end
end
end
function parse_value()
skip_whitespace()
local current = peek()
if current == '"' then
return parse_string()
end
if current == "{" then
return parse_object()
end
if current == "[" then
return parse_array()
end
if current == "-" or current:match("%d") ~= nil then
return parse_number()
end
if text:sub(index, index + 3) == "true" then
index = index + 4
return true
end
if text:sub(index, index + 4) == "false" then
index = index + 5
return false
end
if text:sub(index, index + 3) == "null" then
index = index + 4
return JSON_NULL
end
decode_error("unexpected token")
end
local value = parse_value()
skip_whitespace()
if index <= length then
decode_error("trailing content")
end
return value
end
local function normalize_json_tree(value, null_sentinel, seen)
if null_sentinel ~= nil and value == null_sentinel then
return JSON_NULL
end
if type(value) ~= "table" then
return value
end
seen = seen or {}
if seen[value] ~= nil then
return seen[value]
end
local normalized = {}
seen[value] = normalized
for key, item in pairs(value) do
normalized[key] = normalize_json_tree(item, null_sentinel, seen)
end
return normalized
end
local function load_json_decoder()
if cached_json_decoder ~= nil then
return cached_json_decoder
end
local candidates = {
function()
local ok, module = pcall(require, "cjson.safe")
if ok and module ~= nil and type(module.decode) == "function" then
return function(raw)
local value, err = module.decode(raw)
if err ~= nil then
error(err, 0)
end
return normalize_json_tree(value, module.null)
end
end
end,
function()
local ok, module = pcall(require, "cjson")
if ok and module ~= nil and type(module.decode) == "function" then
return function(raw)
return normalize_json_tree(module.decode(raw), module.null)
end
end
end,
function()
local ok, module = pcall(require, "dkjson")
if ok and module ~= nil and type(module.decode) == "function" then
return function(raw)
local value, _, err = module.decode(raw, 1, nil)
if err ~= nil then
error(err, 0)
end
return normalize_json_tree(value, module.null)
end
end
end,
function()
local ok, module = pcall(require, "lunajson")
if ok and module ~= nil and type(module.decode) == "function" then
return function(raw)
return normalize_json_tree(module.decode(raw), module.null)
end
end
end,
function()
local ok, module = pcall(require, "json")
if ok and module ~= nil then
local decode = module.decode or module.Decode
if type(decode) == "function" then
return function(raw)
return normalize_json_tree(decode(raw), module.null)
end
end
end
end,
}
for _, candidate in ipairs(candidates) do
local ok, decoder = pcall(candidate)
if ok and type(decoder) == "function" then
cached_json_decoder = decoder
return cached_json_decoder
end
end
cached_json_decoder = decode_json_internal
return cached_json_decoder
end
local function decode_json(raw)
local decoder = load_json_decoder()
local ok, value = pcall(decoder, raw)
if not ok then
return nil, trim(value)
end
if value == nil then
return nil, "json decode returned nil"
end
return value, nil
end
local function run_json_command(context, parts)
local exec_runner = make_exec_runner(context)
local command = build_command(parts)
local result = exec_runner(command)
if result == nil then
return nil, "command failed", command, nil
end
if not result.success then
return nil, first_nonempty(result.stderr, result.stdout, "command failed"), command, result
end
local decoded, err = decode_json(result.stdout)
if decoded == nil then
return nil, first_nonempty(err, "failed to decode JSON"), command, result
end
return decoded, nil, command, result
end
local function package_name_from_request(pkg)
return first_nonempty(read_field(pkg, "name"), read_field(pkg, "packageId"), read_field(pkg, "packageName"))
end
local function package_version_from_request(pkg)
return first_nonempty(read_field(pkg, "version"), read_field(pkg, "latestVersion"))
end
local function compiler_list_string(value)
local items = as_string_array(value)
if #items == 0 then
return nil
end
return table.concat(items, ",")
end
local function extract_location_urls(location)
if type(location) ~= "table" then
return nil, nil
end
local repository = first_nonempty(read_field(location, "url"))
if repository ~= nil then
return repository, repository
end
local owner = first_nonempty(read_field(location, "githubOwner"))
local repo = first_nonempty(read_field(location, "githubRepo"))
if owner ~= nil and repo ~= nil then
local url = "https://github.com/" .. owner .. "/" .. repo
return url, url
end
return nil, nil
end
local function highest_version_key(map)
local keys = sorted_keys(map)
return keys[#keys]
end
local function latest_published_entry(metadata)
local published = json_value(read_field(metadata, "published"))
if type(published) ~= "table" then
return nil, nil
end
local version = highest_version_key(published)
if version == nil then
return nil, nil
end
return version, json_value(published[version])
end
local function package_sets_for_version(package_sets_payload, version)
if type(package_sets_payload) ~= "table" or trim(version) == "" then
return nil
end
local matches = {}
for _, item in ipairs(package_sets_payload) do
if type(item) == "table" then
local set_version = first_nonempty(read_field(item, "version"))
local packages = json_value(read_field(item, "packages"))
if set_version ~= nil and type(packages) == "table" then
for package_name, package_version in pairs(packages) do
if trim(package_name) ~= "" and trim(package_version) == trim(version) then
matches[#matches + 1] = tostring(set_version)
end
end
end
end
end
if #matches == 0 then
return nil
end
table.sort(matches)
return table.concat(matches, ",")
end
local function map_metadata_to_package_info(name, metadata, options)
options = options or {}
local location = json_value(read_field(metadata, "location"))
local repository, homepage = extract_location_urls(location)
local version, published = latest_published_entry(metadata)
local package_sets = package_sets_for_version(options.packageSets, version)
local path_info = type(options.paths) == "table" and options.paths or nil
local item = {
name = name,
packageId = name,
version = version,
latestVersion = version,
installed = options.installed == true,
status = options.status or (options.installed == true and "installed" or "available"),
summary = first_nonempty(options.summary, "PureScript package"),
description = first_nonempty(options.description, "PureScript package metadata from Spago Registry"),
homepage = homepage,
repository = repository,
packageType = options.packageType or "registry-package",
type = "package",
extraFields = nil,
}
local extra = {}
if published ~= nil then
local published_time = first_nonempty(read_field(published, "publishedTime"))
local compilers = compiler_list_string(read_field(published, "compilers"))
local ref = first_nonempty(read_field(published, "ref"))
local hash = first_nonempty(read_field(published, "hash"))
if published_time ~= nil then
extra.publishedAt = published_time
end
if compilers ~= nil then
extra.compilerRange = compilers
end
if ref ~= nil then
extra.sourceRef = ref
end
if hash ~= nil then
extra.integrity = hash
end
end
local published_versions = json_value(read_field(metadata, "published"))
if type(published_versions) == "table" then
extra.registryVersions = table.concat(sorted_keys(published_versions), ",")
end
if package_sets ~= nil then
extra.packageSets = package_sets
end
if path_info ~= nil then
extra.globalCachePath = first_nonempty(read_field(path_info, "Global cache path"))
extra.globalRegistryPath = first_nonempty(read_field(path_info, "Global registry path"))
extra.localCachePath = first_nonempty(read_field(path_info, "Local cache path"))
extra.localCachePackagesPath = first_nonempty(read_field(path_info, "Local cache packages path"))
end
item.extraFields = next(extra) ~= nil and extra or nil
return item
end
local function package_type_and_location(entry)
local package_type = first_nonempty(read_field(entry, "type"))
local value = json_value(read_field(entry, "value"))
if package_type == "registry" then
local version = first_nonempty(type(value) == "table" and read_field(value, "version") or value)
return "registry-package", version, nil
end
if package_type == "git" then
return "git-package", first_nonempty(type(value) == "table" and read_field(value, "ref")), type(value) == "table" and read_field(value, "git") or nil
end
if package_type == "local" then
return "local-package", "local", type(value) == "table" and read_field(value, "path") or nil
end
if package_type == "workspace" then
return "workspace-package", "workspace", type(value) == "table" and read_field(value, "path") or nil
end
return first_nonempty(package_type, "package"), nil, nil
end
local function map_ls_entry_to_package_info(name, entry, paths)
local package_type, version, location = package_type_and_location(entry)
local item = {
name = name,
packageId = name,
version = version,
latestVersion = version,
installed = true,
status = "installed",
summary = first_nonempty(name .. " dependency"),
description = "Spago workspace dependency",
repository = location,
packageType = package_type,
type = "package",
extraFields = nil,
}
local extra = {}
if type(paths) == "table" then
extra.globalCachePath = first_nonempty(read_field(paths, "Global cache path"))
extra.globalRegistryPath = first_nonempty(read_field(paths, "Global registry path"))
extra.localCachePath = first_nonempty(read_field(paths, "Local cache path"))
extra.localCachePackagesPath = first_nonempty(read_field(paths, "Local cache packages path"))
end
if location ~= nil then
extra.location = location
end
item.extraFields = next(extra) ~= nil and extra or nil
return item
end
local function decode_map_entries(decoded)
local items = {}
if type(decoded) ~= "table" then
return items
end
for _, key in ipairs(sorted_keys(decoded)) do
items[#items + 1] = { name = tostring(key), value = decoded[key] }
end
return items
end
local function get_paths(context)
local decoded = select(1, run_json_command(context, { REQUIRED_BINARY, "ls", "paths", "--json" }))
if type(decoded) == "table" then
return decoded
end
return nil
end
local function get_dependency_entries(context)
local decoded, err = run_json_command(context, { REQUIRED_BINARY, "ls", "deps", "--json", "--transitive" })
if decoded ~= nil then
return decode_map_entries(decoded), nil
end
decoded = select(1, run_json_command(context, { REQUIRED_BINARY, "ls", "packages", "--json" }))
if decoded ~= nil then
return decode_map_entries(decoded), nil
end
return {}, err
end
local function registry_search(context, prompt)
local decoded, err = run_json_command(context, { REQUIRED_BINARY, "registry", "search", "--json", prompt })
if decoded == nil then
return {}, err
end
local items = {}
for _, entry in ipairs(decode_map_entries(decoded)) do
local name = entry.name
local value = json_value(entry.value)
local version = first_nonempty(type(value) == "table" and read_field(value, "version") or nil)
local published = first_nonempty(type(value) == "table" and read_field(value, "publishedTime") or nil)
items[#items + 1] = {
name = name,
packageId = name,
version = version,
latestVersion = version,
installed = false,
status = "available",
summary = "PureScript registry package",
description = "PureScript registry package",
packageType = "registry-package",
type = "package",
extraFields = published ~= nil and { publishedAt = published } or nil,
}
end
return items, nil
end
local function registry_info(context, name)
local decoded, err = run_json_command(context, { REQUIRED_BINARY, "registry", "info", "--json", name })
if decoded == nil then
return nil, err
end
local paths = get_paths(context)
local package_sets = select(1, run_json_command(context, { REQUIRED_BINARY, "registry", "package-sets", "--json" }))
return map_metadata_to_package_info(name, decoded, {
installed = false,
status = "available",
summary = "PureScript registry package",
description = "PureScript package metadata from Spago Registry",
packageType = "registry-package",
paths = paths,
packageSets = package_sets,
}), nil
end
local function request_action(pkg)
return lower(first_nonempty(read_field(pkg, "action"), "install"))
end
local function missing_due_to_action(pkg, installed_lookup)
local name = package_name_from_request(pkg)
local version = package_version_from_request(pkg)
local action = request_action(pkg)
local installed = installed_lookup[name]
if action == "remove" then
return installed ~= nil
end
if action == "update" then
return true
end
if installed == nil then
return true
end
if version ~= nil and trim(installed.version) ~= trim(version) then
return true
end
return false
end
plugin.fileExtensions = {}
function plugin.getName()
return PLUGIN_NAME
end
function plugin.getVersion()
return PLUGIN_VERSION
end
function plugin.getRequirements()
return {}
end
function plugin.getCategories()
return { "PureScript", "Package Manager", "Wrapper" }
end
function plugin.getMissingPackages(packages)
local requested = packages or {}
if #requested == 0 then
return {}
end
if not command_exists(nil, REQUIRED_BINARY) then
local missing = {}
for _, pkg in ipairs(requested) do
local action = request_action(pkg)
if action ~= "remove" then
missing[#missing + 1] = pkg
end
end
return missing
end
local entries = select(1, get_dependency_entries(nil))
local installed_lookup = {}
for _, entry in ipairs(entries) do
installed_lookup[entry.name] = map_ls_entry_to_package_info(entry.name, entry.value)
end
local missing = {}
for _, pkg in ipairs(requested) do
if missing_due_to_action(pkg, installed_lookup) then
missing[#missing + 1] = pkg
end
end
return missing
end
function plugin.install(context, packages)
local requested = packages or {}
if #requested == 0 then
return true
end
if not command_exists(context, REQUIRED_BINARY) then
tx_failed(context, "spago not installed")
emit_event(context, "unavailable", { reason = "spago-not-installed" })
return false
end
local parts = { REQUIRED_BINARY, "install" }
for _, pkg in ipairs(requested) do
local name = package_name_from_request(pkg)
if name ~= nil then
parts[#parts + 1] = name
end
end
begin_step(context, "install spago dependencies")
local result = make_exec_runner(context)(build_command(parts))
if result == nil or not result.success then
tx_failed(context, first_nonempty(result and result.stderr, result and result.stdout, "spago install failed"))
return false
end
emit_event(context, "installed", requested)
tx_success(context)
return true
end
function plugin.installLocal(context, path)
begin_step(context, "install local spago artifact")
tx_failed(context, "spago local installs are not supported")
emit_event(context, "unavailable", {
path = path,
reason = "local-install-unsupported",
})
return false
end
function plugin.remove(context, packages)
local requested = packages or {}
if #requested == 0 then
return true
end
if not command_exists(context, REQUIRED_BINARY) then
tx_failed(context, "spago not installed")
emit_event(context, "unavailable", { reason = "spago-not-installed" })
return false
end
local parts = { REQUIRED_BINARY, "uninstall" }
for _, pkg in ipairs(requested) do
local name = package_name_from_request(pkg)
if name ~= nil then
parts[#parts + 1] = name
end
end
begin_step(context, "remove spago dependencies")