Use a phing ForEach loop to execute tasks - phing

I want to execute an arbitrary selection of tasks in a Phing build.
I'm passing in a list of modules for building. Each module is of a particular type. The type is specified in the name, as {type}_{unitname}. I started with a build file that took a single module name and built it, that works fine. I now want to pass in a list of modules and have it build all of them. (What I'd really like to do is load the list of modules from an XML manifest file, but perhaps one thing at a time).
I've tried multiple ways and have found a problem with each.
I seem to need an ad-hoc task to derive my properties (task and related directory settings) from the module name. This seems to cause problems, but not at first.
At first I tried to use the loop variable as the target
<foreach list="${mylist}" param="item" target="${item"} />
but it doesn't seem to allow a variable as a target name. So I split it up into two tasks.
<foreach list="${parts}" param="dTarg" target="doIt" >
<task name="DoIt">
<phingcall target="build">
<property name="extension" value="${dTarg}" />
</phingcall -->
</task>
My problem here is (I think) "extension" is a constant and thus can't be overwritten. I tried using "var", which the docs say is a thing, but my setup complains it doesn't exist. Is it a 3.0 feature? I'm running 2.17.
So I tried changing the "phingcall" to "phing" and put my main functionality in a separate file. Here I run into problems with the ad-hoc task again. If I put it in the "subordinate" file, it complains that's it's re-declared (I think, the message isn't very helpful) when the file is called a second time. If I leave it in the main file, the subordinate file can't find it, even with inheritrefs and inheritall set.
How can I execute tasks whose names are in list?

At first I tried to use the loop variable as the target
<foreach list="${mylist}" param="item" target="${item"} />
but it doesn't seem to allow a variable as a target name
The target attribute of the foreach task is able to use variables as a value, but at this point param="item" is not yet available but in the target it is.
So I split it up into two tasks.
<foreach list="${parts}" param="dTarg" target="doIt" >
<task name="DoIt">
<phingcall target="build">
<property name="extension" value="${dTarg}" />
</phingcall>
</task>
Here you try to use a task task which is simply not a valid target.
What you want to do instead is to have targets to iterate over. The following example demonstrates the usage:
Input build.xml
<?xml version="1.0" encoding="utf-8" ?>
<project name="test" default="main">
<property name="mylist" value="A,B,C" />
<target name="main">
<foreach list="${mylist}" param="item" target="DoIt"/>
</target>
<target name="DoIt">
<echo>${item}</echo>
</target>
</project>
Output
test > main:
test > DoIt:
[echo] A
test > DoIt:
[echo] B
test > DoIt:
[echo] C
BUILD FINISHED
Complex Example (with property override and inheritAll)
<?xml version="1.0" encoding="utf-8" ?>
<project name="test" default="main">
<property name="mylist" value="A,B,C" />
<target name="main">
<foreach list="${mylist}" param="item" target="DoIt"/>
</target>
<target name="DoIt">
<phingcall target="${item}" inheritAll="true">
<property name="extension" override="true" value="${item}-ext" />
</phingcall>
</target>
<target name="A">
<echo>Inside target ${item} with ${extension} extension</echo>
</target>
<target name="B">
<echo>Inside target ${item} with ${extension} extension</echo>
</target>
<target name="C">
<echo>Inside target ${item} with ${extension} extension</echo>
</target>
</project>
Output
test > main:
test > DoIt:
test > A:
[echo] Inside target A with A-ext extension
test > DoIt:
test > B:
[echo] Inside target B with B-ext extension
test > DoIt:
test > C:
[echo] Inside target C with C-ext extension
BUILD FINISHED
Example execute as one task with changed values from the list
<?xml version="1.0" encoding="utf-8" ?>
<project name="test" default="main">
<property name="mylist" value="A,B,C" />
<target name="main">
<foreach list="${mylist}" param="item" target="DoIt"/>
</target>
<target name="DoIt">
<phingcall target="build">
<property name="extension" override="true" value="${item}-ext" />
</phingcall>
</target>
<target name="build">
<echo>Inside target build with ${extension}</echo>
</target>
</project>
Output
test > main:
test > DoIt:
test > build:
[echo] Inside target build with A-ext
test > DoIt:
test > build:
[echo] Inside target build with B-ext
test > DoIt:
test > build:
[echo] Inside target build with C-ext
BUILD FINISHED
Simplified and final build
<?xml version="1.0" encoding="utf-8" ?>
<project name="test" default="main">
<property name="mylist" value="A,B,C" />
<target name="main">
<foreach list="${mylist}" param="item" target="build">
<property name="extension" override="true" value="${item}-ext" />
</foreach>
</target>
<target name="build">
<echo>Inside target build with ${extension}</echo>
</target>
</project>
Output
test > main:
test > build:
[echo] Inside target build with A-ext
test > build:
[echo] Inside target build with B-ext
test > build:
[echo] Inside target build with C-ext
BUILD FINISHED

