-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbuild.py
More file actions
executable file
·1137 lines (961 loc) · 39 KB
/
build.py
File metadata and controls
executable file
·1137 lines (961 loc) · 39 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
#!/usr/bin/env python3
"""
Vayu Cross-Platform Build Script
Modern unified build system for C++ Engine + Electron App
"""
import argparse
import json
import os
import platform
import re
import shutil
import subprocess
import sys
import time
from pathlib import Path
from typing import Optional, Tuple, List
import threading
# Global flags
VERBOSE = False
CMAKE_PATH = "cmake" # Will be updated if found in non-standard location
PNPM_PATH = "pnpm" # Will be updated if found in non-standard location
# ANSI codes
class Style:
# Colors
CYAN = '\033[36m'
GREEN = '\033[32m'
YELLOW = '\033[33m'
RED = '\033[31m'
BLUE = '\033[34m'
MAGENTA = '\033[35m'
GRAY = '\033[90m'
WHITE = '\033[97m'
# Styles
BOLD = '\033[1m'
DIM = '\033[2m'
RESET = '\033[0m'
# Symbols
ARROW = '→'
CHECK = '✓'
CROSS = '✗'
INFO = 'ℹ'
WARN = '⚠'
ROCKET = '▲'
DOT = '•'
SPINNER = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏']
class Spinner:
"""Simple spinner for long-running operations."""
def __init__(self, message: str):
self.message = message
self.running = False
self.thread = None
def _spin(self):
idx = 0
while self.running:
sys.stdout.write(f'\r {Style.CYAN}{Style.SPINNER[idx]}{Style.RESET} {self.message}')
sys.stdout.flush()
time.sleep(0.1)
idx = (idx + 1) % len(Style.SPINNER)
def start(self):
if not VERBOSE:
self.running = True
self.thread = threading.Thread(target=self._spin)
self.thread.start()
def stop(self, symbol: str = Style.CHECK, color: str = Style.GREEN):
if not VERBOSE:
self.running = False
if self.thread:
self.thread.join()
sys.stdout.write(f'\r {color}{symbol}{Style.RESET} {self.message}\n')
sys.stdout.flush()
def log(message: str, prefix: str = '', color: str = ''):
"""Simple log with optional prefix and color."""
if color:
print(f' {color}{prefix}{Style.RESET} {message}')
else:
print(f' {prefix} {message}')
def log_dim(message: str):
"""Dimmed log message."""
print(f'{Style.DIM} {message}{Style.RESET}')
def print_header():
"""Print modern compact header."""
print()
print(f'{Style.BOLD}{Style.CYAN}{Style.ROCKET} Vayu Build{Style.RESET}')
print()
def print_build_info(mode: str, components: List[str], platform_name: str, verbose: bool):
"""Print build configuration in a clean table."""
print(f' {Style.DIM}┌{"─" * 58}┐{Style.RESET}')
print(f' {Style.DIM}│{Style.RESET} {Style.GRAY}Platform{Style.RESET} {platform_name:<45}{Style.DIM}│{Style.RESET}')
print(f' {Style.DIM}│{Style.RESET} {Style.GRAY}Mode{Style.RESET} {Style.MAGENTA}{mode}{Style.RESET}{" " * (45 - len(mode))}{Style.DIM}│{Style.RESET}')
components_str = " + ".join(components)
print(f' {Style.DIM}│{Style.RESET} {Style.GRAY}Components{Style.RESET} {Style.CYAN}{components_str}{Style.RESET}{" " * (45 - len(components_str))}{Style.DIM}│{Style.RESET}')
verbose_str = "Yes" if verbose else "No"
print(f' {Style.DIM}│{Style.RESET} {Style.GRAY}Verbose{Style.RESET} {verbose_str:<45}{Style.DIM}│{Style.RESET}')
print(f' {Style.DIM}└{"─" * 58}┘{Style.RESET}')
print()
def print_step(step: int, total: int, title: str):
"""Print step header."""
print()
print(f' {Style.BOLD}[{step}/{total}] {title}{Style.RESET}')
print()
def print_success(elapsed: int, artifacts: List[Tuple[str, str]]):
"""Print success message with artifacts."""
print()
print(f' {Style.GREEN}{Style.CHECK} Build complete{Style.RESET} {Style.DIM}({elapsed}s){Style.RESET}')
if artifacts:
print()
print(f' {Style.BOLD}Artifacts{Style.RESET}')
print()
for label, path in artifacts:
print(f' {Style.DIM}{Style.ARROW}{Style.RESET} {Style.GRAY}{label}{Style.RESET}')
print(f' {Style.CYAN}{path}{Style.RESET}')
print()
def print_error(message: str):
"""Print error and exit."""
print()
print(f' {Style.RED}{Style.CROSS} {message}{Style.RESET}')
print()
sys.exit(1)
def print_command_error(cmd: List[str], exit_code: int, output: str, description: str):
"""Print detailed command error."""
print()
print(f' {Style.RED}{Style.CROSS} {description} failed{Style.RESET}')
print()
print(f' {Style.DIM}Command{Style.RESET}')
print(f' {Style.GRAY}$ {" ".join(cmd)}{Style.RESET}')
print()
print(f' {Style.DIM}Exit code: {exit_code}{Style.RESET}')
if output and not VERBOSE:
print()
print(f' {Style.DIM}Output (last 25 lines){Style.RESET}')
print()
lines = output.strip().split('\n')
display_lines = lines[-25:] if len(lines) > 25 else lines
for line in display_lines:
if any(kw in line.lower() for kw in ['error', 'failed', 'fatal']):
print(f' {Style.RED}{line}{Style.RESET}')
else:
print(f' {Style.GRAY}{line}{Style.RESET}')
if len(lines) > 25:
print()
print(f' {Style.DIM}... ({len(lines) - 25} more lines){Style.RESET}')
print()
print(f' {Style.YELLOW}{Style.INFO} Run with {Style.BOLD}-v{Style.RESET}{Style.YELLOW} for full output{Style.RESET}')
print()
def detect_platform() -> Tuple[str, str]:
"""Detect platform and return (display_name, preset_prefix)."""
system = platform.system()
if system == "Windows":
return ("Windows", "windows")
elif system == "Linux":
return ("Linux", "linux")
elif system == "Darwin":
return ("macOS", "macos")
else:
print_error(f"Unsupported platform: {system}")
def find_visual_studio() -> Optional[str]:
"""Find Visual Studio installation on Windows using vswhere."""
system_name, _ = detect_platform()
if system_name != "Windows":
return None
vswhere_path = Path(os.environ.get('ProgramFiles(x86)', 'C:\\Program Files (x86)')) / 'Microsoft Visual Studio' / 'Installer' / 'vswhere.exe'
if not vswhere_path.exists():
return None
try:
# Find VS with VC++ tools
result = subprocess.run(
[str(vswhere_path), '-latest', '-requires', 'Microsoft.VisualStudio.Component.VC.Tools.x86.x64', '-property', 'installationPath'],
capture_output=True,
text=True,
check=True
)
vs_path = result.stdout.strip()
if vs_path and Path(vs_path).exists():
return vs_path
except:
pass
return None
def find_cmake_windows() -> Optional[str]:
"""Find CMake on Windows (PATH, VS, common locations)."""
# Check PATH first
cmake_cmd = shutil.which("cmake")
if cmake_cmd:
return cmake_cmd
# Check Visual Studio
vs_path = find_visual_studio()
if vs_path:
vs_cmake = Path(vs_path) / 'Common7' / 'IDE' / 'CommonExtensions' / 'Microsoft' / 'CMake' / 'CMake' / 'bin' / 'cmake.exe'
if vs_cmake.exists():
return str(vs_cmake)
# Check common paths
common_paths = [
Path('C:/Program Files/CMake/bin/cmake.exe'),
Path('C:/Program Files (x86)/CMake/bin/cmake.exe'),
]
for path in common_paths:
if path.exists():
return str(path)
return None
def find_pnpm_windows() -> Optional[str]:
"""Find pnpm on Windows (PATH, npm global, common locations)."""
# Check PATH first
pnpm_cmd = shutil.which("pnpm")
if pnpm_cmd:
return pnpm_cmd
# Check npm's global prefix (where npm installs global packages)
try:
result = subprocess.run(
["npm", "config", "get", "prefix"],
capture_output=True,
text=True,
check=True,
timeout=5
)
npm_prefix = result.stdout.strip()
if npm_prefix and npm_prefix != "undefined":
# pnpm is typically in node_modules/.bin or directly in the prefix
pnpm_paths = [
Path(npm_prefix) / "node_modules" / ".bin" / "pnpm.cmd",
Path(npm_prefix) / "node_modules" / ".bin" / "pnpm",
Path(npm_prefix) / "pnpm.cmd",
Path(npm_prefix) / "pnpm",
]
for path in pnpm_paths:
if path.exists():
return str(path)
except:
pass
# Check npm's global directory (alternative method)
try:
result = subprocess.run(
["npm", "root", "-g"],
capture_output=True,
text=True,
check=True,
timeout=5
)
npm_global = result.stdout.strip()
if npm_global:
# pnpm might be in the parent directory's node_modules/.bin
pnpm_paths = [
Path(npm_global).parent / "node_modules" / ".bin" / "pnpm.cmd",
Path(npm_global).parent / "node_modules" / ".bin" / "pnpm",
Path(npm_global).parent.parent / "node_modules" / ".bin" / "pnpm.cmd",
]
for path in pnpm_paths:
if path.exists():
return str(path)
except:
pass
# Check common npm locations (where npm installs global .cmd wrappers)
appdata = os.environ.get('APPDATA', '')
if appdata:
npm_paths = [
Path(appdata) / 'npm' / 'pnpm.cmd',
Path(appdata) / 'npm' / 'pnpm',
]
for path in npm_paths:
if path.exists():
return str(path)
# Check Program Files (Node.js installation directory)
program_files = os.environ.get('ProgramFiles', 'C:/Program Files')
nodejs_paths = [
Path(program_files) / 'nodejs' / 'pnpm.cmd',
Path(program_files) / 'nodejs' / 'pnpm',
Path(program_files) / 'nodejs' / 'node_modules' / 'pnpm' / 'bin' / 'pnpm.cmd',
]
for path in nodejs_paths:
if path.exists():
return str(path)
return None
def find_vcpkg_windows() -> Optional[str]:
"""Find vcpkg on Windows (env vars, PATH, VS, common locations)."""
# Check environment variables first
for env_var in ["VCPKG_ROOT", "VCPKG_INSTALLATION_ROOT"]:
vcpkg_root = os.environ.get(env_var)
if vcpkg_root and Path(vcpkg_root).exists():
return vcpkg_root
# Check PATH
vcpkg_cmd = shutil.which("vcpkg")
if vcpkg_cmd:
return str(Path(vcpkg_cmd).parent)
# Check Visual Studio
vs_path = find_visual_studio()
if vs_path:
vs_vcpkg = Path(vs_path) / 'VC' / 'vcpkg'
if vs_vcpkg.exists() and (vs_vcpkg / 'vcpkg.exe').exists():
return str(vs_vcpkg)
# Check common installation locations
common_paths = [
Path('C:/vcpkg'),
Path('C:/tools/vcpkg'),
Path(os.path.expanduser('~/vcpkg')),
Path(os.environ.get('USERPROFILE', '')) / 'vcpkg' if os.environ.get('USERPROFILE') else None,
Path(os.environ.get('ProgramFiles', 'C:/Program Files')) / 'vcpkg',
]
for path in common_paths:
if path and path.exists() and (path / 'vcpkg.exe').exists():
return str(path)
return None
def check_tool(name: str, command: List[str]) -> Tuple[bool, str]:
"""Check if a tool is available."""
global CMAKE_PATH, PNPM_PATH
system_name, _ = detect_platform()
# Special handling for CMake on Windows
if name == "CMake" and system_name == "Windows":
cmake_path = find_cmake_windows()
if cmake_path:
try:
result = subprocess.run([cmake_path, "--version"], capture_output=True, text=True, check=True)
version = result.stdout.split('\n')[0] if result.stdout else "installed"
# Store for later use
CMAKE_PATH = cmake_path
return True, version
except:
return False, ""
return False, ""
# Special handling for pnpm on Windows
if name == "pnpm" and system_name == "Windows":
pnpm_path = find_pnpm_windows()
if pnpm_path:
try:
# Use the full path directly - Windows can execute .cmd files this way
result = subprocess.run(
[pnpm_path, "--version"],
capture_output=True,
text=True,
check=True
)
version = result.stdout.split('\n')[0] if result.stdout else "installed"
# Store for later use
PNPM_PATH = pnpm_path
return True, version
except:
# If execution fails, fall through to standard check
pass
# Fall through to standard check if Windows-specific search failed
# Standard check for other tools (and fallback for pnpm on Windows)
tool_path = shutil.which(command[0])
if tool_path:
try:
# Use the full path found by shutil.which for better reliability
result = subprocess.run(
[tool_path] + command[1:],
capture_output=True,
text=True,
check=True
)
version = result.stdout.split('\n')[0] if result.stdout else "installed"
# Store pnpm path if found via standard check
if name == "pnpm":
PNPM_PATH = tool_path
return True, version
except:
return False, ""
return False, ""
def check_vcpkg() -> Optional[str]:
"""Check vcpkg and return root path (with Windows-specific detection)."""
system_name, _ = detect_platform()
if system_name == "Windows":
return find_vcpkg_windows()
else:
# Unix-like systems
for env_var in ["VCPKG_ROOT", "VCPKG_INSTALLATION_ROOT"]:
vcpkg_root = os.environ.get(env_var)
if vcpkg_root and os.path.isdir(vcpkg_root):
return vcpkg_root
vcpkg_cmd = shutil.which("vcpkg")
if vcpkg_cmd:
return str(Path(vcpkg_cmd).parent)
return None
def check_prerequisites(skip_app: bool):
"""Check all prerequisites with clean table output."""
tools = [
("CMake", ["cmake", "--version"]),
("Ninja", ["ninja", "--version"]),
]
if not skip_app:
tools.append(("pnpm", ["pnpm", "--version"]))
results = []
all_ok = True
for name, cmd in tools:
ok, version = check_tool(name, cmd)
results.append((name, ok, version))
if not ok:
all_ok = False
# Check vcpkg separately
vcpkg_root = check_vcpkg()
if vcpkg_root:
os.environ["VCPKG_ROOT"] = vcpkg_root
results.append(("vcpkg", True, vcpkg_root))
else:
results.append(("vcpkg", False, "not found"))
all_ok = False
# Print results
system_name, _ = detect_platform()
for name, ok, info in results:
if ok:
log(f'{Style.GREEN}{Style.CHECK}{Style.RESET} {name}')
# Show version for tools, or path for vcpkg
if info:
# Show version for CMake, Ninja, pnpm (always)
# Show path for vcpkg (on Windows or in verbose mode)
if name in ["CMake", "Ninja", "pnpm"]:
log_dim(info)
elif name == "vcpkg" and (VERBOSE or system_name == "Windows"):
log_dim(info)
else:
log(f'{Style.RED}{Style.CROSS}{Style.RESET} {name} {Style.GRAY}(missing){Style.RESET}')
if not all_ok:
print()
if system_name == "Windows":
print(f' {Style.YELLOW}{Style.INFO} Windows Tips:{Style.RESET}')
print(f' {Style.DIM}• Run from "Developer Command Prompt for VS" to use bundled tools{Style.RESET}')
print(f' {Style.DIM}• Or install standalone: CMake, Ninja, vcpkg{Style.RESET}')
print(f' {Style.DIM}• Set VCPKG_ROOT environment variable if vcpkg is installed{Style.RESET}')
print()
print_error("Missing prerequisites - install them and try again")
print()
def run_command(cmd: List[str], cwd: Optional[Path] = None, description: str = "") -> Tuple[bool, str]:
"""Run command with spinner or verbose output."""
global PNPM_PATH
system_name, _ = detect_platform()
# Resolve pnpm path on Windows if needed
if cmd and cmd[0] == "pnpm" and system_name == "Windows" and PNPM_PATH != "pnpm":
cmd = [PNPM_PATH] + cmd[1:]
try:
if VERBOSE:
log_dim(f'$ {" ".join(cmd)}')
result = subprocess.run(cmd, cwd=cwd, check=True)
return True, ""
else:
result = subprocess.run(cmd, cwd=cwd, check=True, capture_output=True, text=True)
return True, result.stdout + result.stderr
except subprocess.CalledProcessError as e:
output = ""
if hasattr(e, 'stdout') and e.stdout:
output += e.stdout
if hasattr(e, 'stderr') and e.stderr:
output += e.stderr
print_command_error(cmd, e.returncode, output, description)
return False, output
except FileNotFoundError:
print_error(f"Command not found: {cmd[0]}")
return False, ""
def build_engine(preset: str, clean: bool, run_tests: bool, project_root: Path) -> Optional[Path]:
"""Build C++ engine."""
engine_dir = project_root / "engine"
if "dev" in preset:
build_dir = engine_dir / "build"
build_type = "Debug"
else:
build_dir = engine_dir / "build-release"
build_type = "Release"
log_dim(f'Build type: {build_type}')
log_dim(f'Preset: {preset}')
print()
# Clean
if clean and build_dir.exists():
spinner = Spinner("Cleaning build directory")
spinner.start()
shutil.rmtree(build_dir)
spinner.stop()
print()
# Configure
spinner = Spinner("Configuring CMake")
spinner.start()
configure_cmd = [CMAKE_PATH, "--preset", preset]
if run_tests:
configure_cmd.append("-DVAYU_BUILD_TESTS=ON")
success, output = run_command(configure_cmd, cwd=engine_dir, description="CMake configuration")
if success:
spinner.stop()
else:
spinner.stop(Style.CROSS, Style.RED)
return None
print()
# Build
spinner = Spinner(f"Building {build_type}")
spinner.start()
build_cmd = [CMAKE_PATH, "--build", "--preset", preset]
success, output = run_command(build_cmd, cwd=engine_dir, description="Build")
if success:
spinner.stop()
else:
spinner.stop(Style.CROSS, Style.RED)
return None
# Verify binary
system_name, _ = detect_platform()
if system_name == "Windows":
binary_name = f"{build_type}/vayu-engine.exe"
else:
binary_name = "vayu-engine"
engine_binary = build_dir / binary_name
if not engine_binary.exists():
print_error(f"Binary not found: {engine_binary}")
return None
# Tests
if run_tests:
print()
spinner = Spinner("Running tests")
spinner.start()
test_cmd = ["ctest", "--preset", preset]
success, output = run_command(test_cmd, cwd=engine_dir, description="Unit tests")
if success:
spinner.stop()
else:
spinner.stop(Style.CROSS, Style.RED)
return None
return engine_binary
def run_tests_only(preset: str, project_root: Path) -> bool:
"""Run tests on existing build without rebuilding."""
engine_dir = project_root / "engine"
# Check if build directory exists
if "dev" in preset:
build_dir = engine_dir / "build"
else:
build_dir = engine_dir / "build-release"
if not build_dir.exists():
print_error(f"Build directory not found: {build_dir}\nRun a build first with: python build.py -e -t")
return False
# Check if tests were built
test_binary = build_dir / "vayu_tests"
system_name, _ = detect_platform()
if system_name == "Windows":
build_type = "Debug" if "dev" in preset else "Release"
test_binary = build_dir / build_type / "vayu_tests.exe"
if not test_binary.exists():
print_error(f"Tests not found. Build with tests first:\npython build.py -e -t")
return False
log_dim(f'Build directory: {build_dir}')
log_dim(f'Preset: {preset}')
print()
spinner = Spinner("Running tests")
spinner.start()
test_cmd = ["ctest", "--preset", preset, "--output-on-failure"]
# Run with output visible in verbose mode
if VERBOSE:
spinner.stop()
log_dim(f'$ {" ".join(test_cmd)}')
result = subprocess.run(test_cmd, cwd=engine_dir)
success = result.returncode == 0
else:
result = subprocess.run(test_cmd, cwd=engine_dir, capture_output=True, text=True)
success = result.returncode == 0
output = result.stdout + result.stderr
if success:
spinner.stop()
# Parse test count from output
if "tests passed" in output.lower():
for line in output.split('\n'):
if "tests passed" in line.lower():
print(f' {Style.GREEN}{Style.CHECK}{Style.RESET} {line.strip()}')
break
else:
spinner.stop(Style.CROSS, Style.RED)
print()
print(f' {Style.RED}{Style.CROSS} Tests failed{Style.RESET}')
print()
# Show failed test output
lines = output.strip().split('\n')
for line in lines:
if any(kw in line.lower() for kw in ['failed', 'error', 'fatal']):
print(f' {Style.RED}{line}{Style.RESET}')
elif line.strip():
print(f' {Style.GRAY}{line}{Style.RESET}')
return success
def setup_icons(project_root: Path):
"""Setup application icons."""
spinner = Spinner("Setting up icons")
spinner.start()
icon_png_dir = project_root / "shared" / "icon_png"
icon_ico_dir = project_root / "shared" / "icon_ico"
build_dir = project_root / "app" / "build"
build_dir.mkdir(exist_ok=True)
# Copy icons
png_256 = icon_png_dir / "vayu_icon_256x256.png"
if png_256.exists():
shutil.copy(png_256, build_dir / "icon.png")
ico_256 = icon_ico_dir / "vayu_icon_256x256.ico"
if ico_256.exists():
shutil.copy(ico_256, build_dir / "icon.ico")
png_512 = icon_png_dir / "vayu_icon_512x512.png"
if png_512.exists():
shutil.copy(png_512, build_dir / "icon.png")
icon_set_dir = build_dir / "icons"
icon_set_dir.mkdir(exist_ok=True)
if icon_png_dir.exists():
for png_file in icon_png_dir.glob("*.png"):
shutil.copy(png_file, icon_set_dir)
spinner.stop()
def build_app(dev_mode: bool, engine_binary: Optional[Path], project_root: Path) -> bool:
"""Build Electron app."""
app_dir = project_root / "app"
if not app_dir.exists():
print_error(f"App directory not found: {app_dir}")
return False
# Install dependencies
if not (app_dir / "node_modules").exists():
spinner = Spinner("Installing dependencies")
spinner.start()
success, _ = run_command(["pnpm", "install"], cwd=app_dir, description="pnpm install")
if success:
spinner.stop()
else:
spinner.stop(Style.CROSS, Style.RED)
return False
print()
# Icons
setup_icons(project_root)
print()
if dev_mode:
# Dev build
spinner = Spinner("Compiling TypeScript")
spinner.start()
success, _ = run_command(["pnpm", "run", "electron:compile"], cwd=app_dir, description="TypeScript compilation")
if success:
spinner.stop()
else:
spinner.stop(Style.CROSS, Style.RED)
return False
else:
# Prod build
spinner = Spinner("Compiling TypeScript")
spinner.start()
success, _ = run_command(["pnpm", "run", "electron:compile"], cwd=app_dir, description="TypeScript compilation")
if success:
spinner.stop()
else:
spinner.stop(Style.CROSS, Style.RED)
return False
print()
spinner = Spinner("Building React app")
spinner.start()
success, _ = run_command(["pnpm", "run", "build"], cwd=app_dir, description="React build")
if success:
spinner.stop()
else:
spinner.stop(Style.CROSS, Style.RED)
return False
print()
# Copy engine binary
if engine_binary and engine_binary.exists():
spinner = Spinner("Copying engine binary")
spinner.start()
resources_dir = app_dir / "build" / "resources" / "bin"
resources_dir.mkdir(parents=True, exist_ok=True)
system_name, _ = detect_platform()
if system_name == "Windows":
target_name = "vayu-engine.exe"
dll_dir = engine_binary.parent
for dll in dll_dir.glob("*.dll"):
shutil.copy(dll, resources_dir)
else:
target_name = "vayu-engine"
shutil.copy(engine_binary, resources_dir / target_name)
os.chmod(resources_dir / target_name, 0o755)
spinner.stop()
print()
# Package
spinner = Spinner("Packaging application")
spinner.start()
success, _ = run_command(["pnpm", "run", "electron:pack"], cwd=app_dir, description="Electron packaging")
if success:
spinner.stop()
else:
spinner.stop(Style.CROSS, Style.RED)
return False
return True
def get_artifacts(dev_mode: bool, skip_engine: bool, skip_app: bool,
engine_binary: Optional[Path], project_root: Path) -> List[Tuple[str, str]]:
"""Collect build artifacts."""
artifacts = []
app_dir = project_root / "app"
if not skip_engine and engine_binary and engine_binary.exists():
try:
rel_path = engine_binary.relative_to(Path.cwd())
except ValueError:
rel_path = engine_binary
artifacts.append(("Engine", str(rel_path)))
if not skip_app and not dev_mode:
release_dir = app_dir / "release"
if release_dir.exists():
system_name, _ = detect_platform()
if system_name == "Windows":
for installer in release_dir.glob("*.exe"):
try:
rel_path = installer.relative_to(Path.cwd())
except ValueError:
rel_path = installer
artifacts.append(("Installer", str(rel_path)))
elif system_name == "Linux":
for appimage in release_dir.glob("*.AppImage"):
try:
rel_path = appimage.relative_to(Path.cwd())
except ValueError:
rel_path = appimage
artifacts.append(("AppImage", str(rel_path)))
for deb in release_dir.glob("*.deb"):
try:
rel_path = deb.relative_to(Path.cwd())
except ValueError:
rel_path = deb
artifacts.append(("Debian", str(rel_path)))
elif system_name == "macOS":
for dmg in release_dir.glob("*.dmg"):
try:
rel_path = dmg.relative_to(Path.cwd())
except ValueError:
rel_path = dmg
artifacts.append(("DMG", str(rel_path)))
return artifacts
def print_next_steps(dev_mode: bool, skip_app: bool, artifacts: List[Tuple[str, str]], project_root: Path):
"""Print next steps."""
print(f' {Style.BOLD}Next Steps{Style.RESET}')
print()
if dev_mode and not skip_app:
app_dir = project_root / "app"
try:
rel_path = app_dir.relative_to(Path.cwd())
except ValueError:
rel_path = app_dir
print(f' {Style.DIM}Run the development app{Style.RESET}')
system_name, _ = detect_platform()
cmd_sep = ";" if system_name == "Windows" else "&&"
print(f' {Style.CYAN}$ cd {rel_path} {cmd_sep} pnpm run electron:dev{Style.RESET}')
elif artifacts:
for label, path in artifacts:
if label == "Engine":
print(f' {Style.DIM}Run the engine{Style.RESET}')
print(f' {Style.CYAN}$ ./{path}{Style.RESET}')
elif label in ["Installer", "AppImage", "Debian", "DMG"]:
system_name, _ = detect_platform()
if system_name == "Windows":
print(f' {Style.DIM}Run the installer{Style.RESET}')
print(f' {Style.CYAN}$ ./{path}{Style.RESET}')
elif system_name == "Linux":
if label == "AppImage":
print(f' {Style.DIM}Run the AppImage{Style.RESET}')
print(f' {Style.CYAN}$ chmod +x {path} && ./{path}{Style.RESET}')
elif label == "Debian":
print(f' {Style.DIM}Install the package{Style.RESET}')
print(f' {Style.CYAN}$ sudo dpkg -i {path}{Style.RESET}')
elif system_name == "macOS":
print(f' {Style.DIM}Open the DMG{Style.RESET}')
print(f' {Style.CYAN}$ open {path}{Style.RESET}')
break # Only show first artifact instruction
print()
def parse_version(version_str: str) -> Tuple[int, int, int]:
"""Parse version string into (major, minor, patch)."""
match = re.match(r'^(\d+)\.(\d+)\.(\d+)$', version_str.strip())
if not match:
print_error(f"Invalid version format: {version_str} (expected: X.Y.Z)")
return int(match.group(1)), int(match.group(2)), int(match.group(3))
def bump_version(bump_type: str, project_root: Path, dry_run: bool = False):
"""Bump version across all project files."""
version_file = project_root / "VERSION"
engine_cmake = project_root / "engine" / "CMakeLists.txt"
engine_version_hpp = project_root / "engine" / "include" / "vayu" / "version.hpp"
engine_vcpkg_json = project_root / "engine" / "vcpkg.json"
app_package_json = project_root / "app" / "package.json"
# Read current version
if not version_file.exists():
print_error(f"VERSION file not found: {version_file}")
current_version = version_file.read_text().strip()
major, minor, patch = parse_version(current_version)
# Calculate new version
if bump_type in ['major', 'minor', 'patch']:
if bump_type == 'major':
new_version = f"{major + 1}.0.0"
elif bump_type == 'minor':
new_version = f"{major}.{minor + 1}.0"
else: # patch
new_version = f"{major}.{minor}.{patch + 1}"
else:
# Specific version provided
new_version = bump_type
# Validate it
parse_version(new_version)
print()
print(f' {Style.BOLD}Version Bump{Style.RESET}')
print()
print(f' {Style.CYAN}{current_version}{Style.RESET} {Style.ARROW} {Style.GREEN}{new_version}{Style.RESET}')
print()
if dry_run:
print(f' {Style.YELLOW}{Style.INFO} Dry run - no changes will be made{Style.RESET}')
print()
return
new_major, new_minor, new_patch = parse_version(new_version)
# Update VERSION file
log(f'{Style.GREEN}{Style.CHECK}{Style.RESET} Updating VERSION')
version_file.write_text(new_version + '\n')
# Update CMakeLists.txt
log(f'{Style.GREEN}{Style.CHECK}{Style.RESET} Updating engine/CMakeLists.txt')
cmake_content = engine_cmake.read_text()
cmake_content = re.sub(
r'VERSION \d+\.\d+\.\d+',
f'VERSION {new_version}',
cmake_content
)
engine_cmake.write_text(cmake_content)
# Update version.hpp
log(f'{Style.GREEN}{Style.CHECK}{Style.RESET} Updating engine/include/vayu/version.hpp')
hpp_content = engine_version_hpp.read_text()
hpp_content = re.sub(r'#define VAYU_VERSION_MAJOR \d+', f'#define VAYU_VERSION_MAJOR {new_major}', hpp_content)
hpp_content = re.sub(r'#define VAYU_VERSION_MINOR \d+', f'#define VAYU_VERSION_MINOR {new_minor}', hpp_content)
hpp_content = re.sub(r'#define VAYU_VERSION_PATCH \d+', f'#define VAYU_VERSION_PATCH {new_patch}', hpp_content)
hpp_content = re.sub(r'#define VAYU_VERSION_STRING ".*"', f'#define VAYU_VERSION_STRING "{new_version}"', hpp_content)
engine_version_hpp.write_text(hpp_content)
# Update engine/vcpkg.json
log(f'{Style.GREEN}{Style.CHECK}{Style.RESET} Updating engine/vcpkg.json')
vcpkg_data = json.loads(engine_vcpkg_json.read_text())
vcpkg_data['version'] = new_version
engine_vcpkg_json.write_text(json.dumps(vcpkg_data, indent=2) + '\n')
# Update app/package.json
log(f'{Style.GREEN}{Style.CHECK}{Style.RESET} Updating app/package.json')
package_data = json.loads(app_package_json.read_text())
package_data['version'] = new_version
app_package_json.write_text(json.dumps(package_data, indent='\t') + '\n')
print()
print(f' {Style.GREEN}{Style.CHECK} Version bumped to {new_version}{Style.RESET}')
print()
print(f' {Style.BOLD}Next Steps{Style.RESET}')
print()
print(f' {Style.DIM}Review changes{Style.RESET}')
print(f' {Style.CYAN}$ git diff{Style.RESET}')
print()
system_name, _ = detect_platform()
cmd_sep = ";" if system_name == "Windows" else "&&"
print(f' {Style.DIM}Commit and tag{Style.RESET}')
print(f' {Style.CYAN}$ git commit -am "chore: bump version to {new_version}"{Style.RESET}')
print(f' {Style.CYAN}$ git tag v{new_version}{Style.RESET}')
print(f' {Style.CYAN}$ git push {cmd_sep} git push --tags{Style.RESET}')
print()
def main():
global VERBOSE
parser = argparse.ArgumentParser(
description='Vayu build system',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=f'''
{Style.BOLD}examples{Style.RESET}
python build.py build everything (production)
python build.py --dev build everything (development)
python build.py -e build engine only
python build.py -a build app only
python build.py -e -t build engine + run tests
python build.py --test-only run tests without rebuilding
python build.py -c -v clean build with verbose output
python build.py --bump-version patch bump patch version (0.1.1 -> 0.1.2)
python build.py --bump-version 2.0.0 set specific version
{Style.BOLD}aliases{Style.RESET}
-e --engine-only -a --app-only -c --clean