How can I inject a XAML transformation into my build?

纵然是瞬间 提交于 2019-12-23 20:20:56

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/4921629/how-can-i-inject-a-xaml-transformation-into-my-build

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