How can I automate getting the date of build into a constant visible to my code?

后端 未结 7 1367
太阳男子
太阳男子 2020-12-08 05:14

I would like to define in my code a constant holding the date on which the executable was built. I would naturally like to automate that process.

I know that I can w

7条回答
  •  囚心锁ツ
    2020-12-08 05:46

    Any version of Delphi, just windows batch to create an .inc file before build. No IDE needed at all.

    Here the script:

    REM CommandInterpreter: $(COMSPEC)
    @echo off
    
    setlocal enabledelayedexpansion  
    setlocal
    rem determine project top level directory from command file name
    set PRJDIR=%~dp0
    cd %PRJDIR%
    
    set BUILD_DATE_FILE=%PRJDIR%Source\BuildDate.inc
    
    call :GetGurrentDateTime&set BUILD_YEAR=!current_year!&set BUILD_MONTH=!current_month!&set BUILD_DAY=!current_day!&set BUILD_TIME=!current_time!
    
    echo const BUILD_YEAR = %BUILD_YEAR%;> "%BUILD_DATE_FILE%"
    :: 3 letter name Apr for April
    echo const BUILD_MONTH = '%BUILD_MONTH%';>> "%BUILD_DATE_FILE%"
    echo const BUILD_DAY = %BUILD_DAY%;>> "%BUILD_DATE_FILE%"
    echo const BUILD_TIME = '%BUILD_TIME%';>> "%BUILD_DATE_FILE%"
    
    goto :EOF
    echo Done
    exit /b 0
    
    :GetGurrentDateTime
    rem GET CURRENT DATE
    echo.>"%TEMP%\~.ddf"
    makecab /D RptFileName="%TEMP%\~.rpt" /D InfFileName="%TEMP%\~.inf" -f "%TEMP%\~.ddf">nul
    for /f "tokens=4,5,6,7" %%a in ('type "%TEMP%\~.rpt"') do (
        if not defined current_date ( 
            set "current_date=%%d-%%a-%%b"
            set "current_time=%%c"   
        set "current_year=%%d"
        set "current_month=%%a"
        set "current_day=%%b"
        )
    )
    

    In the source code just include BuildDate.inc file and use consts BUILD_*.

提交回复
热议问题