-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDesktopCommand.cpp
More file actions
2439 lines (1991 loc) · 55.8 KB
/
Copy pathDesktopCommand.cpp
File metadata and controls
2439 lines (1991 loc) · 55.8 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 DesktopCommand.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/DesktopCommand.hpp>
#include <algorithm>
#include <charconv>
#include <chrono>
#include <cctype>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <optional>
#include <ostream>
#include <sstream>
#include <string>
#include <string_view>
#include <system_error>
#include <vector>
#ifdef _WIN32
#include <io.h>
#define VIX_DESK_ISATTY(fd) _isatty(fd)
#define VIX_DESK_FILENO(stream) _fileno(stream)
#else
#include <unistd.h>
#define VIX_DESK_ISATTY(fd) ::isatty(fd)
#define VIX_DESK_FILENO(stream) ::fileno(stream)
#endif
namespace fs = std::filesystem;
namespace
{
// -------------------------------------------------------------------------
// Color handling (ANSI), auto-detected and overridable.
// -------------------------------------------------------------------------
struct DesktopTheme
{
bool color = true;
std::string_view reset() const { return color ? "\x1b[0m" : ""; }
std::string_view dim() const { return color ? "\x1b[2m" : ""; }
std::string_view bold() const { return color ? "\x1b[1m" : ""; }
std::string_view green() const { return color ? "\x1b[32m" : ""; }
std::string_view cyan() const { return color ? "\x1b[36m" : ""; }
std::string_view yellow() const { return color ? "\x1b[33m" : ""; }
std::string_view red() const { return color ? "\x1b[31m" : ""; }
std::string_view gray() const { return color ? "\x1b[90m" : ""; }
};
// Honor NO_COLOR (https://no-color.org), FORCE_COLOR, and TTY detection.
bool desk_detect_color(std::ostream &os)
{
if (std::getenv("NO_COLOR") != nullptr)
{
return false;
}
if (std::getenv("FORCE_COLOR") != nullptr)
{
return true;
}
std::FILE *target = (&os == &std::cerr) ? stderr : stdout;
return VIX_DESK_ISATTY(VIX_DESK_FILENO(target)) != 0;
}
// -------------------------------------------------------------------------
// Output verbosity, controlled by CLI flags (modern-runtime style).
// -------------------------------------------------------------------------
enum class DesktopLogMode
{
Normal, // banner + lifecycle lines
Quiet, // errors only
Json // machine-readable single-line events on stdout
};
struct DesktopReporter
{
DesktopTheme theme;
DesktopLogMode mode = DesktopLogMode::Normal;
bool normal() const { return mode == DesktopLogMode::Normal; }
bool json() const { return mode == DesktopLogMode::Json; }
void banner(std::string_view title) const
{
if (!normal())
{
return;
}
std::cout << '\n'
<< " " << theme.green() << theme.bold() << title << theme.reset()
<< '\n';
}
// key/value row, padded so values align in a column.
void row(std::string_view key, std::string_view value,
std::string_view valueColor = {}) const
{
if (!normal())
{
return;
}
constexpr std::size_t keyWidth = 9;
std::string label(key);
if (label.size() < keyWidth)
{
label.append(keyWidth - label.size(), ' ');
}
std::cout << " " << theme.dim() << label << theme.reset() << " "
<< (valueColor.empty() ? theme.reset() : valueColor)
<< value << theme.reset() << '\n';
}
void info_line(std::string_view message) const
{
if (!normal())
{
return;
}
std::cout << " " << theme.cyan() << "info" << theme.reset()
<< " " << message << '\n';
}
void step(std::string_view message) const
{
if (!normal())
{
return;
}
std::cout << " " << theme.gray() << "\u2022 " << theme.reset()
<< message << '\n';
}
void hint(std::string_view message) const
{
if (!normal())
{
return;
}
std::cout << " " << theme.gray() << message << theme.reset() << '\n';
}
void success(std::string_view message) const
{
if (!normal())
{
return;
}
std::cout << " " << theme.green() << "\u2714" << theme.reset()
<< " " << message << '\n';
}
// Errors always print (even in quiet mode), to stderr.
void error(std::string_view message) const
{
std::cerr << " " << theme.red() << theme.bold() << "error" << theme.reset()
<< " " << message << '\n';
}
void error_hint(std::string_view message) const
{
if (message.empty())
{
return;
}
std::cerr << " " << theme.gray() << message << theme.reset() << '\n';
}
void event(std::string_view name, std::string_view key = {},
std::string_view value = {}) const
{
if (!json())
{
return;
}
std::cout << "{\"event\":\"" << name << "\"";
if (!key.empty())
{
std::cout << ",\"" << key << "\":\"" << value << "\"";
}
std::cout << "}\n";
std::cout.flush();
}
};
bool is_help_arg(const std::string &arg)
{
return arg == "-h" || arg == "--help" || arg == "help";
}
#ifdef VIX_CLI_HAS_UI
bool is_command_arg(const std::string &arg)
{
return arg == "run" ||
arg == "build" ||
arg == "package";
}
bool parse_prefixed_value(
const std::string &arg,
const char *prefix,
std::string &out)
{
const std::string p(prefix);
if (arg.rfind(p, 0) != 0)
{
return false;
}
out = arg.substr(p.size());
return true;
}
std::string trim_left_copy(std::string value)
{
value.erase(
value.begin(),
std::find_if(
value.begin(),
value.end(),
[](unsigned char ch)
{
return !std::isspace(ch);
}));
return value;
}
bool is_vix_run_server_command(const std::string &command)
{
const std::string value = trim_left_copy(command);
return value == "vix run" ||
value.rfind("vix run ", 0) == 0 ||
value.find(" vix run ") != std::string::npos;
}
bool reject_vix_run_server_command(
const DesktopReporter &out,
const std::string &command)
{
if (!is_vix_run_server_command(command))
{
return false;
}
out.error("Use the C++ file directly.");
out.error_hint("Run: vix desktop run ui_dashboard.cpp --port 8080");
return true;
}
bool consume_value(
const DesktopReporter &out,
const std::vector<std::string> &args,
std::size_t &index,
const std::string &option,
std::string &value)
{
if (index + 1 >= args.size())
{
out.error("Missing value for " + option + ".");
return false;
}
value = args[++index];
if (value.empty())
{
out.error("Value for " + option + " cannot be empty.");
return false;
}
return true;
}
bool parse_port(const std::string &value, std::uint16_t &out)
{
if (value.empty())
{
return false;
}
unsigned long port = 0;
const char *begin = value.data();
const char *end = value.data() + value.size();
auto result = std::from_chars(begin, end, port);
if (result.ec != std::errc{} || result.ptr != end)
{
return false;
}
if (port == 0 || port > 65535)
{
return false;
}
out = static_cast<std::uint16_t>(port);
return true;
}
bool parse_positive_int(const std::string &value, int &out)
{
if (value.empty())
{
return false;
}
long parsed = 0;
const char *begin = value.data();
const char *end = value.data() + value.size();
auto result = std::from_chars(begin, end, parsed);
if (result.ec != std::errc{} || result.ptr != end)
{
return false;
}
if (parsed <= 0 || parsed > 1000000)
{
return false;
}
out = static_cast<int>(parsed);
return true;
}
bool parse_non_negative_int(const std::string &value, int &out)
{
if (value.empty())
{
return false;
}
long parsed = 0;
const char *begin = value.data();
const char *end = value.data() + value.size();
auto result = std::from_chars(begin, end, parsed);
if (result.ec != std::errc{} || result.ptr != end)
{
return false;
}
if (parsed < 0 || parsed > 100000000)
{
return false;
}
out = static_cast<int>(parsed);
return true;
}
std::string shell_quote(const std::string &value)
{
std::string out;
out.reserve(value.size() + 2);
out += "'";
for (char ch : value)
{
if (ch == '\'')
{
out += "'\\''";
}
else
{
out += ch;
}
}
out += "'";
return out;
}
std::string json_escape(const std::string &value)
{
std::string out;
out.reserve(value.size() + 16);
for (char ch : value)
{
switch (ch)
{
case '\\':
out += "\\\\";
break;
case '"':
out += "\\\"";
break;
case '\n':
out += "\\n";
break;
case '\r':
out += "\\r";
break;
case '\t':
out += "\\t";
break;
default:
out += ch;
break;
}
}
return out;
}
std::string slugify(std::string value)
{
std::string out;
out.reserve(value.size());
bool lastDash = false;
for (char ch : value)
{
const unsigned char c = static_cast<unsigned char>(ch);
if (std::isalnum(c))
{
out.push_back(static_cast<char>(std::tolower(c)));
lastDash = false;
continue;
}
if (!lastDash)
{
out.push_back('-');
lastDash = true;
}
}
while (!out.empty() && out.front() == '-')
{
out.erase(out.begin());
}
while (!out.empty() && out.back() == '-')
{
out.pop_back();
}
if (out.empty())
{
out = "vix-desktop-app";
}
return out;
}
std::string bool_text(bool value)
{
return value ? "true" : "false";
}
bool is_cpp_file(const fs::path &path)
{
return path.extension() == ".cpp" ||
path.extension() == ".cc" ||
path.extension() == ".cxx" ||
path.extension() == ".C";
}
bool is_regular_executable(const fs::path &path)
{
std::error_code ec;
if (!fs::is_regular_file(path, ec) || ec)
{
return false;
}
#ifdef _WIN32
return path.extension() == ".exe";
#else
const fs::perms perms = fs::status(path, ec).permissions();
if (ec)
{
return false;
}
using pr = fs::perms;
return (perms & pr::owner_exec) != pr::none ||
(perms & pr::group_exec) != pr::none ||
(perms & pr::others_exec) != pr::none;
#endif
}
void make_executable(const fs::path &path)
{
std::error_code ec;
fs::permissions(
path,
fs::perms::owner_exec |
fs::perms::group_exec |
fs::perms::others_exec,
fs::perm_options::add,
ec);
}
std::optional<fs::path> current_executable_path()
{
#ifdef _WIN32
return std::nullopt;
#else
std::error_code ec;
fs::path path = fs::read_symlink("/proc/self/exe", ec);
if (ec || path.empty())
{
return std::nullopt;
}
return path;
#endif
}
std::string local_url(const std::string &host, std::uint16_t port)
{
return "http://" + host + ":" + std::to_string(port);
}
bool desktop_shell_backend_available()
{
#if defined(_WIN32)
return true;
#elif defined(__APPLE__)
return true;
#elif defined(__linux__)
#if defined(VIX_UI_ENABLE_LINUX_WEBVIEW) && VIX_UI_ENABLE_LINUX_WEBVIEW
return true;
#else
return false;
#endif
#else
return false;
#endif
}
enum class DesktopAction
{
Run,
Build,
Package
};
struct DesktopOptions
{
DesktopAction action{DesktopAction::Run};
std::string name{"Vix Desktop"};
std::string title{"Vix Desktop"};
std::string appId{};
std::string appVersion{"1.0.0"};
std::string vendor{"Vix.cpp"};
std::string iconPath{};
std::string target{};
std::string url{};
std::string readinessUrl{};
std::string host{"127.0.0.1"};
std::uint16_t port{8080};
int width{1200};
int height{800};
bool resizable{true};
bool fullscreen{false};
bool devtools{false};
bool startServer{false};
bool waitForServer{true};
std::chrono::milliseconds startupTimeout{30000};
std::string serverCommand{};
std::string serverWorkingDirectory{};
std::string outputDirectory{};
std::string packageTarget{"dir"};
std::string binaryPath{};
std::vector<std::string> resourcePaths{};
bool autoResources{true};
bool clean{false};
bool withSqlite{false};
bool withMySql{false};
bool localCache{false};
int jobs{0};
DesktopLogMode logMode{DesktopLogMode::Normal};
int colorOverride{-1}; // -1 auto, 0 off, 1 on
};
std::string with_desktop_server_env(
const DesktopOptions &options,
const std::string &command)
{
if (command.empty())
{
return command;
}
std::string out;
out += "SERVER_HOST=";
out += shell_quote(options.host);
out += " ";
out += "SERVER_PORT=";
out += std::to_string(options.port);
out += " ";
out += command;
return out;
}
// Shared output-flag parsing (--quiet / --json / --color).
// Returns true if `arg` was an output flag and was consumed.
bool take_output_flag(const std::string &arg, DesktopOptions &options)
{
if (arg == "--quiet" || arg == "-q" || arg == "--silent")
{
options.logMode = DesktopLogMode::Quiet;
return true;
}
if (arg == "--json")
{
options.logMode = DesktopLogMode::Json;
return true;
}
if (arg == "--no-color" || arg == "--no-colour")
{
options.colorOverride = 0;
return true;
}
if (arg == "--color" || arg == "--colour")
{
options.colorOverride = 1;
return true;
}
return false;
}
int parse_options(
const DesktopReporter &out,
const std::vector<std::string> &args,
DesktopOptions &options)
{
for (std::size_t i = 0; i < args.size(); ++i)
{
const std::string &arg = args[i];
if (is_help_arg(arg))
{
return 2;
}
if (take_output_flag(arg, options))
{
continue;
}
if (arg == "--name")
{
if (!consume_value(out, args, i, "--name", options.name))
{
return 1;
}
continue;
}
if (arg == "--title")
{
if (!consume_value(out, args, i, "--title", options.title))
{
return 1;
}
continue;
}
if (arg == "--app-id")
{
if (!consume_value(out, args, i, "--app-id", options.appId))
{
return 1;
}
continue;
}
if (arg == "--app-version" || arg == "--version")
{
if (!consume_value(out, args, i, arg, options.appVersion))
{
return 1;
}
continue;
}
if (arg == "--vendor")
{
if (!consume_value(out, args, i, "--vendor", options.vendor))
{
return 1;
}
continue;
}
if (arg == "--icon")
{
if (!consume_value(out, args, i, "--icon", options.iconPath))
{
return 1;
}
continue;
}
if (arg == "--url")
{
if (!consume_value(out, args, i, "--url", options.url))
{
return 1;
}
continue;
}
if (arg == "--readiness-url")
{
if (!consume_value(out, args, i, "--readiness-url", options.readinessUrl))
{
return 1;
}
continue;
}
if (arg == "--host")
{
if (!consume_value(out, args, i, "--host", options.host))
{
return 1;
}
continue;
}
if (arg == "--port")
{
std::string value;
if (!consume_value(out, args, i, "--port", value))
{
return 1;
}
if (!parse_port(value, options.port))
{
out.error("Invalid desktop port.");
out.error_hint("Port must be between 1 and 65535.");
return 1;
}
continue;
}
if (arg == "--width")
{
std::string value;
if (!consume_value(out, args, i, "--width", value))
{
return 1;
}
if (!parse_positive_int(value, options.width))
{
out.error("Invalid desktop width.");
out.error_hint("Width must be greater than zero.");
return 1;
}
continue;
}
if (arg == "--height")
{
std::string value;
if (!consume_value(out, args, i, "--height", value))
{
return 1;
}
if (!parse_positive_int(value, options.height))
{
out.error("Invalid desktop height.");
out.error_hint("Height must be greater than zero.");
return 1;
}
continue;
}
if (arg == "--server")
{
std::string value;
if (!consume_value(out, args, i, "--server", value))
{
return 1;
}
if (reject_vix_run_server_command(out, value))
{
return 1;
}
options.serverCommand = std::move(value);
options.startServer = true;
continue;
}
if (arg == "--cwd" || arg == "--working-directory")
{
if (!consume_value(out, args, i, arg, options.serverWorkingDirectory))
{
return 1;
}
continue;
}
if (arg == "--timeout-ms")
{
std::string value;
int timeout = 0;
if (!consume_value(out, args, i, "--timeout-ms", value))
{
return 1;
}
if (!parse_non_negative_int(value, timeout))
{
out.error("Invalid desktop startup timeout.");
out.error_hint("Timeout must be a non-negative number of milliseconds.");
return 1;
}
options.startupTimeout = std::chrono::milliseconds(timeout);
continue;
}
if (arg == "--out" || arg == "-o")
{
if (!consume_value(out, args, i, arg, options.outputDirectory))
{
return 1;
}
continue;
}
if (arg == "--target")
{
if (!consume_value(out, args, i, "--target", options.packageTarget))
{
return 1;
}
continue;
}
if (arg == "--binary" || arg == "--server-binary")
{
if (!consume_value(out, args, i, arg, options.binaryPath))
{
return 1;
}
continue;
}
if (arg == "-j" || arg == "--jobs")
{
std::string value;
if (!consume_value(out, args, i, arg, value))
{
return 1;
}
if (!parse_non_negative_int(value, options.jobs))
{
out.error("Invalid jobs value.");
out.error_hint("Jobs must be a non-negative integer.");
return 1;
}
continue;
}
if (arg == "--devtools")
{
options.devtools = true;
continue;
}
if (arg == "--no-devtools")
{
options.devtools = false;
continue;
}
if (arg == "--fullscreen")
{
options.fullscreen = true;
continue;
}
if (arg == "--resizable")
{
options.resizable = true;
continue;
}
if (arg == "--no-resizable")
{
options.resizable = false;
continue;
}
if (arg == "--wait")
{
options.waitForServer = true;
continue;
}
if (arg == "--no-wait")
{