@REM ============================================================================================
@REM Batch file to auto build libraries and main application.
@REM Stops build if a component fails.
@REM ============================================================================================

@REM Set builder path to the builder executable:
SET builderpath="c:\Program Files (x86)\Anywhere Software\B4J\B4JBuilder.exe"

@REM This is the language valid values are b4j,b4a,b4i
SET type="b4j"

@REM When set to 1, compilation of further builds will halt. Set to zero to carry on after failure.
SET stoponfail=1

@REM Set default values.
@call :SetDefaults

@REM ============================================================================================
@REM Create calls to build each library or application.
@REM ============================================================================================
@REM The order of calls is important. 
@REM If a library has a dependency on another library, then it should be compiled after the library it depend on.
@REM In this case, it should be lower down the list. Application should always be the last to be compiled.
@REM See further down in script for syntax, examples are below.
@REM ============================================================================================

@call:buildproject "C:\MyProjects\MyLibraries\LibraryA" "LibraryA" "Default" BuildLibrary
@call:buildproject "C:\MyProjects\MyLibraries\LibraryB" "LibraryA" "Default" BuildLibrary
@call:buildproject "C:\MyProjects\MyLibraries\LibraryC" "LibraryA" "Default" BuildLibrary
@call:buildproject "C:\MyProjects\MyApp" "MyApp" "Deploy
@REM Return to the original folder that the script was run from.
@cd %~dp0
@goto:eof

@REM ============================================================================================
@REM Function to build a project
@REM ============================================================================================
@REM Syntax: 
@REM @call:buildproject "{PROJECT_PATH}" "{PROJECT_NAME}" "{CONFIGURATION_NAME}" "{TASK}"
@REM Example:
@REM call:buildproject "C:\My Projects\MyB4JTestLibrary" "TestProject" "Default" BuildLibrary
@REM Notes: 
@REM PROJECT_PATH = Path to the root of the project to be compiled
@REM PROJECT_NAME = The projects name, if the project is myproject.b4j then this should be "myproject"
@REM CONFIGURATION_NAME = Indicates the build configuration to use. The default configuration "Default"
@REM TASK = The compilation task to undertake typically 'BuildLibrary' or Build. This propery is not supplied in quotes.
@REM ============================================================================================
:buildproject - Function to build a project.
@REM Check the build success variable.
@REM Build will only go ahead if previous build was ok, or stoponfail=0
@set /A DoBuild=0
@if %BUILDSUCCESS% EQU 1 (set /A DoBuild=1) 
@if %stoponfail% EQU 0 (set /A DoBuild=1) 
@if %DoBuild% EQU 1 ( 
	@echo ===================================================================
	@echo BUILDING=%~2 CONFIG=%~3
	@echo ===================================================================	
	@REM Change directory to the builder path (resolves issues with builder from other paths)
	@cd %~1
	@REM Reset the build success variable. If the build compiles, it will be set back to 1.
	@set /A BUILDSUCCESS=0
	@REM This calls the builder, with the supplied parameters, the for loop is then used to push each output line to the ProcessBuilderOutputLine function to check for success.
	@for /f "delims=" %%r in ('"call %builderpath% -task=%~4 -Project=%~2.%type% -ShowWarnings=True -Configuration=%~3"') do @call :ProcessBuilderOutputLine "%%r"
	@REM Finally output if the compilation failed or not.
	@if %BUILDSUCCESS% EQU 1 (@echo RESULT=Success) else (@echo RESULT=FAIL!)
)
@goto:eof

:ProcessBuilderOutputLine - Function to process single input line, coming in from the builder output checking for compilation success.
@REM Output the line from the builder.
@echo %~1
@REM Check if line contains the completed successfully message.
@echo %1|find "Completed successfully:" >nul
@REM If the find failed the errorlevel is 1. Set the build success state based on this.
@if errorlevel 1 (set /A BUILDSUCCESS=0) else (set /A BUILDSUCCESS=1)
)
@goto:eof

:SetDefaults - Set default values used during script exection.
@REM Set the build success to 1 for the first call.
@SET /A BUILDSUCCESS=1
@goto:eof