Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Pre-Build Events in Visual Studio?

Tags:

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?

like image 733
Kirschstein Avatar asked Sep 24 '09 15:09

Kirschstein


People also ask

What is build events in Visual Studio?

MSBuild is the build engine that Visual Studio uses to process your project file when it performs a build. A build event in the IDE results in an MSBuild target in the project file. You can use any MSBuild property that is available in the target in your project file (for example, $(OutDir) or $(Configuration) ) .

What is PreBuild and PostBuild?

PreBuild and PostBuild aren't only used for hiding or unhiding pages. The main difference really is that PreBuild fires before any peoplecode code event on all the rows and fields in the component such as FieldDefault and RowInit. PostBuild runs after that.

What is PostBuildEvent?

PostBuildEvent. This event executes after the build finishes.

How do I open a build event in Visual Studio?

Go to the Solution Explorer the right-click the project then select Properties then go to the Build Events tab. Now if you build the project based upon the selection of configuration name , the Web. config file will be updated.


Video Answer


2 Answers

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.

like image 170
Kirschstein Avatar answered Oct 08 '22 23:10

Kirschstein


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

like image 22
Nick Avatar answered Oct 08 '22 23:10

Nick