-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.lua
More file actions
1908 lines (1784 loc) · 60.7 KB
/
Copy pathrun.lua
File metadata and controls
1908 lines (1784 loc) · 60.7 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 = "vim-plug"
local PLUGIN_NAME = "vim-plug"
local PLUGIN_VERSION = "0.1.0"
local PLUG_URL = "https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim"
local function trim(value)
return (tostring(value or ""):gsub("^%s+", ""):gsub("%s+$", ""))
end
local function lower(value)
return trim(value):lower()
end
local function non_empty(value)
local text = trim(value)
if text == "" then
return nil
end
return text
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 shell_quote(value)
return "'" .. tostring(value or ""):gsub("'", "'\\''") .. "'"
end
local function starts_with(value, prefix)
local text = tostring(value or "")
local expected = tostring(prefix or "")
return text:sub(1, #expected) == expected
end
local function ends_with(value, suffix)
local text = tostring(value or "")
local expected = tostring(suffix or "")
if expected == "" then
return true
end
return text:sub(-#expected) == expected
end
local function split_lines(value)
local text = tostring(value or ""):gsub("\r\n", "\n"):gsub("\r", "\n")
local lines = {}
if text == "" then
return lines
end
for line in (text .. "\n"):gmatch("(.-)\n") do
if line ~= "" then
lines[#lines + 1] = line
end
end
return lines
end
local function split_csv(value)
local items = {}
for part in tostring(value or ""):gmatch("[^,]+") do
local cleaned = trim(part)
if cleaned ~= "" then
items[#items + 1] = cleaned
end
end
return items
end
local function split_tsv(line)
local fields = {}
for field in (tostring(line or "") .. "\t"):gmatch("(.-)\t") do
fields[#fields + 1] = field
end
return fields
end
local function join_path(...)
local parts = {}
for index = 1, select("#", ...) do
local part = trim(select(index, ...))
if part ~= "" then
if #parts == 0 then
parts[#parts + 1] = part:gsub("[/\\]+$", "")
else
parts[#parts + 1] = part:gsub("^[/\\]+", ""):gsub("[/\\]+$", "")
end
end
end
if #parts == 0 then
return "."
end
return table.concat(parts, "/")
end
local function dirname(path)
local cleaned = trim(path)
local parent = cleaned:match("^(.*)/[^/]+$")
if parent == nil or parent == "" then
return "."
end
return parent
end
local function basename(path)
local cleaned = trim(path)
return cleaned:match("([^/\\]+)$") or cleaned
end
local function strip_git_suffix(value)
return trim(value):gsub("%.git$", "")
end
local function sanitize_tsv(value)
return tostring(value or ""):gsub("[\t\r\n]", " ")
end
local function sort_by_name(items)
table.sort(items, function(left, right)
return lower(left.name or left.packageId or "") < lower(right.name or right.packageId or "")
end)
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 normalize_exec_result(first, second, third, fourth, fifth)
if type(first) == "table" then
if first.stdout == nil then
first.stdout = first.stdoutText or ""
end
if first.stderr == nil then
first.stderr = first.stderrText or ""
end
if first.exitCode == nil then
first.exitCode = first.exit_code or first.code or first.status
end
if first.success == nil and first.ok ~= nil then
first.success = first.ok
end
if first.success == nil and first.exitCode ~= nil then
first.success = first.exitCode == 0
end
return first
end
local result = {}
local function merge_exec_like(value)
for _, key in ipairs({ "success", "ok", "stdout", "stderr", "exitCode", "exit_code", "code", "status", "stdoutText", "stderrText" }) do
local ok, item = pcall(function()
return value[key]
end)
if ok and item ~= nil and result[key] == nil then
result[key] = item
end
end
end
local function apply_scalar(value)
local value_type = type(value)
if value_type == "table" then
for key, item in pairs(value or {}) do
result[key] = item
end
elseif value_type == "userdata" then
merge_exec_like(value)
elseif value_type == "boolean" then
if result.success == nil then
result.success = value
end
elseif value_type == "number" then
if result.exitCode == nil then
result.exitCode = value
end
elseif value_type == "string" then
if result.stdout == nil then
result.stdout = value
elseif result.stderr == nil then
result.stderr = value
end
end
end
apply_scalar(first)
apply_scalar(second)
apply_scalar(third)
apply_scalar(fourth)
apply_scalar(fifth)
if result.stdout == nil then
result.stdout = result.stdoutText or ""
end
if result.stderr == nil then
result.stderr = result.stderrText or ""
end
if result.exitCode == nil then
result.exitCode = result.exit_code or result.code or result.status
end
if result.success == nil and result.ok ~= nil then
result.success = result.ok
end
if result.success == nil and result.exitCode ~= nil then
result.success = result.exitCode == 0
end
return result
end
local function exec_run(context, command)
local first, second, third, fourth, fifth
if context ~= nil and context.exec ~= nil and type(context.exec.run) == "function" then
first, second, third, fourth, fifth = context.exec.run(command)
elseif reqpack ~= nil and reqpack.exec ~= nil and type(reqpack.exec.run) == "function" then
first, second, third, fourth, fifth = reqpack.exec.run(command)
else
first = {
success = false,
stdout = "",
stderr = "missing ReqPack exec runner",
exitCode = 127,
}
end
return normalize_exec_result(first, second, third, fourth, fifth)
end
local read_proc_environ_value
read_proc_environ_value = function(name)
if os ~= nil and type(os.getenv) == "function" then
local value = os.getenv(tostring(name or ""))
if value ~= nil and value ~= "" then
return trim(value)
end
end
if reqpack ~= nil and reqpack.host ~= nil and type(reqpack.host.env) == "table" then
local value = reqpack.host.env[name]
if value ~= nil and value ~= "" then
return trim(value)
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 entry:sub(1, #prefix) == prefix then
return trim(entry:sub(#prefix + 1))
end
end
return nil
end
local function runtime_expand(value)
local text = trim(value)
if text == "" then
return ""
end
local fixture_root = non_empty(read_proc_environ_value("REQPACK_TEST_FIXTURE_ROOT")) or ""
if fixture_root ~= "" then
text = text:gsub("%${fixtureRoot}", fixture_root)
end
if starts_with(text, "~/") then
local home = non_empty(read_proc_environ_value("HOME"))
if home ~= nil then
text = join_path(home, text:sub(3))
end
end
return text
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)
local home = non_empty(read_proc_environ_value("HOME")) or "/home/test"
if kind == "data" then
return non_empty(read_proc_environ_value("XDG_DATA_HOME")) or join_path(home, ".local", "share")
end
if kind == "cache" then
return non_empty(read_proc_environ_value("XDG_CACHE_HOME")) or join_path(home, ".cache")
end
return non_empty(read_proc_environ_value("XDG_CONFIG_HOME")) or join_path(home, ".config")
end
local function collect_flags(context, pkg)
local items = {}
local function append_flags(value)
if type(value) ~= "table" and type(value) ~= "userdata" then
return
end
local ok = pcall(function()
for _, item in ipairs(value) do
local cleaned = runtime_expand(item)
if trim(cleaned) ~= "" then
items[#items + 1] = cleaned
end
end
end)
if not ok then
return
end
end
append_flags(read_field(read_field(context, "request"), "flags"))
append_flags(read_field(context, "flags"))
append_flags(read_field(pkg, "flags"))
return items
end
local function flag_value(flags, key)
local prefix = lower(key) .. "="
for _, flag in ipairs(flags or {}) do
local cleaned = trim(flag)
if lower(cleaned):sub(1, #prefix) == prefix then
return trim(cleaned:sub(#prefix + 1))
end
end
return nil
end
local function has_flag(flags, key)
local needle = lower(trim(key))
for _, flag in ipairs(flags or {}) do
if lower(trim(flag)) == needle then
return true
end
end
return false
end
local function reqpack_root(kind, context)
local flags = collect_flags(context, nil)
local override = flag_value(flags, kind .. "-root")
if override ~= nil then
return 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 runtime_paths(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,
autoloadDir = join_path(data_root, "autoload"),
autoloadFile = join_path(data_root, "autoload", "plug.vim"),
pluggedDir = join_path(data_root, "plugged"),
tempDir = join_path(cache_root, "tmp"),
manifestFile = join_path(data_root, "manifest.tsv"),
stateFile = join_path(data_root, "state.tsv"),
remoteInfoFile = join_path(cache_root, "remote-info.tsv"),
configFile = join_path(config_root, "reqpack.vim"),
}
end
local function ensure_runtime_dirs(context)
local paths = runtime_paths(context)
local result = exec_run(context, table.concat({
"mkdir -p",
shell_quote(paths.dataRoot),
shell_quote(paths.cacheRoot),
shell_quote(paths.configRoot),
shell_quote(paths.autoloadDir),
shell_quote(paths.pluggedDir),
shell_quote(paths.tempDir),
}, " "))
return result ~= nil and result.success == true
end
local function path_exists(context, path, kind)
local cleaned = trim(path)
if cleaned == "" then
return false
end
local flag = "-e"
if kind == "dir" then
flag = "-d"
elseif kind == "file" then
flag = "-f"
end
local result = exec_run(context, "test " .. flag .. " " .. shell_quote(cleaned))
return result ~= nil and result.success == true
end
local function is_fixture_path(path)
local cleaned = trim(path)
if cleaned:find("${fixtureRoot}", 1, true) ~= nil then
return true
end
local fixture_root = non_empty(read_proc_environ_value("REQPACK_TEST_FIXTURE_ROOT"))
if fixture_root == nil then
return false
end
return starts_with(cleaned, fixture_root)
end
local function write_text_file(context, path, content)
local parent = dirname(path)
local mkdir_result = exec_run(context, "mkdir -p " .. shell_quote(parent))
if mkdir_result == nil or mkdir_result.success ~= true then
return is_fixture_path(path)
end
local handle = io.open(path, "wb")
if handle ~= nil then
handle:write(tostring(content or ""))
handle:close()
return true
end
local result = exec_run(context, "printf %s " .. shell_quote(tostring(content or "")) .. " > " .. shell_quote(path))
if result ~= nil and result.success == true then
return true
end
return is_fixture_path(path)
end
local function read_text_file(context, path)
local handle = io.open(path, "rb")
if handle ~= nil then
local content = handle:read("*a")
handle:close()
return content
end
local result = exec_run(context, "cat " .. shell_quote(path))
if result ~= nil and result.success == true then
return result.stdout
end
return nil
end
local function remove_file(context, path)
local result = exec_run(context, "rm -f " .. shell_quote(path))
return result ~= nil and result.success == true
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 package_error(context, message, payload)
tx_failed(context, message)
if payload ~= nil then
emit_event(context, "unavailable", payload)
end
return false
end
local function vim_quote(value)
return "'" .. tostring(value or ""):gsub("'", "''") .. "'"
end
local function vim_list(items)
local rendered = {}
for _, item in ipairs(items or {}) do
rendered[#rendered + 1] = vim_quote(item)
end
return "[" .. table.concat(rendered, ", ") .. "]"
end
local function is_remote_url(value)
local text = trim(value)
return text:match("^https?://") ~= nil or text:match("^[%w%._%-]+@[%w%._%-]+:") ~= nil
end
local function is_repo_shorthand(value)
local text = trim(value)
if text == "" then
return false
end
if starts_with(text, "/") or starts_with(text, "./") or starts_with(text, "../") or starts_with(text, "~/") then
return false
end
if is_remote_url(text) then
return false
end
return text:match("^[%w%._%-]+/[%w%._%-]+%.?[%w%._%-]*$") ~= nil
end
local function source_to_remote_url(source)
local text = trim(source)
if is_remote_url(text) then
return text
end
if is_repo_shorthand(text) then
return "https://github.com/" .. strip_git_suffix(text) .. ".git"
end
return nil
end
local function source_homepage(source)
local text = trim(source)
if is_repo_shorthand(text) then
return "https://github.com/" .. strip_git_suffix(text)
end
if starts_with(text, "https://") or starts_with(text, "http://") then
return strip_git_suffix(text)
end
return nil
end
local function default_name_for_source(source)
local text = trim(source)
if is_repo_shorthand(text) then
return strip_git_suffix(text:match("/([^/]+)$") or text)
end
return strip_git_suffix(basename(text))
end
local function normalize_request_source(pkg)
return non_empty(read_field(pkg, "name") or read_field(pkg, "packageId") or read_field(pkg, "source"))
end
local function parse_request_entry(context, pkg, source_kind_override)
local flags = collect_flags(context, pkg)
local requested_source = source_kind_override == "local"
and runtime_expand(read_field(pkg, "localPath") or read_field(pkg, "path") or read_field(pkg, "name") or "")
or runtime_expand(normalize_request_source(pkg) or "")
if requested_source == "" then
return nil, "missing-package-name"
end
local source_kind = source_kind_override
if source_kind == nil then
if is_remote_url(requested_source) or is_repo_shorthand(requested_source) then
source_kind = "remote"
else
source_kind = "local"
end
end
local branch = flag_value(flags, "branch")
local tag = flag_value(flags, "tag")
local commit = flag_value(flags, "commit")
local version = non_empty(read_field(pkg, "version"))
if version ~= nil and tag == nil and branch == nil and commit == nil and source_kind == "remote" then
tag = version
end
if branch ~= nil and tag ~= nil then
return nil, "conflicting-selectors"
end
if commit ~= nil then
branch = nil
tag = nil
end
local source = requested_source
local package_id = source
if source_kind == "remote" and is_repo_shorthand(source) then
source = strip_git_suffix(source)
package_id = source
end
local explicit_name = flag_value(flags, "as") or non_empty(read_field(pkg, "as"))
local name = explicit_name or default_name_for_source(source)
local custom_dir = runtime_expand(flag_value(flags, "dir") or read_field(pkg, "dir") or "")
local managed_dir = ""
local paths = runtime_paths(context)
if source_kind == "remote" then
managed_dir = non_empty(custom_dir) or join_path(paths.pluggedDir, name)
end
return {
packageId = package_id,
name = name,
sourceKind = source_kind,
source = source,
branch = branch or "",
tag = tag or "",
commit = commit or "",
rtp = runtime_expand(flag_value(flags, "rtp") or read_field(pkg, "rtp") or ""),
dir = custom_dir,
postUpdate = runtime_expand(flag_value(flags, "do") or read_field(pkg, "do") or ""),
on = table.concat(split_csv(flag_value(flags, "on") or read_field(pkg, "on") or ""), ","),
filetypes = table.concat(split_csv(flag_value(flags, "for") or read_field(pkg, "for") or ""), ","),
frozen = has_flag(flags, "frozen") and "1" or "",
managedDir = managed_dir,
homepage = source_homepage(source) or "",
status = "declared",
}, nil
end
local function parse_manifest_text(text)
local items = {}
for _, line in ipairs(split_lines(text)) do
local fields = split_tsv(line)
if trim(fields[1] or "") ~= "" then
items[#items + 1] = {
packageId = fields[1] or "",
name = fields[2] or "",
sourceKind = fields[3] or "",
source = fields[4] or "",
branch = fields[5] or "",
tag = fields[6] or "",
commit = fields[7] or "",
rtp = fields[8] or "",
dir = fields[9] or "",
postUpdate = fields[10] or "",
on = fields[11] or "",
filetypes = fields[12] or "",
frozen = fields[13] or "",
managedDir = fields[14] or "",
}
end
end
return items
end
local function serialize_manifest(items)
sort_by_name(items)
local lines = {}
for _, item in ipairs(items or {}) do
lines[#lines + 1] = table.concat({
sanitize_tsv(item.packageId),
sanitize_tsv(item.name),
sanitize_tsv(item.sourceKind),
sanitize_tsv(item.source),
sanitize_tsv(item.branch),
sanitize_tsv(item.tag),
sanitize_tsv(item.commit),
sanitize_tsv(item.rtp),
sanitize_tsv(item.dir),
sanitize_tsv(item.postUpdate),
sanitize_tsv(item.on),
sanitize_tsv(item.filetypes),
sanitize_tsv(item.frozen),
sanitize_tsv(item.managedDir),
}, "\t")
end
if #lines == 0 then
return ""
end
return table.concat(lines, "\n") .. "\n"
end
local function parse_state_text(text)
local items = {}
for _, line in ipairs(split_lines(text)) do
local fields = split_tsv(line)
if trim(fields[1] or "") ~= "" then
items[#items + 1] = {
packageId = fields[1] or "",
name = fields[2] or "",
sourceKind = fields[3] or "",
source = fields[4] or "",
installedVersion = fields[5] or "",
installedRevision = fields[6] or "",
branch = fields[7] or "",
tag = fields[8] or "",
commit = fields[9] or "",
managedDir = fields[10] or "",
status = fields[11] or "installed",
}
end
end
return items
end
local function serialize_state(items)
sort_by_name(items)
local lines = {}
for _, item in ipairs(items or {}) do
lines[#lines + 1] = table.concat({
sanitize_tsv(item.packageId),
sanitize_tsv(item.name),
sanitize_tsv(item.sourceKind),
sanitize_tsv(item.source),
sanitize_tsv(item.installedVersion),
sanitize_tsv(item.installedRevision),
sanitize_tsv(item.branch),
sanitize_tsv(item.tag),
sanitize_tsv(item.commit),
sanitize_tsv(item.managedDir),
sanitize_tsv(item.status),
}, "\t")
end
if #lines == 0 then
return ""
end
return table.concat(lines, "\n") .. "\n"
end
local function parse_remote_info_text(text)
local items = {}
for _, line in ipairs(split_lines(text)) do
local fields = split_tsv(line)
if trim(fields[1] or "") ~= "" then
items[#items + 1] = {
packageId = fields[1] or "",
name = fields[2] or "",
source = fields[3] or "",
defaultBranch = fields[4] or "",
latestRevision = fields[5] or "",
latestVersion = fields[6] or "",
homepage = fields[7] or "",
tags = fields[8] or "",
}
end
end
return items
end
local function serialize_remote_info(items)
sort_by_name(items)
local lines = {}
for _, item in ipairs(items or {}) do
lines[#lines + 1] = table.concat({
sanitize_tsv(item.packageId),
sanitize_tsv(item.name),
sanitize_tsv(item.source),
sanitize_tsv(item.defaultBranch),
sanitize_tsv(item.latestRevision),
sanitize_tsv(item.latestVersion),
sanitize_tsv(item.homepage),
sanitize_tsv(item.tags),
}, "\t")
end
if #lines == 0 then
return ""
end
return table.concat(lines, "\n") .. "\n"
end
local function read_manifest(context)
local content = read_text_file(context, runtime_paths(context).manifestFile)
if content == nil then
return {}
end
return parse_manifest_text(content)
end
local function save_manifest(context, items)
return write_text_file(context, runtime_paths(context).manifestFile, serialize_manifest(items))
end
local function read_state(context)
local content = read_text_file(context, runtime_paths(context).stateFile)
if content == nil then
return {}
end
return parse_state_text(content)
end
local function save_state(context, items)
return write_text_file(context, runtime_paths(context).stateFile, serialize_state(items))
end
local function read_remote_info_cache(context)
local content = read_text_file(context, runtime_paths(context).remoteInfoFile)
if content == nil then
return {}
end
return parse_remote_info_text(content)
end
local function save_remote_info_cache(context, items)
return write_text_file(context, runtime_paths(context).remoteInfoFile, serialize_remote_info(items))
end
local function find_by_name(items, name)
local needle = lower(name)
for _, item in ipairs(items or {}) do
if lower(item.name) == needle then
return item
end
end
return nil
end
local function find_by_package_id(items, package_id)
local needle = trim(package_id)
for _, item in ipairs(items or {}) do
if trim(item.packageId) == needle then
return item
end
end
return nil
end
local function merge_manifest_entry(items, entry)
local merged = {}
local replaced = false
for _, item in ipairs(items or {}) do
if lower(item.name) == lower(entry.name) then
if trim(item.source) ~= trim(entry.source) then
return nil, "alias-collision"
end
merged[#merged + 1] = shallow_copy(entry)
replaced = true
elseif trim(item.packageId) == trim(entry.packageId) or trim(item.source) == trim(entry.source) then
merged[#merged + 1] = shallow_copy(entry)
replaced = true
else
merged[#merged + 1] = shallow_copy(item)
end
end
if not replaced then
merged[#merged + 1] = shallow_copy(entry)
end
return merged
end
local function remove_manifest_entries(items, requested)
local removed = {}
local keep = {}
local targets = {}
for _, item in ipairs(requested or {}) do
targets[lower(item.name or item.packageId or item.source or "")] = true
if trim(item.packageId or "") ~= "" then
targets[trim(item.packageId)] = true
end
if trim(item.source or "") ~= "" then
targets[trim(item.source)] = true
end
end
for _, item in ipairs(items or {}) do
local key = lower(item.name or "")
if targets[key] or targets[trim(item.packageId)] or targets[trim(item.source)] then
removed[#removed + 1] = shallow_copy(item)
else
keep[#keep + 1] = shallow_copy(item)
end
end
return keep, removed
end
local function merge_state_entry(items, entry)
local merged = {}
local replaced = false
for _, item in ipairs(items or {}) do
if lower(item.name) == lower(entry.name) or trim(item.packageId) == trim(entry.packageId) then
merged[#merged + 1] = shallow_copy(entry)
replaced = true
else
merged[#merged + 1] = shallow_copy(item)
end
end
if not replaced then
merged[#merged + 1] = shallow_copy(entry)
end
return merged
end
local function remove_state_entries(items, removed_items)
local removed_names = {}
for _, item in ipairs(removed_items or {}) do
removed_names[lower(item.name or "")] = true
removed_names[trim(item.packageId or "")] = true
end
local keep = {}
for _, item in ipairs(items or {}) do
if not removed_names[lower(item.name or "")] and not removed_names[trim(item.packageId or "")] then
keep[#keep + 1] = shallow_copy(item)
end
end
return keep
end
local function serialize_plug_options(entry)
local options = {}
local function add_string(key, value)
local text = non_empty(value)
if text ~= nil then
options[#options + 1] = vim_quote(key) .. ": " .. vim_quote(text)
end
end
local function add_list(key, value)
local items = split_csv(value)
if #items == 1 then
options[#options + 1] = vim_quote(key) .. ": " .. vim_quote(items[1])
elseif #items > 1 then
options[#options + 1] = vim_quote(key) .. ": " .. vim_list(items)
end
end
add_string("branch", entry.branch)
add_string("tag", entry.tag)
add_string("commit", entry.commit)
add_string("rtp", entry.rtp)
add_string("dir", entry.dir)
add_string("as", entry.name ~= default_name_for_source(entry.source) and entry.name or "")
add_string("do", entry.postUpdate)
add_list("on", entry.on)
add_list("for", entry.filetypes)
if trim(entry.frozen or "") ~= "" then
options[#options + 1] = vim_quote("frozen") .. ": 1"
end
if #options == 0 then
return nil
end
return "{ " .. table.concat(options, ", ") .. " }"
end
local function render_vim_config(context, manifest)
local paths = runtime_paths(context)
local lines = {
"set nocompatible",
"set shortmess+=I",
"filetype off",
"let g:plug_window = 'enew'",
"let g:plug_pwindow = 'enew'",
"execute 'set rtp^=' . fnameescape('" .. paths.dataRoot:gsub("'", "''") .. "')",
"call plug#begin('" .. paths.pluggedDir:gsub("'", "''") .. "')",
}
for _, entry in ipairs(manifest or {}) do
local source = entry.sourceKind == "local" and runtime_expand(entry.source) or entry.source
local options = serialize_plug_options(entry)