You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ant.apache.org by hi...@apache.org on 2012/08/13 23:03:11 UTC

svn commit: r1372616 - in /ant/ivy/core/trunk: ./ doc/settings/ src/java/org/apache/ivy/core/settings/ test/java/org/apache/ivy/core/settings/

Author: hibou
Date: Mon Aug 13 21:03:10 2012
New Revision: 1372616

URL: http://svn.apache.org/viewvc?rev=1372616&view=rev
Log:
IVY-1367: Support Conditional Setting of a Property

Modified:
    ant/ivy/core/trunk/CHANGES.txt
    ant/ivy/core/trunk/doc/settings/property.html
    ant/ivy/core/trunk/src/java/org/apache/ivy/core/settings/IvySettings.java
    ant/ivy/core/trunk/src/java/org/apache/ivy/core/settings/XmlSettingsParser.java
    ant/ivy/core/trunk/test/java/org/apache/ivy/core/settings/IvySettingsTest.java

Modified: ant/ivy/core/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/CHANGES.txt?rev=1372616&r1=1372615&r2=1372616&view=diff
==============================================================================
--- ant/ivy/core/trunk/CHANGES.txt (original)
+++ ant/ivy/core/trunk/CHANGES.txt Mon Aug 13 21:03:10 2012
@@ -134,6 +134,8 @@ for detailed view of each issue, please 
 - FIX: ivy.xml extends feature complains about Windows filesystem path (IVY-1359) (thanks to Mitch Gitman and Jean-Louis Boudart)
 - FIX: buildlist task chokes on absolute path to parent Ivy module (IVY-1364) (thanks to Mitch Gitman and Jean-Louis Boudart)
 
+- NEW: Support Conditional Setting of a Property (IVY-1367)
+
 - IMPROVEMENT: add support for source bundles from p2 repositories
 - IMPROVEMENT: add support for source URI from OBR repositories
 

Modified: ant/ivy/core/trunk/doc/settings/property.html
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/doc/settings/property.html?rev=1372616&r1=1372615&r2=1372616&view=diff
==============================================================================
--- ant/ivy/core/trunk/doc/settings/property.html (original)
+++ ant/ivy/core/trunk/doc/settings/property.html Mon Aug 13 21:03:10 2012
@@ -41,6 +41,8 @@ The optional override attribute enables 
         <td>Yes</td></tr>
     <tr><td>override</td><td>true if the previous value (if any) of the variable should overriden, false otherwise</td>
         <td>No, defaults to true</td></tr>
+    <tr><td>ifset</td><td>the variable will be set only if the provided 'ifset' variable is already set (Since 2.4)</td><td>No</td></tr>
+    <tr><td>unlessset</td><td>the variable will not be set unless the provided 'unlessset' variable is set (Since 2.4)</td><td>No</td></tr>
 </tbody>
 </table>
 <h1>Examples</h1>
@@ -53,6 +55,13 @@ Sets the variable myvar to the value myv
 <property name="myvar" value="myvalue" override="false"/>
 </code>
 Sets the variable myvar to the value myvalue only if myvar has not been set yet.
+<hr/>
+<code type="xml">
+ <properties environment="env"/>
+ <property name="ivy.repos.server" value="${env.IVY_SERVER}" override="false" ifset="env.IVY_SERVER" />
+ <property name="ivy.repos.server" value="http://ivy:8081" override="false"/>
+</code>
+If the environment variable is set, it takes precedence over of default value.
 
 
 	</textarea>

Modified: ant/ivy/core/trunk/src/java/org/apache/ivy/core/settings/IvySettings.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/core/settings/IvySettings.java?rev=1372616&r1=1372615&r2=1372616&view=diff
==============================================================================
--- ant/ivy/core/trunk/src/java/org/apache/ivy/core/settings/IvySettings.java (original)
+++ ant/ivy/core/trunk/src/java/org/apache/ivy/core/settings/IvySettings.java Mon Aug 13 21:03:10 2012
@@ -576,6 +576,20 @@ public class IvySettings implements Sort
     }
 
     public void setVariable(String varName, String value, boolean overwrite) {
+        setVariable(varName, value, overwrite, null, null);
+    }
+
+    public void setVariable(String varName, String value, boolean overwrite, String ifSetVar, String unlessSetVar) {
+        if (ifSetVar != null && variableContainer.getVariable(ifSetVar) == null) {
+            Message.verbose("Not setting '" + varName + "' to '" + value + "' since '" + ifSetVar
+                + "' is not set.");
+            return;
+        }
+        if (unlessSetVar != null && variableContainer.getVariable(unlessSetVar) != null) {
+            Message.verbose("Not setting '" + varName + "' to '" + value + "' since '" + unlessSetVar
+                + "' is set.");
+            return;
+        }
         variableContainer.setVariable(varName, value, overwrite);
     }
 

