-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInfoCommand.cpp
More file actions
606 lines (494 loc) · 16 KB
/
Copy pathInfoCommand.cpp
File metadata and controls
606 lines (494 loc) · 16 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
/**
*
* @file InfoCommand.cpp
* @author Gaspard Kirira
*
* Copyright 2025, 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/InfoCommand.hpp>
#include <vix/cli/util/Ui.hpp>
#include <vix/cli/Style.hpp>
#include <vix/utils/Env.hpp>
#include <nlohmann/json.hpp>
#include <cstdio>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#ifndef VIX_CLI_VERSION
#define VIX_CLI_VERSION "dev"
#endif
namespace fs = std::filesystem;
using json = nlohmann::json;
namespace vix::commands
{
namespace
{
static 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();
}
static fs::path vix_root()
{
const std::string h = home_dir();
if (h.empty())
return fs::path(".vix");
return fs::path(h) / ".vix";
}
static fs::path registry_index_dir()
{
return vix_root() / "registry" / "index";
}
static fs::path store_git_dir()
{
return vix_root() / "store" / "git";
}
static fs::path global_root_dir()
{
return vix_root() / "global";
}
static fs::path global_manifest_path()
{
return global_root_dir() / "installed.json";
}
static fs::path artifact_cache_root()
{
return vix_root() / "cache" / "build";
}
static bool path_exists(const fs::path &p)
{
std::error_code ec;
return fs::exists(p, ec);
}
static bool is_directory_path(const fs::path &p)
{
std::error_code ec;
return fs::is_directory(p, ec);
}
static bool is_regular_file_path(const fs::path &p)
{
std::error_code ec;
return fs::is_regular_file(p, ec);
}
static std::string state_label(const fs::path &p)
{
return path_exists(p) ? "present" : "missing";
}
static std::string path_with_state(const fs::path &p)
{
return p.string() + " [" + state_label(p) + "]";
}
static std::string kind_of_path(const fs::path &p)
{
if (is_directory_path(p))
return "directory";
if (is_regular_file_path(p))
return "file";
return "path";
}
static std::size_t count_directories(const fs::path &root)
{
std::error_code ec;
if (!fs::exists(root, ec) || !fs::is_directory(root, ec))
return 0;
std::size_t count = 0;
for (const auto &entry : fs::directory_iterator(root, fs::directory_options::skip_permission_denied, ec))
{
if (ec)
break;
std::error_code ec2;
if (entry.is_directory(ec2) && !ec2)
++count;
}
return count;
}
static std::size_t count_store_commits(const fs::path &storeRoot)
{
std::error_code ec;
if (!fs::exists(storeRoot, ec) || !fs::is_directory(storeRoot, ec))
return 0;
std::size_t count = 0;
for (const auto &pkgDir : fs::directory_iterator(storeRoot, fs::directory_options::skip_permission_denied, ec))
{
if (ec)
break;
std::error_code ecPkg;
if (!pkgDir.is_directory(ecPkg) || ecPkg)
continue;
for (const auto &commitDir : fs::directory_iterator(pkgDir.path(), fs::directory_options::skip_permission_denied, ec))
{
if (ec)
break;
std::error_code ecCommit;
if (commitDir.is_directory(ecCommit) && !ecCommit)
++count;
}
}
return count;
}
static std::uintmax_t dir_size_bytes(const fs::path &p)
{
std::uintmax_t total = 0;
std::error_code ec;
if (!fs::exists(p, ec))
return 0;
for (auto it = fs::recursive_directory_iterator(
p,
fs::directory_options::skip_permission_denied,
ec);
it != fs::recursive_directory_iterator();
it.increment(ec))
{
if (ec)
continue;
std::error_code ec2;
if (it->is_regular_file(ec2) && !ec2)
{
const auto sz = it->file_size(ec2);
if (!ec2)
total += sz;
}
}
return total;
}
static std::string human_bytes(std::uintmax_t bytes)
{
const char *units[] = {"B", "KB", "MB", "GB", "TB"};
double value = static_cast<double>(bytes);
int unit = 0;
while (value >= 1024.0 && unit < 4)
{
value /= 1024.0;
++unit;
}
char buf[64];
if (unit == 0)
{
std::snprintf(
buf,
sizeof(buf),
"%llu %s",
static_cast<unsigned long long>(bytes),
units[unit]);
}
else
{
std::snprintf(buf, sizeof(buf), "%.2f %s", value, units[unit]);
}
return std::string(buf);
}
static std::size_t count_global_packages_from_manifest()
{
const fs::path manifest = global_manifest_path();
if (!is_regular_file_path(manifest))
return 0;
try
{
std::ifstream in(manifest);
if (!in)
return 0;
json root;
in >> root;
if (!root.is_object())
return 0;
if (!root.contains("packages") || !root["packages"].is_array())
return 0;
return root["packages"].size();
}
catch (...)
{
return 0;
}
}
static std::string yes_no(bool value)
{
return value ? "yes" : "no";
}
static void print_status_line(const std::string &label, const fs::path &path)
{
const std::string state = state_label(path);
const std::string type = kind_of_path(path);
vix::cli::util::kv(
std::cout,
label,
state + " (" + type + ")");
}
static void print_path_line(const std::string &label, const fs::path &path)
{
vix::cli::util::kv(
std::cout,
label,
path_with_state(path));
}
struct ParsedArgs
{
bool globalMode{false};
std::string packageId;
};
static fs::path project_lock_path()
{
return fs::current_path() / "vix.lock";
}
static ParsedArgs parse_args(const std::vector<std::string> &args)
{
ParsedArgs parsed;
for (const auto &arg : args)
{
if (arg == "-g" || arg == "--global")
{
parsed.globalMode = true;
continue;
}
if (parsed.packageId.empty())
parsed.packageId = arg;
}
return parsed;
}
static 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;
}
static const json *get_packages_array(const json &root)
{
if (root.is_array())
return &root;
if (root.is_object() && root.contains("packages") && root["packages"].is_array())
return &root["packages"];
if (root.is_object() && root.contains("dependencies") && root["dependencies"].is_array())
return &root["dependencies"];
return nullptr;
}
static std::string package_version(const json &pkg)
{
if (pkg.contains("version") && pkg["version"].is_string())
return pkg["version"].get<std::string>();
if (pkg.contains("tag") && pkg["tag"].is_string())
{
std::string tag = pkg["tag"].get<std::string>();
if (!tag.empty() && tag.front() == 'v')
tag.erase(tag.begin());
return tag;
}
return {};
}
static std::string package_repo(const json &pkg)
{
if (!pkg.contains("repo"))
return {};
if (pkg["repo"].is_string())
return pkg["repo"].get<std::string>();
if (pkg["repo"].is_object())
return pkg["repo"].value("url", "");
return {};
}
static const json *find_package_by_id(const json &packages, const std::string &id)
{
for (const auto &pkg : packages)
{
if (pkg.value("id", "") == id)
return &pkg;
}
return nullptr;
}
static void print_package_info(const json &pkg, bool globalMode)
{
const std::string id = pkg.value("id", "");
const std::string version = package_version(pkg);
const std::string commit = pkg.value("commit", "");
const std::string repo = package_repo(pkg);
const std::string type = pkg.value("type", "");
const std::string include = pkg.value("include", "");
const std::string installedPath = pkg.value("installed_path", "");
vix::cli::util::section(std::cout, globalMode ? "Global package" : "Project package");
if (!id.empty())
vix::cli::util::kv(std::cout, "id", id);
if (!version.empty())
vix::cli::util::kv(std::cout, "version", version);
if (!commit.empty())
vix::cli::util::kv(std::cout, "commit", commit);
if (!repo.empty())
vix::cli::util::kv(std::cout, "repo", repo);
if (!type.empty())
vix::cli::util::kv(std::cout, "type", type);
if (!include.empty())
vix::cli::util::kv(std::cout, "include", include);
if (!installedPath.empty())
vix::cli::util::kv(std::cout, "path", installedPath);
vix::cli::util::one_line_spacer(std::cout);
vix::cli::util::ok_line(std::cout, "Package info loaded");
}
static int print_package_info_from_file(
const fs::path &path,
const std::string &packageId,
bool globalMode)
{
if (!is_regular_file_path(path))
{
vix::cli::util::err_line(
std::cerr,
globalMode ? "global package manifest not found" : "project lock file not found");
return 1;
}
json root;
try
{
root = read_json_or_throw(path);
}
catch (const std::exception &ex)
{
vix::cli::util::err_line(std::cerr, std::string("failed to read package data: ") + ex.what());
return 1;
}
const json *packages = get_packages_array(root);
if (!packages)
{
vix::cli::util::err_line(
std::cerr,
globalMode ? "invalid global manifest" : "invalid project lock file");
return 1;
}
const json *pkg = find_package_by_id(*packages, packageId);
if (!pkg)
{
vix::cli::util::err_line(std::cerr, "package not found: " + packageId);
return 1;
}
print_package_info(*pkg, globalMode);
return 0;
}
}
int InfoCommand::run(const std::vector<std::string> &args)
{
const ParsedArgs parsed = parse_args(args);
if (!parsed.packageId.empty())
{
if (parsed.globalMode)
{
return print_package_info_from_file(
global_manifest_path(),
parsed.packageId,
true);
}
return print_package_info_from_file(
project_lock_path(),
parsed.packageId,
false);
}
const fs::path root = vix_root();
const fs::path registry = registry_index_dir();
const fs::path store = store_git_dir();
const fs::path globalRoot = global_root_dir();
const fs::path globalManifest = global_manifest_path();
const fs::path artifacts = artifact_cache_root();
const bool rootExists = path_exists(root);
const bool registryExists = path_exists(registry);
const bool storeExists = path_exists(store);
const bool globalRootExists = path_exists(globalRoot);
const bool globalManifestExists = path_exists(globalManifest);
const bool artifactsExists = path_exists(artifacts);
const std::size_t globalPackageCount = count_global_packages_from_manifest();
const std::size_t storePackageDirs = count_directories(store);
const std::size_t storeCommitCount = count_store_commits(store);
const std::uintmax_t storeBytes = dir_size_bytes(store);
const std::uintmax_t artifactBytes = dir_size_bytes(artifacts);
vix::cli::util::section(std::cout, "Info");
vix::cli::util::kv(std::cout, "version", VIX_CLI_VERSION);
vix::cli::util::kv(std::cout, "home", home_dir().empty() ? "(not set)" : home_dir());
vix::cli::util::kv(std::cout, "root", root.string());
vix::cli::util::kv(std::cout, "root state", rootExists ? "present" : "missing");
vix::cli::util::one_line_spacer(std::cout);
vix::cli::util::section(std::cout, "Environment");
print_path_line("vix root", root);
print_path_line("registry", registry);
print_path_line("store", store);
print_path_line("global root", globalRoot);
print_path_line("global manifest", globalManifest);
vix::cli::util::one_line_spacer(std::cout);
vix::cli::util::section(std::cout, "Caches");
print_path_line("artifact cache", artifacts);
vix::cli::util::kv(std::cout, "store packages", std::to_string(storePackageDirs));
vix::cli::util::kv(std::cout, "store commits", std::to_string(storeCommitCount));
vix::cli::util::kv(std::cout, "global packages", std::to_string(globalPackageCount));
vix::cli::util::kv(std::cout, "store size", human_bytes(storeBytes));
vix::cli::util::kv(std::cout, "artifact size", human_bytes(artifactBytes));
vix::cli::util::one_line_spacer(std::cout);
vix::cli::util::section(std::cout, "Status");
print_status_line("registry index", registry);
print_status_line("store cache", store);
print_status_line("global root", globalRoot);
print_status_line("global manifest", globalManifest);
print_status_line("artifact cache", artifacts);
vix::cli::util::kv(std::cout, "globals usable", yes_no(globalRootExists && globalManifestExists));
vix::cli::util::kv(std::cout, "registry usable", yes_no(registryExists));
vix::cli::util::kv(std::cout, "store usable", yes_no(storeExists));
vix::cli::util::kv(std::cout, "artifacts usable", yes_no(artifactsExists));
vix::cli::util::one_line_spacer(std::cout);
if (rootExists)
vix::cli::util::ok_line(std::cout, "Vix environment detected");
else
vix::cli::util::warn_line(std::cout, "Vix root is missing. Some commands may create it on demand.");
return 0;
}
int InfoCommand::help()
{
std::cout
<< "vix info\n"
<< "Show Vix environment, package details, paths, caches, and local state.\n\n"
<< "Usage\n"
<< " vix info\n"
<< " vix info [@]namespace/name\n"
<< " vix info -g [@]namespace/name\n"
<< " vix info --global [@]namespace/name\n\n"
<< "Examples\n"
<< " vix info\n"
<< " vix info softadastra/json\n"
<< " vix info -g softadastra/json\n"
<< " vix info -g @softadastra/json\n\n"
<< "What happens\n"
<< " • vix info shows the local Vix environment\n"
<< " • vix info <package> shows details for a project dependency from vix.lock\n"
<< " • vix info -g <package> shows details for a globally installed package\n\n"
<< "Environment info\n"
<< " • Vix version\n"
<< " • Vix root path\n"
<< " • Registry index path and state\n"
<< " • Store cache path and state\n"
<< " • Global packages manifest path and state\n"
<< " • Build artifact cache path and state\n"
<< " • Number of global packages\n"
<< " • Number of package directories in store/git\n"
<< " • Number of cached commits in store/git\n"
<< " • Disk usage of store/git\n"
<< " • Disk usage of build artifact cache\n\n"
<< "Package info\n"
<< " • Package id\n"
<< " • Version\n"
<< " • Commit\n"
<< " • Repository\n"
<< " • Type\n"
<< " • Include directory\n"
<< " • Installed path, when available\n\n"
<< "Notes\n"
<< " • Project package info is read from ./vix.lock\n"
<< " • Global package info is read from ~/.vix/global/installed.json\n";
return 0;
}
}