-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.lua
More file actions
1721 lines (1548 loc) · 50.9 KB
/
Copy pathrun.lua
File metadata and controls
1721 lines (1548 loc) · 50.9 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_NAME = "Tempify Template Manager"
local PLUGIN_VERSION = "0.1.2"
local GIT_BINARY = "git"
local STATE_SCHEMA_VERSION = 1
local function trim(value)
return (tostring(value or ""):gsub("^%s+", ""):gsub("%s+$", ""))
end
local function starts_with(value, prefix)
return tostring(value or ""):sub(1, #prefix) == prefix
end
local function ends_with(value, suffix)
if suffix == "" then
return true
end
local text = tostring(value or "")
return text:sub(-#suffix) == suffix
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 split(value, separator, plain)
local text = tostring(value or "")
local parts = {}
if separator == "" then
parts[1] = text
return parts
end
local start_index = 1
while true do
local from_index, to_index = string.find(text, separator, start_index, plain == true)
if from_index == nil then
table.insert(parts, string.sub(text, start_index))
break
end
table.insert(parts, string.sub(text, start_index, from_index - 1))
start_index = to_index + 1
end
return parts
end
local function join(values, separator)
return table.concat(values or {}, separator or "")
end
local function dirname(path)
local value = tostring(path or "")
if value == "" then
return "."
end
local index = value:match("^.*()/")
if index == nil then
return "."
end
if index == 1 then
return "/"
end
return value:sub(1, index - 1)
end
local function join_path(base, child)
local left = tostring(base or "")
local right = tostring(child or "")
if left == "" then
return right
end
if right == "" then
return left
end
if ends_with(left, "/") then
return left .. right
end
return left .. "/" .. right
end
local function sanitize_name(value)
local text = tostring(value or "")
if text == "" then
return "tempify"
end
return text:gsub("[^A-Za-z0-9._-]", "_")
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" or key < 1 or key % 1 ~= 0 then
return false
end
count = count + 1
end
for index = 1, count do
if value[index] == nil then
return false
end
end
return true
end
local function sorted_keys(value)
local keys = {}
for key, _ in pairs(value or {}) do
table.insert(keys, tostring(key))
end
table.sort(keys)
return keys
end
local function json_escape(value)
local text = tostring(value or "")
text = text:gsub("\\", "\\\\")
text = text:gsub('"', '\\"')
text = text:gsub("\b", "\\b")
text = text:gsub("\f", "\\f")
text = text:gsub("\n", "\\n")
text = text:gsub("\r", "\\r")
text = text:gsub("\t", "\\t")
return text
end
local function json_encode(value)
local kind = type(value)
if kind == "nil" then
return "null"
end
if kind == "boolean" then
return value and "true" or "false"
end
if kind == "number" then
if value ~= value or value == math.huge or value == -math.huge then
error("unsupported json number")
end
return tostring(value)
end
if kind == "string" then
return '"' .. json_escape(value) .. '"'
end
if kind ~= "table" then
error("unsupported json type: " .. kind)
end
if is_array(value) then
local parts = {}
for index = 1, #value do
parts[index] = json_encode(value[index])
end
return "[" .. join(parts, ",") .. "]"
end
local parts = {}
local keys = sorted_keys(value)
for index = 1, #keys do
local key = keys[index]
parts[index] = json_encode(key) .. ":" .. json_encode(value[key])
end
return "{" .. join(parts, ",") .. "}"
end
local function json_decode(text)
local input = tostring(text or "")
local length = #input
local index = 1
local function decode_error(message)
error("json parse error at byte " .. tostring(index) .. ": " .. message)
end
local function skip_whitespace()
while index <= length do
local char = input:sub(index, index)
if char ~= " " and char ~= "\n" and char ~= "\r" and char ~= "\t" then
break
end
index = index + 1
end
end
local parse_value
local function parse_string()
if input:sub(index, index) ~= '"' then
decode_error("expected string")
end
index = index + 1
local parts = {}
while index <= length do
local char = input:sub(index, index)
if char == '"' then
index = index + 1
return join(parts, "")
end
if char == "\\" then
local escaped = input:sub(index + 1, index + 1)
if escaped == '"' or escaped == "\\" or escaped == "/" then
table.insert(parts, escaped)
index = index + 2
elseif escaped == "b" then
table.insert(parts, "\b")
index = index + 2
elseif escaped == "f" then
table.insert(parts, "\f")
index = index + 2
elseif escaped == "n" then
table.insert(parts, "\n")
index = index + 2
elseif escaped == "r" then
table.insert(parts, "\r")
index = index + 2
elseif escaped == "t" then
table.insert(parts, "\t")
index = index + 2
elseif escaped == "u" then
local hex = input:sub(index + 2, index + 5)
if #hex ~= 4 or not hex:match("^[0-9A-Fa-f]+$") then
decode_error("invalid unicode escape")
end
local code = tonumber(hex, 16)
if code < 128 then
table.insert(parts, string.char(code))
else
-- Keep non-ASCII escaped payloads readable without depending on utf8 helpers.
table.insert(parts, "?")
end
index = index + 6
else
decode_error("invalid escape")
end
else
table.insert(parts, char)
index = index + 1
end
end
decode_error("unterminated string")
end
local function parse_number()
local start_index = index
local allowed = "-+0123456789.eE"
while index <= length and allowed:find(input:sub(index, index), 1, true) ~= nil do
index = index + 1
end
local raw = input:sub(start_index, index - 1)
local value = tonumber(raw)
if value == nil then
decode_error("invalid number")
end
return value
end
local function parse_array()
index = index + 1
skip_whitespace()
local items = {}
if input:sub(index, index) == "]" then
index = index + 1
return items
end
while true do
table.insert(items, parse_value())
skip_whitespace()
local char = input:sub(index, index)
if char == "]" then
index = index + 1
return items
end
if char ~= "," then
decode_error("expected ',' or ']' in array")
end
index = index + 1
skip_whitespace()
end
end
local function parse_object()
index = index + 1
skip_whitespace()
local object = {}
if input:sub(index, index) == "}" then
index = index + 1
return object
end
while true do
if input:sub(index, index) ~= '"' then
decode_error("expected object key")
end
local key = parse_string()
skip_whitespace()
if input:sub(index, index) ~= ":" then
decode_error("expected ':' after key")
end
index = index + 1
skip_whitespace()
object[key] = parse_value()
skip_whitespace()
local char = input:sub(index, index)
if char == "}" then
index = index + 1
return object
end
if char ~= "," then
decode_error("expected ',' or '}' in object")
end
index = index + 1
skip_whitespace()
end
end
function parse_value()
skip_whitespace()
local char = input:sub(index, index)
if char == '"' then
return parse_string()
end
if char == "{" then
return parse_object()
end
if char == "[" then
return parse_array()
end
if char == "-" or char:match("%d") then
return parse_number()
end
if input:sub(index, index + 3) == "true" then
index = index + 4
return true
end
if input:sub(index, index + 4) == "false" then
index = index + 5
return false
end
if input: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("trailing characters")
end
return value
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 register_artifact(context, payload)
if context == nil or context.artifacts == nil then
return
end
local fn = context.artifacts.register
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 log_error(context, message)
if context == nil or context.log == nil then
return
end
local fn = context.log.error
if type(fn) == "function" then
fn(message)
end
end
local function command_exists(binary)
return reqpack.exec.run("command -v " .. shell_quote(binary) .. " >/dev/null 2>&1").success
end
local function run_shell(command)
local handle = io.popen(command .. " 2>&1")
if handle == nil then
return false, "", 1
end
local output = handle:read("*a") or ""
local ok, _, code = handle:close()
if ok == true then
return true, output, 0
end
return false, output, tonumber(code) or 1
end
local function shell_success(command)
local success = run_shell(command)
return success
end
local function read_file(path)
local file = io.open(path, "rb")
if file == nil then
return nil
end
local content = file:read("*a") or ""
file:close()
return content
end
local function mkdir_p(path)
local success, output = run_shell("mkdir -p " .. shell_quote(path))
if not success and trim(output) ~= "" then
return false, trim(output)
end
return success, trim(output)
end
local function remove_tree(path)
local success, output = run_shell("rm -rf " .. shell_quote(path))
if not success and trim(output) ~= "" then
return false, trim(output)
end
return success, trim(output)
end
local function copy_tree(source, destination)
local created, create_error = mkdir_p(destination)
if not created then
return false, create_error
end
local command = "cp -R " .. shell_quote(join_path(source, ".")) .. " " .. shell_quote(destination)
local success, output = run_shell(command)
if not success and trim(output) ~= "" then
return false, trim(output)
end
return success, trim(output)
end
local function path_exists(path)
local success = shell_success("test -e " .. shell_quote(path))
return success
end
local function directory_exists(path)
local success = shell_success("test -d " .. shell_quote(path))
return success
end
local function write_file(path, content)
local parent = dirname(path)
local created, create_error = mkdir_p(parent)
if not created then
return false, create_error
end
local file, error_message = io.open(path, "wb")
if file == nil then
return false, trim(error_message)
end
file:write(content or "")
file:close()
return true, nil
end
local function is_remote_url(value)
local text = tostring(value or "")
if starts_with(text, "file://") then
return false
end
if text:match("^[A-Za-z][A-Za-z0-9+.-]*://") then
return true
end
if text:match("^[^/%s]+@[^:%s]+:.+$") then
return true
end
return false
end
local function is_absolute_path(value)
local text = tostring(value or "")
return starts_with(text, "/")
end
local function resolve_local_path(base, value)
local text = trim(value)
if text == "" then
return ""
end
if starts_with(text, "file://") then
local stripped = text:sub(8)
if is_absolute_path(stripped) then
return stripped
end
return join_path(base, stripped)
end
if is_absolute_path(text) then
return text
end
if is_remote_url(text) then
return text
end
return join_path(base, text)
end
local function resolve_reference(base, value)
local text = trim(value)
if text == "" then
return ""
end
if starts_with(text, "file://") or is_remote_url(text) or is_absolute_path(text) then
return text
end
if starts_with(base or "", "http://") or starts_with(base or "", "https://") then
if ends_with(base, "/") then
return base .. text
end
return base .. "/" .. text
end
return resolve_local_path(base, text)
end
local function read_text_from_url(context, url, output_base)
if not is_remote_url(url) and not starts_with(url, "file://") then
return read_file(url), nil
end
if starts_with(url, "file://") then
return read_file(url:sub(8)), nil
end
local tmp_root = context ~= nil and context.fs ~= nil and type(context.fs.get_tmp_dir) == "function"
and context.fs.get_tmp_dir()
or ""
local base = output_base
if trim(base) == "" then
base = join_path(tmp_root ~= "" and tmp_root or "/tmp", sanitize_name(url))
end
local extension = url:match("(%.[A-Za-z0-9._-]+)$") or ""
local target = extension ~= "" and (base .. extension) or base
if context ~= nil and context.net ~= nil and type(context.net.download) == "function" then
local downloaded = context.net.download(url, base)
if downloaded then
return read_file(target), nil
end
end
local success, output = run_shell("curl -fsSL " .. shell_quote(url))
if not success then
return nil, trim(output)
end
return output, nil
end
local function parse_json_text(text)
local ok, value = pcall(json_decode, text)
if not ok then
return nil, tostring(value)
end
return value, nil
end
local function read_json_file(path)
local text = read_file(path)
if text == nil then
return nil, nil
end
return parse_json_text(text)
end
local function write_json_file(path, value)
local ok, encoded = pcall(json_encode, value)
if not ok then
return false, tostring(encoded)
end
return write_file(path, encoded .. "\n")
end
local function string_array(value)
if type(value) ~= "table" then
return {}
end
local items = {}
for _, item in ipairs(value) do
local text = trim(item)
if text ~= "" then
table.insert(items, text)
end
end
return items
end
local function table_field(value, key)
if type(value) ~= "table" then
return nil
end
return value[key]
end
local function source_table_from_value(value)
if type(value) ~= "table" then
return nil
end
local source_type = trim(value.type)
local source_url = trim(value.url)
if source_type == "" or source_url == "" then
return nil
end
return {
type = source_type,
url = source_url,
ref = trim(value.ref),
subdir = trim(value.subdir),
}
end
local function parse_catalog_template(entry, repository, catalog_base)
if type(entry) ~= "table" then
return nil
end
local source = source_table_from_value(entry.source)
if source == nil or source.type ~= "git" then
return nil
end
local id = trim(entry.id)
local version = trim(entry.version)
if id == "" or version == "" then
return nil
end
return {
id = id,
name = trim(entry.name) ~= "" and trim(entry.name) or id,
description = trim(entry.description),
version = version,
tags = string_array(entry.tags),
source = source,
repository = {
id = trim(repository.id) ~= "" and trim(repository.id) or "repo",
url = trim(repository.url),
priority = tonumber(repository.priority) or 0,
},
catalogBase = catalog_base,
}
end
local function candidate_beats(candidate, current)
if current == nil then
return true
end
if candidate.repository.priority ~= current.repository.priority then
return candidate.repository.priority > current.repository.priority
end
return candidate.repository.id < current.repository.id
end
local function sort_records_by_id(records)
table.sort(records, function(left, right)
return tostring(left.id or "") < tostring(right.id or "")
end)
return records
end
local function state_paths(data_root)
local index_root = join_path(data_root, "index")
return {
dataRoot = data_root,
templatesRoot = join_path(data_root, "templates"),
indexRoot = index_root,
tempifyIndex = join_path(index_root, "templates.json"),
availableState = join_path(index_root, "reqpack-available.json"),
installedState = join_path(index_root, "reqpack-installed.json"),
}
end
local function default_data_root_without_context()
local success, output = run_shell("printf '%s' \"${XDG_DATA_HOME:-$HOME/.local/share}\"")
local root = success and trim(output) or ""
if root == "" then
return nil
end
return join_path(root, "tempify")
end
local function template_target_path(paths, id)
return join_path(paths.templatesRoot, id)
end
local function normalize_installed_record(paths, record)
return {
id = trim(record.id),
name = trim(record.name) ~= "" and trim(record.name) or trim(record.id),
description = trim(record.description),
version = trim(record.version),
tags = string_array(record.tags),
path = template_target_path(paths, trim(record.id)),
source = type(record.source) == "table" and {
type = trim(record.source.type),
url = trim(record.source.url),
ref = trim(record.source.ref),
subdir = trim(record.source.subdir),
} or { type = "git", url = "", ref = "", subdir = "" },
repository = type(record.repository) == "table" and {
id = trim(record.repository.id),
url = trim(record.repository.url),
priority = tonumber(record.repository.priority) or 0,
} or { id = "", url = "", priority = 0 },
installedAt = trim(record.installedAt),
}
end
local function load_installed_records(paths)
local root = read_json_file(paths.installedState)
if root == nil or type(root) ~= "table" or type(root.templates) ~= "table" then
return {}
end
local records = {}
for _, entry in ipairs(root.templates) do
local record = normalize_installed_record(paths, entry)
if record.id ~= "" then
table.insert(records, record)
end
end
return sort_records_by_id(records)
end
local function save_installed_records(paths, records)
local ordered = sort_records_by_id(records)
return write_json_file(paths.installedState, {
schemaVersion = STATE_SCHEMA_VERSION,
templates = ordered,
})
end
local function load_available_records(paths)
local root = read_json_file(paths.availableState)
if root == nil or type(root) ~= "table" or type(root.templates) ~= "table" then
return {}
end
local records = {}
for _, entry in ipairs(root.templates) do
local record = parse_catalog_template(entry, entry.repository or {}, trim(entry.catalogBase))
if record ~= nil then
table.insert(records, record)
end
end
return sort_records_by_id(records)
end
local function save_available_records(paths, records)
local ordered = sort_records_by_id(records)
local payload = {}
for _, record in ipairs(ordered) do
table.insert(payload, {
id = record.id,
name = record.name,
description = record.description,
version = record.version,
tags = record.tags,
source = record.source,
repository = record.repository,
catalogBase = record.catalogBase,
})
end
return write_json_file(paths.availableState, {
schemaVersion = STATE_SCHEMA_VERSION,
templates = payload,
})
end
local function save_tempify_index(paths, records)
local ordered = sort_records_by_id(records)
local templates = {}
for _, record in ipairs(ordered) do
table.insert(templates, {
id = record.id,
name = record.name,
description = record.description,
version = record.version,
path = template_target_path(paths, record.id),
})
end
return write_json_file(paths.tempifyIndex, { templates = templates })
end
local function ensure_store_layout(paths)
local ok, message = mkdir_p(paths.templatesRoot)
if not ok then
return false, message
end
return mkdir_p(paths.indexRoot)
end
local function installed_record_map(records)
local result = {}
for _, record in ipairs(records or {}) do
result[record.id] = record
end
return result
end
local function requested_package_name(pkg)
if type(pkg) ~= "table" then
return ""
end
return trim(pkg.name or pkg.packageId)
end
local function package_is_missing(installed_map, pkg)
local name = requested_package_name(pkg)
if name == "" then
return true
end
local installed = installed_map[name]
if installed == nil then
return true
end
local requested_version = trim(type(pkg) == "table" and pkg.version or "")
return requested_version ~= "" and requested_version ~= installed.version
end
local function filter_missing_packages(installed_records, packages)
local installed_map = installed_record_map(installed_records)
local missing = {}
for _, pkg in ipairs(packages or {}) do
if package_is_missing(installed_map, pkg) then
table.insert(missing, pkg)
end
end
return missing
end
local function upsert_record(records, record)
local updated = {}
local replaced = false
for _, existing in ipairs(records or {}) do
if existing.id == record.id then
table.insert(updated, record)
replaced = true
else
table.insert(updated, existing)
end
end
if not replaced then
table.insert(updated, record)
end
return sort_records_by_id(updated)
end
local function remove_record(records, id)
local updated = {}
for _, existing in ipairs(records or {}) do
if existing.id ~= id then
table.insert(updated, existing)
end
end
return sort_records_by_id(updated)
end
local function now_utc_string()
local success, output = run_shell("date -u +%Y-%m-%dT%H:%M:%SZ")
if success then
local value = trim(output)
if value ~= "" then
return value
end
end
return "1970-01-01T00:00:00Z"
end
local function flags_from_context(context)
local values = {
dataRoot = nil,
seedFrom = nil,
repositories = {},
}
for _, raw in ipairs(context ~= nil and context.flags or {}) do
local flag = trim(raw)
if starts_with(flag, "data-root=") then
values.dataRoot = trim(flag:sub(#"data-root=" + 1))
elseif starts_with(flag, "seed-from=") then
values.seedFrom = trim(flag:sub(#"seed-from=" + 1))
elseif starts_with(flag, "repo=") then
local parts = split(flag:sub(#"repo=" + 1), "|", true)
if #parts >= 3 then
local url_parts = {}
for index = 3, #parts do
table.insert(url_parts, parts[index])
end
table.insert(values.repositories, {
id = trim(parts[1]),
priority = tonumber(trim(parts[2])) or 0,
url = trim(join(url_parts, "|")),
enabled = true,
})
end
end
end
return values
end
local function resolve_data_root(context, options)
local override = trim(options.dataRoot)
if override ~= "" then
if starts_with(override, "@tmp") then
local temp_root = context ~= nil and context.fs ~= nil and type(context.fs.get_tmp_dir) == "function"
and context.fs.get_tmp_dir()
or "/tmp"
local suffix = trim(override:sub(5))
suffix = starts_with(suffix, "/") and suffix:sub(2) or suffix
if suffix == "" then
return temp_root
end
return join_path(temp_root, suffix)
end
return resolve_local_path(context.plugin.dir, override)
end
local success, output = run_shell("printf '%s' \"${XDG_DATA_HOME:-$HOME/.local/share}\"")
local root = success and trim(output) or ""
if root == "" then
root = join_path(context.plugin.dir, ".tempify-data")
end
return join_path(root, "tempify")
end
local function load_builtin_repositories(context)
local manifest_path = join_path(context.plugin.dir, "default-template-repositories.json")
local root, read_error = read_json_file(manifest_path)
if root == nil then
if read_error ~= nil then
log_warn(context, "skip built-in tempify repositories: " .. tostring(read_error))
end
return {}
end
if type(root) ~= "table" or type(root.repositories) ~= "table" then
log_warn(context, "skip built-in tempify repositories: invalid manifest")
return {}
end
local repositories = {}
for _, repo in ipairs(root.repositories) do
local url = resolve_local_path(context.plugin.dir, trim(type(repo) == "table" and repo.url or ""))
if url ~= "" and (type(repo) ~= "table" or repo.enabled ~= false) then
table.insert(repositories, {