ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gilbert Rebhan <...@schillbaer.de>
Subject Re: Rename directories in Ant
Date Fri, 15 Jun 2007 20:25:27 GMT
Hi,

as there's still no post how to achieve what you
need with vanilla ant ...

Gilbert Rebhan wrote:
> Finally <script> will do the job.
> sorry for now ...

... i wrote a small ruby script for that purpose
might be used for backups too

you need available for ant (simply put
in %ANT_HOME%/lib )=

1. the BeanScriptingFramework (bsf.jar) as for
    all other scripting langueages (groovy,javascript,
    beanshell ...), get it here =
    http://jakarta.apache.org/bsf/

2. the jruby-complete-1.0.jar, as it contains all
    needed ruby libraries the script includes with
    'require ...' (like import in java)
    the normal jruby.jar will not be enough
    get it here =
    http://dist.codehaus.org/jruby/

Notice =

1. the distributed jruby-complete-1.0.jar is build
    with jdk1.5, so if you're running with 1.4, you'll
    get a "Unsupported major.minor version 49.0"
    ** i'll ask the developers to provide a version for 1.4
    in the meantime you may try to backport it with
    retroweaver, get it here =
    http://retroweaver.sourceforge.net/
    (mostly it's only 1.5 because they used StringBuilder and stuff)

2. Ant properties are referenced as $propertyname in jruby,
    if you have 'dotted' properties, i.e $my.dotted.property
    you have to use $project.getProperty('my.dotted.property')

Script, see ruby code in <script>, runs also with ruby
interpreter outside ant

first step : recursive copy of srcdir to destdir
renaming inplace might be dangerous, if error your
srcdir is corrupted
second step : replace pattern for dir/subdir names in destdir
changing filenames and dir/subdir names also possible, see
comment in script

finally you may delete your srcdir

example, sorry if formatting gets corrupted =

-------------------------------------------------

<?xml version="1.0"?>
<project name="bla" default="main">

<property name="srcdir" value="J:/test"/>
<property name="destdir" value="J:/test_"/>
<property name="torepl" value="foobar"/>
<property name="replwith" value="foobaz_"/>

<target name="depends">

   <script language="ruby">
   <![CDATA[

   require 'fileutils'
   require 'find'
   require 'pathname'

   srcdir=$srcdir
   destdir=$destdir

   torepl=$torepl
   replwith=$replwith

   # Method for renaming dirs/subdirs
   # if needed to change filenames too,
   # delete '&& File.directory?(file)' and
   # i.e. foobar.txt gets foobaz_.txt

   def rename(headOfTree, what, withWhat)
     p = Pathname.new(headOfTree)
     p.find() do |file|
       path = file.to_s
       File.rename(path, path.gsub(what,withWhat)) if
path.include?(what) && File.directory?(file)
     end
   end

   Dir.mkdir(destdir) unless File.exists?(destdir)
   Dir.entries(srcdir).each do | i |
     if i !='.' && i !='..'
       FileUtils.cp_r Dir["#{srcdir}/**"], destdir
     end
   end

   Find.find(destdir) do |file|
     rename(file,torepl,replwith)
   end

   ]]>
   </script>

</target>

<target name="main" depends="depends">
<echo>
   copied   = ${srcdir}
   to       = ${destdir}
   replaced = ${torepl}
   with     = ${replwith}
</echo>
</target>

</project>

-------------------------------------------------

result =

srcdir
J:\>dir /S/B test

J:\test\bla.txt
J:\test\foobar
J:\test\sub1
J:\test\foobar\asdf.txt
J:\test\foobar\foobar
J:\test\foobar\subsub2
J:\test\foobar\foobar\foobar
J:\test\foobar\subsub2\foobar
J:\test\foobar\subsub2\foobar.txt
J:\test\sub1\bla.txt

destdir looks like
J:\>dir /S/B test_

J:\test_\bla.txt
J:\test_\foobaz_
J:\test_\sub1
J:\test_\foobaz_\asdf.txt
J:\test_\foobaz_\foobaz_
J:\test_\foobaz_\subsub2
J:\test_\foobaz_\foobaz_\foobaz_
J:\test_\foobaz_\subsub2\foobar.txt
J:\test_\foobaz_\subsub2\foobaz_
J:\test_\sub1\bla.txt



Regards, Gilbert







---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Mime
View raw message