Welcome to MSDN Blogs Sign in | Join | Help

WiX: More powerful foreach

I've been meaning to post about this for a while but just never got around to it. A few months ago I extended the implementation in WiX to allow foreach loops using any variable over any given set of values. Previously it was limited to only those variables that were defined in your preprocessor extension.

While this example wont do much for you, it demonstrates what a foreach loop will do. <?foreach?> loops can simplify authoring in many ways, especially for complex products.

Example:

<?foreach number in 1;2;3 ?>
    <Property Id="Property$(var.number)" Value="value$(var.number) />
<?endforeach ?>

Will result in the following being passed to the compiler:

    <Property Id="Property1" Value="value1 />
    <Property Id="Property2" Value="value2 />
    <Property Id="Property3" Value="value3 />

I want to note as a caveat, It can be very easy to break component rules by changing the contents of a component by simply changing the values you iterate over. I recommend not using foreach loops anywhere near components unless you have really thought it through.  Also, while using the preprocessor can be very handy for minimizing the amount of wix code you need to maintain, it can really confuse some of the tools that are out there that help you generate or maintain your wix source. In general, think about other options before you use the preprocessor as a solution but if you think its your best option, enjoy :)

Published Tuesday, December 04, 2007 10:30 PM by Petermarcu
Filed under: , ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Free People Searches &raquo; WiX: More powerful foreach

Wednesday, December 05, 2007 2:56 AM by Free People Searches » WiX: More powerful foreach

# Different List Separator

Is there any way to change the separator used for the list?  I want to pass an item list from msbuild in and do a foreach over it inside the wxs file but I can't get the Candle task to work right.  For example I use (leaving out wakas, not sure if your blog handles it):

Candle DefineConstants="MyList=$(MyListItems)"

In msbuild this expands to "MyList=Item1;Item2;Item3" but then the wix task gives a command line like "candle -dMyList= -dList1 -dList2 -dList3".

I can get msbuild to use a different separator for $(MyListItems) but then I can't get the wix preprocessor to process them correctly.

After writing all of that, I guess this might be better directed at someone working on msbuild or the Wix/msbuild integration, but do you have any suggestions?

Thanks

Thursday, January 24, 2008 4:39 PM by Merit

# re: WiX: More powerful foreach

Not sure why I didnt see your question earlier. Sorry for the late response. Currently there is no way to change the delimiter. This would be a good bug to file on SourceForge and someone working on MSBuild can take a look at it. A lot of work is being done there now so its a good time to file a bug.

Tuesday, May 20, 2008 8:14 PM by Petermarcu

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker