Building ItemGroup with TextString StartsWith

Sep 28, 2009 at 7:36 PM

I would like to know how to filter an item using the MSBuild.ExtensionPack.Framework.TextString TaskAction="StartsWith" so that I get a subset of the original items.

For example if I have a list of items: "XXX_something; YYY_something; XXX_something_else", I might want to keep just the elements that start with "XXX_". I wish to create a new ItemGroup that contains just "XXX_something;XXX_something_else".

The code snippet belows - accomplishes part of what I want - it creates an item group called "TheResult" - but it is populated with true/false from the "StartsWith" test on the TextString task. I want to get the actual value (String1) that I am passing into StartsWith:

 

 

 

<Project ToolsVersion="3.5" DefaultTargets="Default" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Import Project="C:\Program Files\MSBuild\ExtensionPack\MSBuild.ExtensionPack.tasks"/>
  <ItemGroup>
    <AnItem Include = "NET">
     <ASubItem>XXX_something</ASubItem>
    </AnItem>
    <AnItem Include = "NET">
     <ASubItem>YYY_something</ASubItem>
    </AnItem>
    <AnItem Include = "NET">
     <ASubItem>XXX_something_else</ASubItem>
    </AnItem>
  </ItemGroup>
    <Target Name="Default">
        <!-- Check whether a string starts with another string -->
        <MSBuild.ExtensionPack.Framework.TextString TaskAction="StartsWith" String1="%(AnItem.ASubItem)" String2="XXX">
            <Output ItemName="TheResult" TaskParameter="Result"/>
        </MSBuild.ExtensionPack.Framework.TextString>
        <Message Text="The Result: @(TheResult)"/>
    </Target>
</Project>

Coordinator
Sep 30, 2009 at 10:28 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Coordinator
Sep 30, 2009 at 11:07 PM

Hi, can you try the latest code. I've added a FilterItems TaskAction to the MSBuildHelper class.

 

From the sample:

 

<XXX Include="AA"/>
            <XXX Include="AAB"/>
            <XXX Include="ABA"/>
            <XXX Include="AABA"/>
            <XXX Include="BBAA"/>
            <YYY Include="AA">
                <Filter>CC</Filter>
            </YYY>
            <YYY Include="AA">
                <Filter>CHJC</Filter>
            </YYY>
            <YYY Include="BB">
                <Filter>CCDG</Filter>
            </YYY>
            <YYY Include="CC">
                <Filter>CDC</Filter>
            </YYY>
            <YYY Include="DD">
                <Filter>CCEE</Filter>
            </YYY>
        </ItemGroup>
        <!-- Filter Items based on Name -->
        <MSBuild.ExtensionPack.Framework.MsBuildHelper TaskAction="FilterItems" InputItems1="@(XXX)" RegexPattern="^AA">
            <Output TaskParameter="OutputItems" ItemName="filtered"/>
        </MSBuild.ExtensionPack.Framework.MsBuildHelper>
        <Message Text="filtered Items: %(filtered.Identity)"/>
        <!-- Filter Items based on MetaData -->
        <MSBuild.ExtensionPack.Framework.MsBuildHelper TaskAction="FilterItems" InputItems1="@(YYY)" Metadata="Filter" RegexPattern="^CC">
            <Output TaskParameter="OutputItems" ItemName="filteredbymeta"/>
        </MSBuild.ExtensionPack.Framework.MsBuildHelper>
        <Message Text="filteredbymeta Items: %(filteredbymeta.Identity)"/>

Oct 1, 2009 at 5:59 PM

Thanks. I was also able to do this with a custom task. E.g.:

 

    public class MyCustomTask : Microsoft.Build.Utilities.Task
    {
        private ITaskItem[] _references;

        [Required]
        public ITaskItem[] References
        {
            get { return _references;  }
            set { _references = value; }
        }

        private ITaskItem[] _strippedReferences;

        [Output]
        public ITaskItem[] StrippedReferences
        {
            get { return _strippedReferences; }
            set { _strippedReferences = value; }
        }

        public override bool  Execute()
        {
            Log.LogMessage(MessageImportance.Normal, "Yokely, Dokely");


            ArrayList items = new ArrayList();

            foreach (ITaskItem x in References)
            {
                if (x.GetMetadata("ASubItem").StartsWith("XXX"))
                {
                    items.Add(x);
                }
            }

            StrippedReferences = (ITaskItem[])items.ToArray(typeof(ITaskItem));

            return true;
        }
    }

Coordinator
Oct 1, 2009 at 8:09 PM

Ok, at a  quick glance i think the new FilterItems task will accomplish the same result.

Thanks for the feedback.

Mike