How to list the two most recent folders inside a directory using their timestamp - list

I have a parent folder and inside that I have a few folders. For an automation, I want to take the latest of the two folders according to timestamp.
I have tried to take the latest folder by using timstampselector.
<timestampselector property="latest.modified">
<path>
<dirset dir="MyDirectoryPath">
<include name="*" />
</dirset>
</path>
</timestampselector>
Inside my parent folder, I have the following folders:
test (Last modified on 07/04/2019 10:30 AM)
check (Last modified on 08/04/2019 05:00 PM)
integrate (Last modified on 08/04/2019 12:30 PM)
slave (Last modified on 09/04/2019 05:00 PM)
Our script should take the latest two modified folders, which is in the above case it should be integrate & slave.
How can I achieve that?

Generally speaking, it's a good idea to stay away from ant-contrib whenever possible. This particular problem can be quickly solved with native Ant's resource collections:
<last count="2" id="latest.two.files">
<sort>
<date />
<fileset dir="MyDirectoryPath" />
</sort>
</last>
Full example target:
<target name="select-latest">
<delete dir="testdir" />
<mkdir dir="testdir" />
<touch file="testdir/test" datetime="07/04/2019 10:30 AM" />
<touch file="testdir/check" datetime="08/04/2019 05:00 PM" />
<touch file="testdir/integrate" datetime="08/04/2019 12:30 PM" />
<touch file="testdir/slave" datetime="09/04/2019 05:00 PM" />
<last count="2" id="latest.two.files">
<sort>
<date />
<fileset dir="testdir" />
</sort>
</last>
<echo message="${toString:latest.two.files}" />
</target>

The task you are using is part of Ant-Contrib rather than core Ant. The documentation says you can use the count attribute to say how many items you want to select. In your case, set it to two:
<timestampselector property="latest.modified" count="2">
<path>
<dirset dir="MyDirectoryPath">
<include name="*" />
</dirset>
</path>
</timestampselector>
This appeared to work fine for me: the property was set to a comma-separated list of two directories.

Related

How do i pull in 2 materials into a stage using GoCD?

I have 2 types of materials:
A static git repo
Artifacts from an upstream pipeline
Down stream i have an AnalysisBuilders that requires both to function. When the task executes, i only seem to have access to the git repository and not the 'web' artifacts.
xml:
<pipeline name="FishAnalysis">
<materials>
<git url="https://fish:XXXXXXX#redacted.com/fish/analysis.git" />
</materials>
<stage name="CommitHandler" cleanWorkingDir="true">
<jobs>
<job name="builder">
<tasks>
<exec command="yarn" workingdir="web">
<arg>install</arg>
<runif status="passed" />
</exec>
<exec command="npm" workingdir="web">
<arg>run</arg>
<arg>build</arg>
</exec>
</tasks>
<artifacts>
<artifact src="web/dist" dest="web" />
<artifact src="web/package.json" dest="web" />
<artifact src="web/node_modules" dest="web" />
<artifact src="web/nginx.conf" dest="web" />
</artifacts>
</job>
</jobs>
</stage>
</pipeline>
.....
<pipeline name="AnalysisBuilders">
<materials>
<pipeline pipelineName="FishAnalysis" stageName="CommitHandler" materialName="FishAnalysis" />
<git url="https://fish:XXXXX#redacted.com/fish/docker.git" dest="docker" materialName="Docker">
</git>
</materials>
<stage name="Builders">
<jobs>
<job name="shellScripts">
<tasks>
<exec command="ls">
<arg>-R</arg>
<arg>.</arg>
<runif status="passed" />
</exec>
</tasks>
</job>
</jobs>
</stage>
</pipeline>
I would expect the ls -R output to have a 'web' & 'docker' folder. It does not. It only has the contents of the docker repo. How do i make both materials available?
Artifacts are not automatically propagated to downstream pipelines. You need to add a fetch artifact task, as shown here:
<tasks>
<fetchartifact pipeline="FishAnalysis" stage="CommitHandler" job="builder" srcdir="web" dest="web">
<runif status="passed" />
</fetchartifact>
<exec command="ls">
...
</exec>
</tasks>
This is because artifacts can be published in multiple upstream jobs and each upstream job can publish different artifacts. Notice that in your upstream material definition in AnalysisBuilders pipeline, you didn't specify a job.
GoCD will ensure that the version of the artifact is correct. That is, it corresponds to the upstream pipeline instance that caused this pipeline to run. Even if you re-run the pipeline sometime later.

Rename a file before Copy Task in ant build

I am new to ant build files.
Currently I get a list of files for build as:
a.cls
b.cls
c.cls
but in my local I have to run build on files, in the same directory:
a-meta.cls
b-meta.cls
c-meta.cls
Here meta keyword stays consistent. And I am using the following build.xml file. I am not sure how can I rename filename before actually copying them. I tried replace, mapper and other antlib tasks. But not helpful.
<project name="test" default="compile">
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="lib/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
<loadfile property="file" srcfile="filesToMove.txt"/> <!-- these are the list of files, i mentioned earlier -->
<target name="compile">
<echo>${file}</echo> <!-- here i have to rename file name to include -meta -->
<copy file="./classes/${file}" tofile="./src/classes/${file}" overwrite="true"/>
</target>
</project>
How to rename the files before moving them.
The solution to it was replacing the .cls to find only the name and then append the -meta.html. As follows (some portion is changed compared to previous version in the question)
<project name="test" default="compile">
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="lib/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
<loadfile property="file" srcfile="filesToMove.txt"/> <!-- these are the list of files, i mentioned earlier -->
<target name="compile">
<echo>${file}</echo> <!-- here i have to rename file name to include -meta -->
<copy file="./classes/${file}" tofile="./src/classes/${file}" overwrite="true"/>
<for param="file">
<path>
<fileset dir="./" includes="*.cls"/>
</path>
<sequential>
<basename file="#{file}" property="#{file}" suffix=".md"/>
<echo message=" ${#{file}}"/>
<copy file="${#{file}}-meta.cls" toDir="test"/>
</sequential>
</for>
</target>
</project>

MapForce - Add dayTimeDuration to dayTimeDuration

I am trying to use mapforce to generate an xslt 2.0 file. The mapping is adding 2 dayTimeDuration elements, doing so results in the following error;
No match for core.add(xs:dayTimeDuration, xs:dayTimeDuration). Check argument types.
Supported: +(xs:double, xs:double) -> xs:double
I thought that xslt 2.0 supported adding 2 dayTimeDurations. Is there a way of doing this using mapforce?
Cheers
Stew
Had almost the same problem, first tried to add functx-library but saw it creates absolute path in the generated xslt2-code, which isn't very good.
Well, turns out you can implement that function, but first you have to do some modifications...
Find your Mapforce installation directory, and MapForceLibraries -subdirectory. From that open the "core.mff", and find
<group name="math functions">
<component name="add" growable="true" growablebasename="value">
<sources>
<datapoint name="value1" type="xs:decimal"/>
<datapoint name="value2" type="xs:decimal"/>
</sources>
<targets>
<datapoint name="result" type="xs:decimal"/>
</targets>
As you can seem the "sources" and "targets" elements seems to define the in- and out data types. As it is, they have only implemented "add"-function for "xs:decimal". You can copy/paste this component, then rename it and give new in- out- data types, in your case they are both "xs:dayTimeDuration". Note that there are implementations for each supported language, but you can omit those that are not needed. Here's
what should work:
<component name="addDayTimeDuration" growable="true" growablebasename="value">
<sources>
<datapoint name="value1" type="xs:dayTimeDuration"/>
<datapoint name="value2" type="xs:dayTimeDuration"/>
</sources>
<targets>
<datapoint name="result" type="xs:dayTimeDuration"/>
</targets>
<implementations>
<implementation language="xslt">
<operator value="+"/>
</implementation>
<implementation language="xslt2">
<operator value="+"/>
</implementation>
<implementation language="builtin">
<function name="Core_Add"/>
</implementation>
</implementations>
<description>
<short>result = value1 + value2</short>
<long>Result is the dayTimeDuration value of adding value1 and value2.</long>
</description>
</component>
Your new function should now appear in the "math functions" and should be good to use.
After contacting Altova (the makers of MapForce);
While XPath 2 does offer a subtract-dayTimeDurations operation, this is not presently offered as a function inside MapForce.

Remove version number from jar in ant task

