When there is an error or bug occured in our program. And we want to know which source version that this files was built. Here is one approach to solve this problem. “To the specify the exact build number to those DLL (or Exe)”
In this article, I will explain how to build the product number with Git Hash:
- Create Visual Studio Project, Set and prepare git repo, then do some commit
- Now please try to run
git rev-parse --short HEAD
to check the hash (which we will use as product number). - To applied the product number for DLL or (Exe) in Visual Studio, we need to do some changes in
AssemblyInfo.cs
- Copy AssemblyInfo.cs and rename it to
AssemblyInfo_temp.cs
- Do some changes in
AssemblyInfo_temp.cs
like this... [assembly: AssemblyVersion("1.0.*")] //This make version number as auto increment [assembly: AssemblyInformationalVersion("$GITVERSION$")] //Here we will replace $GITVERSION$ with git hash code ...
- Prepare batch script (update-version-number.bat) for using in post-build
REM Get Git hash and set to gitversion FOR /F "tokens=* USEBACKQ" %%A IN (`"%GIT_PATH%git.exe" rev-parse --short HEAD`) DO SET gitVersion=%%A ECHO %gitVersion% REM Replace text in temp file and rename it REM %1 = $(ProjectDir) POWERSHELL -COMMAND "(gc %1Properties\AssemblyInfo_temp.cs) -replace '\$GITVERSION\$', '%gitVersion%' | out-file %1Properties\AssemblyInfo.cs" ;
- Edit post-build (in Visual Studio project) like this
CALL %PATH_TO_FILE%update-version-number.bat $(ProjectDir)
- Test Build, then go check your file property, Good Luck :)