Multiple Pre-Build Events in Visual Studio?

21,852

Solution 1

Turns out the problem is Scott's example doesn't include the call command at the start of the line. This is fine as long as you don't want to execute the .bat file multiple times with different parameters.

This:

call "$(ProjectDir)copyifnewer.bat" "$(ProjectDir)connectionStrings.config.$(ConfigurationName)" "$(ProjectDir)connectionStrings.config"
call "$(ProjectDir)copyifnewer.bat" "$(ProjectDir)appSettings.config.$(ConfigurationName)" "$(ProjectDir)appSettings.config"

worked fine for me.

Solution 2

I'm using Visual Studio 2010 on Windows 7 64-bits.

I found a solution that's better to my liking, I'm chaining commands together with && ^, for example, editing my .vcxproj xml file like so:

    <PreBuildEvent>
      <Command>echo "hello 1" &amp;&amp; ^
echo "hello 2" &amp;&amp; ^
echo "hello 3"</Command>
      <Message>Performaing pre-build actions ...</Message>
    </PreBuildEvent>

I've chained three commands together. The '&&' tells the shell to stop executing if the previous command errors out.

Note, in the .xml file, one can't use && directly, so '& amp;& amp;' must be used instead. Also note the use of the ^ character, this is the cmd shell's line continuation character (like BASH's \ char).

Each new command must start at the beginning of the line. Using this approach, one can chain as many commands as one wants, but if any fail, the rest in the chain won't get executed.

Here's my actual usecase, I'm performing some environment checks with a Python script, followed by copying my pre-compiled header's debug file to another sub-project's directory:

    <PreBuildEvent>
        <Command>C:\Python27\python.exe "$(SolutionDir)check_path.py" 32 &amp;&amp; ^
copy "$(SolutionDir)h1ksim_lib\vc$(PlatformToolsetVersion).pdb" "$(IntDir)" /-Y > nul</Command>
        <Message>Performaing pre-build actions ...</Message>
    </PreBuildEvent>

The > nul usage on the copy command prevents any output showing up in the build window, so my teammates won't get freaked out when it says 0 file(s) copied.

Reference to sharing pre-compiled headers between sub-projects is here:

Sharing precompiled headers between projects in Visual Studio

Share:
21,852
Kirschstein
Author by

Kirschstein

.NET developer with a love for TDD. Event Sourcing and CQRS have destroyed my ability to work contentedly in systems that don't use them. Creator &amp; Admin of the Social Deduction game werewolv.es

Updated on August 02, 2022

Comments

  • Kirschstein
    Kirschstein almost 2 years

    I've followed a blog post by Scott Hanselman for managing configuration with PreBuild Events and have it working fine.

    I now want to split up my configuration into a couple of different files, so need to exectue the command again before the build. The problem is the PreBuild event text all gets executed as one console command. How can I split it up as several commands?