Multiple Pre-Build Events in Visual Studio?

混江龙づ霸主 提交于 2019-11-30 07:23:36

问题


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?


回答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.




回答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




回答3:


You should be able to put the commands on a separate line, and then each command will be executed in sequence. Alternatively, a "cheater" way to do the same thing is as follows:

  • Create a second batch file that runs the desired portion of new functionality, then create a parent batch that runs the two component batch files, and run that new batch in your pre-build event. For example: batch1.bat modifies file 1, batch2.bat modifies file 2, then batch3.bat runs both batch1.bat and batch2.bat


来源:https://stackoverflow.com/questions/1472550/multiple-pre-build-events-in-visual-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!