deleting Ant dirset using regex - ant

I am trying to delete a set of directories using a regex expression, but not having much luck at all, can anyone take a look at the target please:
<delete failonerror="true" includeemptydirs="true">
<dirset dir="C:\Users\martin\Desktop\testing" includes="*">
<containsregexp expression="[0-9]*-0" />
</dirset>
</delete>
It just won't delete the files, I want it to match the folder [0-9]*-0 and if it matches it, to delete it

The delete task will delete an individual directory but not ones from a dirset.
I have the following directory structure:
C:\dev\debug_investigations\purge_deployment\test\012345-0
C:\dev\debug_investigations\purge_deployment\test\012345-1
C:\dev\debug_investigations\purge_deployment\test\987565-0
C:\dev\debug_investigations\purge_deployment\test\012345-0\sometext.txt
C:\dev\debug_investigations\purge_deployment\test\012345-0\subdir1
C:\dev\debug_investigations\purge_deployment\test\012345-0\subdir2
C:\dev\debug_investigations\purge_deployment\test\012345-0\subdir1\sometext.txt
C:\dev\debug_investigations\purge_deployment\test\012345-0\subdir2\sometext.txt
C:\dev\debug_investigations\purge_deployment\test\012345-1\sometext.txt
C:\dev\debug_investigations\purge_deployment\test\012345-1\subdir1
C:\dev\debug_investigations\purge_deployment\test\012345-1\subdir2
C:\dev\debug_investigations\purge_deployment\test\012345-1\subdir1\sometext.txt
C:\dev\debug_investigations\purge_deployment\test\012345-1\subdir2\sometext.txt
C:\dev\debug_investigations\purge_deployment\test\987565-0\sometext.txt
C:\dev\debug_investigations\purge_deployment\test\987565-0\subdir1
C:\dev\debug_investigations\purge_deployment\test\987565-0\subdir2
C:\dev\debug_investigations\purge_deployment\test\987565-0\subdir1\sometext.txt
C:\dev\debug_investigations\purge_deployment\test\987565-0\subdir2\sometext.txt
I created this ant target (ant 1.8.2)
<target name="regexptest">
<fileset dir="C:\dev\debug_investigations\purge_deployment\test\" includes="**/**" id="regexp.todelete">
<filename regex="[0-9]*-0" />
</fileset>
<!-- Which files are selected? -->
<pathconvert pathsep="${line.separator}" property="prop.regexp.test" refid="regexp.todelete" />
<echo message="Will delete:" />
<echo message="${prop.regexp.test}" />
<delete failonerror="true" includeemptydirs="true" verbose="true">
<fileset refid="regexp.todelete" />
</delete>
</target>
The directory structure after the task had run:
C:\dev\debug_investigations\purge_deployment\test\012345-1
C:\dev\debug_investigations\purge_deployment\test\012345-1\sometext.txt
C:\dev\debug_investigations\purge_deployment\test\012345-1\subdir1
C:\dev\debug_investigations\purge_deployment\test\012345-1\subdir2
C:\dev\debug_investigations\purge_deployment\test\012345-1\subdir1\sometext.txt
C:\dev\debug_investigations\purge_deployment\test\012345-1\subdir2\sometext.txt
It's more verbose than it needs to be for production, but I hope this helps future regex directory deleters!

Related

Ant task: add a file to an archive with a parameter

everyone!
I'm currently struggling with an Ant task for a DITA plugin. It would consist in adding a file (whatever the extension is) to an archive via the command line. The idea is to set a parameter like this :
dita -f mymap.ditamap -transtype htmlCustom -Dadd-file=fileOfMyChoice.txt
The part that interests me is the last one: -Dadd-file=fileOfMyChoice.txt
So far, I've tried to include two choices into my target.
The user sets a add-file parameter and its value is added into the fileset with the base set of generated files.
Or, nothing is passed via this parameter and the base set of generated files is included in the fileset.
The part that is still challenging to me if the one that identifies if a parameter add-file is passed and how its value is transmitted in a fileset.
If you have any pointers for me, that would be awesome.
<target name="map2exportmap">
<dirname property="dita.temp.dir.fullpath" file="${dita.temp.dir}${file.separator}dummy.file"/>
<xslt in="${dita.temp.dir.fullpath}${file.separator}.job.xml"
out="${dita.temp.dir.fullpath}${file.separator}allResources.out"
style="${dita.plugin.com.myplugin.dir}/xsl/generateAllResources.xsl"
force="true"/>
<mkdir dir="${dita.map.output.dir}/temp"/>
<fileset id="global" dir="${user.input.dir}/" includesfile="${dita.temp.dir.fullpath}${file.separator}allResources.out" />
<union id="globalAddendum">
<fileset id="ditaGeneration" dir="${user.input.dir}/" includesfile="${dita.temp.dir.fullpath}${file.separator}allResources.out"/>
<fileset id="additionalFile" dir="${user.input.dir}/" includesfile="${add-File}"/>
</union>
<condition property="withAddedFile" refid="globalAddendum" else="global">
<isset property="add-file"/>
</condition>
<copy todir="${dita.map.output.dir}/temp">
<fileset dir="${user.input.dir}/" includesfile="${withAddedFile}"/>
</copy>
<zip destfile="${dita.map.output.dir}/${dita.map.filename.root}.zip">
<fileset dir="${dita.map.output.dir}/temp"/>
</zip>
<delete dir="${dita.map.output.dir}/temp"/>
</target>
everyone!
I found the solution!
Here is the code if it can help anyone.
<target name="map2exportmap">
<dirname property="dita.temp.dir.fullpath" file="${dita.temp.dir}${file.separator}dummy.file"/>
<xslt in="${dita.temp.dir.fullpath}${file.separator}.job.xml"
out="${dita.temp.dir.fullpath}${file.separator}allResources.out"
style="${dita.plugin.com.xxxxx.xxxxxxx.dir}/xsl/generateAllResources.xsl"
force="true"/>
<mkdir dir="${dita.map.output.dir}/temp"/>
<!-- Create two filesets depending on the argument `add-file` sent in the command line -->
<fileset id="base" dir="${user.input.dir}/" includesfile="${dita.temp.dir.fullpath}${file.separator}allResources.out" />
<fileset id="addedFile" file="${add-file}"/>
<!-- this is the core of the condition. It tests if there was an argument for `add-file, then ii sets the property `with.or.without.added.file`-->
<condition property="with.or.without.added.file" value="addedFile" else="base">
<isset property="add-file"/>
</condition>
<echo>${with.or.without.added.file}</echo>
<echo>${add-file}</echo>
<!-- Process the copy of content in the tempoerary folder-->
<copy todir="${dita.map.output.dir}/temp">
<fileset refid="${with.or.without.added.file}"/>
<fileset id="base" dir="${user.input.dir}/" includesfile="${dita.temp.dir.fullpath}${file.separator}allResources.out" />
</copy>
<!-- Execute the zip packaging -->
<zip destfile="${dita.map.output.dir}/${dita.map.filename.root}.zip">
<fileset dir="${dita.map.output.dir}/temp"/>
</zip>
<delete dir="${dita.map.output.dir}/temp"/>

Exclude files in ant-migration tool while deploying

I am new to salesforce. We are using ant-migration tool. There are a few classes/dashboards/triggers that we are trying to exclude using file sets. All of the below folders are inside src.
<property file="build.properties"/>
<property name="src.dir" value="../src"/>
<fileset dir="${src.dir}" casesensitive="yes">
<echo message="Inside file set"/>
<exclude name="**/classes/Abs*.cls"/>
</fileset>
<target name="deploy">
<sf:deploy
username="${sf.username}.${org}"
password="${sf.password}${sf.securitytoken}"
serverurl="${sf.serverurl}"
checkOnly="${checkOnly}"
maxPoll="${maxPoll}"
deployRoot="${src.dir}"
allowMissingFiles="${allowMissingFiles}"
ignoreWarnings="${ignoreWarnings}"
testLevel="${testLevel}" />
</target>
It looks like I am unable to exclude the same.
Never used filesets, sorry.
My Ant pulls project's structure from Git to temp directory so in the build.xml we just delete stuff which we know is pain to deploy. We still want these files in the repo for ease of use / repo completeness.
<target name="deploy_target">
...
<delete file="${src.dir}/workflows/Reply.workflow" />
<delete file="${src.dir}/workflows/Question.workflow" />
<delete file="${src.dir}/layouts/SocialPost-Social Post Layout.layout" />
<delete file="${src.dir}/layouts/CommunityMemberLayout-Community Member Layout.layout" />
</target>

