Wednesday, October 15, 2008

nant tips and tricks - part #4 Nant Expressions and the rest

After working through three good sized projects with nant - there are some things that should have been more clear, but weren't for me. So heres some experience, hoping it saves you time.

Expressions
I got off to the wrong foot thinking ${} was part of how you referenced a property itself. This made it confusing when trying to reference properties within an expression. Cluu Phone!!! ${ } is about an expression, not just a property. Evaluating ${propertyname} is just an expression that just looks up the name's value, nothing else.

So when looking at the following
logdir in the first line does not have ${} around it because its inside an existing expression (which contains a function which is referning the name logdir...). Seeing the name without quotes, it is automatically assuming the text is the name of a property. If you were trying to put a string in there instead of an existing property, the string would be in quotes ' '.
In the second line, the expression is only evaluating the value of the property named 'logdir' so its just ${logdir}.

You'd be amazed how many tasks you can write before you recognize this :) However, when you start getting into functions to manipulate things.. you will fall down quickly with errors if you don't reference names or strings properly. Here is an example of a property definition that uses multiple nested functions, property references and strings.
This disaster is actually.. getting the current directory, then getting the parent of that directory, twice (to move up two levels), then using directory combine to append the string 'trendData' to that result. Note, only the string trendData has any quotes or other extra material.

Avoiding extra warning messages when moving, deleting, creating files
Well this one is just simple best practice coding. Ask before you do it. Use the directory:: and file:: functions to check for the existance of things before you go and try to delete or create new ones. Example:
Here's another example that includes using strings properly inside a function
A simple check before you delete...
Rename a group of files
Filesets/include sets can be a bit confusing. I had a task where I needed to rename all the files in a directory with a prefix. Here's the example

No comments: