[gant-scm] [10445] gant/trunk: Apply a minor variation on the diff provided by Eric Van Dewoestine to fix the problem of GANT-110.

View: New views
1 Messages — Rating Filter:   Alert me  

[gant-scm] [10445] gant/trunk: Apply a minor variation on the diff provided by Eric Van Dewoestine to fix the problem of GANT-110.

by russel-9 :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.
[10445] gant/trunk: Apply a minor variation on the diff provided by Eric Van Dewoestine to fix the problem of GANT-110.

Diff

Modified: gant/trunk/releaseNotes.txt (10444 => 10445)

--- gant/trunk/releaseNotes.txt	2009-11-09 17:40:44 UTC (rev 10444)
+++ gant/trunk/releaseNotes.txt	2009-11-09 17:41:29 UTC (rev 10445)
@@ -7,7 +7,9 @@
 Change the way in which the final message is output when Gant is executed from the command line from being
 hardwired to being the execution of a hook, terminateHook.  cf. GANT-104.
 
+Altered the Gant Ant task so as to provide an inheritAll attribute.  cf. GANT-110.
 
+
 1.8.1
 -----
 

Modified: gant/trunk/src/main/groovy/org/codehaus/gant/ant/Gant.java (10444 => 10445)

--- gant/trunk/src/main/groovy/org/codehaus/gant/ant/Gant.java	2009-11-09 17:40:44 UTC (rev 10444)
+++ gant/trunk/src/main/groovy/org/codehaus/gant/ant/Gant.java	2009-11-09 17:41:29 UTC (rev 10445)
@@ -17,13 +17,16 @@
 import java.io.File ;
 
 import java.util.ArrayList ;
+import java.util.Enumeration;
 import java.util.HashMap ;
+import java.util.Hashtable;
 import java.util.List ;
 import java.util.Map ;
 
 import org.apache.tools.ant.AntClassLoader ;
 import org.apache.tools.ant.BuildException ;
 import org.apache.tools.ant.BuildListener ;
+import org.apache.tools.ant.MagicNames ;
 import org.apache.tools.ant.Project ;
 import org.apache.tools.ant.Task ;
 
@@ -62,6 +65,10 @@
    */
   private String file = "build.gant" ;
   /**
+   *  Flag determining whether properties are inherited from the parent project.
+   */
+  private boolean inheritAll = false ;
+  /**
    *  A class representing a nested definition tag.
    */
   public static final class Definition {
@@ -126,6 +133,12 @@
     return definition ;
   }
   /**
+   *  If true, pass all properties to the new Ant project.
+   *
+   *  @param value if true pass all properties to the new Ant project.
+   */
+  public void setInheritAll ( final boolean value ) { inheritAll = value ; }
+  /**
    * Load the file and then execute it.
    */
   @Override public void execute ( ) throws BuildException {
@@ -156,6 +169,8 @@
     //  Deal with GANT-50 by getting the base directory from the Ant instance Project object and use it for
     //  the new Project object.  GANT-93 leads to change in the way the Gant file is extracted.
     newProject.setBaseDir ( antProject.getBaseDir ( ) ) ;
+    //  Deal with GANT-110 by using the strategy proposed by Eric Van Dewoestine.
+    if ( inheritAll ){ addAlmostAll ( newProject , antProject ) ; }
     final File gantFile = newProject.resolveFile( file ) ;
     if ( ! gantFile.exists ( ) ) { throw new BuildException ( "Gantfile does not exist." , getLocation ( ) ) ; }
     final GantBuilder ant = new GantBuilder ( newProject ) ;
@@ -177,4 +192,24 @@
     final int returnCode =  gant.processTargets ( targetsAsStrings ) ;
     if ( returnCode != 0 ) { throw new BuildException ( "Gant execution failed with return code " + returnCode + '.' , getLocation ( ) ) ; }
   }
