Get Visual Studio to run a T4 Template on every build

后端 未结 22 2749
余生分开走
余生分开走 2020-11-22 08:29

How do I get a T4 template to generate its output on every build? As it is now, it only regenerates it when I make a change to the template.

I have found other ques

22条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 09:20

    Here is my solution - similar to the accepted answer. We had a problem with our source control. The target .cs files are read-only and the T4 was failing. Here is the code, that runs T4 in temp folder, compares target files, and copies it only in case of same change. It does not fix the problem with read.only files, but at least it does not occur very often:

    Transform.bat

    ECHO Transforming T4 templates
    SET CurrentDirBackup=%CD%
    CD %1
    ECHO %1
    FOR /r %%f IN (*.tt) DO call :Transform %%f
    CD %CurrentDirBackup%
    ECHO T4 templates transformed
    goto End
    
    :Transform
    set ttFile=%1
    set csFile=%1
    
    ECHO Transforming %ttFile%:
    SET csFile=%ttFile:~0,-2%cs
    For %%A in ("%ttFile%") do Set tempTT=%TEMP%\%%~nxA
    For %%A in ("%csFile%") do Set tempCS=%TEMP%\%%~nxA
    
    copy "%ttFile%" "%tempTT%
    "%COMMONPROGRAMFILES(x86)%\microsoft shared\TextTemplating\11.0\TextTransform.exe"  "%tempTT%"
    
    fc %tempCS% %csFile% > nul
    if errorlevel 1 (
     :: You can try to insert you check-out command here.
     "%COMMONPROGRAMFILES(x86)%\microsoft shared\TextTemplating\11.0\TextTransform.exe"  "%ttFile%"
    ) ELSE (
     ECHO  no change in %csFile%
    )
    
    del %tempTT%
    del %tempCS%
    goto :eof
    
    :End
    

    You can try to add your check-out command on a line (:: You can try ....)

    In your project set this as a prebuild action:

    Path-To-Transform.bat "$(ProjectDir)"
    

提交回复
热议问题