ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gilbert Rebhan <>
Subject Re: scriptdef with nested fileset and mapper
Date Fri, 17 Oct 2008 21:48:15 GMT

Stefan wrote =
I'm trying to write a custom copy task using scriptdef & ruby
but I don't find the documentation I need. The script should
(as an example) do the same as the copy task. How do I access
the original file and the mapped file ? Like in this skeleton:

<scriptdef name="mycopy" language="ruby">

   <fileset dir="." casesensitive="false">
       <include name="MyFiles.*"/>
      <globmapper handledirsep="true" from="MyFiles.*"

i did some quick hack and came up with =

<?xml version="1.0" encoding="UTF-8"?>
<project name="project" default="main">

<target name="depends">

<scriptdef name="mycopy" language="ruby">
  <attribute name="fromdir"/>
  <attribute name="includes"/>
  <attribute name="destdir"/>
  <attribute name="namefrom"/>
  <attribute name="nameto"/>

  require 'fileutils'
  require 'find'


  FileUtils.mkdir(destdir) unless File.exists?(destdir)
  # you need Dir.glob ... for use of include patterns
  # if you just want a recursive copy you simply use
  # FileUtils.cp_r("#{fromdir}/.", "#{destdir"})
  Dir.glob("#{fromdir}/#{includes}").each do |source|
    file_name = File.basename(source)
    target_dir = File.dirname(source).sub(/#{fromdir}/,"#{destdir}")
    FileUtils.makedirs target_dir, :verbose => true
    FileUtils.cp source,target_dir +'/'+ file_name,:verbose => true

  # the rename action goes here
  regexp =
  Find.find(destdir) do |file|
   # assuming you want to rename files only
   # instead when renaming dirs also just use
   # File.rename (file, ....)
      if file =~ /#{regexp}/
       # testing with puts
       # puts "File = " + file.sub(/#{regexp}/, nameto)
       #the real work
       File.rename(file,file.sub(/#{regexp}/, nameto))


<target name="main" depends="depends">

  <mycopy fromdir="/home/gilreb/temp" includes="**/commons*"
          destdir="/home/gilreb/temp2" namefrom="commons"

Maybe this snippet is enough to get you going.

*** i used the jruby-complete-1.1.4.jar to have all
the stuff at my fingertips, maybe the smaller jruby.jar
is enough, don't know if fileutils/find are contained ***

The recursive copy works fine, the rename actions has to be
adapted to your needs, f.e. use another regexp or when
up/downcase has to be fixed you go with
File.rename(file,file.downcase)) ... etc.

If you need more help, just let me/us know and provide
more details.

Regards, Gilbert

[1] jruby-complete

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message