mbenson     2004/08/31 14:34:36

  Modified:    .        WHATSNEW
               docs/manual/CoreTasks move.html
               src/main/org/apache/tools/ant/taskdefs Move.java
               src/etc/testcases/taskdefs move.xml
               src/testcases/org/apache/tools/ant/taskdefs MoveTest.java
  Log:
  Allow <move>'s file attribute to rename a directory.
  PR: 22863
  
  Revision  Changes    Path
  1.654     +4 -1      ant/WHATSNEW
  
  Index: WHATSNEW
  ===================================================================
  RCS file: /home/cvs/ant/WHATSNEW,v
  retrieving revision 1.653
  retrieving revision 1.654
  diff -u -r1.653 -r1.654
  --- WHATSNEW  25 Aug 2004 14:56:41 -0000      1.653
  +++ WHATSNEW  31 Aug 2004 21:34:35 -0000      1.654
  @@ -42,7 +42,10 @@
     wsdl run on mono, as well as most of the .NET WSE2.0 options. Extra
     schemas (files or urls) can be named in the <schema> element.
     Compilers can be selected using the compiler attribute, which defaults
  -  to "microsoft" on windows, and "mono" on everything else.   
  +  to "microsoft" on windows, and "mono" on everything else.
  +
  +* Allow file attribute of <move> to rename a directory.
  +  Bugzilla Report 22863.
   
   Changes from Ant 1.6.2 to current Ant 1.6 CVS version
   =====================================================
  
  
  
  1.18      +10 -2     ant/docs/manual/CoreTasks/move.html
  
  Index: move.html
  ===================================================================
  RCS file: /home/cvs/ant/docs/manual/CoreTasks/move.html,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- move.html 9 Feb 2004 21:50:05 -0000       1.17
  +++ move.html 31 Aug 2004 21:34:35 -0000      1.18
  @@ -16,6 +16,10 @@
   the destination file, or when the destination file does not exist.</p>
   <p><a href="../CoreTypes/fileset.html">FileSet</a>s are used to select sets 
of files
   to move to the <var>todir</var> directory.</p>
  +<p><b>Since Ant 1.6.3</b>, the <i>file</i> attribute may be used to move
  +(rename) an entire directory.  If <i>tofile</i> denotes an existing file, or
  +there is a directory by the same name in <i>todir</i>, the action will fail.
  +</p>
   <h3>Parameters</h3>
   <table border="1" cellpadding="2" cellspacing="0">
     <tr>
  @@ -25,7 +29,7 @@
     </tr>
     <tr>
       <td valign="top">file</td>
  -    <td valign="top">the file to move</td>
  +    <td valign="top">the file or directory to move</td>
       <td valign="top" align="center">One of <var>file</var> or
        at least one nested fileset element</td>
     </tr>
  @@ -78,7 +82,7 @@
     </tr>
     <tr>
       <td valign="top">failonerror</td>
  -     <td valign="top">Log a warning message, but do not stop the
  +     <td valign="top">If false, log a warning message, but do not stop the
          build, when the file to copy does not exist or one of the nested
          filesets points to a directory that doesn't exist or an error occurs
          while moving.
  @@ -154,6 +158,10 @@
     &lt;move todir=&quot;new/dir/to/move/to&quot;&gt;
       &lt;fileset dir=&quot;src/dir&quot;/&gt;
     &lt;/move&gt;
  +</pre>
  +  <i>or, since Ant 1.6.3:</i>
  +<pre>
  +  &lt;move file=&quot;src/dir&quot; tofile=&quot;new/dir/to/move/to&quot; 
