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">
        ???
</scriptdef>

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

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"/>

  <![CDATA[
  require 'fileutils'
  require 'find'

  fromdir=$attributes.get("fromdir")
  includes=$attributes.get("includes")
  destdir=$attributes.get("destdir")
  namefrom=$attributes.get("namefrom")
  nameto=$attributes.get("nameto")

  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
  end

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

</target>

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

  <mycopy fromdir="/home/gilreb/temp" includes="**/commons*"
          destdir="/home/gilreb/temp2" namefrom="commons"
          nameto="foobar"
  />
</target>
</project>


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
http://repository.codehaus.org/org/jruby/jruby-complete/1.1.4/jruby-complete-1.1.4.jar

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to