I'm trying to copy all JARs from one directory to another. During this process I want to remove version numbers at the end of the file names. (E.g. my-jar-1.2.3.jar to my-jar.jar)
I tried to wrap my head around mapper but I can't find a regexp to get that to work. I've tried this:
<copy todir="lib" flatten="true">
<mapper type="regexp" from="(.*)-[^.]*(\.jar)" to="\1\2" />
<fileset dir="my.files.dir">
<include name="**/*.jar" />
<type type="file" />
</fileset>
</copy>
here is a better version of the below answer:
<project>
<mkdir dir="lib"/>
<copy todir="lib"
verbose="true">
<fileset dir="jars/">
<include name="*.jar"/>
</fileset>
<mapper type="regexp"
from="^(.+?)-[0-9]+.*\.jar$"
to="\1.jar"/>
</copy>
</project>
to handle "That last one is a trouble maker"
This should work properly:
<copy todir="lib" flatten="true">
<mapper type="regexp" from="(.*)-[^-]*(\.jar)" to="\1\2" />
<fileset dir="my.files.dir">
<include name="**/*.jar" />
<type type="file" />
</fileset>
</copy>
THere's a mapper in the maven-ant-task-lib which does just that.
Try this...
<project>
<mkdir dir="lib"/>
<copy todir="lib"
verbose="true">
<fileset dir="jars/">
<include name="*.jar"/>
</fileset>
<mapper type="regexp"
from="^(.+?)-[0-9].*$"
to="\1.jar"/>
</copy>
</project>
In a regex mapper, the from parameter must match the entire name. I use the +? non-greedy pattern matcher. This matches the pattern of . which means any character but not greedily. Normally, this would match the entire line. However, I'm capturing up to the first time a dash followed by a number is found.
The problem happens if the jar has no version number, or it's starts with a non-numeric value. I can successfully, do these:
foo-2.3.2.jar
foo-2r1.jar
But not these:
foo-alpha.jar
foo.jar
So, I tweaked the pattern a bit:
<project>
<mkdir dir="lib"/>
<copy todir="lib"
verbose="true">
<fileset dir="jars/">
<include name="*.jar"/>
</fileset>
<mapper type="regexp"
from="^(.+?)-[0-9]*.*\.jar$"
to="\1.jar"/>
</copy>
</project>
$ ant
[mkdir] Created dir: lib
[copy] Copying 5 files to lib
[copy] Copying jars/bar-3.4.5.jar to lib/bar.jar
[copy] Copying biff-86.4.2.jar to lib/biff.jar
[copy] Copying jars/boff-2.31.2.jar to lib/boff.jar
[copy] Copying jars/foo-1.2.3.jar tolib/foo.jar
[copy] Copying jars/foo-bar-3.3.2.3.jar to lib/foo.jar
That last one is a trouble maker...
It might be worth doing this in two copies: One to take care if a version number is found, and a second to take care of jars without version numbers.

nant script doesn't display unit test details

Can someone please tell me why my build script (nant) doesn't display the unit test details in the command prompt window? I have verbose set to true, but it doesn't want to display any details about my unit tests. Here's the target:
<target name="run-unit-tests" depends="compile, move.assemblies.for.tests, rebuildDatabase">
<mkdir dir="${tests.output.dir}" />
<nunit2 haltonfailure="true" failonerror="true" verbose="true">
<formatter type="Xml" extension=".xml" outputdir="${tests.output.dir}" usefile="true" />
<test assemblyname="${test.assembly.file}" />
</nunit2>
<echo message="Unit Testing Done!" />
</target>
The command prompt window just displays this:
[mkdir] Creating directory 'C:\Projects\TestProject\build\artifacts\UnitTestOutput'.
[echo] Unit Testing Done!
build:
BUILD SUCCEEDED
Am I missing something here?
Thanks!
I found the answer. I looked at the source for CodeCampServer and saw a line
<formatter type="Plain" />
and added it to my build script so it looks like this:
<nunit2 haltonfailure="true" failonerror="true" verbose="true">
<formatter type="Xml" extension=".xml" outputdir="${tests.output.dir}" usefile="true" />
<formatter type="Plain" />
<test assemblyname="${test.assembly.file}" />
</nunit2>
and now it displays the details.
Sorry to ask the question prematurely on here, but at least it might help someone in the future if they have a similar problem.
Is there a log file in ${tests.output.dir} ? If so, what if you set usefile to false and type to "Plain"?