/&gt;
   </pre>
   <p><b>Move a set of files to a new directory</b></p>
   <pre>
  
  
  
  1.46      +64 -10    ant/src/main/org/apache/tools/ant/taskdefs/Move.java
  
  Index: Move.java
  ===================================================================
  RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/Move.java,v
  retrieving revision 1.45
  retrieving revision 1.46
  diff -u -r1.45 -r1.46
  --- Move.java 9 Mar 2004 16:48:06 -0000       1.45
  +++ Move.java 31 Aug 2004 21:34:35 -0000      1.46
  @@ -61,6 +61,58 @@
           setOverwrite(true);
       }
   
  +    /**
  +     * Performs the move operation.
  +     */
  +    public void execute() throws BuildException {
  +        if (file != null && file.isDirectory()) {
  +            if (destFile != null && destDir != null) {
  +                throw new BuildException("Only one of tofile and todir "
  +                                         + "may be set.");
  +            }
  +
  +            if (destFile == null && destDir == null) {
  +                throw new BuildException("One of tofile or todir must be 
set.");
  +            }
  +
  +            destFile = (destFile != null)
  +                ? destFile : new File(destDir, file.getName());
  +
  +            try {
  +                boolean renamed = false;
  +                log("Moving directory " + file
  +                    + " to " + destFile, Project.MSG_INFO);
  +                try {
  +                    renamed =
  +                        renameFile(file, destFile, filtering, 
forceOverwrite);
  +                } catch (IOException eyeOhEx) {
  +                    throw new BuildException(eyeOhEx.getMessage());
  +                }
  +                if (!renamed) {
  +                    StringBuffer buf = new StringBuffer(
  +                        "Failed to move directory ").append(
  +                        file.getAbsolutePath());
  +
  +                    if ((getFilterChains() != null && 
getFilterChains().size() > 0)
  +                        || (getFilterSets() != null && 
getFilterSets().size() > 0)
  +                        || filtering) {
  +                        buf.append(
  +                            "; use a fileset to move directories with 
filtering");
  +                    }
  +                    throw new BuildException(buf.append('.').toString());
  +                }
  +            } catch (BuildException e) {
  +                if (!failonerror) {
  +                    log("Warning: " + e.getMessage(), Project.MSG_ERR);
  +                } else {
  +                    throw e;
  +                }
  +            }
  +        } else {
  +            super.execute();
  +        }
  +    }
  +
   //************************************************************************
   //  protected and private methods
   //************************************************************************
  @@ -325,17 +377,19 @@
           } else {
               if (!filtering) {
                   // ensure that parent dir of dest file exists!
  -                // not using getParentFile method to stay 1.1 compatibility
  -                String parentPath = destFile.getParent();
  -                if (parentPath != null) {
  -                    File parent = new File(parentPath);
  -                    if (!parent.exists()) {
  -                        parent.mkdirs();
  -                    }
  +                File parent = destFile.getParentFile();
  +                if (parent != null && !parent.exists()) {
  +                    parent.mkdirs();
                   }
   
  -                if (destFile.exists() && destFile.isFile()) {
  -                    if (!destFile.delete()) {
  +                if (destFile.exists()) {
  +                    if (sourceFile.isDirectory()) {
  +                     throw new BuildException(
  +                        new StringBuffer("Cannot replace ").append(
  +                        ((destFile.isFile()) ? "file " : "directory 
")).append(
  +                        destFile).append(" with directory ").append(
  +                        sourceFile).toString());
  +                    } else if (destFile.isFile() && !destFile.delete()) {
                           throw new BuildException("Unable to remove existing "
                                                    + "file " + destFile);
                       }
  
  
  
  1.6       +69 -2     ant/src/etc/testcases/taskdefs/move.xml
  
  Index: move.xml
  ===================================================================
  RCS file: /home/cvs/ant/src/etc/testcases/taskdefs/move.xml,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- move.xml  19 May 2003 14:27:43 -0000      1.5
  +++ move.xml  31 Aug 2004 21:34:36 -0000      1.6
  @@ -59,11 +59,78 @@
       </move>
     </target>
   
  +  <target name="testCompleteDirectoryMoveFileToFile">
  +    <mkdir dir="A"/>
  +    <touch file="A/1"/>
  +    <move file="A" tofile="E" />
  +    <fail message="E/1 not available">
  +      <condition>
  +        <not>
  +          <available file="E/1" type="file" />
  +        </not>
  +      </condition>
  +    </fail>
  +    <fail message="A remains">
  +      <condition>
  +        <available file="A" type="dir" />
  +      </condition>
  +    </fail>
  +  </target>
  +
  +  <target name="testCompleteDirectoryMoveFileToDir">
  +    <mkdir dir="A"/>
  +    <touch file="A/1"/>
  +    <move file="A" todir="E" />
  +    <fail message="E/A/1 not available">
  +      <condition>
  +        <not>
  +          <available file="E/A/1" type="file" />
  +        </not>
  +      </condition>
  +    </fail>
  +    <fail message="A remains">
  +      <condition>
  +        <available file="A" type="dir" />
  +      </condition>
  +    </fail>
  +  </target>
  +
  +  <target name="testCompleteDirectoryMoveFileToExistingFile">
  +    <mkdir dir="A"/>
  +    <touch file="A/1"/>
  +    <touch file="B"/>
  +    <move file="A" tofile="B" />
  +  </target>
  +
  +  <target name="testCompleteDirectoryMoveFileToExistingDir">
  +    <mkdir dir="A"/>
  +    <touch file="A/1"/>
  +    <mkdir dir="E"/>
  +    <move file="A" tofile="E" />
  +  </target>
  +
  +  <target name="testCompleteDirectoryMoveFileToDirWithExistingFile">
  +    <mkdir dir="A"/>
  +    <touch file="A/1"/>
  +    <mkdir dir="E"/>
  +    <touch file="E/A"/>
  +    <move file="A" todir="E" />
  +  </target>
  +
  +  <target name="testCompleteDirectoryMoveFileToDirWithExistingDir">
  +    <mkdir dir="A"/>
  +    <touch file="A/1"/>
  +    <mkdir dir="E"/>
  +    <mkdir dir="E/A"/>
  +    <move file="A" todir="E" />
  +  </target>
  +
     <target name="cleanup"> 
       <delete file="move.filterset.tmp"/>
       <delete file="move.filterchain.tmp"/>
  -    <delete dir="A"/>
  -    <delete dir="E"/>
  +    <delete dir="A" />
  +    <delete file="B" />
  +    <delete dir="E" />
     </target>
   
   </project>
  
  
  
  1.13      +29 -0     
ant/src/testcases/org/apache/tools/ant/taskdefs/MoveTest.java
  
  Index: MoveTest.java
  ===================================================================
  RCS file: 
/home/cvs/ant/src/testcases/org/apache/tools/ant/taskdefs/MoveTest.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- MoveTest.java     9 Mar 2004 16:48:57 -0000       1.12
  +++ MoveTest.java     31 Aug 2004 21:34:36 -0000      1.13
  @@ -88,4 +88,33 @@
           assertTrue(!getProject().resolveFile("A/1").exists());
           assertTrue(!getProject().resolveFile("A").exists());
       }
  +
  +    public void testCompleteDirectoryMoveFileToFile() {
  +        executeTarget("testCompleteDirectoryMoveFileToFile");
  +    }
  +
  +    public void testCompleteDirectoryMoveFileToDir() {
  +        executeTarget("testCompleteDirectoryMoveFileToDir");
  +    }
  +
  +    public void testCompleteDirectoryMoveFileToExistingFile() {
  +        
expectBuildExceptionContaining("testCompleteDirectoryMoveFileToExistingFile",
  +                                       "", "Cannot replace file");
  +    }
  +
  +    public void testCompleteDirectoryMoveFileToExistingDir() {
  +        
expectBuildExceptionContaining("testCompleteDirectoryMoveFileToExistingDir",
  +                                       "", "Cannot replace directory");
  +    }
  +
  +    public void testCompleteDirectoryMoveFileToDirWithExistingFile() {
  +        
expectBuildExceptionContaining("testCompleteDirectoryMoveFileToDirWithExistingFile",
  +                                       "", "Cannot replace file");
  +    }
  +
  +    public void testCompleteDirectoryMoveFileToDirWithExistingDir() {
  +        
expectBuildExceptionContaining("testCompleteDirectoryMoveFileToDirWithExistingDir",
  +                                       "", "Cannot replace directory");
  +    }
  +
   }
  
  
  

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

Reply via email to