2
0
mirror of https://github.com/rehlds/metamod-r.git synced 2024-12-27 23:25:36 +03:00
metamod-r/metamod/msvc/PreBuild.bat

218 lines
5.2 KiB
Batchfile
Raw Normal View History

2016-07-26 03:22:47 +03:00
@setlocal enableextensions enabledelayedexpansion
@echo off
::
:: Pre-build auto-versioning script
::
chcp 65001
2016-07-26 03:22:47 +03:00
set srcdir=%~1
set repodir=%~2
set old_version=
set version_major=0
set version_minor=0
set version_maintenance=0
2017-07-31 17:44:24 +03:00
set version_modifed=
set commitSHA=
set commitURL=
set commitCount=0
set branch_name=master
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set "dt=%%a"
set "YYYY=%dt:~0,4%"
set "MM=%dt:~4,2%"
set "DD=%dt:~6,2%"
set "hour=%dt:~8,2%"
set "min=%dt:~10,2%"
set "sec=%dt:~12,2%"
::
:: Remove leading zero from MM (e.g 09 > 9)
::
for /f "tokens=* delims=0" %%I in ("%MM%") do set MM=%%I
::
:: Index into array to get month name
::
2017-07-31 17:44:24 +03:00
for /f "tokens=%MM%" %%I in ("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec") do set "month=%%I"
2016-07-26 03:22:47 +03:00
::
:: Check for git.exe presence
::
CALL git.exe describe >NUL 2>&1
set errlvl="%ERRORLEVEL%"
::
:: Read old appversion.h, if present
::
IF EXIST "%srcdir%\appversion.h" (
FOR /F "usebackq tokens=1,2,3" %%i in ("%srcdir%\appversion.h") do (
IF %%i==#define (
2017-07-31 17:44:24 +03:00
IF %%j==APP_VERSION (
:: Remove quotes
set v=%%k
set old_version=!v:"=!
)
2016-07-26 03:22:47 +03:00
)
)
)
IF %errlvl% == "1" (
echo can't locate git.exe - auto-versioning step won't be performed
:: if we haven't appversion.h, we need to create it
IF NOT "%old_version%" == "" (
2017-07-31 17:44:24 +03:00
set commitCount=0
2016-07-26 03:22:47 +03:00
)
)
::
:: Read major, minor and maintenance version components from Version.h
::
IF EXIST "%srcdir%\version.h" (
FOR /F "usebackq tokens=1,2,3" %%i in ("%srcdir%\version.h") do (
IF %%i==#define (
IF %%j==VERSION_MAJOR set version_major=%%k
IF %%j==VERSION_MINOR set version_minor=%%k
IF %%j==VERSION_MAINTENANCE set version_maintenance=%%k
2016-07-26 03:22:47 +03:00
)
)
)
::
:: Read revision and release date from it
::
IF NOT %errlvl% == "1" (
2017-07-31 17:44:24 +03:00
:: Get current branch
FOR /F "tokens=*" %%i IN ('"git -C "%repodir%\." rev-parse --abbrev-ref HEAD"') DO (
set branch_name=%%i
)
FOR /F "tokens=*" %%i IN ('"git -C "%repodir%\." rev-list --count !branch_name!"') DO (
2016-07-26 03:22:47 +03:00
IF NOT [%%i] == [] (
set commitCount=%%i
2016-07-26 03:22:47 +03:00
)
)
)
::
:: Get remote url repository
::
IF NOT %errlvl% == "1" (
set branch_remote=origin
:: Get remote name by current branch
FOR /F "tokens=*" %%i IN ('"git -C "%repodir%\." config branch.!branch_name!.remote"') DO (
set branch_remote=%%i
)
:: Get remote url
FOR /F "tokens=2 delims=@" %%i IN ('"git -C "%repodir%\." config remote.!branch_remote!.url"') DO (
2017-07-31 17:44:24 +03:00
set commitURL=%%i
2016-07-26 03:22:47 +03:00
)
:: Get commit id
FOR /F "tokens=*" %%i IN ('"git -C "%repodir%\." rev-parse --verify HEAD"') DO (
2017-07-31 17:44:24 +03:00
set shafull=%%i
set commitSHA=!shafull:~0,+7!
2016-07-26 03:22:47 +03:00
)
2017-07-31 17:44:24 +03:00
IF [!commitURL!] == [] (
2016-07-26 03:22:47 +03:00
FOR /F "tokens=1" %%i IN ('"git -C "%repodir%\." config remote.!branch_remote!.url"') DO (
2017-07-31 17:44:24 +03:00
set commitURL=%%i
2016-07-26 03:22:47 +03:00
)
:: strip .git
2017-07-31 17:44:24 +03:00
if "x!commitURL:~-4!"=="x.git" (
set commitURL=!commitURL:~0,-4!
2016-07-26 03:22:47 +03:00
)
:: append extra string
2017-07-31 17:44:24 +03:00
If NOT "!commitURL!"=="!commitURL:bitbucket.org=!" (
set commitURL=!commitURL!/commits/
2016-07-26 03:22:47 +03:00
) ELSE (
2017-07-31 17:44:24 +03:00
set commitURL=!commitURL!/commit/
2016-07-26 03:22:47 +03:00
)
) ELSE (
:: strip .git
2017-07-31 17:44:24 +03:00
if "x!commitURL:~-4!"=="x.git" (
set commitURL=!commitURL:~0,-4!
2016-07-26 03:22:47 +03:00
)
:: replace : to /
2017-07-31 17:44:24 +03:00
set commitURL=!commitURL::=/!
2016-07-26 03:22:47 +03:00
:: append extra string
2017-07-31 17:44:24 +03:00
If NOT "!commitURL!"=="!commitURL:bitbucket.org=!" (
set commitURL=https://!commitURL!/commit/
) ELSE (
set commitURL=https://!commitURL!/commits/
2016-07-26 03:22:47 +03:00
)
)
)
::
:: Detect local modifications
::
set localChanged=0
IF NOT %errlvl% == "1" (
FOR /F "tokens=*" %%i IN ('"git -C "%repodir%\." ls-files -m"') DO (
set localChanged=1
)
)
IF [%localChanged%]==[1] (
2017-07-31 17:44:24 +03:00
set version_modifed=+m
2016-07-26 03:22:47 +03:00
)
2017-07-31 17:44:24 +03:00
::
:: Now form full version string like 1.0.0.1
::
set new_version=%version_major%.%version_minor%.%version_maintenance%.%commitCount%%version_modifed%
2017-07-31 17:44:24 +03:00
2016-07-26 03:22:47 +03:00
::
:: Update appversion.h if version has changed or modifications/mixed revisions detected
::
IF NOT "%new_version%"=="%old_version%" goto _update
2017-07-31 17:44:24 +03:00
2016-07-26 03:22:47 +03:00
goto _exit
:_update
2017-07-31 17:44:24 +03:00
::
:: Write appversion.h
::
echo Updating appversion.h, new version is "%new_version%", the old one was %old_version%
2016-07-26 03:22:47 +03:00
echo #ifndef __APPVERSION_H__>"%srcdir%\appversion.h"
echo #define __APPVERSION_H__>>"%srcdir%\appversion.h"
echo.>>"%srcdir%\appversion.h"
echo //>>"%srcdir%\appversion.h"
2016-07-26 03:22:47 +03:00
echo // This file is generated automatically.>>"%srcdir%\appversion.h"
echo // Don't edit it.>>"%srcdir%\appversion.h"
echo //>>"%srcdir%\appversion.h"
2016-07-26 03:22:47 +03:00
echo.>>"%srcdir%\appversion.h"
echo // Version defines>>"%srcdir%\appversion.h"
2017-07-31 17:44:24 +03:00
echo #define APP_VERSION "%new_version%">>"%srcdir%\appversion.h"
2016-07-26 03:22:47 +03:00
>>"%srcdir%\appversion.h" echo #define APP_VERSION_C %version_major%,%version_minor%,%version_maintenance%,%commitCount%
echo #define APP_VERSION_STRD "%version_major%.%version_minor%.%version_maintenance%.%commitCount%">>"%srcdir%\appversion.h"
2017-07-31 17:44:24 +03:00
echo #define APP_VERSION_FLAGS 0x0L>>"%srcdir%\appversion.h"
2016-07-26 03:22:47 +03:00
echo.>>"%srcdir%\appversion.h"
echo #define APP_COMMIT_DATE "%month% %DD% %YYYY%">>"%srcdir%\appversion.h"
2017-07-31 17:44:24 +03:00
echo #define APP_COMMIT_TIME "%hour%:%min%:%sec%">>"%srcdir%\appversion.h"
2016-07-26 03:22:47 +03:00
2017-07-31 17:44:24 +03:00
echo.>>"%srcdir%\appversion.h"
echo #define APP_COMMIT_SHA "%commitSHA%">>"%srcdir%\appversion.h"
echo #define APP_COMMIT_URL "%commitURL%">>"%srcdir%\appversion.h"
2016-07-26 03:22:47 +03:00
echo.>>"%srcdir%\appversion.h"
echo #endif //__APPVERSION_H__>>"%srcdir%\appversion.h"
echo.>>"%srcdir%\appversion.h"
:_exit
2017-07-31 17:44:24 +03:00
exit /B 0