gap
gap

Reputation: 2796

How to perform custom build step for VS2012 C# Project?

We have a VS2012 solution with a dozen or so C# projects in it. One of the C# projects makes use of a text file, which is converted to SQL by a separate C# tool we've written. We want to automatically invoke this TXT-2-SQL file conversion during a build whenever the TXT file has changed (Release and Debug build).

Furthermore, we'd like the Debug build to cause the SQL to be loaded into the local DB; ie, invoke one of our existing batch files, populate_db.bat, with the SQL filename as an argument (this just wraps the invocation of the mysql client and causes the SQL to be executed).

What's the best way to do this?

Upvotes: 3

Views: 2082

Answers (1)

gap
gap

Reputation: 2796

From my own research, I've found the following choices:

  1. Custom MSBuild Task - create a separate assembly containing a class implementing ITask; reference it from our csproj; let that task do the work just described.
  2. Custom Build Tool (Single File Generators) - create a separate assembly containing a class implementing IVsSingleFileGenerator; register that on each dev machine; use that tool name in the 'Custom Build Tool' item in the properties of the aforementioned TXT file in our existing csproj
  3. C++ Makefile project - use CustomBuild or CustomBuildTool statements in an otherwise empty C++ Makefile project; Include TXT file of interest in that other project file; Add that project to solution.
  4. Target + Execs - Add custom target nodes to our existing csproj:

    <Target Name="TXT2DB" AfterTargets="Build" Condition=" '$(Configuration)' == 'Debug' " Inputs="MyTextFile.txt"
    Outputs="MySqlFile.sql" Label="TXT2DB" > <Exec Command="$(ProjectDir)\Tools\txt2sql\txt2sql.exe MyTextFile.txt MySqlFile.sql" /> <Exec Command="populate_db.bat MySqlFile.sql" /> </Target>

  5. Post Build Events - use post build events. Mentioned for completeness, but discarded b/c it happens in both debug and release, and doesn't seem to be sensitive to a TXT file change.

Currently, we've selected #4 as the easiest to impl and maintain solution, and satisfies all the requirements.

Upvotes: 6

Related Questions