0

I am executing a T4 template using a pre-build event with the following script added in the Properties > Build Events > Pre-build event command line:

set textTemplatingPath="%CommonProgramFiles(x86)%\Microsoft Shared\TextTemplating\$(VisualStudioVersion)\texttransform.exe"
if %textTemplatingPath%=="\Microsoft Shared\TextTemplating\$(VisualStudioVersion)\texttransform.exe" set textTemplatingPath="%CommonProgramFiles%\Microsoft Shared\TextTemplating\$(VisualStudioVersion)\texttransform.exe"
%textTemplatingPath% "$(ProjectDir)AssemblyFileVersion.tt"

While building the solution the above script is working fine and I am able to get output from T4 template file. But I am looking to execute this script while publishing the WPF application instead of building the solution.

I have moved the script in the batch file and put a reference of that batch file inside .csproj file.

<Target Name="BeforePublish" BeforeTargets="MSDeployPublish">
    <Exec Command="$(SolutionDir)v1.bat" />
</Target>

While I am publishing the project, I am getting following error.

The command "xx\v1.bat" exited with code 9009

The file exists in the location but I am not able to figure out if there is an issue with the script or the way I am trying to achieve the result.

Edit:

When I checked the Output window, it was due to the space in one of the directories (Visual Studio 2015 in my case) included in the path. For now, I have placed v1.bat inside D: folder.

<Target Name="BeforePublish" BeforeTargets="MSDeployPublish">
    <Exec Command="D\v1.bat" />
</Target>

Now when I am trying to publish the app, it shows me the following error.

The command "D:\v1.bat" exited with code 1

and the Output window is showing "The system cannot find the path specified".

It seems like the problem is with the variables used in the script and not in the path of the v1.bat file defined in the Target above because when I am simply keeping echo StackOverflow in the batch file it is showing the text and successfully publishing the app. To me, it seems like the way I used the script in the pre-build command line in Visual Studio IDE would be quite different the way if I need to run the same script from batch file.

Thanks

Ajendra Prasad
  • 299
  • 1
  • 8
  • 22
  • Are you sure you're looking at the right folder? The .sln file is there right next to v1.bat? – Andy Apr 16 '18 at 16:33
  • Hi @Andy, yes v1.bat is there at the same level as .sln file. I have confirmed the location using the path shown in the error itself. – Ajendra Prasad Apr 16 '18 at 19:12
  • @Andy please check the Edit section in the question and let me know if you have any solution. – Ajendra Prasad Apr 16 '18 at 20:36
  • Are you using relative paths in there? – Andy Apr 16 '18 at 20:48
  • Where @Andy?? I am using absolute path for the batch file in the target inside .csproj file and it is working fine as mentioned in the updated Edit section. It seems like there are some changes needed in the script itself though it is working fine when added as Pre-build event command line in Visual Studio IDE. – Ajendra Prasad Apr 16 '18 at 21:07
  • It's the bat file I'm talking about. – Andy Apr 17 '18 at 09:17

1 Answers1

0

The following pre-build event script added in the Properties > Build Events > Pre-build event command line in Visual Studio IDE:

set textTemplatingPath="%CommonProgramFiles(x86)%\Microsoft Shared\TextTemplating\$(VisualStudioVersion)\texttransform.exe"
if %textTemplatingPath%=="\Microsoft Shared\TextTemplating\$(VisualStudioVersion)\texttransform.exe" set textTemplatingPath="%CommonProgramFiles%\Microsoft Shared\TextTemplating\$(VisualStudioVersion)\texttransform.exe"
%textTemplatingPath% "$(ProjectDir)AssemblyFileVersion.tt"

needs some changes when it is moved to a batch file which is intended to be executed while publishing the application instead of building it.

I need to change all variables from $(Variable1) to %Variable1%, so my script became

set textTemplatingPath="%CommonProgramFiles(x86)%\Microsoft Shared\TextTemplating\%VisualStudioVersion%\texttransform.exe"
if %textTemplatingPath%=="\Microsoft Shared\TextTemplating\%VisualStudioVersion%\texttransform.exe" set textTemplatingPath="%CommonProgramFiles%\Microsoft Shared\TextTemplating\%VisualStudioVersion%\texttransform.exe"
%textTemplatingPath% "%ProjectDir%AssemblyFileVersion.tt"
Ajendra Prasad
  • 299
  • 1
  • 8
  • 22