views:

277

answers:

3

Is there anyway to use compiler constants in Build Events in Visual Studio - VB.NET? (especially in Post-Build events)

Scenario

If TEST_EDITION=TRUE is defined I want to run an executable during the Post-Build event so if it's FALSE then I'll run something else.

This can be used for creating different installers for different editions.

P.S. Before someone suggests: No I don't want to use nant, msbuild or something like that

A: 

Not sure about Visual Basic's syntax, but the following trick can be used by C++: the file global_inc.bat reads as:

SET PARAMETER=TRUE

This could be input by a batch script which was called in post-build event. The C++ code used the file as follows:

#define PARAMETER const int parameter
#define SET /**/
#include "global_inc.bat"
;
#undef PARAMETER

The postbuild step looked like this:

call global_inc.bat
if "%PARAMETER%" == "TRUE" echo True

Another possibility would be for the prebuild step to generate a .vb file, as well as a configuration file used in postbuild step.

Vlad
I assume this is your workaround for this limitation, because I don't really need to compile anything. All I want to do this run an executable only when a constant is TRUE during the post-build event.
dr. evil
changed the example to be closer to the question. the answer by nobugz seems me a better solution however
Vlad
A: 

Have you tried the MsBuild PostEvents? this is an extract of a .csproj... but the same applies to vbproj files

  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
       Other similar extension points exist, see Microsoft.Common.targets.
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
  </Target>
  -->
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
    <Copy SourceFiles="$(OutputPath)$(AssemblyName).dll" DestinationFolder="$(BinariesFolder)" ContinueOnError="true" />
  </Target>

You can use it with the TaskExec Target, which allows you to run a batch file or an executable.

<Target Name="DoSomething">
    <Exec Command="D:\DoSomething.exe"/>
</Target>
Jhonny D. Cano -Leftware-
I don't want to use msbuild because it's not suitable for our current build server setup.
dr. evil
Also I'm not even quite sure if Msbuild supports this.
dr. evil
There is an ExecTask for MsBuild which allows you to run a Batch file or Executable... I've updated my answer
Jhonny D. Cano -Leftware-
+5  A: 

Yes, the $(DefineConstants) macro is available and can be tested in a build event. For example, Project + Compile, Advanced Compile Options, Custom constants = Test can be tested like this:

if /i "$(DefineConstants)" NEQ "TEST" goto skiptest
echo Setting up for test environment
:skiptest

More complicated custom constants like Test=TRUE or compound constants need to be parsed differently. Admittedly I quickly gave up trying to figure out how to use the horrid FOR command.

Hans Passant
awesome, thanks.
dr. evil