Skip to content

Instantly share code, notes, and snippets.

@esfand
Created March 20, 2010 12:49
Show Gist options
  • Save esfand/338651 to your computer and use it in GitHub Desktop.
Save esfand/338651 to your computer and use it in GitHub Desktop.
MSBuild Inline Task
<!-- Sample Demonstrates Inline Tasks © 2010 Sayed Ibrahim Hashimi -->
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0"
DefaultTargets="Demo"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask
TaskName="FilterList"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >
<ParameterGroup>
<ListToFilter ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
<Filter Required="true" />
<FilteredList ParameterType="Microsoft.Build.Framework.ITaskItem[]" Output="true" />
</ParameterGroup>
<Task>
<Using Namespace="System.Text.RegularExpressions" />
<Code Type="Fragment" Language="cs">
<![CDATA[
var results = (from l in ListToFilter where Regex.IsMatch(l.ItemSpec, Filter) select l).ToList();
FilteredList = results.ToArray();
]]>
</Code>
</Task>
</UsingTask>
<ItemGroup>
<Source Include="src\01.cs" />
<Source Include="src\02.cs" />
<Source Include="test\test01.cs" />
<Source Include="test\sub\test02.cs" />
<Source Include="test\sub\test03.cs" />
<Source Include="test\sub\sub2\test04.cs" />
</ItemGroup>
<Target Name="Demo">
<FilterList ListToFilter="@(Source)" Filter="test">
<Output ItemName="_filteredList" TaskParameter="FilteredList" />
</FilterList>
<Message Text="Filter: test. Results: @(_filteredList)" />
<!-- Clear the list before calling again -->
<ItemGroup>
<_filteredList Remove="@(_filteredList)" />
</ItemGroup>
<Message Text="======" />
<FilterList ListToFilter="@(Source)" Filter="sub\\">
<Output ItemName="_filteredList" TaskParameter="FilteredList" />
</FilterList>
<Message Text="Filter: .\sub. Results: @(_filteredList)" />
</Target>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment