Search code examples
conditional-statementsnantisnullorempty

Include argument in NAnt executable call if property is not null or empty


For a build script, part of my process is to deploy files using an in-house executable. We call this with a number of parameters.

On an application I'm working on, it turns out we need to add a parameter. This isn't something that retroactively applies to anything else I've previously worked on, so the idea is that I would only include the new argument if the corresponding property is not null or empty.

Relevant NAnt Call:

<property name="deploy.NewArg" value="" />
<echo message="deploy.NewArg = ${deploy.NewArg}" />

<exec program="C:\Deploy\MyAwesomeDeployProgram.exe">
    <arg value="AppTitle=${deploy.AppTitle}" />
    <arg value="Environment=${deploy.Environment}" />
    <!-- Here's the problem argument... -->
    <arg value="MyNewProperty=${deploy.NewArg}" if="${deploy.NewArg}" />
</exec>

The reason what I have is not working, is because of the if clause on the new <arg> tag - the deploy.NewArg string doesn't convert to a boolean statement.

Question: In what way can I perform an "Is Null or Empty" check on an <arg if> parameter? As noted above, I want the MyNewProperty=... argument added if deploy.NewArg is anything but nothing or an empty string.

I checked a number of other StackOverflow questions, as well as the official NAnt arg tag documentation, but could not find how to do this.


Solution

  • It turns out, I needed to get back to the basics, and check out some of my fundamentals and functions. The way to do an 'is empty' check on a property is as below:

    <exec program="C:\Deploy\MyAwesomeDeployProgram.exe">
        <!-- Other args... -->
        <arg value="MyNewProperty=${deploy.NewArg}" if=${string::get-length(deploy.NewArg) > 0}" />
    </exec>
    

    For someone who hasn't yet done the research, if only works with a boolean. That being said, booleans can be generated in one of two ways: either an explicit true or false, or by using an expression. Expressions are always contained in ${} brackets. The use of string::get-length() should be obvious.

    Bring it all together, and you only include an argument if it's specified.