views:

478

answers:

2

Howdy friends,

Given a zipfile with an unknown directory, how can I rename or move that directory to a normalized path?

<!-- Going to fetch some stuff -->
<target name="get.remote">

    <!-- Get the zipfile -->
    <get src="http://myhost.com/package.zip"
         dest="package.zip"/>

    <!-- Unzip the file -->
    <unzip src="package.zip"
           dest="./"/>

    <!-- Now there is a package-3d28djh3 directory.  The part after package- is
         a hash and cannot be known ahead of time -->

    <!-- Remove the zipfile -->
    <delete file="package.zip"/>

    <!-- Now we need to rename "package-3d28djh3" to "package".  My best attempt
         is below, but it just moves package-3d28djh3 into package instead of
         renaming the directory. -->

    <!-- Make a new home for the contents. -->
    <mkdir dir="package" />

    <!-- Move the contents -->
    <move todir="package/">
      <fileset dir=".">
        <include name="package-*/*"/>
      </fileset>
    </move>

</target>

I'm not much of an ant user, any insight would be helpful.

Thanks much, -Matt

+1  A: 

This will only work as long as the dirset only returns 1 item.

<project name="Test rename" basedir=".">

  <target name="rename">
    <path id="package_name">
      <dirset dir=".">
        <include name="package-*"/>
      </dirset>
    </path>
    <property name="pkg-name" refid="package_name" />
    <echo message="renaming ${pkg-name} to package" />
    <move file="${pkg-name}" tofile="package" />
  </target>

</project>
mamboking
Holy crap mamboking that worked. I have *no idea* what the logical flow is supposed to be there...we make a path with dirset, then stash it in a properly so we can access the string value of it in move? Nice goin'.
mixonic
Drat, and of course this won't work in a macro because it uses properties. @#$@( ant.
mixonic
A: 

If there are no subdirectories inside the package-3d28djh3 directory (or whatever it is called once you extracted it) you can use

<move todir="package" flatten="true" />
  <fileset dir=".">
    <include name="package-*/*"/>
  </fileset>
</move>

Otherwise, use the regexp mapper for the move task and get rid of the package-xxx directory:

<move todir="package">
  <fileset dir=".">
    <include name="package-*/*"/>
  </fileset>
  <mapper type="regexp" from="^package-.*/(.*)" to="\1"/>
</move>
pitpod
Howdy pitpod! Damn that regex is almost it. It's not recursive though, only the directories are copied and not their contents. I've got a directory tree maybe 6 levels deep to move. Obviously, ant's "move" is not simply mapped to mv. Hell.
mixonic