NAnt IF task doesn't seem to work - nant

I'm trying the example from the NAnt documentation for the if task at:
http://nant.sourceforge.net/release/0.85/help/tasks/if.html
Specifically the following code...
<if test="${build.configuration='release'}">
<echo>Build release configuration</echo>
</if>
where build.configuration has been defined beforehand as
<property name="build.configuration" value="debug" overwrite="false" />
When I run it using nant.exe (version 0.91.3881.0), I get the following error:
'}' expected
Expression: ${build.configuration='release'}
^
I'm guessing I'm missing something simple?

You need to double the = symbol as per your web page.
When programming, = is an assignment operator in most languages, whereas == is the boolean comparison operator.

Maybe the NAnt reference should be changed then....
<if test="${build.configuration='release'}">
<echo>Build release configuration</echo>
</if>
See the manual.

Related

How to return a value from a phing target?

I would like to have a foreach task like this, which iterates over all the files/directories in a directory "A" -
<foreach param="dirname" absparam="absname" target="subtask">
<fileset dir="${dir.destination}/${dir.subdir}/">
<type type="file" />
</fileset>
</foreach>
The target "subtask" should check if the counterpart of the file/folder exists in another directory "B" (I am comparing directory A and B basically), and return either of the following if it does not -
a flag.
name of the file.
Following is some code for reference -
<target name="subtask">
<if>
<filesmatch file1="${file1}" file2="${file2}"/>
<then>
Return false. But how?
</then>
<else>
Return true of name of the file. How?
</else>
</if>
</target>
Note - It is okay if this can be done without calling a target. I am not sure if the logic can be fit inside the foreachtask itself. Could not find any such thing in the phing documentation.
Basically, I should be having the list of file names which are not present in the directory B, by the end of the loop.
You may also read this question of mine, if you can give some pointers to solve the issue in some other way.
Update
Rephrasing this question, since I feel that the problem description is not clear. The phing documentation says, a target has no return value -
Targets are collections of project components (but not other targets)
that are assigned a unique name within their project. A target
generally performs a specific task -- or calls other targets that
perform specific tasks -- and therefore a target is a bit like a
function (but a target has no return value).
I don't understand why is it designed so. With this bounty, I would like to know if there is some workaround for me other than having to define my own custom tasks in PHP, and then set properties -
$this->getProject()->setNewProperty('modifiedElements', implode("\n\n",$modifiedElementsArray));
which can be accessed in the build file
I have a target which checks whether my production code base has any differences from the expected git revision -
<target name="compare_prod_with_expected_revision">
<input propertyname="box.git_version">
Enter git version of the production codebase:
</input>
<exec command="git reset --hard ${box.git_version}" dir="${dir.scratchpad}" />
<!-- Scratchpad brought to revision ${box.git_version} -->
<echo>Verifying whether production is at revision ${box.git_version}..</echo>
<exec command="diff -arq --exclude='.git' ${dir.scratchpad}/${dir.subdir} ${dir.destination}/${dir.subdir}" outputProperty="diffList"/><!-- #TODO ignore.swp files in this step. Diff says .swp files present in production code. But doing ls -a there does not show the same. -->
<php function="strlen" returnProperty="productionDeviationFromExpectedBranch"><!-- #TODO - find how to not show this step during build process. Put it in a target and set hidden="true" -->
<param value="${diffList}"/>
</php>
<if>
<equals arg1="${productionDeviationFromExpectedBranch}" arg2="0" />
<then>
<echo>Verified production is at revision ${box.git_version}</echo>
</then>
<else>
<echo>Differences - </echo>
<echo>${diffList}</echo>
</else>
</if>
</target>
Now, I want to phingcall this target and would like to access some property set by it.
I think I understood your purposes, and at the same time I feel like you chosen not the optimal tool of doing this.
As you mentioned in your question, official documentation on phing is clear about tasks (targets):
Targets are collections of project components (but not other targets) that are assigned a unique name within their project. A target generally performs a specific task -- or calls other targets that perform specific tasks -- and therefore a target is a bit like a function (but a target has no return value).
Targets should be components of your application, which execute specific task, atomic task. It could be initialization task, configuration fetching, compilation step, assets preparation and dumping, deployment task, clean-up task, etc. There's no "output", returned by target in the standard sense, but the result of target execution is the success of execution itself: success or failure.
One should not try to put way too much of logic into such project targets, as it is not intended to do complicated calculations, do heavy logical decisions, etc. I mean, Phing can do it, such things are possible, but this setup would be bulky, unreadable, and hard to scale/re-factor.
With Phing you can easily define conditional execution and branching of logical flow, you may define the sequence of execution of tasks (dependencies) - this is what makes it laconic and elegant. Keep targets as simple as possible, split the project into small, finished logical tasks.
Based on the projects I've been working with, the biggest targets, probably, were initialization stage and configs fetching. Here's some example, to understand what it might contain, I took it from real project:
<target name="init_configuration">
<echo msg="Define initial configuration for the deployment..." />
<if>
<not>
<isset property="host" />
</not>
<then>
<property name="host" value="dev" override="true" />
<echo message="The value of hostname has been set to ${host}" />
</then>
<else>
<echo message="The value of hostname is ${host}" />
</else>
</if>
<if>
<not>
<isset property="version" />
</not>
<then>
<property name="version" value="1.0.0" override="true" />
<echo message="The value of version has been set to ${version}" />
</then>
<else>
<echo message="The value of version is ${version}" />
</else>
</if>
<property name="host_credital_file" value="config/hosts/${host}.properties" />
<property file="${host_credital_file}" />
<available file="${host_credital_file}" property="hostfilefound" value="true"/>
<fail unless="hostfilefound" message="Missing Hostfile configuration file (${host_credital_file})!" />
<echo msg="Configuration is done" />
</target>
Other targets were extremely simplistic, they are normally – 1-5 lines long, and do only small purpose, small task. This would be, probably, the best recommendation when working with Phing.
The logic which you are trying to put on shoulders of Phing is possible, but would be extremely bulky.
Consider the point: how much quicker, easier, and more readable the same thing could be done with simple bash script in your example. Or even to write small CLI utility in PHP, which will do the job elegantly and quick. After that in Phing you'll leave parametric target which will execute this "revision diff script" from CLI.
Phing is a great tool for what it is designed for, but it can't be an optimal choice for every purpose. Just do not put way to much responsibility and logic into it.
As a workaround, for more complicated things it's better to combine Phing with with something specialized: bash scripting, PHP CLI, nodeJS (+ Grunt, Gulp, etc)... and just to add calls of a Phing targets later.
This is the way I managed to have targets which behave like functions:
<target name="-console-cmd-return-property" hidden="true">
<exec command="${command}" checkreturn="${checkreturn}" logoutput="${logoutput}" outputProperty="${outputProperty}"/>
</target>
It gets invoked like this:
<phingcall target="--console-return-property">
<property name="command" value="ps auxwww"/>
<property name="checkreturn" value="true"/>
<property name="logoutput" value="false"/>
<property name="outputProperty" value="ps_output"/>
</phingcall>
Of course it works because it relies on existing exec, and it is not generic...
The target "subtask" should check if the counterpart of the file/folder exists in another directory "B" (I am comparing directory A and B basically), and return either of the following if it does not -
a flag.
name of the file.
You could compare two directories without using a foreach task like this:
<project name="Phing Build Test" default="print-missing" basedir=".">
<resolvepath propertyName="dir.a" path="path/to/dir/a"/>
<resolvepath propertyName="dir.b" path="path/to/dir/b"/>
<target name="print-missing">
<apply executable="echo" failonerror="false" returnProperty="files.found" outputProperty="missing">
<srcfile/>
<fileset id="srcfiles" dir="${dir.a}" includes="*">
<present present="srconly" targetdir="${dir.b}"/>
</fileset>
</apply>
<if>
<equals arg1="${files.found}" arg2="0"/>
<then>
<echo msg="${missing}"/>
</then>
</if>
</target>
</project>