+  /**
+   *  Copy all properties from the given project to the new project -- omitting those that have already been
+   *  set in the new project as well as properties named basedir or ant.file. Inspired by the {@code
+   *  org.apache.tools.ant.taskdefs.Ant} source.
+   *
+   *  @param newProject the {@code Project} to copy into.
+   *  @param oldProject the {@code Project} to copy properties from.
+   */
+  //  Russel Winder rehacked the code provided by Eric Van Dewoestine.
+  private void addAlmostAll ( final Project newProject , final Project oldProject ) {
+    @SuppressWarnings ( "unchecked" )
+     final Hashtable<String,String> properties = oldProject.getProperties ( ) ;
+    final Enumeration<String> e = properties.keys ( ) ;
+    while ( e.hasMoreElements ( ) ) {
+      final String key = e.nextElement ( ) ;
+      if ( ! ( MagicNames.PROJECT_BASEDIR.equals ( key ) || MagicNames.ANT_FILE.equals ( key ) ) ) {
+        if ( newProject.getProperty ( key ) == null ) { newProject.setNewProperty ( key , properties.get ( key ) ) ; }
+      }
+    }
+  }
 }

Modified: gant/trunk/src/test/groovy/org/codehaus/gant/ant/tests/Gant_Test.java (10444 => 10445)

--- gant/trunk/src/test/groovy/org/codehaus/gant/ant/tests/Gant_Test.java	2009-11-09 17:40:44 UTC (rev 10444)
+++ gant/trunk/src/test/groovy/org/codehaus/gant/ant/tests/Gant_Test.java	2009-11-09 17:41:29 UTC (rev 10445)
@@ -29,6 +29,8 @@
 import org.apache.tools.ant.Project ;
 import org.apache.tools.ant.ProjectHelper ;
 
+import org.apache.tools.ant.util.StringUtils ;
+
 /**
  *  Unit tests for the Gant Ant task.  In order to test things appropriately this test must be initiated
  *  without any of the Groovy, Gant or related jars in the class path.  Also of course it must be a JUnit
@@ -380,4 +382,15 @@
     assertEquals ( "     [exec] Result: 253\n" , result.get ( 1 ) ) ;
   }
   */
+
+  //  For dealing with GANT-110 -- thanks to Eric Van Dewoestine for providing this.
+
+  public void testInheritAll ( ) {
+    List<String> result = runAnt ( antFile.getPath ( ) , "gantTestInheritAll" , 0 , true ) ;
+    @SuppressWarnings("unchecked")
+     List<String> output = (List<String>) StringUtils.lineSplit ( result.get ( 0 ) ) ;
+    assertEquals ( "     [echo] ${gant.test.inheritAll}" , output.get ( 12 ) ) ;
+    assertEquals ( "     [echo] ${gant.test.inheritAll}" , output.get ( 15 ) ) ;
+    assertEquals ( "     [echo] gantInheritAllWorks" , output.get ( 18 ) ) ;
+  }
 }

Modified: gant/trunk/src/test/groovy/org/codehaus/gant/ant/tests/build.gant (10444 => 10445)

--- gant/trunk/src/test/groovy/org/codehaus/gant/ant/tests/build.gant	2009-11-09 17:40:44 UTC (rev 10444)
+++ gant/trunk/src/test/groovy/org/codehaus/gant/ant/tests/build.gant	2009-11-09 17:41:29 UTC (rev 10445)
@@ -59,4 +59,10 @@
   Gant_Test.returnValue = 'gant' + ( flob ? ' -Dflob=' + flob : '' ) + ( burble ? ' -Dburble' : '' ) + ' gantParameters' 
 }
 
+//  For dealing with GANT-110 -- thanks to Eric Van Dewoestine for providing this.
+
+target ( gantInheritAll : 'Check that a inheritAll works.' ) {
+  echo ( message : '${gant.test.inheritAll}' )
+}
+
 setDefaultTarget ( test )

Modified: gant/trunk/src/test/groovy/org/codehaus/gant/ant/tests/gantTest.xml (10444 => 10445)

--- gant/trunk/src/test/groovy/org/codehaus/gant/ant/tests/gantTest.xml	2009-11-09 17:40:44 UTC (rev 10444)
+++ gant/trunk/src/test/groovy/org/codehaus/gant/ant/tests/gantTest.xml	2009-11-09 17:41:29 UTC (rev 10445)
@@ -62,4 +62,13 @@
     </gant>
   </target>
 
+  <!-- For GANT-110.  Thanks to Eric Van Dewoestine for providing this. -->
+
+  <target name="gantTestInheritAll" depends="-defineGantTask">
+    <property name="gant.test.inheritAll" value="gantInheritAllWorks"/>
+    <gant file="build.gant" target="gantInheritAll"/>
+    <gant file="build.gant" target="gantInheritAll" inheritAll="false"/>
+    <gant file="build.gant" target="gantInheritAll" inheritAll="true"/>
+  </target>
+  
 </project>


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email