-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathxlings.cppm
More file actions
853 lines (725 loc) · 32.6 KB
/
xlings.cppm
File metadata and controls
853 lines (725 loc) · 32.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
// mcpp.xlings — unified abstraction layer for all xlings (external package
// manager) interactions. Consolidates NDJSON event parsing, subprocess
// command building, path helpers, and bootstrap progress types that were
// previously scattered across config.cppm, package_fetcher.cppm, cli.cppm,
// flags.cppm, ninja_backend.cppm, and stdmod.cppm.
//
// This module is a LEAF dependency: it only imports `std` and
// `mcpp.pm.compat`. It must NOT import mcpp.config or any other mcpp module.
module;
#include <cstdio> // stderr
#include <cstdlib>
export module mcpp.xlings;
import std;
import mcpp.pm.compat;
import mcpp.platform;
export namespace mcpp::xlings {
// ─── Env: resolved xlings binary + home directory ───────────────────
struct Env {
std::filesystem::path binary; // xlings binary path
std::filesystem::path home; // XLINGS_HOME directory
std::filesystem::path projectDir; // XLINGS_PROJECT_DIR (empty = global mode)
};
// ─── Pinned version constants ───────────────────────────────────────
namespace pinned {
inline constexpr std::string_view kPatchelfVersion = "0.18.0";
inline constexpr std::string_view kNinjaVersion = "1.12.1";
inline constexpr std::string_view kXlingsVersion = "0.4.31";
}
// ─── Path helpers (pure functions, no subprocess) ───────────────────
namespace paths {
// xpkgs base: env.home / "data" / "xpkgs"
std::filesystem::path xpkgs_base(const Env& env);
// sandbox bin: env.home / "subos" / "default" / "bin"
std::filesystem::path sandbox_bin(const Env& env);
// sandbox sysroot: env.home / "subos" / "default"
std::filesystem::path sysroot(const Env& env);
// xim tool root: xpkgs_base / "xim-x-<tool>"
std::filesystem::path xim_tool_root(const Env& env, std::string_view tool);
// xim tool versioned: xpkgs_base / "xim-x-<tool>" / "<version>"
std::filesystem::path xim_tool(const Env& env, std::string_view tool,
std::string_view version);
// From compiler binary, climb parent dirs to find "xpkgs" directory.
// Replaces 3 duplicate implementations in flags.cppm, ninja_backend.cppm,
// stdmod.cppm.
std::optional<std::filesystem::path>
xpkgs_from_compiler(const std::filesystem::path& compilerBin);
// Find a sibling xim tool relative to a compiler binary.
// e.g. find_sibling_tool(gcc_bin, "binutils") returns highest version
// dir of xim-x-binutils.
std::optional<std::filesystem::path>
find_sibling_tool(const std::filesystem::path& compilerBin,
std::string_view tool);
// Find a binary inside a sibling tool (e.g. binutils/bin/ar,
// ninja/ninja).
std::optional<std::filesystem::path>
find_sibling_binary(const std::filesystem::path& compilerBin,
std::string_view tool,
std::string_view binaryRelPath);
// index data root: env.home / "data"
std::filesystem::path index_data(const Env& env);
// sandbox init marker: env.home / "subos" / "default" / ".xlings.json"
std::filesystem::path sandbox_init_marker(const Env& env);
} // namespace paths
// ─── Shell quoting ──────────────────────────────────────────────────
// Shell-escape (single-quote) a string for the command line.
std::string shq(std::string_view s);
// ─── Shell command builders ─────────────────────────────────────────
// Build the standard xlings command prefix with proper env vars.
// cd '<home>' && env -u XLINGS_PROJECT_DIR PATH=<sandbox_bin>:"$PATH"
// XLINGS_HOME='<home>' '<binary>'
std::string build_command_prefix(const Env& env);
// Build full xlings interface command.
// <prefix> interface <capability> --args '<argsJson>' 2>/dev/null
std::string build_interface_command(const Env& env,
std::string_view capability,
std::string_view argsJson);
// ─── NDJSON event types ─────────────────────────────────────────────
struct ProgressEvent {
std::string phase; // "download", "extract", "configure", ...
int percent; // 0..100
std::string message;
};
struct LogEvent {
std::string level; // "debug" | "info" | "warn" | "error"
std::string message;
};
struct DataEvent {
std::string dataKind; // "install_plan", "styled_list", ...
std::string payloadJson;// raw JSON (let caller parse)
};
struct ErrorEvent {
std::string code;
std::string message;
std::string hint;
bool recoverable = false;
};
struct ResultEvent {
int exitCode = 0;
std::string dataJson; // additional payload, may be empty
};
using Event = std::variant<ProgressEvent, LogEvent, DataEvent,
ErrorEvent, ResultEvent>;
// Parse one NDJSON line into an Event.
std::optional<Event> parse_event_line(std::string_view line);
// ─── JSON extraction helpers (for NDJSON parsing) ───────────────────
std::string extract_string(std::string_view text, std::string_view key);
std::optional<long long> extract_int(std::string_view text, std::string_view key);
std::optional<bool> extract_bool(std::string_view text, std::string_view key);
std::string extract_object(std::string_view text, std::string_view key);
// ─── Subprocess call ────────────────────────────────────────────────
struct CallResult {
int exitCode = 0;
std::vector<DataEvent> dataEvents;
std::optional<ErrorEvent> error;
std::string resultJson;
};
struct EventHandler {
virtual ~EventHandler() = default;
virtual void on_progress(const ProgressEvent&) {}
virtual void on_log (const LogEvent&) {}
virtual void on_data (const DataEvent&) {}
virtual void on_error (const ErrorEvent&) {}
virtual void on_result (const ResultEvent&) {}
};
std::expected<CallResult, std::string>
call(const Env& env, std::string_view capability,
std::string_view argsJson, EventHandler* handler = nullptr);
// ─── Bootstrap progress types ───────────────────────────────────────
struct BootstrapFile {
std::string name; // xim package id, e.g. "xim:patchelf@0.18.0"
double downloadedBytes = 0;
double totalBytes = 0;
bool started = false;
bool finished = false;
};
struct BootstrapProgress {
std::vector<BootstrapFile> files;
double elapsedSec = 0;
};
using BootstrapProgressCallback = std::function<void(const BootstrapProgress&)>;
// Run xlings install with progress callback (used by bootstrap functions).
int install_with_progress(const Env& env, std::string_view target,
const BootstrapProgressCallback& cb);
// ─── Sandbox lifecycle ──────────────────────────────────────────────
// Write .xlings.json seed file.
void seed_xlings_json(const Env& env,
std::span<const std::pair<std::string,std::string>> repos,
std::string_view mirror = "CN");
// Persist the xlings mirror selection in .xlings.json via xlings itself.
int config_show(const Env& env);
int config_set_mirror(const Env& env, std::string_view mirror, bool quiet = false);
// Run xlings self init.
void ensure_init(const Env& env, bool quiet);
// Ensure patchelf is installed.
void ensure_patchelf(const Env& env, bool quiet,
const BootstrapProgressCallback& cb);
// Ensure ninja is installed.
void ensure_ninja(const Env& env, bool quiet,
const BootstrapProgressCallback& cb);
// ─── Index freshness ────────────────────────────────────────────────
// Check whether local index data exists and is fresh (within ttlSeconds).
// Returns true if index is present and fresh, false otherwise.
bool is_index_fresh(const Env& env, std::int64_t ttlSeconds);
// Run `xlings update` to refresh all index repos. Streams output to stdout.
// Returns the xlings exit code.
int update_index(const Env& env, bool quiet = false);
// Ensure the local index is present and fresh. Runs `xlings update` if
// the index is missing or older than ttlSeconds. Idempotent and quiet
// when no update is needed.
void ensure_index_fresh(const Env& env, std::int64_t ttlSeconds, bool quiet = false);
// ─── run_capture utility ────────────────────────────────────────────
std::expected<std::string, std::string> run_capture(const std::string& cmd);
} // namespace mcpp::xlings
// ═══════════════════════════════════════════════════════════════════════
// Implementation
// ═══════════════════════════════════════════════════════════════════════
namespace mcpp::xlings {
namespace {
// Right-pad a verb to 12 columns for bootstrap status lines.
void print_status(std::string_view verb, std::string_view msg) {
constexpr std::size_t W = 12;
if (verb.size() >= W) {
std::println("{} {}", verb, msg);
} else {
std::println("{}{} {}", std::string(W - verb.size(), ' '), verb, msg);
}
}
void write_file(const std::filesystem::path& p, std::string_view content) {
std::error_code ec;
std::filesystem::create_directories(p.parent_path(), ec);
std::ofstream os(p);
os << content;
}
// LineScan: cheap field extraction for bootstrap install progress lines.
// Handles flat JSON; no nested array/object — the keys we extract are
// all leaves.
struct LineScan {
std::string_view s;
std::string find_str(std::string_view key) const {
std::string n = std::format("\"{}\":\"", key);
auto p = s.find(n);
if (p == std::string_view::npos) return "";
p += n.size();
std::string out;
while (p < s.size() && s[p] != '"') {
if (s[p] == '\\' && p + 1 < s.size()) {
out.push_back(s[p+1]); p += 2; continue;
}
out.push_back(s[p++]);
}
return out;
}
double find_num(std::string_view key) const {
std::string n = std::format("\"{}\":", key);
auto p = s.find(n);
if (p == std::string_view::npos) return 0;
p += n.size();
auto e = p;
while (e < s.size()
&& (std::isdigit(static_cast<unsigned char>(s[e]))
|| s[e] == '.' || s[e] == '-' || s[e] == '+'
|| s[e] == 'e' || s[e] == 'E')) ++e;
try { return std::stod(std::string(s.substr(p, e - p))); }
catch (...) { return 0; }
}
bool find_bool(std::string_view key) const {
std::string n = std::format("\"{}\":", key);
auto p = s.find(n);
if (p == std::string_view::npos) return false;
p += n.size();
return s.size() - p >= 4 && s.substr(p, 4) == "true";
}
};
} // anonymous namespace
// ─── run_capture ────────────────────────────────────────────────────
std::expected<std::string, std::string> run_capture(const std::string& cmd) {
auto r = mcpp::platform::process::capture(cmd);
if (r.exit_code != 0 && r.output.empty())
return std::unexpected("command failed: " + cmd);
return r.output;
}
// ─── Shell quoting ──────────────────────────────────────────────────
std::string shq(std::string_view s) {
return mcpp::platform::shell::quote(s);
}
// ─── Path helpers ───────────────────────────────────────────────────
namespace paths {
std::filesystem::path xpkgs_base(const Env& env) {
return env.home / "data" / "xpkgs";
}
std::filesystem::path sandbox_bin(const Env& env) {
return env.home / "subos" / "default" / "bin";
}
std::filesystem::path sysroot(const Env& env) {
return env.home / "subos" / "default";
}
std::filesystem::path xim_tool_root(const Env& env, std::string_view tool) {
return xpkgs_base(env) / std::format("xim-x-{}", tool);
}
std::filesystem::path xim_tool(const Env& env, std::string_view tool,
std::string_view version) {
return xpkgs_base(env) / std::format("xim-x-{}", tool) / std::string(version);
}
std::optional<std::filesystem::path>
xpkgs_from_compiler(const std::filesystem::path& compilerBin) {
for (auto p = compilerBin.parent_path();
p.has_parent_path() && p != p.root_path();
p = p.parent_path()) {
if (p.filename() == "xpkgs") return p;
}
return std::nullopt;
}
std::optional<std::filesystem::path>
find_sibling_tool(const std::filesystem::path& compilerBin,
std::string_view tool) {
auto xpkgs = xpkgs_from_compiler(compilerBin);
if (!xpkgs) return std::nullopt;
auto root = *xpkgs / std::format("xim-x-{}", tool);
std::error_code ec;
if (!std::filesystem::exists(root, ec)) return std::nullopt;
// Return the first (highest) version dir that exists.
for (auto& v : std::filesystem::directory_iterator(root, ec)) {
if (v.is_directory(ec)) return v.path();
}
return std::nullopt;
}
std::optional<std::filesystem::path>
find_sibling_binary(const std::filesystem::path& compilerBin,
std::string_view tool,
std::string_view binaryRelPath) {
auto xpkgs = xpkgs_from_compiler(compilerBin);
if (!xpkgs) return std::nullopt;
auto root = *xpkgs / std::format("xim-x-{}", tool);
std::error_code ec;
if (!std::filesystem::exists(root, ec)) return std::nullopt;
for (auto& v : std::filesystem::directory_iterator(root, ec)) {
auto candidate = v.path() / std::string(binaryRelPath);
if (std::filesystem::exists(candidate, ec))
return candidate;
}
return std::nullopt;
}
std::filesystem::path index_data(const Env& env) {
return env.home / "data";
}
std::filesystem::path sandbox_init_marker(const Env& env) {
return env.home / "subos" / "default" / ".xlings.json";
}
} // namespace paths
// ─── Shell command builders ─────────────────────────────────────────
std::string build_command_prefix(const Env& env) {
auto xvmBin = paths::sandbox_bin(env).string();
if constexpr (mcpp::platform::is_windows) {
mcpp::platform::env::set("XLINGS_HOME", env.home.string());
mcpp::platform::env::set("XLINGS_PROJECT_DIR",
env.projectDir.empty() ? "" : env.projectDir.string());
mcpp::platform::windows::prepend_path(xvmBin);
return env.binary.string();
} else {
if (env.projectDir.empty()) {
// Global mode: unset XLINGS_PROJECT_DIR (existing behavior).
return std::format(
"cd {} && env -u XLINGS_PROJECT_DIR PATH={}:\"$PATH\" XLINGS_HOME={} {}",
shq(env.home.string()),
shq(xvmBin),
shq(env.home.string()),
shq(env.binary.string()));
}
// Project-level mode: set XLINGS_PROJECT_DIR so xlings uses
// additive project repos alongside global repos.
return std::format(
"cd {} && env PATH={}:\"$PATH\" XLINGS_HOME={} XLINGS_PROJECT_DIR={} {}",
shq(env.home.string()),
shq(xvmBin),
shq(env.home.string()),
shq(env.projectDir.string()),
shq(env.binary.string()));
}
}
std::string build_interface_command(const Env& env,
std::string_view capability,
std::string_view argsJson) {
return std::format("{} interface {} --args {} {}",
build_command_prefix(env), capability, shq(argsJson),
mcpp::platform::null_redirect);
}
// ─── JSON extraction helpers ────────────────────────────────────────
std::string extract_string(std::string_view text, std::string_view key) {
auto needle = std::string{"\""} + std::string(key) + "\":\"";
auto p = text.find(needle);
if (p == std::string_view::npos) return "";
p += needle.size();
std::string out;
while (p < text.size()) {
char c = text[p++];
if (c == '\\' && p < text.size()) {
char nc = text[p++];
switch (nc) {
case 'n': out.push_back('\n'); break;
case 't': out.push_back('\t'); break;
case 'r': out.push_back('\r'); break;
case '"': out.push_back('"'); break;
case '\\': out.push_back('\\'); break;
default: out.push_back(nc);
}
} else if (c == '"') {
return out;
} else {
out.push_back(c);
}
}
return out;
}
std::optional<long long> extract_int(std::string_view text, std::string_view key) {
auto needle = std::string{"\""} + std::string(key) + "\":";
auto p = text.find(needle);
if (p == std::string_view::npos) return std::nullopt;
p += needle.size();
while (p < text.size() && text[p] == ' ') ++p;
bool neg = false;
if (p < text.size() && text[p] == '-') { neg = true; ++p; }
long long n = 0;
bool any = false;
while (p < text.size() && std::isdigit(static_cast<unsigned char>(text[p]))) {
n = n * 10 + (text[p++] - '0');
any = true;
}
if (!any) return std::nullopt;
return neg ? -n : n;
}
std::optional<bool> extract_bool(std::string_view text, std::string_view key) {
auto needle = std::string{"\""} + std::string(key) + "\":";
auto p = text.find(needle);
if (p == std::string_view::npos) return std::nullopt;
p += needle.size();
while (p < text.size() && text[p] == ' ') ++p;
if (text.substr(p, 4) == "true") return true;
if (text.substr(p, 5) == "false") return false;
return std::nullopt;
}
std::string extract_object(std::string_view text, std::string_view key) {
auto needle = std::string{"\""} + std::string(key) + "\":";
auto p = text.find(needle);
if (p == std::string_view::npos) return "";
p += needle.size();
while (p < text.size() && text[p] == ' ') ++p;
if (p >= text.size() || (text[p] != '{' && text[p] != '[')) return "";
char open = text[p];
char close = (open == '{') ? '}' : ']';
int depth = 0;
std::size_t start = p;
bool in_string = false;
while (p < text.size()) {
char c = text[p];
if (in_string) {
if (c == '\\' && p + 1 < text.size()) { p += 2; continue; }
if (c == '"') in_string = false;
++p; continue;
}
if (c == '"') { in_string = true; ++p; continue; }
if (c == open) { ++depth; }
else if (c == close) {
--depth;
if (depth == 0) return std::string(text.substr(start, p - start + 1));
}
++p;
}
return "";
}
// ─── NDJSON event parser ────────────────────────────────────────────
std::optional<Event> parse_event_line(std::string_view line) {
auto kind = extract_string(line, "kind");
if (kind == "progress") {
ProgressEvent e;
e.phase = extract_string(line, "phase");
e.percent = static_cast<int>(extract_int(line, "percent").value_or(0));
e.message = extract_string(line, "message");
return e;
}
if (kind == "log") {
LogEvent e;
e.level = extract_string(line, "level");
e.message = extract_string(line, "message");
return e;
}
if (kind == "data") {
DataEvent e;
e.dataKind = extract_string(line, "dataKind");
e.payloadJson= extract_object(line, "payload");
return e;
}
if (kind == "error") {
ErrorEvent e;
e.code = extract_string(line, "code");
e.message = extract_string(line, "message");
e.hint = extract_string(line, "hint");
e.recoverable = extract_bool(line, "recoverable").value_or(false);
return e;
}
if (kind == "result") {
ResultEvent e;
e.exitCode = static_cast<int>(extract_int(line, "exitCode").value_or(0));
return e;
}
// heartbeat and unknown kinds
return std::nullopt;
}
// ─── Subprocess call ────────────────────────────────────────────────
std::expected<CallResult, std::string>
call(const Env& env, std::string_view capability,
std::string_view argsJson, EventHandler* handler)
{
auto cmd = build_interface_command(env, capability, argsJson);
CallResult result;
int rc = mcpp::platform::process::run_streaming(cmd,
[&](std::string_view line) {
if (line.empty()) return;
auto ev = parse_event_line(line);
if (!ev) return;
std::visit([&](auto&& e) {
using T = std::decay_t<decltype(e)>;
if constexpr (std::is_same_v<T, ProgressEvent>) {
if (handler) handler->on_progress(e);
} else if constexpr (std::is_same_v<T, LogEvent>) {
if (handler) handler->on_log(e);
} else if constexpr (std::is_same_v<T, DataEvent>) {
result.dataEvents.push_back(e);
if (handler) handler->on_data(e);
} else if constexpr (std::is_same_v<T, ErrorEvent>) {
result.error = e;
if (handler) handler->on_error(e);
} else if constexpr (std::is_same_v<T, ResultEvent>) {
result.exitCode = e.exitCode;
result.resultJson = e.dataJson;
if (handler) handler->on_result(e);
}
}, *ev);
});
if (rc != 0 && result.exitCode == 0) result.exitCode = rc;
return result;
}
// ─── install_with_progress ──────────────────────────────────────────
int install_with_progress(const Env& env, std::string_view target,
const BootstrapProgressCallback& cb)
{
auto argsJson = std::format(
R"({{"targets":["{}"],"yes":true}})", target);
// All platforms: try direct `xlings install ... -y` first.
// The direct command is more reliable for large packages (e.g. LLVM
// ~800MB) because:
// - it doesn't pipe through NDJSON interface (simpler subprocess chain)
// - xlings manages its own stdin/stdout/stderr
// - extraction subprocess coordination works normally
// The NDJSON interface path is kept as a fallback for progress reporting.
{
auto directCmd = build_command_prefix(env) +
std::format(" install {} -y {}", target, mcpp::platform::shell::silent_redirect);
// Use std::system() directly — do NOT redirect stdin via </dev/null
// because xlings may need stdin for subprocess coordination during
// large package extraction.
int directRc = mcpp::platform::process::extract_exit_code(
std::system(directCmd.c_str()));
if (directRc == 0) return 0;
}
// Fallback: NDJSON interface path (provides progress callbacks).
auto cmd = [&]() -> std::string {
if constexpr (mcpp::platform::is_windows) {
return std::format("{} interface install_packages --args {} {}",
build_command_prefix(env),
shq(argsJson),
mcpp::platform::null_redirect);
} else {
return std::format(
"cd {} && env -u XLINGS_PROJECT_DIR XLINGS_HOME={} {} interface install_packages --args {} {}",
shq(env.home.string()),
shq(env.home.string()),
shq(env.binary.string()),
shq(argsJson),
mcpp::platform::null_redirect);
}
}();
int resultExitCode = -1;
auto handle_line = [&](std::string_view line) {
LineScan ls{line};
auto kind = ls.find_str("kind");
if (kind == "result") {
resultExitCode = static_cast<int>(ls.find_num("exitCode"));
return;
}
if (kind != "data") return;
if (ls.find_str("dataKind") != "download_progress") return;
if (!cb) return;
auto p = line.find("\"files\":[");
if (p == std::string_view::npos) return;
p += 9;
BootstrapProgress prog;
prog.elapsedSec = ls.find_num("elapsedSec");
while (p < line.size()) {
while (p < line.size() && (line[p] == ' ' || line[p] == '\n'
|| line[p] == ',')) ++p;
if (p >= line.size() || line[p] == ']') break;
if (line[p] != '{') break;
int depth = 0;
auto start = p;
bool in_string = false;
for (; p < line.size(); ++p) {
char c = line[p];
if (in_string) {
if (c == '\\' && p + 1 < line.size()) { ++p; continue; }
if (c == '"') in_string = false;
continue;
}
if (c == '"') in_string = true;
else if (c == '{') ++depth;
else if (c == '}') { if (--depth == 0) { ++p; break; } }
}
LineScan fl{line.substr(start, p - start)};
BootstrapFile f;
f.name = fl.find_str("name");
f.downloadedBytes = fl.find_num("downloadedBytes");
f.totalBytes = fl.find_num("totalBytes");
f.started = fl.find_bool("started");
f.finished = fl.find_bool("finished");
if (!f.name.empty()) prog.files.push_back(std::move(f));
}
if (!prog.files.empty()) cb(prog);
};
int closeRc = mcpp::platform::process::run_streaming(cmd, handle_line);
return (resultExitCode != -1) ? resultExitCode : closeRc;
}
// ─── Sandbox lifecycle ──────────────────────────────────────────────
void seed_xlings_json(const Env& env,
std::span<const std::pair<std::string,std::string>> repos,
std::string_view mirror)
{
auto path = env.home / ".xlings.json";
std::string json = "{\n";
json += " \"index_repos\": [\n";
for (std::size_t i = 0; i < repos.size(); ++i) {
json += std::format(" {{ \"name\": \"{}\", \"url\": \"{}\" }}{}\n",
repos[i].first, repos[i].second,
i + 1 == repos.size() ? "" : ",");
}
json += " ],\n";
json += " \"lang\": \"en\",\n";
json += std::format(" \"mirror\": \"{}\"\n", mirror);
json += "}\n";
write_file(path, json);
}
int config_show(const Env& env) {
auto cmd = std::format("{} config", build_command_prefix(env));
return mcpp::platform::process::run_silent(cmd);
}
int config_set_mirror(const Env& env, std::string_view mirror, bool quiet) {
if (mirror.empty()) return 0;
auto cmd = std::format(
"{} config --mirror {} {}",
build_command_prefix(env),
shq(mirror),
quiet ? mcpp::platform::shell::silent_redirect : "");
return mcpp::platform::process::run_silent(cmd);
}
void ensure_init(const Env& env, bool quiet) {
auto marker = paths::sandbox_init_marker(env);
if (std::filesystem::exists(marker)) return;
// Ensure the home directory exists before cd'ing into it.
std::error_code ec;
std::filesystem::create_directories(env.home, ec);
if (!quiet)
print_status("Initialize", "mcpp sandbox layout (one-time)");
std::string cmd;
if constexpr (mcpp::platform::is_windows) {
mcpp::platform::env::set("XLINGS_HOME", env.home.string());
mcpp::platform::env::set("XLINGS_PROJECT_DIR", "");
cmd = env.binary.string() + " self init "
+ std::string(mcpp::platform::shell::silent_redirect);
} else {
cmd = std::format(
"cd {} && env -u XLINGS_PROJECT_DIR XLINGS_HOME={} {} self init {}",
shq(env.home.string()),
shq(env.home.string()),
shq(env.binary.string()),
mcpp::platform::shell::silent_redirect);
}
int rc = mcpp::platform::process::run_silent(cmd);
if (rc != 0 && !quiet) {
std::println(stderr,
"warning: `xlings self init` failed for sandbox at '{}'",
env.home.string());
}
}
void ensure_patchelf(const Env& env, bool quiet,
const BootstrapProgressCallback& cb)
{
auto marker = paths::xim_tool(env, "patchelf", pinned::kPatchelfVersion)
/ "bin" / "patchelf";
if (std::filesystem::exists(marker)) return;
if (!quiet)
print_status("Bootstrap", "patchelf into mcpp sandbox (one-time)");
int rc = install_with_progress(env,
std::format("xim:patchelf@{}", pinned::kPatchelfVersion), cb);
if (rc != 0 && !quiet) {
std::println(stderr,
"warning: failed to bootstrap patchelf into mcpp sandbox; "
"subsequent xim installs may skip ELF rewriting");
}
}
void ensure_ninja(const Env& env, bool quiet,
const BootstrapProgressCallback& cb)
{
auto root = paths::xim_tool_root(env, "ninja");
if (std::filesystem::exists(root)) {
std::error_code ec;
auto ninja_name = std::string("ninja") + std::string(mcpp::platform::exe_suffix);
for (auto& v : std::filesystem::directory_iterator(root, ec)) {
if (std::filesystem::exists(v.path() / ninja_name)) return;
}
}
if (!quiet)
print_status("Bootstrap", "ninja into mcpp sandbox (one-time)");
int rc = install_with_progress(env,
std::format("xim:ninja@{}", pinned::kNinjaVersion), cb);
if (rc != 0 && !quiet) {
std::println(stderr,
"warning: failed to bootstrap ninja into mcpp sandbox (exit {})",
rc);
}
}
// ─── Index freshness ────────────────────────────────────────────────
bool is_index_fresh(const Env& env, std::int64_t ttlSeconds) {
auto data = paths::index_data(env);
if (!std::filesystem::exists(data)) return false;
// Look for any directory under data/ that has a pkgs/ subdirectory —
// that's a cloned index repo.
std::error_code ec;
bool hasIndex = false;
std::filesystem::file_time_type newest{};
for (auto& entry : std::filesystem::directory_iterator(data, ec)) {
if (!entry.is_directory()) continue;
auto pkgsDir = entry.path() / "pkgs";
if (!std::filesystem::exists(pkgsDir)) continue;
hasIndex = true;
auto t = std::filesystem::last_write_time(pkgsDir, ec);
if (!ec && t > newest) newest = t;
}
if (!hasIndex) return false;
// Check TTL
auto now = std::filesystem::file_time_type::clock::now();
auto age = std::chrono::duration_cast<std::chrono::seconds>(now - newest);
return age.count() < ttlSeconds;
}
int update_index(const Env& env, bool quiet) {
std::string cmd = build_command_prefix(env) + " update 2>&1";
return mcpp::platform::process::run_streaming(cmd,
[quiet](std::string_view line) {
if (!quiet) std::println("{}", line);
});
}
void ensure_index_fresh(const Env& env, std::int64_t ttlSeconds, bool quiet) {
if (is_index_fresh(env, ttlSeconds)) return;
if (!quiet)
print_status("Updating", "package index (auto-refresh)");
update_index(env, quiet);
}
} // namespace mcpp::xlings