@@ -61,17 +61,29 @@ def build(profile: str = typer.Option("release", "--profile", "-p", help="Specif
6161 if is_cmake : # Repo uses CMake, so execute CMake commands.
6262 print ("[#cccccc](ner build):[/#cccccc] [blue]CMake-based project detected.[/blue]" )
6363 if clean :
64- run_command_docker ('cmake --build build --target clean ; find . -type d -name "build" -exec rm -rf {} +' )
64+ returncode = run_command_docker ('cmake --build build --target clean ; find . -type d -name "build" -exec rm -rf {} +' )
6565 print ("[#cccccc](ner build):[/#cccccc] [green]Ran build-cleaning command.[/green]" )
66+ if returncode != 0 :
67+ sys .exit (returncode )
6668 else :
67- run_command_docker (f"mkdir -p build && cd build && cmake -DCMAKE_BUILD_TYPE={ profile .capitalize ()} -DCMAKE_TOOLCHAIN_FILE=cmake/gcc-arm-none-eabi.cmake .. && cmake --build ." , stream_output = True )
68- run_command_docker ('chmod 777 -R ./build/*' )
69+ returncode = run_command_docker (f"mkdir -p build && cd build && cmake -DCMAKE_BUILD_TYPE={ profile .capitalize ()} -DCMAKE_TOOLCHAIN_FILE=cmake/gcc-arm-none-eabi.cmake .. && cmake --build ." , stream_output = True )
70+ if returncode != 0 :
71+ sys .exit (returncode )
72+ returncode = run_command_docker ('chmod 777 -R ./build/*' )
73+ if returncode != 0 :
74+ sys .exit (returncode )
6975 else : # Repo uses Make, so execute Make commands.
7076 print ("[#cccccc](ner build):[/#cccccc] [blue]Makefile-based project detected.[/blue]" )
7177 if clean :
72- run_command_docker ("make clean" , stream_output = True )
78+ returncode = run_command_docker ("make clean" , stream_output = True )
79+ if returncode != 0 :
80+ sys .exit (returncode )
81+
7382 else :
74- run_command_docker (f"make -j{ os .cpu_count ()} " , stream_output = True )
83+ returncode = run_command_docker (f"make -j{ os .cpu_count ()} " , stream_output = True )
84+ if returncode != 0 :
85+ sys .exit (returncode )
86+
7587
7688 if not clean and is_cmake :
7789 fix_compile_commands ()
@@ -346,9 +358,8 @@ def usbip(connect: bool = typer.Option(False, "--connect", help="Connect to a US
346358# Helper functions - not direct commands
347359# ==============================================================================
348360
349- def run_command (command , stream_output = False , exit_on_fail = False ):
350- """Run a shell command. Optionally stream the output in real-time."""
351-
361+ def run_command (command , stream_output = False , exit_on_fail = False ) -> int :
362+ """Run a shell command. Optionally stream the output in real-time. Returns the returncode of the command called"""
352363 if stream_output :
353364
354365 process = subprocess .Popen (command , text = True )
@@ -358,17 +369,23 @@ def run_command(command, stream_output=False, exit_on_fail=False):
358369 print (f"Error: Command exited with code { returncode } " , file = sys .stderr )
359370 if exit_on_fail :
360371 sys .exit (returncode )
372+ else :
373+ return returncode
361374
375+ return 0
362376 else :
363377 try :
364378 result = subprocess .run (command , check = True , capture_output = True , text = True )
365379 if result .stdout and result .stdout .strip (): # Only print if stdout is not empty or just whitespace
366380 print (result .stdout )
381+ return 0 # Code should be zero if it reaches this point
367382 except subprocess .CalledProcessError as e :
368383 print (f"Error occurred: { e } " , file = sys .stderr )
369384 print (e .stderr , file = sys .stderr )
370385 if exit_on_fail :
371386 sys .exit (e .returncode )
387+ return e .returncode
388+
372389
373390def fix_compile_commands ():
374391 '''
@@ -421,11 +438,11 @@ def fix_compile_commands():
421438
422439 print ('Successfully patched compile_commands.json' )
423440
424- def run_command_docker (command , stream_output = False ):
441+ def run_command_docker (command , stream_output = False ) -> int :
425442 """Run a command in the Docker container."""
426443 docker_command = ["docker" , "compose" , "run" , "--rm" , "ner-gcc-arm" , "sh" , "-c" , command ]
427444 print (f"[bold blue](ner-gcc-arm): Running command '{ command } ' in Docker container." )
428- run_command (docker_command , stream_output = stream_output )
445+ return run_command (docker_command , stream_output = stream_output )
429446
430447def disconnect_usbip ():
431448 """Disconnect the current USB device."""
0 commit comments