-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRegistryCommand.cpp
More file actions
316 lines (265 loc) · 8.29 KB
/
Copy pathRegistryCommand.cpp
File metadata and controls
316 lines (265 loc) · 8.29 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
/**
*
* @file RegistryCommand.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/RegistryCommand.hpp>
#include <vix/cli/util/Shell.hpp>
#include <vix/cli/util/Ui.hpp>
#include <vix/cli/Style.hpp>
#include <vix/utils/Env.hpp>
#include <nlohmann/json.hpp>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
namespace fs = std::filesystem;
using json = nlohmann::ordered_json;
namespace vix::commands
{
using namespace vix::cli::style;
namespace
{
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 manifest_path()
{
return fs::current_path() / "vix.json";
}
std::string registry_repo_url()
{
return "https://github.com/vixcpp/registry.git";
}
static int git_run(const std::string &cmd)
{
return vix::cli::util::run_cmd_retry_debug(cmd);
}
static std::string default_project_name()
{
std::string name = fs::current_path().filename().string();
if (name.empty())
name = "my-lib";
return name;
}
static int normalize_registry_worktree(const fs::path &dir, bool quiet)
{
if (!quiet)
step("fetching origin (prune)...");
{
const std::string cmd =
"git -C " + dir.string() + " fetch -q origin --prune";
const int rc = git_run(cmd);
if (rc != 0)
return rc;
}
if (!quiet)
step("checking out main...");
{
const std::string cmd =
"git -C " + dir.string() + " checkout -q -B main origin/main";
const int rc = git_run(cmd);
if (rc != 0)
return rc;
}
if (!quiet)
step("resetting to origin/main...");
{
const std::string cmd =
"git -C " + dir.string() + " reset -q --hard origin/main";
const int rc = git_run(cmd);
if (rc != 0)
return rc;
}
return 0;
}
static int init_registry_manifest(bool force)
{
const fs::path path = manifest_path();
vix::cli::util::section(std::cout, "Registry");
vix::cli::util::kv(std::cout, "path", path.string());
if (fs::exists(path) && !force)
{
vix::cli::util::err_line(std::cerr, "vix.json already exists");
vix::cli::util::warn_line(std::cerr, "Use: vix registry init --force");
return 1;
}
const std::string projectName = default_project_name();
json manifest = json::object();
manifest["name"] = projectName;
manifest["namespace"] = "your-namespace";
manifest["version"] = "0.1.0";
manifest["type"] = "header-only";
manifest["include"] = "include";
manifest["deps"] = json::array();
manifest["license"] = "MIT";
manifest["description"] = "A tiny header-only C++ library.";
manifest["keywords"] = json::array({"cpp", "header-only", "vix"});
manifest["repository"] = "https://github.com/your-username/" + projectName;
manifest["authors"] = json::array({
json::object({
{"name", "Your Name"},
{"github", "your-username"},
}),
});
std::ofstream out(path);
if (!out)
{
vix::cli::util::err_line(std::cerr, "failed to create vix.json");
return 1;
}
out << manifest.dump(2) << "\n";
out.close();
vix::cli::util::ok_line(std::cout, "created: " + path.string());
return 0;
}
int sync_registry_impl(bool quiet, bool manualHints)
{
const fs::path dir = registry_dir();
fs::create_directories(dir.parent_path());
if (!quiet)
{
vix::cli::util::section(std::cout, "Registry");
vix::cli::util::kv(std::cout, "path", dir.string());
}
auto print_sync_failure = [&]()
{
if (!quiet)
vix::cli::util::err_line(std::cerr, "registry sync failed");
if (manualHints)
vix::cli::util::warn_line(std::cerr, "Check network + git access, then retry: vix registry sync");
if (manualHints && !vix::cli::util::debug_enabled())
vix::cli::util::warn_line(std::cerr, "Tip: re-run with VIX_DEBUG=1 to see git output");
};
if (!fs::exists(dir))
{
if (!quiet)
step("cloning index (depth=1)...");
const std::string cmd =
"git clone -q --depth 1 " + registry_repo_url() + " " + dir.string();
const int rc = git_run(cmd);
if (rc != 0)
{
print_sync_failure();
return rc;
}
const int nrc = normalize_registry_worktree(dir, quiet);
if (nrc != 0)
{
print_sync_failure();
return nrc;
}
if (!quiet)
vix::cli::util::ok_line(std::cout, "registry synced: " + dir.string());
return 0;
}
if (!quiet)
step("normalizing worktree...");
const int nrc = normalize_registry_worktree(dir, quiet);
if (nrc != 0)
{
print_sync_failure();
return nrc;
}
if (!quiet)
vix::cli::util::ok_line(std::cout, "registry synced: " + dir.string());
return 0;
}
} // namespace
int RegistryCommand::sync(bool quiet, bool manualHints)
{
return sync_registry_impl(quiet, manualHints);
}
int RegistryCommand::run(const std::vector<std::string> &args)
{
if (args.empty())
return help();
const std::string sub = args[0];
if (sub == "sync")
return RegistryCommand::sync();
if (sub == "path")
{
vix::cli::util::section(std::cout, "Registry");
vix::cli::util::kv(std::cout, "path", registry_dir().string());
return 0;
}
if (sub == "init")
{
bool force = false;
for (std::size_t i = 1; i < args.size(); ++i)
{
const std::string &arg = args[i];
if (arg == "--force")
{
force = true;
continue;
}
vix::cli::util::err_line(std::cerr, "unknown flag: " + arg);
vix::cli::util::warn_line(std::cerr, "Try: vix registry init");
vix::cli::util::warn_line(std::cerr, "Try: vix registry init --force");
return 1;
}
return init_registry_manifest(force);
}
vix::cli::util::err_line(std::cerr, "unknown registry subcommand: " + sub);
vix::cli::util::warn_line(std::cerr, "Try: vix registry init");
vix::cli::util::warn_line(std::cerr, "Try: vix registry sync");
vix::cli::util::warn_line(std::cerr, "Try: vix registry path");
return help();
}
int RegistryCommand::help()
{
std::cout
<< "Usage:\n"
<< " vix registry <subcommand>\n\n"
<< "Subcommands:\n"
<< " init Create a local vix.json manifest for a package\n"
<< " sync Update the local registry index\n"
<< " path Print the local registry index path\n\n"
<< "Init options:\n"
<< " --force Overwrite existing vix.json\n\n"
<< "Related commands:\n"
<< " vix search <query> Search packages\n"
<< " vix add <pkg>@<ver> Add a dependency\n"
<< " vix remove <pkg> Remove a dependency\n"
<< " vix list List project dependencies\n"
<< " vix publish <version> Publish a package version\n"
<< " vix store <subcommand> Manage local dependency store\n"
<< " vix registry unpublish <namespace/name> [-y|--yes]\n\n"
<< "Examples:\n"
<< " vix registry init\n"
<< " vix registry init --force\n"
<< " vix registry sync\n"
<< " vix registry path\n"
<< " vix publish 0.2.0\n"
<< " vix store gc\n";
return 0;
}
} // namespace vix::commands