Modified: ant/ivy/core/trunk/src/java/org/apache/ivy/core/settings/XmlSettingsParser.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/core/settings/XmlSettingsParser.java?rev=1372616&r1=1372615&r2=1372616&view=diff
==============================================================================
--- ant/ivy/core/trunk/src/java/org/apache/ivy/core/settings/XmlSettingsParser.java (original)
+++ ant/ivy/core/trunk/src/java/org/apache/ivy/core/settings/XmlSettingsParser.java Mon Aug 13 21:03:10 2012
@@ -497,6 +497,8 @@ public class XmlSettingsParser extends D
         String name = (String) attributes.get("name");
         String value = (String) attributes.get("value");
         String override = (String) attributes.get("override");
+        String isSetVar = (String) attributes.get("ifset");
+        String unlessSetVar = (String) attributes.get("unlessset");
         if (name == null) {
             throw new IllegalArgumentException("missing attribute name on property tag");
         }
@@ -504,7 +506,7 @@ public class XmlSettingsParser extends D
             throw new IllegalArgumentException("missing attribute value on property tag");
         }
         ivy.setVariable(name, value, override == null ? true : Boolean.valueOf(override)
-                .booleanValue());
+                .booleanValue(), isSetVar, unlessSetVar);
     }
 
     private void typedefStarted(Map attributes) {

Modified: ant/ivy/core/trunk/test/java/org/apache/ivy/core/settings/IvySettingsTest.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/test/java/org/apache/ivy/core/settings/IvySettingsTest.java?rev=1372616&r1=1372615&r2=1372616&view=diff
==============================================================================
--- ant/ivy/core/trunk/test/java/org/apache/ivy/core/settings/IvySettingsTest.java (original)
+++ ant/ivy/core/trunk/test/java/org/apache/ivy/core/settings/IvySettingsTest.java Mon Aug 13 21:03:10 2012
@@ -44,5 +44,58 @@ public class IvySettingsTest extends Tes
         assertNotSame("default resolver has changed", defaultResolver, newDefault);
         assertEquals("resolver changed successfully", "public", newDefault.getName());
     }
-    
+
+    public void testVariables() throws Exception {
+        Ivy ivy = new Ivy();
+        ivy.configureDefault();
+        IvySettings settings = ivy.getSettings();
+
+        // test set
+        assertNull(settings.getVariable("foo"));
+        settings.setVariable("foo", "bar", false, null, null);
+        assertEquals("bar", settings.getVariable("foo"));
+
+        // test no override
+        settings.setVariable("foo", "wrong", false, null, null);
+        assertEquals("bar", settings.getVariable("foo"));
+
+        // test override
+        settings.setVariable("foo", "right", true, null, null);
+        assertEquals("right", settings.getVariable("foo"));
+
+        // test ifset no exist
+        assertNull(settings.getVariable("bar"));
+        settings.setVariable("bar", "foo", true, "noexist", null);
+        assertNull(settings.getVariable("bar"));
+
+        // test ifset exist
+        settings.setVariable("bar", "foo", true, "foo", null);
+        assertEquals("foo", settings.getVariable("bar"));
+
+        // test unlessset exist
+        assertNull(settings.getVariable("thing"));
+        settings.setVariable("thing", "foo", true, null, "foo");
+        assertNull(settings.getVariable("thing"));
+
+        // test unlessset noexist
+        settings.setVariable("thing", "foo", true, null, "noexist");
+        assertEquals("foo", settings.getVariable("thing"));
+
+        // test ifset no exist and unlessset exist
+        assertNull(settings.getVariable("ivy"));
+        settings.setVariable("ivy", "rocks", true, "noexist", "foo");
+        assertNull(settings.getVariable("ivy"));
+
+        // test ifset no exist and unlessset no exist
+        settings.setVariable("ivy", "rocks", true, "noexist", "noexist");
+        assertNull(settings.getVariable("ivy"));
+
+        // test ifset exist and unlessset exist
+        settings.setVariable("ivy", "rocks", true, "foo", "foo");
+        assertNull(settings.getVariable("ivy"));
+
+        // test ifset exist and unlessset no exist
+        settings.setVariable("ivy", "rocks", true, "foo", "noexist");
+        assertEquals("rocks", settings.getVariable("ivy"));
+    }
 }