Related

Phing update version number in XML manifest

I need to add the ability to a phing build to:
Parse an existing xml file within the project area to get an existing build number (in format 1.2.3)
Ask the user what type of 'change' this is (i.e. major, minor, fix)
Based on the response of the user at the time of run, upgrade the respective digit from the build number (if major increase 1 by 1; if minor increase 2 by 1; if fix increase 3 by 1)
Store the build number back into the original xml file
Have the new build number available for use when naming a zip file (later in the build).
Wondering if anyone already has a phing build file that does something like this or if you happen to know what phing tasks might help with these steps?
As a starting point you could do it without overhead using the version task (it uses a property file to store the version information) or with some more effort from a xml file.
The following example build script (documentation links can be found in the description attributes) contains both ways.
<?xml version="1.0" encoding="UTF-8" ?>
<project name="version test"
default="help"
phingVersion="3.0"
description="https://stackoverflow.com/questions/68584221/phing-update-version-number-in-xml-manifest"
>
<target name="help" description="usage help">
<echo>Usage:</echo>
<echo>bin/phing xml-file-based-workflow</echo>
<echo>bin/phing property-file-based-workflow</echo>
</target>
<target name="xml-file-based-workflow"
description="version handling with xml file"
depends="user-input,handle-xml-version,use-version"
/>
<target name="property-file-based-workflow"
description="version handling with property file"
depends="user-input,handle-property-version,use-version"
/>
<target name="user-input"
description="https://www.phing.info/guide/hlhtml/#InputTask"
hidden="true"
>
<input message="what is your release type?" propertyName="release.type" defaultValue="Bugfix"/>
</target>
<target name="handle-property-version"
description="https://www.phing.info/guide/hlhtml/#VersionTask"
hidden="true"
>
<version releasetype="${release.type}" file="VERSION.txt" property="version.number"/>
</target>
<target name="handle-xml-version"
description="
https://www.phing.info/guide/hlhtml/#XmlPropertyTask
https://www.phing.info/guide/hlhtml/#EchoPropertiesTask
https://www.phing.info/guide/hlhtml/#VersionTask
https://www.phing.info/guide/hlhtml/#DeleteTask
https://www.phing.info/guide/hlhtml/#EchoXMLTask
"
hidden="true"
>
<xmlproperty file="VERSION.xml" />
<echoproperties destfile="VERSION.txt" regex="/version\.number/"/>
<version releasetype="${release.type}" file="VERSION.txt" property="version.number"/>
<delete file="VERSION.txt"/>
<echoxml file="VERSION.xml">
<version>
<number>${version.number}</number>
</version>
</echoxml>
</target>
<target name="use-version"
description="https://www.phing.info/guide/hlhtml/#EchoTask"
hidden="true"
>
<echo message="${version.number}" />
</target>
</project>

phing toString method with fileset

Phing's documentation shows an example that should display a list all php files of a certain folder using a fileset with an id of "sourcefiles":
https://www.phing.info/guide/chunkhtml/ch04s02.html
(See 4.2.4)
So I put together a build-xml with this task:
<project default="countFile">
<property name="BUILD_DIR" value="./buildTest"/>
<target name="dist" depends="build">
<fileset id="sourcefiles" dir="${BUILD_DIR}">
<include name="./*.*" />
</fileset>
<echo>files in build dir: ${toString:sourcefiles}</echo>
<echo>build dir: ${BUILD_DIR}</echo>
<echo>OS: ${os.name}</echo>
</target>
</project>
I also tried the one-liner version from the documentation: <fileset id="sourcefiles" dir=".build" includes="**/*.txt"/>.
However, the output is nothing else but exactly that dollar-curly-brace-text as text: '${toString:sourcefiles}' instead of its interpolated result.
What would be the proper way to right it? Is this an error in the docs?
(I am not new to deployment, but new to phing.)
The include element inside your fileset has a non valid pattern. From the documentation
In patterns, one asterisk (*) maps to a part of a file/directory name within a
directory level. Two asterisks (**) may include above the "border" of the directory
separator.
Your example becomes:
<project default="countFile">
<property name="BUILD_DIR" value="./buildTest"/>
<target name="dist" depends="build">
<fileset id="sourcefiles" dir="${BUILD_DIR}">
<include name="*.*" />
</fileset>
<echo>files in build dir: ${toString:sourcefiles}</echo>
<echo>build dir: ${BUILD_DIR}</echo>
<echo>OS: ${os.name}</echo>
</target>
</project>
And will output something like:
Buildfile: C:\Users\XXX\test\build.xml
[autoloader] Loading autoloader from C:\Users\XXX\test/bootstrap.php
[php] Evaluating PHP expression: ini_set('error_reporting', -1);
[php] Evaluating PHP expression: ini_set('memory_limit', -1);
[php] Evaluating PHP expression: ini_set('date.timezone', 'UTC');
Phing Build Tests > test:
[echo] files in build dir: bootstrap.php;build.xml;phpunit-sparse.xml;phpunit.xml
BUILD FINISHED
Total time: 0.4785 seconds
Build finished at 30.11.2020 23:19 with exit code 0.
Also note that the ${toString:XXX} feature is only available for phing 3.x

