views:

673

answers:

2

Hi folks,

i have a custom Task which i use in MSBuild. Works great. Previously, i had some properties which accepted some string data. It was suggested I should change them to ITaskItem's. This way, if i have spaces, i shouldn't have a problem.

Previous code

public class CompressorTask : Task
    {
    ....
    public string CssFiles { get; set; }
    public string JavaScriptFiles { get; set; }
}

example msbuild file (eg. MsBuildSample.xml)...

<CompressorTask
    CssFiles="StylesheetSample1.css, StylesheetSample2.css, 
              StylesheetSample3.css, StylesheetSample4.css"
    JavaScriptFiles="jquery-1.2.6-vsdoc.js"
... />

Notice how i've got 4 css files? i manually extracted them by the , or space delimeter. Kewl.

New Code

 public ITaskItem[] CssFiles { get; set; }
 public ITaskItem[] JavaScriptFiles { get; set; }

Now, i'm not sure what values i need to set, in the CssFiles property, in the MSBuild file. Can anyone help me or make any suggestions, please?

thanks :)

+5  A: 

I think you now need to put the files into an itemlist with the name of the property:

<ItemGroup>
    <CssFiles Include="StylesheetSample1.css"/>
    <CssFiles Include="StylesheetSample2.css"/>
    <CssFiles Include="StylesheetSample3.css"/>
    <CssFiles Include="StylesheetSample4.css"/>
    <!-- or <CssFiles Include="*.css" /> -->
    <JavaScriptFiles Include="jquery-1.2.6-vsdoc.js"/>
</ItemGroup>
<CompressorTask CssFiles="@(CssFiles)" JavaScriptFiles="@(JavaScriptFiles)"/>
John Saunders
what happens if the css file has a space in it?
Pure.Krome
+1. FWIW, I stick with string for ease of use if there is only ever going to be one file, and use ITaskItem[] when there may be more than one.
Si
@Pure Krome: You're right. I've fixed the includes.
John Saunders
Perfect! thanks heaps gents.
Pure.Krome
+1  A: 

This is actually way easier, just use a string array. When specifying the values, use a single value, a semi-colon separated list (with whitespace if you like), or an ItemGroup (e.g. @(someItemGroup)), MSBuild is smart enough to figure this out for you:

<CompressorTask
    CssFiles="StylesheetSample1.css; StylesheetSample2.css;
              StylesheetSample3.css; StylesheetSample4.css"
    JavaScriptFiles="jquery-1.2.6-vsdoc.js"
... />

MSBuild task source:

public string[] CssFiles { get; set; }
public string[] JavaScriptFiles { get; set; }
Wedge
i used to do that but i've now moved to ITaskItem.
Pure.Krome