You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ant.apache.org by bo...@apache.org on 2008/11/12 14:53:58 UTC

svn commit: r713373 - in /ant/core/trunk: ./ docs/manual/CoreTasks/ src/main/org/apache/tools/ant/ src/main/org/apache/tools/ant/helper/ src/main/org/apache/tools/ant/taskdefs/ src/tests/antunit/taskdefs/ src/tests/antunit/taskdefs/importtests/

Author: bodewig
Date: Wed Nov 12 05:53:57 2008
New Revision: 713373

URL: http://svn.apache.org/viewvc?rev=713373&view=rev
Log:
Make prefix separator configurable, nest prefixes in include

Added:
    ant/core/trunk/src/tests/antunit/taskdefs/importtests/nested.xml   (with props)
Modified:
    ant/core/trunk/WHATSNEW
    ant/core/trunk/docs/manual/CoreTasks/import.html
    ant/core/trunk/docs/manual/CoreTasks/include.html
    ant/core/trunk/src/main/org/apache/tools/ant/ProjectHelper.java
    ant/core/trunk/src/main/org/apache/tools/ant/helper/ProjectHelper2.java
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/ImportTask.java
    ant/core/trunk/src/tests/antunit/taskdefs/include-test.xml

Modified: ant/core/trunk/WHATSNEW
URL: http://svn.apache.org/viewvc/ant/core/trunk/WHATSNEW?rev=713373&r1=713372&r2=713373&view=diff
==============================================================================
--- ant/core/trunk/WHATSNEW (original)
+++ ant/core/trunk/WHATSNEW Wed Nov 12 05:53:57 2008
@@ -516,8 +516,9 @@
    STARTTLS.
    Bugzilla Report 46063.
 
- * <import> has a new attribute "as" that can be used to control the
-   prefix prepended to the imported target's names.
+ * <import> has new attributes "as" and "prefixSeparator" that can be
+   used to control the prefix prepended to the imported target's
+   names.
 
  * a new task <include> provides an alternative to <import> that
    should be preferred when you don't want to override any targets.

Modified: ant/core/trunk/docs/manual/CoreTasks/import.html
URL: http://svn.apache.org/viewvc/ant/core/trunk/docs/manual/CoreTasks/import.html?rev=713373&r1=713372&r2=713373&view=diff
==============================================================================
--- ant/core/trunk/docs/manual/CoreTasks/import.html (original)
+++ ant/core/trunk/docs/manual/CoreTasks/import.html Wed Nov 12 05:53:57 2008
@@ -175,6 +175,16 @@
       </td>
       <td valign="top" align="center">No</td>
     </tr>
+    <tr>
+      <td valign="top">
+        prefixSeparator
+      </td>
+      <td valign="top">
+        Specifies the separator to be used between the prefix and the
+        target name.  Defaults to ".".
+      </td>
+      <td valign="top" align="center">No</td>
+    </tr>
   </tbody>
 </table>
 

Modified: ant/core/trunk/docs/manual/CoreTasks/include.html
URL: http://svn.apache.org/viewvc/ant/core/trunk/docs/manual/CoreTasks/include.html?rev=713373&r1=713372&r2=713373&view=diff
==============================================================================
--- ant/core/trunk/docs/manual/CoreTasks/include.html (original)
+++ ant/core/trunk/docs/manual/CoreTasks/include.html Wed Nov 12 05:53:57 2008
@@ -69,6 +69,11 @@
   all target names are prefixed as well.  This makes the included file
   self-contained.</p>
 
+<p>Note that prefixes nest, so if a build file includes a file with
+  prefix "a" and the included file includes another file with prefix
+  "b", then the targets of that last build file will be prefixed by
+  "a.b.".</p>
+
 <h4>Special Properties</h4>
 
 <p>Included files are treated as they are present in the main
@@ -171,6 +176,16 @@
       <td valign="top" align="center">Yes, if the included file's
         project tag doesn't specify a name attribute.</td>
     </tr>
