Automated Build System Progress

logistics-852936_1280_falco_pixabay

I’ve been building up to this for years, and still have some cogs to put into place, but I’ve finally got a major part working; the master script. The master script is something I started about 6 months ago now, wow, and ran into some issues. First I needed it to recursively search directories and run a set of scripts that would be part of the auto build process. These scripts have been part of my template project for many years waiting.

Just searching and calling the scripts, if they exist, was not enough though. If one of the steps failed, say there are no updates, or the build failed, then the rest of the scripts should not be called, so I needed the ability to pass back a value to the master script to halt that process if needed. I finally have this working, for batch scripts, and am that much closer to having a fully automated build system, of my own.

It was relatively easy to send a return value back in batch, but the difficult part was doing it within a loop. See, batch scripts don’t work like C++ or other languages, instead the FOR command contains ALL the commands within. What this means is when the interpreter reads “FOR …” anything in the … gets expanded and then executed multiple times as needed for the loop.

@ECHO off
SET list=auto_clean.bat auto_build.bat auto_deploy.bat
FOR %%f IN (%list%) DO (
  IF EXIST %%f (
    SET auto_return_value=0
    CALL %%f
    ECHO Returned value: %auto_return_value%
    IF 0==%auto_return_value% (
      REM Continue like normal
    ) ELSE (
      GOTO BreakForLoop
    )
  ) ELSE (
    REM echo no %%i file here
  )
)

This bit of code will always continue like normal because of the above reasons. Instead what is needed it a way to delay the expansion so that it happens when the command is executed vs when it is read. Batch scripts actually have a way to do this, though turned off by default. Enter the command “help set” in command prompt and read about delayed environment variable expansion. Essentially, one must use the command SETLOCAL ENABLEDELAYEDEXPANSION to allow this. In addition to enabling that behavior the variable must be treated with exclamation ! marks. !variable!

@ECHO off
SETLOCAL ENABLEDELAYEDEXPANSION
SET list=auto_clean.bat auto_build.bat auto_deploy.bat

REM Searching the current directory ./ before searching the child directories.
FOR %%f IN (%list%) DO (
  IF EXIST %%f (
    SET auto_return_value=0
    CALL %%f
    ECHO Returned value: !auto_return_value!
    IF 0==!auto_return_value! (
      REM Continue like normal
    ) ELSE (
      GOTO BreakForLoop
    )
  ) ELSE (
    REM echo no %%i file here
  )
)

This allowed the loop to process the return value correctly and if one of the scripts returns a non-zero value the rest of the scripts will get skipped from the early break out of the loop. I’d like to thank Jïn Muhjo of Ayphix Entertainment for helping create the recursive search and call, and g12345 from LudumDare IRC for helping me figure out the delayed expansion issue.

If the automated build system seems like it would be useful for you, grab it from GitHub!

Comments are closed.