Calabash 1.0.23 throws on xsl-formatter step: ERROR: Failed to instantiate FO provider

I'm trying to use XML Calabash 1.0.23 to run XSLT transformation and FO formatting in a single pipeline. Although the XSLT step works fine, I cannot get the xsl-formatter step to work with FOP.
Every time I run the pipeline, Calabash throws:
ERROR: pipeline.xpl:13:68:Failed to instantiate FO provider
ERROR: Underlying exception: org/apache/fop/apps/FopFactory
My call to Calabash from the command line is:
java com.xmlcalabash.drivers.Main -c cfg.xml myPipeline.xpl
And the cfg.xml configuration file being referred to in the above line is:
<cc:xproc-config xmlns:cc="http://xmlcalabash.com/ns/configuration">
<cc:fo-processor class-name="com.xmlcalabash.util.FoFOP"/>
</cc:xproc-config>
For some reason, Calabash seems to ignore the config file setting, because regardless of the value of the class-name attribute on <cc:fo-processor>, it always throws the same error message. For example, if I use com.xmlcalabash.util.FoAH, the same happens; and if put a nonsensical value, the same occurs. It's always an exception on org/apache/fop/apps/FopFactory.
Just for the sake of completeness, this is my XPL:
<declare-step name="main" version="1.0" xmlns="http://www.w3.org/ns/xproc">
<input port="parameters" kind="parameter" />
<xslt name="transformation">
<input port="source">
<document href="myMarkup.xml" />
</input>
<input port="stylesheet">
<document href="myStylesheet.xsl" />
</input>
</xslt>
<xsl-formatter href="newDoc.pdf" >
<input port="source">
<pipe step="transformation" port="result" />
</input>
</xsl-formatter>
</declare-step>
Of course, if I manually pass the generated FO from the XSLT step to FOP 1.1, it converts it to PDF with no problems. The issue only arises when trying to do the conversion within the pipeline.
I could really use some help to solve this. I'm clueless at this point.
This may seem like a very pedantic reply, but do you have fop.jar (and fop-hyph.jar, which I think FOP requires) on your classpath? They're not bundled in the XML Calabash distribution, you have to get them from Apache.