NAnt ignoring property in included build file

I'm trying to make my project build file include a local build file, to allow for some customization for each developer, without having to keep exclulding the build file from version control commits etc.
But NAnt keeps ignoring the properties in my included build file, and not overwriting the properties set in the global build file.
For demo purposes this short build file behaves the same:
<project name="FooProject" default="showme" basedir="." >
<description>Foo</description>
<!-- Overwrite this property in local.build -->
<property name="database.connectionstring" overwrite="true" readonly="false" value="foo" />
<include buildfile="local.build" failonerror="true" verbose="true" />
<target name="showme" description="Show connectionstring variable">
<echo message="Connectionstring: ${database.connectionstring}" />
</target>
</project>
-and my local.build file looks like this:
<property name="database.connectionstring" value="bar" />
The expected output when running NAnt with this build file is "Connectionstring: bar", but the resulit is "Connectionstring: foo", no matter which combination of readonly and overwrite I try.
It does fail if I rename the file to something else, so NAnt is aware of the included file.
NAnt is v0.91 alpha.
Am I overlooking something or is NAnt not supposed to work like I expect?
It seems you should still wrap the contents of the included build file inside a project-element. Like so:
<project>
<property name="database.connectionstring" value="bar" />
</project>
When I did that the connectionstring was "bar".
Granted: I use Nant 0.91 final.

Publishing my asp.net mvc application via script and not Visual Studio

I dont really know much about this to be honest with you...
I have managed to download mscommunity build and I have managed to use the script below to successfully compile and build my application, however I want to get my asp.net mvc application "published" so I want the same files that you when clicking "publish" inside visual studio. My current build file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- Import the MSBuild Tasks -->
<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Release</Configuration>
<ClassLibraryOutputDirectory>c:\publish\</ClassLibraryOutputDirectory>
<ProjectDir>..\PetProject\</ProjectDir >
<ProjectTestDir>$(ProjectDir)PetProject.WebUI\</ProjectTestDir >
<ProjectFile>$(ProjectDir)PetProject.sln</ProjectFile >
<TestProjectFile>$(ProjectTestDir)PetProject.WebUI.csproj</TestProjectFile >
</PropertyGroup>
<!-- Build projects by calling the Project files generated by VS -->
<Target Name="Build">
<MSBuild Projects="$(ProjectFile)" />
<MSBuild Projects="$(TestProjectFile)" />
</Target>
</Project>
I call this in command line using:
C:\Windows\Microsoft.NET\Framework\v3.5>msbuild.exe C:\Projects\PetProject\build
\PetProject.build
Help is greatly appreciated...
NOTE: I want to avoid CI, Nant etc. because I dont really know what they are and I ideally want to get the above working as First Base, then move onto other things like CI or whatever else, I dont want to confuse myself too much...
This should give you the same result as publishing from within Visual Studio:
<Project DefaultTargets="BuildAndPublish" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets" />
<PropertyGroup>
<ProjectFile>C:\PetProject\PetProject.csproj</ProjectFile >
<OutDir>C:\PetProject\MyPublish</OutDir>
</PropertyGroup>
<Target Name="BuildAndPublish">
<MSBuild Projects="$(ProjectFile)" Targets="Package" Properties="Configuration=Release;PackageLocation=$(OutDir)\MSDeploy\Package.zip;_PackageTempDir=$(OutDir)\Temp" />
</Target>
</Project>
for your project.
Don't forget to import Microsoft.Web.Publishing.targets which contains the Package target (which I mixed up with Publish in my inital answer).
If you want to build your solution your script should look something like this:
<Project DefaultTargets="BuildAndPublish" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets" />
<PropertyGroup>
<OutDir>C:\PetProject\MyPublish\</OutDir>
</PropertyGroup>
<ItemGroup>
<Solution Include="C:\PetProject\PetProject.sln">
<Properties>
OutDir=$(OutDir);
Platform=Any CPU;
Configuration=Release;
DeployOnBuild=True;
DeployTarget=Package;
PackageLocation=$(OutDir)\MSDeploy\Package.zip;
_PackageTempDir=$(OutDir)\Temp
</Properties>
</Solution>
</ItemGroup>
<Target Name="BuildAndPublish">
<MSBuild Projects="#(Solution)" />
</Target>
</Project>
There's a blog post by Code Inside which describes basically the same approach but didn't work when I tried it in my environment.

