Tip: Creating Code Snippets for Custom Build Tasks
As I mentioned on my other blog, I am currently working on some Team System courseware. While developing code snippets for the Team Foundation Build content, I encountered an interesting problem. Here is the original code I came up with for the code snippet:
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Custom Build Code Snippet</Title>
</Header>
<Snippet>
<Code Language="XML">
<![CDATA[
<UsingTask TaskName="BuildTask.CustomTask" AssemblyFile="C:\BuildTask.dll" />
<Target Name="BeforeDropBuild">
<BinSize SourceDir="$(SolutionRoot)" />
</Target>
</Project>
]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
When I tried importing the code snippet (Tools > Code Snippet Manager > Import) and then use it within the TfsBuild file, I got the following error:
The source of the error is the dollar sign. The code snippet framework within Visual Studio 2005 recognizes dollar signs ($) as variable identifiers. My code snippet requires a dollar sign as a means of identifying a path in Team Foundation Version Control.
To alleviate the problem, I defined a variable called "dollarsign" and assigned it the value of "$". The following updated snippet code fixes the problem:
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Custom Build Code Snippet</Title>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>dollarsign</ID>
<ToolTip>Dollar Sign</ToolTip>
<Default>$</Default>
</Literal>
</Declarations>
<Code Language="XML">
<![CDATA[
<UsingTask TaskName="BuildTask.CustomTask" AssemblyFile="C:\BuildTask.dll" />
<Target Name="BeforeDropBuild">
<BinSize SourceDir="$dollarsign$(SolutionRoot)" />
</Target>
</Project>
]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>