目的是在编译前时候需要将vendor_custom.h中的内容进行修改,在编译完成后再将vendor_custom.h恢复为修改前的。
vendor_custom.h文件原内容如下
#define VENDOR_SUPPORT_PAYMENT 1
#define VENDOR_AUTHTOKEN_HMAC 1
需要修改为
#define VENDOR_SUPPORT_PAYMENT 0
#define VENDOR_AUTHTOKEN_HMAC 0
步骤 1:VS中增加pre-build/post-build命令
命令格式:call "<path>\vs_pre_build.bat"
,path为vs_pre_build.bat文件相较于.vcxproj的相对路径
步骤 2:实现vs_pre_build.bat
直接使用bat命令比较麻烦,所以在bat中调用vbs脚本实现文本替换,实现如下
echo "VS Pre-Build Event enter!"
@echo off&setlocal enabledelayedexpansion
goto:start
:modify_vendor_custom
@REM path base xx.vcxproj
set VENDOR_CUSTOM_PATH=..\..\..\..\..\..\sdk\source\common\definition
@REM backup vendor_custom.h
echo F|xcopy %VENDOR_CUSTOM_PATH%\vendor_custom.h /y %VENDOR_CUSTOM_PATH%\vendor_custom_bk.h
@REM create Replace.vbs
(echo Set FSO=Wscript.CreateObject("Scripting.FileSystemObject"^)
@REM read vendor_custom.h
echo Set READ=FSO.OpenTextFile("%VENDOR_CUSTOM_PATH%\vendor_custom.h"^)
echo FILE_STREAM=READ.ReadAll
@REM modify vendor attribute start
@REM disable support_payment
echo Str=Replace(FILE_STREAM,"#define VENDOR_SUPPORT_PAYMENT 1", "#define VENDOR_SUPPORT_PAYMENT 0"^)
@REM disable authtoken_hmac
echo Str=Replace(Str,"#define VENDOR_AUTHTOKEN_HMAC 1", "#define VENDOR_AUTHTOKEN_HMAC 0"^)
@REM modify vendor attribute end
echo READ.Close
@REM save after modification vendor_custom.h
echo Set WRITE=FSO.OpenTextFile("%VENDOR_CUSTOM_PATH%\vendor_custom.h",2,True^)
echo WRITE.Write Str
echo Wscript.Quit)>Replace.vbs
Replace.vbs
del /f /q Replace.vbs
goto :eof
:start
call:modify_vendor_custom
如果需要增加修改,直接在如下行后增加一行即可:
echo Str=Replace(Str,"#define VENDOR_AUTHTOKEN_HMAC 1", "#define VENDOR_AUTHTOKEN_HMAC 0"^)
增加参考如下:
echo Str=Replace(Str,"old line", "new line"^)
步骤 3:实现vs_post_build.bat
比较简单,直接删除修改后的vendor_custom.h,将vendor_custom_bk.h重命名为vendor_custom.h
echo "VS Post-Build Event enter!"
@echo off&setlocal enabledelayedexpansion
goto:start
:recovery_vendor_custom
@REM path base xx.vcxproj
set VENDOR_CUSTOM_PATH=..\..\..\..\..\..\sdk\source\common\definition
@REM remove after modification vendor_custom.h
del /f /q %VENDOR_CUSTOM_PATH%\vendor_custom.h
@REM recovery vendor_custom.h
move %VENDOR_CUSTOM_PATH%\vendor_custom_bk.h %VENDOR_CUSTOM_PATH%\vendor_custom.h
goto:eof
:start
call:recovery_vendor_custom
PS: 在后续使用过程中出现了VBS的权限报错,pre_build修改成纯bat实现
echo "VS Pre-Build Event enter!"
@echo off&setlocal enabledelayedexpansion
goto:start
:modify_vendor_custom
@REM path base xx.vcxproj
set VENDOR_CUSTOM_PATH=..\definition
@REM backup def_custom.h
echo F|xcopy %VENDOR_CUSTOM_PATH%\df_custom.h /y %VENDOR_CUSTOM_PATH%\vendor_custom_bk.h
@REM delete original vendor_custom.h
del /f /q %VENDOR_CUSTOM_PATH%\vendor_custom.h
@REM do Replace
for /f "delims=" %%a in ('findstr /n .* %VENDOR_CUSTOM_PATH%\vendor_custom_bk.h') do (
set "str=%%a"
set "str=!str:VENDOR_SUPPORT_PAYMENT 1=VENDOR_SUPPORT_PAYMENT 0!"
set "str=!str:VENDOR_AUTHTOKEN_HMAC 1=VENDOR_AUTHTOKEN_HMAC 0!"
if "!str:*:=!"=="" (
@REM 如果当前行是空行
call echo. >> %VENDOR_CUSTOM_PATH%\vendor_custom.h
) else (
@REM 非空行截取:后的内容
echo !str:*:=! >> %VENDOR_CUSTOM_PATH%\vendor_custom.h
)
)
goto :eof
:start
call:modify_vendor_custom