+    <tr>
+      <td valign="top">
+        prefixSeparator
+      </td>
+      <td valign="top">
+        Specifies the separator to be used between the prefix and the
+        target name.  Defaults to ".".
+      </td>
+      <td valign="top" align="center">No</td>
+    </tr>
   </tbody>
 </table>
 

Modified: ant/core/trunk/src/main/org/apache/tools/ant/ProjectHelper.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/ProjectHelper.java?rev=713373&r1=713372&r2=713373&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/ProjectHelper.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/ProjectHelper.java Wed Nov 12 05:53:57 2008
@@ -143,6 +143,32 @@
         targetPrefix.set(prefix);
     }
 
+    private final static ThreadLocal prefixSeparator = new ThreadLocal() {
+            protected Object initialValue() {
+                return ".";
+            }
+        };
+
+    /**
+     * The separator between the prefix and the target name.
+     *
+     * <p>May be set by &lt;import&gt;'s prefixSeperator attribute.</p>
+     *
+     * @since Ant 1.8.0
+     */
+    public static String getCurrentPrefixSeparator() {
+        return (String) prefixSeparator.get();
+    }
+
+    /**
+     * Sets the separator between the prefix and the target name.
+     *
+     * @since Ant 1.8.0
+     */
+    public static void setCurrentPrefixSeparator(String sep) {
+        prefixSeparator.set(sep);
+    }
+
     private final static ThreadLocal inIncludeMode = new ThreadLocal() {
             protected Object initialValue() {
                 return Boolean.FALSE;

Modified: ant/core/trunk/src/main/org/apache/tools/ant/helper/ProjectHelper2.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/helper/ProjectHelper2.java?rev=713373&r1=713372&r2=713373&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/helper/ProjectHelper2.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/helper/ProjectHelper2.java Wed Nov 12 05:53:57 2008
@@ -849,6 +849,8 @@
             String prefix = null;
             boolean isInIncludeMode =
                 context.isIgnoringProjectTag() && isInIncludeMode();
+            String sep = getCurrentPrefixSeparator();
+
             if (isInIncludeMode) {
                 prefix = getTargetPrefix(context);
                 if (prefix == null) {
@@ -858,7 +860,7 @@
                                              + " and the project tag doesn't"
                                              + " specify a name attribute");
                 }
-                name = prefix + "." + name;
+                name = prefix + sep + name;
             }
 
             // Check if this target is in the current build file
@@ -878,6 +880,7 @@
                 project.addOrReplaceTarget(name, target);
                 usedTarget = true;
             }
+
             if (depends.length() > 0) {
                 if (!isInIncludeMode) {
                     target.setDepends(depends);
@@ -885,7 +888,7 @@
                     for (Iterator iter =
                              Target.parseDepends(depends, name).iterator();
                          iter.hasNext(); ) {
-                        target.addDependency(prefix + "." + iter.next());
+                        target.addDependency(prefix + sep + iter.next());
                     }
                 }
             }
