-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNewCommand.cpp
More file actions
576 lines (509 loc) · 18.4 KB
/
Copy pathNewCommand.cpp
File metadata and controls
576 lines (509 loc) · 18.4 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
/**
* @file NewCommand.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.
*
* Entry-point for `vix new`. Orchestrates argument parsing, interactive
* prompts, and project scaffolding by delegating to:
* - vix::commands::new_cmd::tui (interactive menus)
* - vix::commands::new_cmd::generator (file-system scaffolding)
*/
#include <vix/cli/commands/NewCommand.hpp>
#include <vix/cli/commands/new/NewGenerator.hpp>
#include <vix/cli/commands/new/NewTui.hpp>
#include <vix/cli/commands/new/NewTypes.hpp>
#include <vix/cli/Utils.hpp>
#include <vix/cli/util/Ui.hpp>
#include <algorithm>
#include <filesystem>
#include <iostream>
#include <optional>
#include <stdexcept>
#include <string>
#include <vector>
namespace fs = std::filesystem;
namespace gen = vix::commands::new_cmd::generator;
namespace tui = vix::commands::new_cmd::tui;
using vix::commands::new_cmd::FeaturesSelection;
using vix::commands::new_cmd::InPlaceDirChoice;
using vix::commands::new_cmd::OverwriteChoice;
using vix::commands::new_cmd::TemplateKind;
using namespace vix::cli::util;
namespace
{
// ------------------------------------------------------------------
// Argv helpers (local to this TU)
// ------------------------------------------------------------------
bool consume_flag(std::vector<std::string> &a, const std::string &flag)
{
auto it = std::find(a.begin(), a.end(), flag);
if (it == a.end())
return false;
a.erase(it);
return true;
}
bool has_any(const std::vector<std::string> &a, const std::vector<std::string> &candidates)
{
for (const auto &x : candidates)
if (std::find(a.begin(), a.end(), x) != a.end())
return true;
return false;
}
std::optional<std::string> take_option_value(
std::vector<std::string> &a,
const std::vector<std::string> &names)
{
for (const auto &n : names)
{
for (std::size_t i = 0; i < a.size(); ++i)
{
if (a[i] == n)
{
if (i + 1 >= a.size())
return std::nullopt;
std::string v = a[i + 1];
a.erase(a.begin() + static_cast<std::ptrdiff_t>(i), a.begin() + static_cast<std::ptrdiff_t>(i) + 2);
return v;
}
const std::string prefix = n + "=";
if (a[i].rfind(prefix, 0) == 0)
{
std::string v = a[i].substr(prefix.size());
a.erase(a.begin() + static_cast<std::ptrdiff_t>(i));
return v;
}
}
}
return std::nullopt;
}
} // anonymous namespace
namespace vix::commands::NewCommand
{
int run(const std::vector<std::string> &argsIn)
{
// Return codes: 0 = success | 2 = user cancelled | 1 = error
if (argsIn.empty())
{
error("Missing project name.");
hint("Usage: vix new <name|path> [-d|--dir <base_dir>] [--app|--lib|--game] [--force]");
return 1;
}
std::vector<std::string> args = argsIn;
const bool force = consume_flag(args, "--force");
const bool apiOnly = consume_flag(args, "--api-only");
const std::optional<std::string> baseOpt = take_option_value(args, {"-d", "--dir"});
const std::optional<std::string> templateOpt =
take_option_value(args, {"--template"});
const bool wantsLib = has_any(args, {"--lib", "--library", "--type=lib", "--type=library"});
const bool wantsApp = has_any(args, {"--app", "--application", "--type=app", "--type=application"});
const bool wantsGame = has_any(args, {"--game", "--type=game"});
for (const auto &f : {"--lib", "--library", "--type=lib", "--type=library",
"--app", "--application", "--type=app", "--type=application",
"--game", "--type=game"})
consume_flag(args, f);
if (args.empty())
{
error("Missing project name.");
hint("Usage: vix new <name|path> [--template vue|game] [--app|--lib|--game] [--force]");
return 1;
}
if ((wantsLib && wantsApp) || (wantsLib && wantsGame) || (wantsApp && wantsGame))
{
error("Conflicting options: choose only one project type.");
return 1;
}
if (!args[0].empty() && args[0][0] == '-' && !gen::is_dot_path(args[0]))
{
error("Missing project name.");
hint("Options must come after a project name or be followed by one.");
step("vix new my-app --template vue");
return 1;
}
if (templateOpt.has_value())
{
const std::string tpl = *templateOpt;
if (tpl != "vue" && tpl != "game" && tpl != "backend" && tpl != "web")
{
error("Unknown template: " + tpl);
hint("Supported templates: backend, web, vue, game");
return 1;
}
if (wantsLib)
{
error("Conflicting options: --template cannot be used with --lib.");
hint("Templates currently supported: vue, game, backend.");
return 1;
}
if (wantsApp && *templateOpt == "game")
{
error("Conflicting options: --template game cannot be used with --app.");
hint("Use --game or --template game.");
return 1;
}
if (wantsGame && *templateOpt == "vue")
{
error("Conflicting options: --template vue cannot be used with --game.");
hint("Use --app, --template vue, or --template game.");
return 1;
}
if (wantsGame && *templateOpt == "backend")
{
error("Conflicting options: --template backend cannot be used with --game.");
hint("Use --template backend.");
return 1;
}
if (wantsGame && *templateOpt == "web")
{
error("Conflicting options: --template web cannot be used with --game.");
hint("Use --template web.");
return 1;
}
}
if (apiOnly && (!templateOpt.has_value() || *templateOpt != "backend"))
{
error("Conflicting options: --api-only requires --template backend.");
hint("Use: vix new <name> --template backend --api-only");
return 1;
}
const std::string nameOrPath = args[0];
const bool inPlace = gen::is_dot_path(nameOrPath);
try
{
tui::SignalGuard sig;
if (tui::g_cancelled.load())
return 2;
// ------------------------------------------------------------------
// Step 0 – Resolve destination path
// ------------------------------------------------------------------
fs::path dest;
fs::path np = fs::path(nameOrPath);
if (baseOpt.has_value())
{
fs::path base = fs::path(*baseOpt);
std::error_code ec;
if (!fs::exists(base, ec) || !fs::is_directory(base, ec))
{
error("Base directory '" + base.string() + "' is not a valid folder.");
hint("Make sure it exists and is a directory, or omit --dir.");
return 1;
}
dest = np.is_absolute() ? np : fs::weakly_canonical(base / np, ec);
if (ec)
dest = base / np;
}
else
{
std::error_code ec;
dest = np.is_absolute() ? np : fs::weakly_canonical(fs::current_path() / np, ec);
if (ec)
dest = fs::current_path() / np;
}
fs::path projectDir = inPlace ? fs::current_path() : dest;
std::string projName = inPlace ? gen::current_dir_name() : projectDir.filename().string();
if (tui::g_cancelled.load())
return 2;
// ------------------------------------------------------------------
// Step 1 – Existing-directory handling
// ------------------------------------------------------------------
if (fs::exists(projectDir))
{
if (!gen::dir_exists(projectDir))
{
error("Path exists but is not a directory: '" + projectDir.string() + "'");
return 1;
}
if (!gen::dir_is_empty(projectDir))
{
if (inPlace)
{
if (!force)
{
if (tui::can_interact())
{
const auto choice = tui::confirm_inplace_dir_interactive(projectDir);
if (choice.cancelled || choice.value == InPlaceDirChoice::Cancel)
return 2;
}
else
{
error("Cannot create project in current directory: directory is not empty.");
hint("Use --force to overwrite template files.");
return 1;
}
}
// force=true → proceed (overwrite files, do NOT delete the dir)
}
else
{
if (force)
{
std::error_code ec;
fs::remove_all(projectDir, ec);
if (ec)
{
error("Failed to remove existing directory.");
hint(ec.message());
return 1;
}
}
else if (tui::can_interact())
{
const auto choice = tui::confirm_overwrite_interactive(projectDir);
if (choice.cancelled || choice.value == OverwriteChoice::Cancel)
return 2;
std::error_code ec;
fs::remove_all(projectDir, ec);
if (ec)
{
error("Failed to remove existing directory.");
hint(ec.message());
return 1;
}
}
else
{
error("Cannot create project in '" + projectDir.string() + "': directory is not empty.");
hint("Use --force to overwrite.");
return 1;
}
}
if (tui::g_cancelled.load())
return 2;
}
}
// ------------------------------------------------------------------
// Step 2 – Choose template
// ------------------------------------------------------------------
TemplateKind kind = TemplateKind::App;
if (templateOpt.has_value() && *templateOpt == "backend")
{
kind = TemplateKind::Backend;
}
else if (templateOpt.has_value() && *templateOpt == "web")
{
kind = TemplateKind::Web;
}
else if (templateOpt.has_value() && *templateOpt == "game")
{
kind = TemplateKind::Game;
}
else if (templateOpt.has_value() && *templateOpt == "vue")
{
kind = TemplateKind::Vue;
}
else if (wantsGame)
{
kind = TemplateKind::Game;
}
else if (wantsLib)
{
kind = TemplateKind::Lib;
}
else if (wantsApp)
{
kind = TemplateKind::App;
}
else if (tui::can_interact())
{
const auto sel = tui::choose_template_interactive();
if (sel.cancelled)
return 2;
kind = sel.value;
}
if (tui::g_cancelled.load())
return 2;
// ------------------------------------------------------------------
// Step 3 – Choose features
// ------------------------------------------------------------------
FeaturesSelection features{};
if ((kind == TemplateKind::App || kind == TemplateKind::Vue || kind == TemplateKind::Backend) &&
tui::can_interact())
{
bool cancelled = false;
features = tui::choose_features_interactive(cancelled);
if (cancelled)
return 2;
}
if (features.full_static)
features.static_rt = true;
if (tui::g_cancelled.load())
return 2;
// ------------------------------------------------------------------
// Step 4 – Create project root
// ------------------------------------------------------------------
{
std::string err;
if (!gen::ensure_dir(projectDir, err))
{
error("Failed to create project directory.");
hint(err);
return 1;
}
}
if (tui::g_cancelled.load())
return 2;
// ------------------------------------------------------------------
// Step 5 – Generate files
// ------------------------------------------------------------------
std::string genErr;
if (kind == TemplateKind::Vue)
{
if (!gen::generate_vue_project(projectDir, projName, features, genErr))
{
vix::cli::util::err_line(std::cerr, "Failed to create project files.");
vix::cli::util::warn_line(std::cerr, genErr);
return 1;
}
vix::cli::util::ok_line(std::cout, "Project created.");
vix::cli::util::kv(std::cout, "Location", projectDir.string());
gen::print_next_steps_vue(projectDir, projName);
return 0;
}
if (kind == TemplateKind::Backend)
{
if (!gen::generate_backend_project(projectDir, projName, features, apiOnly, genErr))
{
vix::cli::util::err_line(std::cerr, "Failed to create project files.");
vix::cli::util::warn_line(std::cerr, genErr);
return 1;
}
vix::cli::util::ok_line(std::cout, "Project created.");
vix::cli::util::kv(std::cout, "Location", projectDir.string());
gen::print_next_steps_backend(projectDir, projName);
return 0;
}
if (kind == TemplateKind::Web)
{
if (!gen::generate_web_project(projectDir, projName, features, genErr))
{
vix::cli::util::err_line(std::cerr, "Failed to create project files.");
vix::cli::util::warn_line(std::cerr, genErr);
return 1;
}
vix::cli::util::ok_line(std::cout, "Project created.");
vix::cli::util::kv(std::cout, "Location", projectDir.string());
gen::print_next_steps_web(projectDir, projName);
return 0;
}
if (kind == TemplateKind::Game)
{
if (!gen::generate_game_project(projectDir, projName, genErr))
{
vix::cli::util::err_line(std::cerr, "Failed to create project files.");
vix::cli::util::warn_line(std::cerr, genErr);
return 1;
}
vix::cli::util::ok_line(std::cout, "Project created.");
vix::cli::util::kv(std::cout, "Location", projectDir.string());
gen::print_next_steps_game(projectDir, projName);
return 0;
}
if (kind == TemplateKind::App)
{
if (!gen::generate_app_project(projectDir, projName, features, genErr))
{
vix::cli::util::err_line(std::cerr, "Failed to create project files.");
vix::cli::util::warn_line(std::cerr, genErr);
return 1;
}
vix::cli::util::ok_line(std::cout, "Project created.");
vix::cli::util::kv(std::cout, "Location", projectDir.string());
gen::print_next_steps_app(projectDir, projName);
return 0;
}
if (!gen::generate_lib_project(projectDir, projName, genErr))
{
vix::cli::util::err_line(std::cerr, "Failed to create project files.");
vix::cli::util::warn_line(std::cerr, genErr);
return 1;
}
vix::cli::util::ok_line(std::cout, "Project created.");
vix::cli::util::kv(std::cout, "Location", projectDir.string());
gen::print_next_steps_lib(projectDir, projName);
return 0;
}
catch (const std::exception &ex)
{
error("Failed to create project.");
hint(ex.what());
return 1;
}
}
int help()
{
std::ostream &out = std::cout;
out
<< "vix new\n"
<< "Create a new Vix project.\n\n"
<< "Usage\n"
<< " vix new <name|path> [options]\n\n"
<< "Examples\n"
<< " vix new api\n"
<< " vix new .\n"
<< " vix new tree --lib\n"
<< " vix new api --template backend\n"
<< " vix new blog --template backend --api-only\n"
<< " vix new blog --template web\n"
<< " vix new mario --game\n"
<< " vix new shop --template vue\n"
<< " vix new platformer --template game\n"
<< " vix new blog -d ./projects\n"
<< " vix new api --force\n\n"
<< "What happens\n"
<< " • Generates a ready-to-run Vix project\n"
<< " • Sets up source structure and config files\n"
<< " • Creates a vix.json manifest\n"
<< " • Creates a vix.app manifest when the template uses the Vix app workflow\n"
<< " • For apps, creates an executable target matching the project name\n"
<< " • For libraries, creates a header-only CMake interface target\n"
<< " • Applies the selected template: app, backend, game, library, or Vue\n\n"
<< "Options\n"
<< " --app Generate an application (default)\n"
<< " --game Generate a Vix game project\n"
<< " --lib Generate a header-only library\n"
<< " --template Project template: backend, web, vue, game\n"
<< " --api-only Generate backend API without public files or views\n"
<< " -d, --dir Base directory for project creation\n"
<< " --force Overwrite existing directory\n\n"
<< "Web workflow\n"
<< " cd blog/\n"
<< " cp .env.example .env\n"
<< " vix dev\n"
<< " open http://127.0.0.1:8080\n\n"
<< "Application workflow\n"
<< " cd api/\n"
<< " vix build\n"
<< " vix run\n\n"
<< "Backend workflow\n"
<< " cd api/\n"
<< " cp .env.example .env\n"
<< " vix dev\n"
<< " curl http://127.0.0.1:8080/health\n\n"
<< "Game workflow\n"
<< " cd mario/\n"
<< " vix build\n"
<< " vix run\n\n"
<< "Library workflow\n"
<< " cd tree/\n"
<< " vix build --build-target all\n"
<< " vix build --build-target all -- -Dtree_BUILD_TESTS=ON\n"
<< " vix tests\n\n"
<< "Environment\n"
<< " VIX_NONINTERACTIVE=1 Disable prompts\n"
<< " CI=1 Disable prompts\n\n"
<< "Notes\n"
<< " • Use '.' to initialize in the current directory\n"
<< " • Use 'vix add <pkg>' to add dependencies\n"
<< " • Use 'vix install' to install from vix.lock\n"
<< " • Use 'vix task <name>' to run generated tasks\n"
<< " • Backend projects are generated with production-oriented folders and config\n"
<< " • For header-only libraries, use '--build-target all' to build the generated project\n"
<< " • Tests for header-only libraries are disabled by default and must be enabled with CMake args\n"
<< " • Designed for fast start with zero setup\n";
return 0;
}
} // namespace vix::commands::NewCommand