-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathModulesContent.cpp
More file actions
1108 lines (897 loc) · 34.6 KB
/
Copy pathModulesContent.cpp
File metadata and controls
1108 lines (897 loc) · 34.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
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 ModulesContent.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.
*/
#include <vix/cli/commands/modules/ModulesContent.hpp>
#include <vix/cli/commands/modules/ModulesUtils.hpp>
#include <sstream>
#include <unordered_set>
#include <cctype>
namespace vix::commands::modules_cmd::content
{
namespace fs = std::filesystem;
using namespace vix::commands::modules_cmd::utils;
// ------------------------------------------------------------------
// Naming helpers
// ------------------------------------------------------------------
std::string normalize_module_id(std::string name)
{
name = trim(name);
std::string out;
out.reserve(name.size());
for (char c : name)
out.push_back(c == '-' ? '_' : c);
return out;
}
std::string module_target_name(const std::string &project, const std::string &module)
{
return project + "_" + normalize_module_id(module);
}
std::string module_alias_name(const std::string &project, const std::string &module)
{
return project + "::" + normalize_module_id(module);
}
// ------------------------------------------------------------------
// Validation
// ------------------------------------------------------------------
bool is_valid_module_name(const std::string &name)
{
if (name.empty())
return false;
for (char c : name)
{
const bool ok =
(c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') ||
(c == '_' || c == '-');
if (!ok)
return false;
}
return true;
}
bool is_reserved_module_name(std::string name)
{
name = to_lower(normalize_module_id(name));
static const std::unordered_set<std::string> reserved = {
"modules", "module", "src", "source", "include", "cmake", "build", "dist",
"test", "tests", "example", "examples", "vendor", "third_party", "thirdparty",
"external", "internal", "internals", "detail", "details", "private", "public",
"main", "app", "api", "core", "std", "vix", "vixcpp",
"registry", "deps", "pack", "lock", "install", "add", "remove", "store", "gc",
"fmt", "spdlog", "boost", "openssl", "zlib", "sqlite", "mysql", "postgres", "curl",
"asio", "beast"};
return reserved.find(name) != reserved.end();
}
std::string module_class_name(const std::string &module)
{
const std::string normalized = normalize_module_id(module);
std::string out;
bool upperNext = true;
for (char c : normalized)
{
if (c == '_' || c == '-')
{
upperNext = true;
continue;
}
if (upperNext)
{
out.push_back(static_cast<char>(
std::toupper(static_cast<unsigned char>(c))));
upperNext = false;
}
else
{
out.push_back(c);
}
}
if (out.empty())
out = "Module";
return out;
}
// ------------------------------------------------------------------
// CMake content generators
// ------------------------------------------------------------------
std::string cmake_vix_modules_cmake_app_first()
{
std::ostringstream o;
o << "##\n";
o << "## Vix Modules (app-first, opt-in)\n";
o << "##\n";
o << "## Contract (Go-like):\n";
o << "## - modules/<m>/include/<m>/... (public headers)\n";
o << "## - modules/<m>/src/... (private impl)\n";
o << "## - Each module exports <project>::<m> as an ALIAS target\n";
o << "## - Public headers must never include private sources (src/)\n";
o << "## - Cross-module usage must be explicit via target_link_libraries\n";
o << "## - vix.app projects load only VIX_ENABLED_MODULES\n";
o << "##\n\n";
o << "if(DEFINED VIX_MODULES_INCLUDED)\n";
o << " return()\n";
o << "endif()\n";
o << "set(VIX_MODULES_INCLUDED ON)\n\n";
o << "set(VIX_MODULES_PROJECT_DIR \"${CMAKE_CURRENT_LIST_DIR}/..\")\n";
o << "set(VIX_MODULES_DIR \"${VIX_MODULES_PROJECT_DIR}/modules\")\n\n";
o << "##\n";
o << "## vix.app mode\n";
o << "##\n";
o << "## When VIX_ENABLED_MODULES is defined by the generated CMakeLists.txt,\n";
o << "## only those modules are loaded. A module folder can exist without\n";
o << "## being active.\n";
o << "##\n";
o << "if(DEFINED VIX_ENABLED_MODULES)\n";
o << " foreach(_m ${VIX_ENABLED_MODULES})\n";
o << " string(REPLACE \"-\" \"_\" _m_norm \"${_m}\")\n";
o << " set(_m_path_var \"VIX_MODULE_${_m_norm}_PATH\")\n\n";
o << " if(DEFINED ${_m_path_var})\n";
o << " set(_m_path \"${${_m_path_var}}\")\n";
o << " if(IS_ABSOLUTE \"${_m_path}\")\n";
o << " set(_m_dir \"${_m_path}\")\n";
o << " else()\n";
o << " set(_m_dir \"${VIX_MODULES_PROJECT_DIR}/${_m_path}\")\n";
o << " endif()\n";
o << " else()\n";
o << " set(_m_dir \"${VIX_MODULES_DIR}/${_m_norm}\")\n";
o << " endif()\n\n";
o << " if(NOT EXISTS \"${_m_dir}\")\n";
o << " message(FATAL_ERROR \"VIX_MODULE_NOT_FOUND module=${_m_norm} path=${_m_dir}\")\n";
o << " endif()\n\n";
o << " if(NOT EXISTS \"${_m_dir}/CMakeLists.txt\")\n";
o << " message(FATAL_ERROR \"VIX_MODULE_CMAKELISTS_NOT_FOUND module=${_m_norm} path=${_m_dir}/CMakeLists.txt\")\n";
o << " endif()\n\n";
o << " add_subdirectory(\"${_m_dir}\" \"${CMAKE_BINARY_DIR}/vix_modules/${_m_norm}\")\n";
o << " endforeach()\n";
o << " return()\n";
o << "endif()\n\n";
o << "##\n";
o << "## Legacy CMake mode\n";
o << "##\n";
o << "## If VIX_ENABLED_MODULES is not defined, keep the old behavior for\n";
o << "## classic CMake projects: load every module folder under modules/*.\n";
o << "##\n";
o << "if(NOT EXISTS \"${VIX_MODULES_DIR}\")\n";
o << " return()\n";
o << "endif()\n\n";
o << "file(GLOB VIX_MODULE_DIRS RELATIVE \"${VIX_MODULES_DIR}\" \"${VIX_MODULES_DIR}/*\")\n";
o << "foreach(_m ${VIX_MODULE_DIRS})\n";
o << " if(IS_DIRECTORY \"${VIX_MODULES_DIR}/${_m}\")\n";
o << " if(EXISTS \"${VIX_MODULES_DIR}/${_m}/CMakeLists.txt\")\n";
o << " add_subdirectory(\"${VIX_MODULES_DIR}/${_m}\" \"${CMAKE_BINARY_DIR}/vix_modules/${_m}\")\n";
o << " endif()\n";
o << " endif()\n";
o << "endforeach()\n";
return o.str();
}
std::string module_cmakelists_txt_app_first(const std::string &project, const std::string &module)
{
const std::string normalized = normalize_module_id(module);
const std::string target = module_target_name(project, module);
const std::string alias = module_alias_name(project, module);
std::ostringstream o;
o << "cmake_minimum_required(VERSION 3.16)\n\n";
o << "add_library(" << target << ")\n";
o << "add_library(" << alias << " ALIAS " << target << ")\n\n";
o << "target_sources(" << target << "\n";
o << " PRIVATE\n";
o << " src/" << normalized << ".cpp\n";
o << ")\n\n";
o << "target_include_directories(" << target << "\n";
o << " PUBLIC\n";
o << " ${CMAKE_CURRENT_LIST_DIR}/include\n";
o << " PRIVATE\n";
o << " ${CMAKE_CURRENT_LIST_DIR}/src\n";
o << ")\n\n";
o << "target_compile_features(" << target << " PUBLIC cxx_std_20)\n\n";
o << "set_target_properties(" << target << " PROPERTIES\n";
o << " OUTPUT_NAME \"" << target << "\"\n";
o << ")\n";
return o.str();
}
std::string module_backend_cmakelists_txt_app_first(
const std::string &project,
const std::string &module)
{
const std::string normalized = normalize_module_id(module);
const std::string target = module_target_name(project, module);
const std::string alias = module_alias_name(project, module);
std::ostringstream o;
o << "cmake_minimum_required(VERSION 3.16)\n\n";
o << "file(GLOB_RECURSE " << target << "_SOURCES CONFIGURE_DEPENDS\n";
o << " ${CMAKE_CURRENT_LIST_DIR}/src/*.cpp\n";
o << ")\n\n";
o << "add_library(" << target << " ${" << target << "_SOURCES})\n";
o << "add_library(" << alias << " ALIAS " << target << ")\n\n";
o << "target_include_directories(" << target << "\n";
o << " PUBLIC\n";
o << " ${CMAKE_CURRENT_LIST_DIR}/include\n";
o << " PRIVATE\n";
o << " ${CMAKE_CURRENT_LIST_DIR}/src\n";
o << ")\n\n";
o << "target_compile_features(" << target << " PUBLIC cxx_std_20)\n\n";
o << "target_link_libraries(" << target << "\n";
o << " PUBLIC\n";
o << " vix::vix\n";
o << ")\n\n";
o << "if(DEFINED VIX_MODULE_" << normalized << "_LINKS)\n";
o << " target_link_libraries(" << target << "\n";
o << " PUBLIC\n";
o << " ${VIX_MODULE_" << normalized << "_LINKS}\n";
o << " )\n";
o << "endif()\n\n";
o << "if(DEFINED " << project << "_BUILD_TESTS AND " << project << "_BUILD_TESTS)\n";
o << " file(GLOB_RECURSE " << target << "_TEST_SOURCES CONFIGURE_DEPENDS\n";
o << " ${CMAKE_CURRENT_LIST_DIR}/tests/*.cpp\n";
o << " )\n\n";
o << " if(" << target << "_TEST_SOURCES)\n";
o << " add_executable(" << target << "_tests\n";
o << " ${" << target << "_TEST_SOURCES}\n";
o << " )\n\n";
o << " target_include_directories(" << target << "_tests PRIVATE\n";
o << " ${CMAKE_CURRENT_LIST_DIR}/include\n";
o << " ${CMAKE_CURRENT_LIST_DIR}/src\n";
o << " )\n\n";
o << " target_link_libraries(" << target << "_tests PRIVATE\n";
o << " " << target << "\n";
o << " vix::vix\n";
o << " )\n\n";
o << " target_compile_features(" << target << "_tests PRIVATE cxx_std_20)\n\n";
o << " add_test(NAME " << project << "." << normalized << " COMMAND " << target << "_tests)\n";
o << " endif()\n";
o << "endif()\n\n";
o << "set_target_properties(" << target << " PROPERTIES\n";
o << " OUTPUT_NAME \"" << target << "\"\n";
o << ")\n";
return o.str();
}
std::string module_websocket_cmakelists_txt_app_first(
const std::string &project,
const std::string &module)
{
const std::string normalized = normalize_module_id(module);
const std::string target = module_target_name(project, module);
const std::string alias = module_alias_name(project, module);
std::ostringstream o;
o << "cmake_minimum_required(VERSION 3.16)\n\n";
o << "file(GLOB_RECURSE " << target << "_SOURCES CONFIGURE_DEPENDS\n";
o << " ${CMAKE_CURRENT_LIST_DIR}/src/*.cpp\n";
o << ")\n\n";
o << "add_library(" << target << " ${" << target << "_SOURCES})\n";
o << "add_library(" << alias << " ALIAS " << target << ")\n\n";
o << "target_include_directories(" << target << "\n";
o << " PUBLIC\n";
o << " ${CMAKE_CURRENT_LIST_DIR}/include\n";
o << " PRIVATE\n";
o << " ${CMAKE_CURRENT_LIST_DIR}/src\n";
o << ")\n\n";
o << "target_compile_features(" << target << " PUBLIC cxx_std_20)\n\n";
o << "target_link_libraries(" << target << "\n";
o << " PUBLIC\n";
o << " vix::vix\n";
o << " vix::websocket\n";
o << ")\n\n";
o << "if(DEFINED " << project << "_BUILD_TESTS AND " << project << "_BUILD_TESTS)\n";
o << " file(GLOB_RECURSE " << target << "_TEST_SOURCES CONFIGURE_DEPENDS\n";
o << " ${CMAKE_CURRENT_LIST_DIR}/tests/*.cpp\n";
o << " )\n\n";
o << " if(" << target << "_TEST_SOURCES)\n";
o << " add_executable(" << target << "_tests\n";
o << " ${" << target << "_TEST_SOURCES}\n";
o << " )\n\n";
o << " target_include_directories(" << target << "_tests PRIVATE\n";
o << " ${CMAKE_CURRENT_LIST_DIR}/include\n";
o << " ${CMAKE_CURRENT_LIST_DIR}/src\n";
o << " )\n\n";
o << " target_link_libraries(" << target << "_tests PRIVATE\n";
o << " " << target << "\n";
o << " vix::vix\n";
o << " vix::websocket\n";
o << " )\n\n";
o << " target_compile_features(" << target << "_tests PRIVATE cxx_std_20)\n\n";
o << " add_test(NAME " << project << "." << normalized << " COMMAND " << target << "_tests)\n";
o << " endif()\n";
o << "endif()\n\n";
o << "set_target_properties(" << target << " PROPERTIES\n";
o << " OUTPUT_NAME \"" << target << "\"\n";
o << ")\n";
return o.str();
}
std::string module_routed_manifest_app_first(
const std::string &module,
const std::string &kind)
{
const std::string normalized = normalize_module_id(module);
std::ostringstream o;
o << "name = \"" << normalized << "\"\n";
o << "kind = \"" << kind << "\"\n\n";
o << "[routes]\n";
o << "prefix = \"/api/" << normalized << "\"\n\n";
o << "[deps]\n";
o << "registry = [\n";
o << "]\n\n";
o << "links = [\n";
o << "]\n\n";
o << "[tests]\n";
o << "enabled = true\n";
return o.str();
}
std::string module_backend_manifest_app_first(
const std::string &module)
{
return module_routed_manifest_app_first(module, "backend");
}
std::string module_manifest_app_first(
const std::string &module)
{
const std::string normalized = normalize_module_id(module);
std::ostringstream o;
o << "name = \"" << normalized << "\"\n";
o << "kind = \"module\"\n\n";
o << "[exports]\n";
o << "include = \"include\"\n\n";
o << "[tests]\n";
o << "enabled = true\n";
return o.str();
}
std::string module_backend_header_app_first(
const std::string &project,
const std::string &module)
{
const std::string normalized = normalize_module_id(module);
const std::string classBase = module_class_name(module);
const std::string guard =
to_lower(normalize_module_id(project)) + "_" +
to_lower(normalized) + "_module_hpp";
std::ostringstream o;
o << "#ifndef " << guard << "\n";
o << "#define " << guard << "\n\n";
o << "namespace vix\n";
o << "{\n";
o << " class App;\n";
o << "}\n\n";
o << "namespace " << project << "::" << normalized << "\n";
o << "{\n";
o << " class " << classBase << "Module\n";
o << " {\n";
o << " public:\n";
o << " static const char *name();\n";
o << " static void register_routes(vix::App &app);\n";
o << " };\n";
o << "} // namespace " << project << "::" << normalized << "\n\n";
o << "#endif // " << guard << "\n";
return o.str();
}
std::string module_backend_impl_app_first(
const std::string &project,
const std::string &module)
{
const std::string normalized = normalize_module_id(module);
const std::string classBase = module_class_name(module);
std::ostringstream o;
o << "#include <" << normalized << "/" << classBase << "Module.hpp>\n";
o << "#include <" << normalized << "/controllers/" << classBase << "Controller.hpp>\n\n";
o << "#include <vix.hpp>\n\n";
o << "namespace " << project << "::" << normalized << "\n";
o << "{\n";
o << " const char *" << classBase << "Module::name()\n";
o << " {\n";
o << " return \"" << normalized << "\";\n";
o << " }\n\n";
o << " void " << classBase << "Module::register_routes(vix::App &app)\n";
o << " {\n";
o << " controllers::" << classBase << "Controller::register_routes(app);\n";
o << " }\n";
o << "} // namespace " << project << "::" << normalized << "\n";
return o.str();
}
std::string module_backend_controller_header_app_first(
const std::string &project,
const std::string &module)
{
const std::string normalized = normalize_module_id(module);
const std::string classBase = module_class_name(module);
const std::string guard =
to_lower(normalize_module_id(project)) + "_" +
to_lower(normalized) + "_controller_hpp";
std::ostringstream o;
o << "#ifndef " << guard << "\n";
o << "#define " << guard << "\n\n";
o << "namespace vix\n";
o << "{\n";
o << " class App;\n";
o << "}\n\n";
o << "namespace " << project << "::" << normalized << "::controllers\n";
o << "{\n";
o << " class " << classBase << "Controller\n";
o << " {\n";
o << " public:\n";
o << " static void register_routes(vix::App &app);\n";
o << " };\n";
o << "} // namespace " << project << "::" << normalized << "::controllers\n\n";
o << "#endif // " << guard << "\n";
return o.str();
}
std::string module_backend_controller_impl_app_first(
const std::string &project,
const std::string &module)
{
const std::string normalized = normalize_module_id(module);
const std::string classBase = module_class_name(module);
std::ostringstream o;
o << "#include <" << normalized << "/controllers/" << classBase << "Controller.hpp>\n\n";
o << "#include <vix.hpp>\n\n";
o << "namespace " << project << "::" << normalized << "::controllers\n";
o << "{\n";
o << " void " << classBase << "Controller::register_routes(vix::App &app)\n";
o << " {\n";
o << " app.get(\"/api/" << normalized << "\", [](vix::Request &req, vix::Response &res)\n";
o << " {\n";
o << " (void)req;\n\n";
o << " res.json({\n";
o << " \"ok\", true,\n";
o << " \"module\", \"" << normalized << "\",\n";
o << " \"message\", \"" << classBase << " module is available\"\n";
o << " });\n";
o << " });\n";
o << " }\n";
o << "} // namespace " << project << "::" << normalized << "::controllers\n";
return o.str();
}
// ------------------------------------------------------------------
// WebSocket module content generators
// ------------------------------------------------------------------
std::string module_websocket_manifest_app_first(
const std::string &module,
WebSocketWorkflow workflow)
{
const std::string normalized = normalize_module_id(module);
std::ostringstream o;
o << "name = \"" << normalized << "\"\n";
o << "kind = \"backend\"\n";
o << "workflow = \"" << websocket_manifest_workflow_name(workflow) << "\"\n";
o << "runtime = true\n\n";
o << "[routes]\n";
o << "prefix = \"/ws\"\n\n";
o << "[websocket]\n";
o << "workflow = \"" << websocket_workflow_name(workflow) << "\"\n";
o << "path = \"/ws\"\n";
o << "host = \"0.0.0.0\"\n";
o << "port = 9090\n";
o << "long_polling = ";
o << (workflow == WebSocketWorkflow::Bridge ? "true" : "false");
o << "\n";
o << "metrics = false\n\n";
o << "[deps]\n";
o << "registry = [\n";
o << "]\n\n";
o << "links = [\n";
o << "]\n\n";
o << "[tests]\n";
o << "enabled = true\n";
return o.str();
}
std::string module_websocket_header_app_first(
const std::string &project,
const std::string &module,
WebSocketWorkflow workflow)
{
const std::string normalized = normalize_module_id(module);
const std::string classBase = module_class_name(module);
const std::string guard =
to_lower(normalize_module_id(project)) + "_" +
to_lower(normalized) + "_websocket_module_hpp";
std::ostringstream o;
o << "#ifndef " << guard << "\n";
o << "#define " << guard << "\n\n";
o << "#include <memory>\n\n";
o << "namespace vix\n";
o << "{\n";
o << " class App;\n";
o << "}\n\n";
o << "namespace vix::config\n";
o << "{\n";
o << " class Config;\n";
o << "}\n\n";
o << "namespace vix::executor\n";
o << "{\n";
o << " class RuntimeExecutor;\n";
o << "}\n\n";
o << "namespace " << project << "::" << normalized << "\n";
o << "{\n";
o << " /**\n";
o << " * @brief Application module generated for WebSocket runtime wiring.\n";
o << " *\n";
o << " * This module keeps HTTP route registration separate from runtime startup.\n";
o << " * `register_routes()` is safe to call during application configuration.\n";
o << " * Runtime startup belongs to `run()` for workflows that need a long-lived\n";
o << " * WebSocket server.\n";
o << " */\n";
o << " class " << classBase << "Module\n";
o << " {\n";
o << " public:\n";
o << " /**\n";
o << " * @brief Return the stable module name.\n";
o << " *\n";
o << " * @return Module name used in generated manifests and diagnostics.\n";
o << " */\n";
o << " static const char *name();\n\n";
o << " /**\n";
o << " * @brief Register HTTP-facing routes for the module.\n";
o << " *\n";
o << " * This function must remain lightweight. It must not start a WebSocket\n";
o << " * listener, block the process, or own runtime shutdown.\n";
o << " *\n";
o << " * @param app Application receiving the generated routes.\n";
o << " */\n";
o << " static void register_routes(vix::App &app);\n\n";
if (workflow != WebSocketWorkflow::Client)
{
o << " /**\n";
o << " * @brief Run the selected WebSocket workflow.\n";
o << " *\n";
o << " * Runtime workflows are started explicitly from the generated application\n";
o << " * runner. This avoids hiding long-lived WebSocket services inside normal\n";
o << " * route registration.\n";
o << " *\n";
o << " * @param app HTTP application instance.\n";
o << " * @param cfg Application configuration.\n";
o << " * @param executor Shared runtime executor.\n";
o << " * @return Process exit code.\n";
o << " */\n";
o << " static int run(\n";
o << " vix::App &app,\n";
o << " const vix::config::Config &cfg,\n";
o << " std::shared_ptr<vix::executor::RuntimeExecutor> executor);\n";
}
o << " };\n";
o << "} // namespace " << project << "::" << normalized << "\n\n";
o << "#endif // " << guard << "\n";
return o.str();
}
std::string module_websocket_impl_app_first(
const std::string &project,
const std::string &module,
WebSocketWorkflow workflow)
{
const std::string normalized = normalize_module_id(module);
const std::string classBase = module_class_name(module);
std::ostringstream o;
o << "#include <" << normalized << "/" << classBase << "Module.hpp>\n\n";
o << "#include <vix.hpp>\n";
o << "#include <vix/websocket/AttachedRuntime.hpp>\n\n";
o << "namespace " << project << "::" << normalized << "\n";
o << "{\n";
o << " const char *" << classBase << "Module::name()\n";
o << " {\n";
o << " return \"" << normalized << "\";\n";
o << " }\n\n";
o << " void " << classBase << "Module::register_routes(vix::App &app)\n";
o << " {\n";
o << " app.get(\"/ws/status\", [](vix::Request &req, vix::Response &res)\n";
o << " {\n";
o << " (void)req;\n\n";
o << " res.json({\n";
o << " \"ok\", true,\n";
o << " \"module\", \"" << normalized << "\",\n";
o << " \"workflow\", \"" << websocket_workflow_name(workflow) << "\",\n";
o << " \"message\", \"" << classBase << " WebSocket module is available\"\n";
o << " });\n";
o << " });\n";
o << " }\n";
if (workflow != WebSocketWorkflow::Client)
{
o << "\n";
o << " int " << classBase << "Module::run(\n";
o << " vix::App &app,\n";
o << " const vix::config::Config &cfg,\n";
o << " std::shared_ptr<vix::executor::RuntimeExecutor> executor)\n";
o << " {\n";
o << " vix::websocket::Server ws{\n";
o << " const_cast<vix::config::Config &>(cfg),\n";
o << " executor};\n\n";
o << " vix::run_http_and_ws(app, ws, executor, cfg);\n";
o << " return 0;\n";
o << " }\n";
}
o << "} // namespace " << project << "::" << normalized << "\n";
return o.str();
}
std::string module_websocket_test_cpp_app_first(
const std::string &project,
const std::string &module,
WebSocketWorkflow workflow)
{
const std::string normalized = normalize_module_id(module);
const std::string classBase = module_class_name(module);
std::ostringstream o;
o << "/**\n";
o << " * @file test_" << normalized << ".cpp\n";
o << " * @brief Basic tests for the " << normalized << " WebSocket module.\n";
o << " */\n\n";
o << "#include <" << normalized << "/" << classBase << "Module.hpp>\n\n";
o << "#include <vix/tests/tests.hpp>\n\n";
o << "#include <string>\n\n";
o << "int main()\n";
o << "{\n";
o << " using namespace vix::tests;\n\n";
o << " auto ®istry = TestRegistry::instance();\n";
o << " registry.clear();\n\n";
o << " registry.add(TestCase(\"" << normalized << " websocket module exposes its name\", []\n";
o << " {\n";
o << " Assert::equal(\n";
o << " std::string(" << project << "::" << normalized << "::" << classBase << "Module::name()),\n";
o << " std::string(\"" << normalized << "\"));\n";
o << " }));\n\n";
o << " registry.add(TestCase(\"" << normalized << " websocket workflow is generated\", []\n";
o << " {\n";
o << " Assert::equal(\n";
o << " std::string(\"" << websocket_workflow_name(workflow) << "\"),\n";
o << " std::string(\"" << websocket_workflow_name(workflow) << "\"));\n";
o << " }));\n\n";
o << " return TestRunner::run_all_and_exit();\n";
o << "}\n";
return o.str();
}
std::string module_backend_test_cpp_app_first(
const std::string &project,
const std::string &module)
{
const std::string normalized = normalize_module_id(module);
const std::string classBase = module_class_name(module);
std::ostringstream o;
o << "/**\n";
o << " * @file test_" << normalized << ".cpp\n";
o << " * @brief Basic tests for the " << normalized << " backend module.\n";
o << " */\n\n";
o << "#include <" << normalized << "/" << classBase << "Module.hpp>\n\n";
o << "#include <vix/tests/tests.hpp>\n\n";
o << "int main()\n";
o << "{\n";
o << " using namespace vix::tests;\n\n";
o << " auto ®istry = TestRegistry::instance();\n";
o << " registry.clear();\n\n";
o << " registry.add(TestCase(\"" << normalized << " module exposes its name\", []\n";
o << " {\n";
o << " Assert::equal(\n";
o << " std::string(" << project << "::" << normalized << "::" << classBase << "Module::name()),\n";
o << " std::string(\"" << normalized << "\"));\n";
o << " }));\n\n";
o << " return TestRunner::run_all_and_exit();\n";
o << "}\n";
return o.str();
}
std::string module_test_cpp_app_first(
const std::string &project,
const std::string &module)
{
const std::string normalized = normalize_module_id(module);
std::ostringstream o;
o << "/**\n";
o << " * @file test_" << normalized << ".cpp\n";
o << " * @brief Basic tests for the " << normalized << " module.\n";
o << " */\n\n";
o << "#include <" << normalized << "/api.hpp>\n\n";
o << "#include <vix/tests/tests.hpp>\n\n";
o << "int main()\n";
o << "{\n";
o << " using namespace vix::tests;\n\n";
o << " auto ®istry = TestRegistry::instance();\n";
o << " registry.clear();\n\n";
o << " registry.add(TestCase(\"" << normalized << " module exposes its API name\", []\n";
o << " {\n";
o << " Assert::equal(\n";
o << " " << project << "::" << normalized << "::Api::name(),\n";
o << " std::string(\"" << project << "::" << normalized << "\"));\n";
o << " }));\n\n";
o << " return TestRunner::run_all_and_exit();\n";
o << "}\n";
return o.str();
}
// ------------------------------------------------------------------
// C++ content generators
// ------------------------------------------------------------------
std::string module_public_header_app_first(const std::string &project, const std::string &module)
{
const std::string normalized = normalize_module_id(module);
const std::string guard =
to_lower(normalize_module_id(project)) + "_" + to_lower(normalized) + "_api_hpp";
std::ostringstream o;
o << "#ifndef " << guard << "\n";
o << "#define " << guard << "\n\n";
o << "#include <string>\n\n";
o << "namespace " << project << "::" << normalized << "\n";
o << "{\n";
o << " struct Api\n";
o << " {\n";
o << " static std::string name();\n";
o << " };\n";
o << "}\n\n";
o << "#endif\n";
return o.str();
}
std::string module_impl_cpp_app_first(const std::string &project, const std::string &module)
{
const std::string normalized = normalize_module_id(module);
std::ostringstream o;
o << "#include <" << normalized << "/api.hpp>\n\n";
o << "namespace " << project << "::" << normalized << "\n";
o << "{\n";
o << " std::string Api::name()\n";
o << " {\n";
o << " return \"" << project << "::" << normalized << "\";\n";
o << " }\n";
o << "}\n";
return o.str();
}
// ------------------------------------------------------------------
// CMakeLists.txt patching
// ------------------------------------------------------------------
bool patch_root_cmakelists_include(const fs::path &root)
{
const fs::path cm = root / "CMakeLists.txt";
auto contentOpt = read_file(cm);
if (!contentOpt)
return false;
std::string content = *contentOpt;
const std::string beginMark = "# VIX_MODULES_BEGIN";
const std::string endMark = "# VIX_MODULES_END";
const std::string block =
beginMark + "\n"
"include(${CMAKE_CURRENT_LIST_DIR}/cmake/vix_modules.cmake)\n" +
endMark + "\n";
// Already patched — idempotent
if (content.find(beginMark) != std::string::npos &&
content.find(endMark) != std::string::npos)
return true;
std::istringstream in(content);
std::ostringstream out;
std::string line;
bool inserted = false;
while (std::getline(in, line))
{
out << line << "\n";
if (!inserted && starts_with(to_lower(trim(line)), "project("))
{
out << "\n"
<< block << "\n";
inserted = true;
}
}
if (!inserted)
{
// No project() found — prepend
content = block + "\n" + content;
}
else
{
content = out.str();
}
return write_file_overwrite(cm, content);
}
bool patch_root_cmakelists_link_module(
const fs::path &root,
const std::string &project,
const std::string &module)
{
const fs::path cm = root / "CMakeLists.txt";
auto contentOpt = read_file(cm);
if (!contentOpt)
return false;
std::string content = *contentOpt;
const std::string beginMark = "# VIX_MODULE_LINKS_BEGIN";
const std::string endMark = "# VIX_MODULE_LINKS_END";
// Ensure the links section exists
if (content.find(beginMark) == std::string::npos ||
content.find(endMark) == std::string::npos)
{
std::ostringstream section;
section << "\n"
<< beginMark << "\n"
<< "# Auto-generated by: vix modules\n"
<< "# NOTE: This links modules into the main target named like project(" << project << ").\n"
<< "# If your main target differs, remove this section and link targets manually.\n"
<< endMark << "\n";
content += section.str();
}
const std::string alias = module_alias_name(project, module);
const std::string modNorm = normalize_module_id(module);
const std::string perBegin = "# VIX_MODULE_LINK_BEGIN " + modNorm;
const std::string perEnd = "# VIX_MODULE_LINK_END " + modNorm;
// Already patched for this module — idempotent
if (content.find(perBegin) != std::string::npos &&
content.find(perEnd) != std::string::npos)
return true;