-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUpgradeCommand.cpp
More file actions
3818 lines (3168 loc) · 101 KB
/
Copy pathUpgradeCommand.cpp
File metadata and controls
3818 lines (3168 loc) · 101 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
/**
*
* @file UpgradeCommand.cpp
* @author Gaspard Kirira
*
* Copyright 2026, Gaspard Kirira. All rights reserved.
* https://github.com/vixcpp/vix
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Vix.cpp
*
*/
#include <vix/cli/commands/UpgradeCommand.hpp>
#include <vix/cli/util/Ui.hpp>
#include <vix/cli/util/Shell.hpp>
#include <vix/cli/util/Hash.hpp>
#include <vix/cli/Style.hpp>
#include <vix/cli/sdk/SdkProfiles.hpp>
#include <vix/utils/Env.hpp>
#include <nlohmann/json.hpp>
#include <algorithm>
#include <array>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <filesystem>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <optional>
#include <sstream>
#include <stdexcept>
#include <string>
#include <system_error>
#include <vector>
#ifndef _WIN32
#include <unistd.h>
#endif
#ifdef _WIN32
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#endif
namespace fs = std::filesystem;
using json = nlohmann::json;
namespace vix::commands
{
namespace
{
#ifdef _WIN32
FILE *popen_wrap(const char *cmd, const char *mode)
{
return _popen(cmd, mode);
}
int pclose_wrap(FILE *f)
{
return _pclose(f);
}
#else
FILE *popen_wrap(const char *cmd, const char *mode)
{
return popen(cmd, mode);
}
int pclose_wrap(FILE *f)
{
return pclose(f);
}
#endif
enum class UpgradeKind
{
Cli,
Sdk,
GlobalPackage,
};
struct Options
{
bool check{false};
bool dryRun{false};
bool jsonOut{false};
bool verbose{false};
bool globalMode{false};
bool sdkMode{false};
bool sdkList{false};
bool sdkInfo{false};
std::string sdkProfile{"default"};
std::vector<std::string> sdkProfiles;
std::optional<std::string> version;
std::optional<std::string> globalSpec;
};
struct Platform
{
std::string os;
std::string arch;
std::string label() const
{
return os + "/" + arch;
}
};
struct ReleaseAsset
{
std::string name;
std::string url;
std::string sha256Url;
std::string minisignUrl;
std::optional<long long> downloadBytes;
};
struct UpgradePlan
{
UpgradeKind kind{UpgradeKind::Cli};
Platform platform;
std::string repo;
std::string version;
std::optional<std::string> currentVersion;
std::string sdkProfile;
ReleaseAsset asset;
fs::path installDir;
fs::path destination;
bool supported{true};
std::string unsupportedReason;
};
struct PkgSpec
{
std::string ns;
std::string name;
std::string requestedVersion;
std::string resolvedVersion;
std::string id() const
{
return ns + "/" + name;
}
};
struct DepResolved
{
std::string id;
std::string version;
std::string repo;
std::string tag;
std::string commit;
std::string hash;
std::string type;
std::string include{"include"};
fs::path checkout;
fs::path linkDir;
};
struct ScopedCleanup
{
fs::path p;
~ScopedCleanup()
{
std::error_code ec;
if (!p.empty())
fs::remove_all(p, ec);
}
};
constexpr std::array<const char *, 8> kSdkProfiles{
"default",
"web",
"data",
"desktop",
"p2p",
"game",
"agent",
"all",
};
constexpr const char *kMinisignPubkey =
"RWSIfpPSznK9A1gWUc8Eg2iXXQwU5d9BYuQNKGOcoujAF2stPu5rKFjQ";
std::string trim_copy(std::string s)
{
while (!s.empty() &&
(s.back() == '\n' || s.back() == '\r' ||
std::isspace(static_cast<unsigned char>(s.back()))))
{
s.pop_back();
}
std::size_t i = 0;
while (i < s.size() && std::isspace(static_cast<unsigned char>(s[i])))
++i;
return s.substr(i);
}
std::string to_lower(std::string s)
{
std::transform(
s.begin(),
s.end(),
s.begin(),
[](unsigned char c)
{ return static_cast<char>(std::tolower(c)); });
return s;
}
bool starts_with(const std::string &s, const std::string &prefix)
{
return s.size() >= prefix.size() && s.compare(0, prefix.size(), prefix) == 0;
}
bool wants_json(const std::vector<std::string> &args)
{
return std::find(args.begin(), args.end(), "--json") != args.end();
}
std::string shell_quote(const std::string &s)
{
#ifdef _WIN32
std::string out = "\"";
for (char c : s)
{
if (c == '"')
out += "\\\"";
else
out += c;
}
out += "\"";
return out;
#else
std::string out = "'";
for (char c : s)
{
if (c == '\'')
out += "'\\''";
else
out += c;
}
out += "'";
return out;
#endif
}
std::string quiet_suffix()
{
#ifdef _WIN32
return " >nul 2>&1";
#else
return " >/dev/null 2>&1";
#endif
}
std::string stderr_null_suffix()
{
#ifdef _WIN32
return " 2>nul";
#else
return " 2>/dev/null";
#endif
}
bool have_cmd(const std::string &name)
{
#ifdef _WIN32
const std::string cmd = "where " + shell_quote(name) + " >nul 2>&1";
#else
const std::string cmd = "command -v " + shell_quote(name) + " >/dev/null 2>&1";
#endif
return std::system(cmd.c_str()) == 0;
}
std::string exec_capture(const std::string &cmd)
{
FILE *pipe = popen_wrap(cmd.c_str(), "r");
if (!pipe)
return {};
std::string out;
char buf[4096];
while (true)
{
const std::size_t n = std::fread(buf, 1, sizeof(buf), pipe);
if (n > 0)
out.append(buf, buf + n);
if (n < sizeof(buf))
break;
}
pclose_wrap(pipe);
return out;
}
int exec_status(const std::string &cmd)
{
return std::system(cmd.c_str());
}
int run_quiet_or_verbose(const std::string &cmd, bool verbose)
{
if (verbose)
{
std::cerr << "debug: " << cmd << "\n";
return exec_status(cmd);
}
return exec_status(cmd + quiet_suffix());
}
void print_json(const json &j)
{
std::cout << j.dump(2) << "\n";
}
std::vector<std::string> split_sdk_profiles(const std::string &raw)
{
std::vector<std::string> out;
std::string cur;
auto push = [&]()
{
std::string v = to_lower(trim_copy(cur));
cur.clear();
if (!v.empty())
out.push_back(v);
};
for (char c : raw)
{
if (c == ',' || c == ';')
{
push();
continue;
}
cur.push_back(c);
}
push();
return out;
}
void add_sdk_profile_arg(Options &o, const std::string &raw)
{
for (const auto &profile : split_sdk_profiles(raw))
{
if (!profile.empty())
o.sdkProfiles.push_back(profile);
}
if (!o.sdkProfiles.empty())
o.sdkProfile = o.sdkProfiles.front();
}
namespace ansi
{
constexpr const char *reset = "\033[0m";
constexpr const char *bold = "\033[1m";
constexpr const char *dim = "\033[2m";
constexpr const char *green = "\033[38;5;35m";
constexpr const char *blue = "\033[38;5;32m";
constexpr const char *amber = "\033[38;5;136m";
constexpr const char *red = "\033[38;5;124m";
constexpr const char *muted = "\033[38;5;242m";
}
bool is_tty(std::ostream &os)
{
#ifdef _WIN32
return false;
#else
if (&os == &std::cout)
return isatty(STDOUT_FILENO) != 0;
if (&os == &std::cerr)
return isatty(STDERR_FILENO) != 0;
return false;
#endif
}
struct Fmt
{
bool color;
explicit Fmt(std::ostream &os)
: color(is_tty(os))
{
}
std::string ok() const
{
return color ? std::string(ansi::green) + "✓" + ansi::reset : "ok";
}
std::string dl() const
{
return color ? std::string(ansi::blue) + "↓" + ansi::reset : "↓";
}
std::string arrow() const
{
return color ? std::string(ansi::muted) + "→" + ansi::reset : "→";
}
std::string err() const
{
return color ? std::string(ansi::red) + "✗" + ansi::reset : "error";
}
std::string dot() const
{
return color ? std::string(ansi::muted) + "·" + ansi::reset : " ";
}
std::string hdr() const
{
return color ? std::string(ansi::muted) + "▲" + ansi::reset : "»";
}
std::string grn(const std::string &s) const
{
return color ? std::string(ansi::green) + s + ansi::reset : s;
}
std::string blu(const std::string &s) const
{
return color ? std::string(ansi::blue) + s + ansi::reset : s;
}
std::string red_(const std::string &s) const
{
return color ? std::string(ansi::red) + s + ansi::reset : s;
}
std::string dim_(const std::string &s) const
{
return color ? std::string(ansi::dim) + s + ansi::reset : s;
}
std::string bold_(const std::string &s) const
{
return color ? std::string(ansi::bold) + s + ansi::reset : s;
}
std::string amb(const std::string &s) const
{
return color ? std::string(ansi::amber) + s + ansi::reset : s;
}
};
void blank_line(std::ostream &os = std::cout)
{
os << "\n";
}
void sym_line(std::ostream &os, const std::string &sym, const std::string &msg)
{
os << " " << sym << " " << msg << "\n";
}
void hint_line(std::ostream &os, const Fmt &f, const std::string &msg)
{
os << " " << f.dot() << " " << f.dim_(msg) << "\n";
}
constexpr int kKeyWidth = 10;
void print_kv(
std::ostream &os,
const Fmt &f,
const std::string &key,
const std::string &value)
{
const std::size_t padSize =
key.size() < static_cast<std::size_t>(kKeyWidth)
? static_cast<std::size_t>(kKeyWidth) - key.size()
: 1;
os << " " << f.dim_(key) << std::string(padSize, ' ') << value << "\n";
}
void print_header_box(
std::ostream &os,
const Fmt &f,
const std::string &cmdLabel,
const std::vector<std::pair<std::string, std::string>> &fields)
{
os << " " << f.hdr() << " " << f.grn(f.bold_("vix")) << " "
<< f.dim_(cmdLabel) << "\n";
os << " " << f.dim_(std::string(36, '-')) << "\n";
for (const auto &[key, value] : fields)
print_kv(os, f, key, value);
os << "\n";
}
void styled_step(
std::ostream &os,
const Fmt &f,
const std::string &symKind,
const std::string &msg)
{
std::string sym =
symKind == "dl" ? f.dl()
: symKind == "ok" ? f.ok()
: f.arrow();
sym_line(os, sym, msg);
}
void styled_ok(std::ostream &os, const Fmt &f, const std::string &msg)
{
sym_line(os, f.ok(), msg);
}
void styled_done(std::ostream &os, const Fmt &f, const std::string &detail)
{
sym_line(os, f.ok(), f.bold_(f.grn("Done")) + f.dim_(" — " + detail));
}
void styled_error(
std::ostream &os,
const Fmt &f,
const std::string &msg,
const std::string &hint = "")
{
sym_line(os, f.err(), f.red_(msg));
if (!hint.empty())
hint_line(os, f, hint);
}
// Compatibility wrappers.
// Keep these because run_global_upgrade and old error paths still call them.
void info_line(std::ostream &os, const std::string &line)
{
Fmt f(os);
sym_line(os, f.dot(), line);
}
void step_line(std::ostream &os, const std::string &line)
{
Fmt f(os);
styled_step(os, f, "arrow", line);
}
void error_line(std::ostream &os, const std::string &line)
{
Fmt f(os);
sym_line(os, f.err(), f.red_(line));
}
void field_line(std::ostream &os, const std::string &key, const std::string &value)
{
Fmt f(os);
print_kv(os, f, to_lower(key), value);
}
void verbose_line(const Options &opt, const std::string &line)
{
if (opt.verbose && !opt.jsonOut)
std::cerr << "debug: " << line << "\n";
}
std::string utc_now_iso()
{
std::time_t t = std::time(nullptr);
std::tm tm{};
#ifdef _WIN32
gmtime_s(&tm, &t);
#else
gmtime_r(&t, &tm);
#endif
std::ostringstream oss;
oss << std::setw(4) << std::setfill('0') << (tm.tm_year + 1900)
<< "-"
<< std::setw(2) << std::setfill('0') << (tm.tm_mon + 1)
<< "-"
<< std::setw(2) << std::setfill('0') << tm.tm_mday
<< "T"
<< std::setw(2) << std::setfill('0') << tm.tm_hour
<< ":"
<< std::setw(2) << std::setfill('0') << tm.tm_min
<< ":"
<< std::setw(2) << std::setfill('0') << tm.tm_sec
<< "Z";
return oss.str();
}
std::string home_dir()
{
#ifdef _WIN32
const char *home = vix::utils::vix_getenv("USERPROFILE");
#else
const char *home = vix::utils::vix_getenv("HOME");
#endif
return home ? std::string(home) : std::string();
}
fs::path vix_root()
{
const std::string h = home_dir();
if (h.empty())
return fs::path(".vix");
return fs::path(h) / ".vix";
}
fs::path registry_dir()
{
return vix_root() / "registry" / "index";
}
fs::path registry_index_dir()
{
return registry_dir() / "index";
}
fs::path store_git_dir()
{
return vix_root() / "store" / "git";
}
fs::path global_root_dir()
{
return vix_root() / "global";
}
fs::path global_pkgs_dir()
{
return global_root_dir() / "packages";
}
fs::path global_manifest_path()
{
return global_root_dir() / "installed.json";
}
fs::path sdk_root_dir()
{
return vix_root() / "sdk";
}
fs::path sdk_profile_root(const std::string &profile)
{
return sdk_root_dir() / profile;
}
fs::path sdk_install_dir(const std::string &profile, const std::string &version)
{
return sdk_profile_root(profile) / version;
}
fs::path sdk_current_metadata_path(const std::string &profile)
{
return sdk_profile_root(profile) / "current.json";
}
fs::path sdk_current_pointer_path(const std::string &profile)
{
return sdk_profile_root(profile) / "current";
}
fs::path cmake_user_package_registry_dir()
{
#ifdef _WIN32
if (const char *appdata = vix::utils::vix_getenv("APPDATA"))
return fs::path(appdata) / "CMake" / "packages" / "Vix";
return fs::current_path() / "CMake" / "packages" / "Vix";
#else
if (const char *home = vix::utils::vix_getenv("HOME"))
return fs::path(home) / ".cmake" / "packages" / "Vix";
return fs::current_path() / ".cmake" / "packages" / "Vix";
#endif
}
std::string sanitize_cmake_registry_entry_name(std::string s)
{
for (char &c : s)
{
const unsigned char uc = static_cast<unsigned char>(c);
if (!(std::isalnum(uc) || c == '.' || c == '_' || c == '-'))
c = '_';
}
if (s.empty())
return "vix-sdk";
return s;
}
void register_sdk_with_cmake_user_registry(
const std::string &profile,
const fs::path &installDir)
{
const fs::path configDir = installDir / "lib" / "cmake" / "Vix";
const fs::path configFile = configDir / "VixConfig.cmake";
std::error_code ec;
if (!fs::exists(configFile, ec) || ec)
{
throw std::runtime_error(
"installed SDK is missing VixConfig.cmake: " + configFile.string());
}
const fs::path registryDir = cmake_user_package_registry_dir();
fs::create_directories(registryDir, ec);
if (ec)
{
throw std::runtime_error(
"failed to create CMake user package registry: " + registryDir.string());
}
const fs::path entry =
registryDir / sanitize_cmake_registry_entry_name("vix-sdk-" + profile);
std::ofstream out(entry);
if (!out)
{
throw std::runtime_error(
"failed to write CMake package registry entry: " + entry.string());
}
out << configDir.string() << "\n";
}
fs::path stats_file()
{
#ifdef _WIN32
if (const char *p = vix::utils::vix_getenv("LOCALAPPDATA"))
return fs::path(p) / "Vix" / "install.json";
return fs::current_path() / "install.json";
#else
if (const char *home = vix::utils::vix_getenv("HOME"))
return fs::path(home) / ".local" / "share" / "vix" / "install.json";
return fs::current_path() / "install.json";
#endif
}
json read_json_or_throw(const fs::path &p)
{
std::ifstream in(p);
if (!in)
throw std::runtime_error("cannot open: " + p.string());
json j;
in >> j;
return j;
}
std::optional<json> read_json_if_exists(const fs::path &p)
{
std::error_code ec;
if (!fs::exists(p, ec))
return std::nullopt;
try
{
return read_json_or_throw(p);
}
catch (...)
{
return std::nullopt;
}
}
void write_json_or_throw(const fs::path &p, const json &j)
{
std::error_code ec;
fs::create_directories(p.parent_path(), ec);
std::ofstream out(p);
if (!out)
throw std::runtime_error("cannot write: " + p.string());
out << j.dump(2) << "\n";
}
void write_cli_metadata(
const std::string &repoStr,
const std::string &tag,
const std::string &os,
const std::string &arch,
const fs::path &installDir,
std::optional<long long> downloadBytes,
const std::string &installedVersion,
const std::string &assetUrl)
{
json j;
j["repo"] = repoStr;
j["version"] = tag;
j["installed_version"] = installedVersion;
j["installed_at"] = utc_now_iso();
j["os"] = os;
j["arch"] = arch;
j["install_dir"] = installDir.string();
j["download_bytes"] = downloadBytes.has_value() ? json(*downloadBytes) : json(nullptr);
j["asset_url"] = assetUrl;
write_json_or_throw(stats_file(), j);
}
json make_sdk_metadata(
const std::string &profile,
const std::string &tag,
const Platform &platform,
const fs::path &installDir,
const ReleaseAsset &asset)
{
return json{
{"kind", "sdk"},
{"profile", profile},
{"version", tag},
{"installed_version", tag},
{"os", platform.os},
{"arch", platform.arch},
{"install_dir", installDir.string()},
{"asset_url", asset.url},
{"installed_at", utc_now_iso()},
{"download_bytes", asset.downloadBytes.has_value() ? json(*asset.downloadBytes) : json(nullptr)},
};
}
void write_sdk_metadata(
const std::string &profile,
const std::string &tag,
const Platform &platform,
const fs::path &installDir,
const ReleaseAsset &asset)
{
const json meta = make_sdk_metadata(profile, tag, platform, installDir, asset);
write_json_or_throw(installDir / "install.json", meta);
write_json_or_throw(sdk_current_metadata_path(profile), meta);
}
std::optional<std::string> read_sdk_current_version(const std::string &profile)
{
const auto meta = read_json_if_exists(sdk_current_metadata_path(profile));
if (!meta.has_value())
return std::nullopt;
const std::string version = meta->value("installed_version", meta->value("version", ""));
if (version.empty())
return std::nullopt;
const std::string installDir = meta->value("install_dir", "");
if (!installDir.empty())
{
std::error_code ec;
if (!fs::exists(fs::path(installDir), ec))
return std::nullopt;
}
return version;
}
std::string sanitize_id_dot(const std::string &id)
{
std::string s = id;
for (char &c : s)
{
if (c == '/')
c = '.';
}
return s;
}
fs::path store_checkout_path(const std::string &id, const std::string &commit)
{
return store_git_dir() / sanitize_id_dot(id) / commit;
}
fs::path global_pkg_dir(const std::string &id, const std::string &commit)
{
return global_pkgs_dir() / sanitize_id_dot(id) / commit;
}
fs::path entry_path(const std::string &ns, const std::string &name)
{
return registry_index_dir() / (ns + "." + name + ".json");
}
std::string repo()
{
if (const char *v = vix::utils::vix_getenv("VIX_REPO"))
return std::string(v);
return "vixcpp/vix";
}
std::string current_exe_path()
{
if (const char *p = vix::utils::vix_getenv("VIX_CLI_PATH"))
return std::string(p);
#ifdef _WIN32
return "vix.exe";
#else
return "vix";
#endif
}
std::string normalize_os(std::string os)
{
os = to_lower(trim_copy(os));
if (os == "darwin" || os == "osx" || os == "mac")
return "macos";
if (os == "win32" || os == "win64" || os == "mingw" || os == "msys")
return "windows";
return os;
}
std::string normalize_arch(std::string arch)
{
arch = to_lower(trim_copy(arch));
if (arch == "amd64" || arch == "x64")
return "x86_64";
if (arch == "arm64")
return "aarch64";
return arch;
}
Platform detect_platform()
{
Platform p;
#ifdef _WIN32
p.os = "windows";
#elif __APPLE__
p.os = "macos";
#else
p.os = "linux";
#endif
#if defined(__x86_64__) || defined(_M_X64)
p.arch = "x86_64";
#elif defined(__aarch64__) || defined(_M_ARM64)
p.arch = "aarch64";
#else
p.arch = "unknown";
#endif
p.os = normalize_os(p.os);
p.arch = normalize_arch(p.arch);
return p;
}
bool is_supported_sdk_profile(const std::string &profile)
{
return vix::cli::sdk::is_supported_profile(profile);
}
bool is_sdk_list_token(const std::string &value)
{
const std::string v = to_lower(trim_copy(value));
return v == "list" || v == "ls" || v == "profiles";
}
bool is_sdk_info_token(const std::string &value)
{
const std::string v = to_lower(trim_copy(value));
return v == "info" || v == "about" || v == "show" || v == "deps";
}
std::string cli_asset_name(const Platform &platform)
{
if (platform.os == "windows")
return "vix-windows-" + platform.arch + ".zip";
return "vix-" + platform.os + "-" + platform.arch + ".tar.gz";
}
std::string sdk_asset_name(const std::string &profile, const Platform &platform)
{
return "vix-sdk-" + profile + "-" + platform.os + "-" + platform.arch + ".tar.gz";
}
std::string legacy_sdk_asset_name(const Platform &platform)