How to setup xampp, phpunit, ant, zend framework

First post here, so take it easy please.
I'm working through the first chapter of Zend Framework 1.8 Web Application Development by Keith Pope in an effort to learn PHP and the MVC framework.
I thought I included all the paths correctly and installed PHPUnit 3.3 correctly as well as Zend 1.8 and 1.7.4 Xampp. with Ant version 1.8.2. However, I can't seem to get the first simplest build to build.
From looking at other q's and a's on this site I can tell some of the calls in the build.xml file for PHPunit aren't correct and I've tried to correct them, but now I'm getting the error that C:\xampp\htdocs\zendStoreFront/library/Zend does not exist and when I add the Zend directory in I get lots and lots of errors which is telling me that's probably not the right answer.
I understand there's a lot going on in the whole project and I'm willing to take the time to learn, but one question- On the line that says " --log-xml ${basedir}/build/logs/phpunit.xml ", is this file expected to be in this directory already, or does the build process create the file? On some attempts an error that's echo'd back say that file doesn't exist and currently there is no file in that directory.
Thanks,
hestes
cmd line output
*****************
C:\xampp\htdocs\zendStoreFront\build>ant
Buildfile: C:\xampp\htdocs\zendStoreFront\build\build.xml
buildPreparation:
getProps:
[echo] ---- Build Properties ----
[echo]
[echo] OS is Windows XP
[echo] Basedir is C:\xampp\htdocs\zendStoreFront
[echo] Property file is C:\xampp\htdocs\zendStoreFront/build/ant.properties
[echo] Script-suffix is .bat
[echo]
[echo] ---- Storefront Properties ----
[echo]
[echo] Environment is development
configure:
[copy] Copying 1 file to C:\xampp\htdocs\zendStoreFront\application
test:
[exec] The filename, directory name, or volume label syntax is incorrect.
[exec] PHPUnit 3.3.10 by Sebastian Bergmann.
[exec]
[exec] C:\xampp\htdocs\zendStoreFront/library/Zend does not exist
BUILD FAILED
C:\xampp\htdocs\zendStoreFront\build\build.xml:28: exec returned: 1
Total time: 1 second
C:\xampp\htdocs\zendStoreFront\build>cd ..
C:\xampp\htdocs\zendStoreFront>ant -version
Apache Ant(TM) version 1.8.2 compiled on December 20 2010
Build.xml File
<target name="getProps">
<property file="${basedir}/build/ant.properties" />
<condition property="script-suffix" value=".bat" else="">
<os family="windows" />
</condition>
<echo message="---- Build Properties ----" />
<echo message="" />
<echo message="OS is ${os.name}" />
<echo message="Basedir is ${basedir}" />
<echo message="Property file is ${basedir}/build/ant.properties" />
<echo message="Script-suffix is ${script-suffix}" />
<echo message="" />
<echo message="---- Storefront Properties ----" />
<echo message="" />
<echo message="Environment is ${environment}" />
</target>
<target name="test" depends="getProps">
<exec dir="${basedir}/tests" executable="phpunit${script-suffix}" failonerror="true">
<arg line="--colors --coverage-html ${basedir}/build/report
--log-xml ${basedir}/build/logs/phpunit.xml
--log-pmd ${basedir}/build/logs/phpunit.pmd.xml
--log-metrics ${basedir}/build/logs/phpunit.metrics.xml
--coverage-clover ${basedir}/build/logs/phpunit.coverage.xml
AllTests.php"/>
</exec>
</target>
<target name="configure" depends="getProps">
<copy file="${basedir}/application/application.php.dist"
tofile="${basedir}/application/application.php" overwrite="true" />
<replace file="${basedir}/application/application.php" token="#ENVIRONMENT#" value="${environment}" />
</target>
<target name="buildPreparation">
<mkdir dir="${basedir}/build/logs" />
<mkdir dir="${basedir}/build/report" />
</target>
<target name="clean">
<delete dir="${basedir}/build/logs" />
<delete dir="${basedir}/build/report" />
</target>
<target name="build" depends="buildPreparation,configure,test"/>