3

I'd like to run a custom EXE against my XAML Resource Dictionaries. Let's say this exe that I've got is going to strip out comments, whitespace and unused resources. The original XAML files need to be untouched, but the XAML (silverlight) and BAML (wpf) that ends up in the XAPs and DLLs needs to be transformed. It needs to work on my computer and the build server.

My question is: what's the simplest and most reliable way to run this exe?

My first thought was to have a pre-build event. But this would have to work on the original XAML file. Development would become quite painful.

By the time the post build event has been run, my resources are already compiled into the dlls.

What are my options?

Rob Fonseca-Ensor
  • 15,510
  • 44
  • 57
  • If not an expert in WPF, but can't you use XamlReader.Load()? Google that. – Ken D Feb 07 '11 at 13:18
  • I think the question is more about how to invoke the transformation code as part of MSBuild, not how to implement the transform itself. – Joe White Feb 07 '11 at 13:40

1 Answers1

2

You should implement the "exe" as an MSbuild Task.

Essentially you build a C# class that inherits from the Microsoft.Build.Utilities.Task class, and overide the Execute() method.

Something like

public class CleanXAML : Task 
{
}

Then you spefify (either in your build file, or an external .tasks file that you import, the task name and the path to the DLL you just built)

<UsingTask AssemblyFile="C:\customtasks\XamlTasks.dll"
   TaskName="Rob.CustomTasks.Xaml.CleanXaml"/>

That enables you to invoke this like any other MsBuild task

<CleanXaml Source="$(PathtoOriginalXaml)" 
   Destination="$(SourceCodePath)\$(cleanXaml.xaml)" />

From there you need to figure out the best way to "inject" this into your build process. Depending on how you are building (msbuild, vs2010, teambuild, or teambuild workflow) there are different ways to do this. Essentially you need this to happen BEFORE the CoreCompile target is invoked and make sure your "output xaml" properly replaces what CSC.exe is going to expect.

Do some searches on MsBuild tasks for more info, or ask any question you've got here.

I would highly recommend this approach vs. using a CallEXE task in MSBuild, b/c this way the MSBuild properties and items stay in context so you are just moving from build step to build step, vs sidetracking the whole thing to do the transform, then hoping it keeps working.

Taylor Bird
  • 7,767
  • 1
  • 25
  • 31
  • and how do i keep the original xaml safe while invoking corecompile on the clean xaml? – Rob Fonseca-Ensor Feb 07 '11 at 16:26
  • that would have to be a "trick" you come up with. What we do, for example, is take all the files, create two copies of them. Then do any pre-compile on one copy, run compile against that copy, output to that copy. Then create a 3rd copy from that which is "cleaned" for deployment. So in the end we come up with Original, a BuildOutput, and Deployment folder. Post-Compile, you could then move whatever you needed from original into the final output – Taylor Bird Feb 07 '11 at 16:31
  • Another option would be for you to take in the source and output paths into the MsBuild task, then have your "exe"/xamlcleaner create the two copies of the files in the right spot – Taylor Bird Feb 07 '11 at 16:32