2

TL;DR

Can I have a Visual Studio pre-build event that skips the build of a project without a visual error?

Details

I have a project that contains a custom XML file and a Powershell script that generates a resource file from the XML during pre-build event.

My goal is to only build the project if there are changes to the XML file. I can already determine if the file has changed, but I can't inform Visual Studio to skip the build. Either I stop the script with an exit code of 0 (which lets the build continue) or any other number (which shows an ugly error in the Error List).

Can I have a pre-build script decide whether to build or skip the project?

Example

# Check to see if the current file is different from the file copied during build.
if((Test-Path $buildXmlFile) -and (Compare-Object -ReferenceObject (Get-Content $projectXmlFile) -DifferenceObject (Get-Content $buildXmlFile))){
    Write-Host "Changes found! Rebuilding!"
} else {
    Write-Host "No changes found! Skipping Build"
    # Exit 0 will cause it to still build...
    # Exit -1, Exit 1, etc. will cause a big error to show...
    # HOW DO I SKIP???
}
Jim Buck
  • 2,383
  • 23
  • 42
  • So you only want to build when an xml file changes? Not when other source files change? – Mike Zboray Mar 17 '15 at 19:03
  • Yes, but the real question is can I cause a build to skip (not fail) from a pre-build event. – Jim Buck Mar 17 '15 at 19:08
  • I don't think you can do this in prebuild script. I assume this all to just speed up builds by not rebuilding items that haven't changed. The only way I know of to accomplish this is via Target inputs and outputs. See [How to: Build Incrementally](https://msdn.microsoft.com/en-us/library/ms171483.aspx). You'd add your generation step as a target with BeforeTargets="Build" in the proj file with the input as the xml file and the output as whatever generated files. – Mike Zboray Mar 17 '15 at 19:14

1 Answers1

0

I was able to get this working, here's how:

Terminology:

  • input file - The user edited file
  • output file - The file generated from the input file
  • output directory - Usually .\bin\debug\ or .\bin\release

Steps:

  1. On build, copy the input file to the output directory (change this in the properties window of Visual Studio).
  2. Change the script to only generate if the input file is different than the previous input file (currently sitting in the output directory).
  3. Run the script as a BeforeBuild target, like so:

Since the generated file only lives in the output directory, MSBuild has nothing to compare it to. That means it will only do a full build if the input file is different than the input file that was previously copied over (remember, the copy only happens after a build, but the comparison happens before the build).

Jim Buck
  • 2,383
  • 23
  • 42