Ant to delete subdirectories and files in subdirectories but not any file from the directory itself

My requirement is to delete all subdirectories from a specified directory, but NOT delete files in the specified directory.
I have fussed around with fileset and dirset and I could not get a single collection to do the job. What works is this:
<delete includeemptydirs="true" verbose="true" >
<fileset dir="release/reports" >
<exclude name="*.*" />
</fileset>
<dirset dir="release/reports" includes="**/*" />
</delete>
Isn't there a way to do this with one collection (either fileset or dirset)?
It should work like that :
<delete includeEmptyDirs="true">
<fileset dir="C:/yourdir" includes="**/*" excludes="*.*"/>
</delete>

Ant: How to select the latest modified file from a directory?

Assume I have a directory which contains several files with the same name prefix and a timestamp, e.g.
my-directory:
- file-0749
- file-1253
- file-2304
How can I tell ANT to select the latest modified file from my directory (in this case this would be file-2304)?
You can do that with the TimestampSelector task from ant-contrib.
<timestampselector property="latest.modified">
<path>
<fileset dir="${my-directory.dir}">
<include name="file-*" />
</fileset>
</path>
</timestampselector>
<echo message="${latest.modified}" />
Found a way without an additional library:
<copy todir="${tmp.last.modified.dir}">
<last id="last.modified">
<sort>
<date />
<fileset dir="${my.dir}" />
</sort>
</last>
</copy>
<echo message="last modified file in ${my.dir}: ${ant.refid:last.modified}" />
You can work directly with ant.refid:last.modified like the echo task does. Don't forget to delete tmp.last.modified.dir.