Is there an MSBuild macro for build date/time?

Suppose I have a build script with a Target section like the following:
<Target Name="AssemblyVersionMAIN" Inputs="#(AssemblyVersionFiles)" Outputs="UpdatedAssemblyVersionFiles">
<Attrib Files="%(AssemblyVersionFiles.FullPath)" Normal="true"/>
<AssemblyInfo
CodeLanguage="CS"
OutputFile="%(AssemblyVersionFiles.FullPath)"
AssemblyProduct="$(ProductName)"
AssemblyTitle="$(ProductName)"
AssemblyCompany="$(CompanyName)"
AssemblyCopyright="© $(CompanyName) 2014" <!-- THIS LINE -->
AssemblyVersion="$(Major).$(Minor).$(Build).$(Revision)"
AssemblyFileVersion="$(Major).$(Minor).$(Build).$(Revision)"
AssemblyInformationalVersion="$(Major).$(Minor).$(Build).$(Revision)">
<Output TaskParameter="OutputFile" ItemName="UpdatedAssemblyVersionFiles"/>
</AssemblyInfo>
</Target>
At the moment, the year is static and has to be changed manually. Is there a simple way of replacing "2014" with something like $(Year)? I've checked the MSBuild reference but nothing jumps out at me.
Since you are using MsBuild 4 you could also use a property function for this, like e.g.:
<PropertyGroup>
<CurrentDate>$([System.DateTime]::Now.ToString(yyyy.MM.dd))</CurrentDate>
</PropertyGroup>
Just format the recieved date as you need it. There are also plenty of other functions available, see also http://msdn.microsoft.com/en-us/library/dd633440(v=vs.100).aspx.
Since I'm using MSBuild Community Tasks and MSBuild 4, I can substitute the following:
AssemblyCopyright="© $(CompanyName) $([System.DateTime]::Now.ToString(`yyyy`))"
which seems to work.

In NAnt <exec>, how to have a conditional <arg> based on property value?

I've got an NAnt <exec> task. I want one argument presence to be conditional to some property being true.
For example, I want the -c command line argument of psExec to be conditional. It should be outputted only if ${pExec.copyprog == 'true'}.
The following does not work:
<property name="psExec.copyprog" value="false" />
...
<exec program="${psExec.path}" failonerror="false">
...
<arg line="-c" if="${psExec.copyprog}==true" />
</exec>
It yields the following error:
'false==true' is not a valid value for attribute 'if' of <arg ... />.
Cannot resolve 'false==true' to boolean value.
String was not recognized as a valid Boolean.
How can I achieve this?
Properties in NAnt are tricky since they don't have a type and simply are considered as of type string. So this would be the solution:
<exec program="${psExec.path}" failonerror="false">
<!-- ... -->
<arg line="-c" if="${bool::parse(psExec.copyprog)}" />
</exec>
Update: Mea culpa! I was wrong. if="${psExec.copyprog}" does also work. So there is some sort of property typing.
You'd need to put ==true inside {}, but you can also just skip it:
<arg line="-c" if="${psExec.copyprog}" />
Comparing a true boolean expression to true does not change the result.

How do I optionally require a command line arguement for Ant?

I'm new to ant, and I want to require a file name if something other than the default target is used, so the calling syntax would be something like this:
ant specifictarget -Dfile=myfile
I'm using the ant contrib package to give me additional functionality, so I have this:
<if>
<equals arg1="${file}" arg2="" />
<then>
<!-- fail here -->
</then>
</if>
My thinking is that if file was not specified it might be equal to the empty string. Obviously, this didn't work, and I'm not finding any examples on google or the right syntax in the manual.
So what syntax should I use?
You don't really need the contrib package. This is more conveniently done using built-in ant capabilities like if/unless and depends. See below:
<target name="check" unless="file" description="check that the file property is set" >
<fail message="set file property in the command line (e.g. -Dfile=someval)"/>
</target>
<target name="specifictarget" if="file" depends="check" description=" " >
<echo message="do something ${file}"/>
</target>
You've got the right idea. The
ant specifictarget -Dfile=myfile
sets Ant Properties from the command line. All you really need is
<property name="file" value=""/>
for your default value. That way if file is not specified, it will be equal to the empty string.
Since properties are not mutable in Ant, you can add this:
<property name="file" value="" />
This will set the property file to an empty string if it hasn't already been set on the command line. Then your equality test will work as you intended.
Alternately, you can use escape the value since ant just spits out the actual text when it can't do a property substitution.
<if>
<equals arg1="${file}" arg2="$${file}" />
<then>
<echo>BARF!</echo>
</then>
</if>