@@ -893,7 +896,7 @@
                 && (prefix = getTargetPrefix(context)) != null) {
                 // In an imported file (and not completely
                 // ignoring the project tag or having a preconfigured prefix)
-                String newName = prefix + "." + name;
+                String newName = prefix + sep + name;
                 Target newTarget = usedTarget ? new Target(target) : target;
                 newTarget.setName(newName);
                 context.getCurrentTargets().put(newName, newTarget);
@@ -909,10 +912,17 @@
             if (configuredValue != null) {
                 return configuredValue;
             }
+
             String projectName = context.getCurrentProjectName();
-            if (projectName != null && projectName.length() == 0) {
+            if ("".equals(projectName)) {
                 projectName = null;
             }
+
+            // help nested include tasks
+            if (projectName != null) {
+                setCurrentTargetPrefix(projectName);
+            }
+
             return projectName;
         }
 

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/ImportTask.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/ImportTask.java?rev=713373&r1=713372&r2=713373&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/ImportTask.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/ImportTask.java Wed Nov 12 05:53:57 2008
@@ -56,6 +56,7 @@
     private String file;
     private boolean optional;
     private String targetPrefix;
+    private String prefixSeparator = ".";
     private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
 
     /**
@@ -89,9 +90,15 @@
     }
 
     /**
-     *  This relies on the task order model.
+     * The separator to use between prefix and target name, default is
+     * ".".
      *
+     * @since Ant 1.8.0
      */
+    public void setPrefixSeparator(String s) {
+        prefixSeparator = s;
+    }
+
     public void execute() {
         if (file == null) {
             throw new BuildException("import requires file attribute");
@@ -156,16 +163,22 @@
         // importing another one
         String oldPrefix = ProjectHelper.getCurrentTargetPrefix();
         boolean oldIncludeMode = ProjectHelper.isInIncludeMode();
+        String oldSep = ProjectHelper.getCurrentPrefixSeparator();
         try {
-            ProjectHelper.setCurrentTargetPrefix(targetPrefix);
-            ProjectHelper.setInIncludeMode(isInIncludeMode());
+            String prefix = targetPrefix;
+            if (isInIncludeMode() && oldPrefix != null
+                && targetPrefix != null) {
+                prefix = oldPrefix + oldSep + targetPrefix;
+            }
+            setProjectHelperProps(prefix, prefixSeparator,
+                                  isInIncludeMode());
+
             helper.parse(getProject(), importedFile);
         } catch (BuildException ex) {
             throw ProjectHelper.addLocationToBuildException(
                 ex, getLocation());
         } finally {
-            ProjectHelper.setCurrentTargetPrefix(oldPrefix);
-            ProjectHelper.setInIncludeMode(oldIncludeMode);
+            setProjectHelperProps(oldPrefix, oldSep, oldIncludeMode);
         }
     }
 
@@ -187,4 +200,16 @@
         return "include".equals(getTaskType());
     }
 
+    /**
+     * Sets a bunch of Thread-local ProjectHelper properties.
+     * 
+     * @since Ant 1.8.0
+     */
+    private static void setProjectHelperProps(String prefix,
+                                              String prefixSep,
+                                              boolean inIncludeMode) {
+        ProjectHelper.setCurrentTargetPrefix(prefix);
+        ProjectHelper.setCurrentPrefixSeparator(prefixSep);
+        ProjectHelper.setInIncludeMode(inIncludeMode);
+    }
 }

Added: ant/core/trunk/src/tests/antunit/taskdefs/importtests/nested.xml
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/tests/antunit/taskdefs/importtests/nested.xml?rev=713373&view=auto
==============================================================================
--- ant/core/trunk/src/tests/antunit/taskdefs/importtests/nested.xml (added)
+++ ant/core/trunk/src/tests/antunit/taskdefs/importtests/nested.xml Wed Nov 12 05:53:57 2008
@@ -0,0 +1,21 @@
+<?xml version="1.0"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+<project>
+  <include file="b.xml" as="b" prefixSeparator="::"/>
+  <include file="a.xml" as="a" prefixSeparator=""/>
+</project>

Propchange: ant/core/trunk/src/tests/antunit/taskdefs/importtests/nested.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: ant/core/trunk/src/tests/antunit/taskdefs/include-test.xml
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/tests/antunit/taskdefs/include-test.xml?rev=713373&r1=713372&r2=713373&view=diff
==============================================================================
--- ant/core/trunk/src/tests/antunit/taskdefs/include-test.xml (original)
+++ ant/core/trunk/src/tests/antunit/taskdefs/include-test.xml Wed Nov 12 05:53:57 2008
@@ -38,4 +38,10 @@
   <target name="testNoOverride" depends="override.dummy">
     <au:assertEquals expected="in included/imported" actual="${prop}"/>
   </target>
+
+  <include file="importtests/nested.xml" as="nested"/>
+
+  <!-- really only tests that the targets have the expected names by
+       forcing an exception if the dependencies don't exist -->
+  <target name="testNesting" depends="nested.b::b, nested.aa"/>
 </project>