How do I "expand" an ant path (accessed with refId=..) to all files in the path except some?

I am trying to get ant4eclipse to work and I have used ant a bit, but not much above a simple scripting language. We have multiple source folders in our Eclipse projects so the example in the ant4eclipse documentation needs adapting:
Currently I have the following:
<target name="build">
<!-- resolve the eclipse output location -->
<getOutputpath property="classes.dir" workspace="${workspace}" projectName="${project.name}" />
<!-- init output location -->
<delete dir="${classes.dir}" />
<mkdir dir="${classes.dir}" />
<!-- resolve the eclipse source location -->
<getSourcepath pathId="source.path" project="." allowMultipleFolders='true'/>
<!-- read the eclipse classpath -->
<getEclipseClasspath pathId="build.classpath"
workspace="${workspace}" projectName="${project.name}" />
<!-- compile -->
<javac destdir="${classes.dir}" classpathref="build.classpath" verbose="false" encoding="iso-8859-1">
<src refid="source.path" />
</javac>
<!-- copy resources from src to bin -->
<copy todir="${classes.dir}" preservelastmodified="true">
<fileset refid="source.path">
<include name="**/*"/>
<!--
patternset refid="not.java.files"/>
-->
</fileset>
</copy>
</target>
The task runs successfully, but I cannot get the to work - it is supposed to copy all non-java files over too to emulate the behaviour of eclipse.
So, I have a pathId named source.path which contains multiple directories, which I somehow needs to massage into something the copy-task like. I have tried nesting which is not valid, and some other wild guesses.
How can I do this - thanks in advance.
You might consider using pathconvert to build a pattern that fileset includes can work with.
<pathconvert pathsep="/**/*," refid="source.path" property="my_fileset_pattern">
<filtermapper>
<replacestring from="${basedir}/" to="" />
</filtermapper>
</pathconvert>
That will populate ${my_fileset_pattern} with a string like:
1/**/*,2/**/*,3
if source.path consisted of the three directories 1, 2, and 3 under the basedir. We're using the pathsep to insert wildcards that will expand to the full set of files later.
The property can now be used to generate a fileset of all the files. Note that an extra trailing /**/* is needed to expand out the last directory in the set. Exclusion can be applied at this point.
<fileset dir="." id="my_fileset" includes="${my_fileset_pattern}/**/*">
<exclude name="**/*.java" />
</fileset>
The copy of all the non-java files then becomes:
<copy todir="${classes.dir}" preservelastmodified="true">
<fileset refid="my_fileset" />
</copy>
That will copy the source files over retaining the source directory structure under todir. If needed, the flatten attribute of the copy task can be set to instead make all the source files copy directly to todir.
Note that the pathconvert example here is for a unix fileseystem, rather than windows. If something portable is needed, then the file.separator property should be used to build up the pattern:
<property name="wildcard" value="${file.separator}**${file.separator}*" />
<pathconvert pathsep="${wildcard}," refid="source.path" property="my_fileset">
...
You could use the foreach task from the ant-contrib library:
<target name="build">
...
<!-- copy resources from src to bin -->
<foreach target="copy.resources" param="resource.dir">
<path refid="source.path"/>
</foreach>
</target>
<target name="copy.resources">
<copy todir="${classes.dir}" preservelastmodified="true">
<fileset dir="${resource.dir}" exclude="**/*.java">
</copy>
</target>
If your source.path contains file paths as well then you could the if task (also from ant-contrib) to prevent attempting to copy files for a file path, e.g.
<target name="copy.resources">
<if>
<available file="${classes.dir}" type="dir"/>
<then>
<copy todir="${classes.dir}" preservelastmodified="true">
<fileset dir="${resource.dir}" exclude="**/*.java">
</copy>
</then>
</if>
</target>

Resources