Create Text File on the Fly in Build Scripts

· January 30, 2009

Yesterday, we made a final touch to our build script (by “we” I mean “I talked about it for five minutes, Christer did it”).

We needed to create a .bat file that installs our database for a specific version and environment. The necessary information is only known at runtime in the build script.

I remembered there was an MSBuild community task for this purpose, and Christer found the task WriteLinesToFile. He then wrote the following target to create the .bat file:

<ItemGroup>
  <CmdLine Include="@ECHO OFF"/>
  <CmdLine Include="ECHO About to install version $(BuildVersion) on environment $(DeployEnv)"/>
  <CmdLine Include="SET /p passw=Enter DB password for $(DeployEnv): "/>
  <CmdLine Include="CALL main.bat $(DeployEnv) . $(BuildVersion) %passw%"/>
  <CmdLine Include="PAUSE"/>
</ItemGroup>
<WriteLinesToFile
  File="$(PublishWebSitePath)\Database\Installera_$(DeployEnv)_version_$(BuildVersion).bat"
  Lines="@(CmdLine)"
  Overwrite="true"/>

This script will prompt the user for a password so that it doesn’t need to be hard-coded into the script.

Nice job, Christer—nicely done!

Twitter, Facebook