ant-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j..@apache.org
Subject ant git commit: add magic property that override's tstamp's idea of "now" in ISO-8601 format https://bz.apache.org/bugzilla/show_bug.cgi?id=61079
Date Tue, 09 May 2017 09:02:22 GMT
Repository: ant
Updated Branches:
  refs/heads/master 966708a29 -> 7a7307bc9


add magic property that override's tstamp's idea of "now" in ISO-8601 format
https://bz.apache.org/bugzilla/show_bug.cgi?id=61079


Project: http://git-wip-us.apache.org/repos/asf/ant/repo
Commit: http://git-wip-us.apache.org/repos/asf/ant/commit/7a7307bc
Tree: http://git-wip-us.apache.org/repos/asf/ant/tree/7a7307bc
Diff: http://git-wip-us.apache.org/repos/asf/ant/diff/7a7307bc

Branch: refs/heads/master
Commit: 7a7307bc999be080c99412b1c67d111af1366ef7
Parents: 966708a
Author: Jan Matèrne <jhm@apache.org>
Authored: Tue May 9 11:01:51 2017 +0200
Committer: Jan Matèrne <jhm@apache.org>
Committed: Tue May 9 11:01:51 2017 +0200

----------------------------------------------------------------------
 manual/Tasks/tstamp.html                        | 10 +++--
 manual/running.html                             |  4 ++
 src/main/org/apache/tools/ant/MagicNames.java   | 13 ++++++
 .../org/apache/tools/ant/taskdefs/Tstamp.java   | 47 +++++++++++++++++---
 src/tests/antunit/taskdefs/tstamp-test.xml      | 17 +++++++
 5 files changed, 80 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ant/blob/7a7307bc/manual/Tasks/tstamp.html
----------------------------------------------------------------------
diff --git a/manual/Tasks/tstamp.html b/manual/Tasks/tstamp.html
index c9a83c9..f35d558 100644
--- a/manual/Tasks/tstamp.html
+++ b/manual/Tasks/tstamp.html
@@ -41,10 +41,12 @@ to indicate, for example, the release date. The best place for this task
is
 probably in an initialization target.</p>
 
 <p><em>Since Ant 1.10.2</em> the magic
-  property <code>ant.tstamp.now</code> can be used to specify a fixed
-  date value in order to create reproducible builds. Its value must be
-  a number and is interpreted as seconds since the epoch (midnight
-  1970-01-01).</p>
+property <code>ant.tstamp.now</code> can be used to specify a fixed
+date value in order to create reproducible builds. Its value must be
+a number and is interpreted as seconds since the epoch (midnight
+1970-01-01). With <code>ant.tstamp.now.iso</code> you could also specify that
+value in ISO-8601 format (<code>1972-04-17T08:07:00Z</code>). If you specify
a value
+in an invalid format an INFO message will be logged and the value will be ignored.</p>
 
 <h3>Parameters</h3>
 <table border="1" cellpadding="2" cellspacing="0">

http://git-wip-us.apache.org/repos/asf/ant/blob/7a7307bc/manual/running.html
----------------------------------------------------------------------
diff --git a/manual/running.html b/manual/running.html
index 3bfb500..dac2ef3 100644
--- a/manual/running.html
+++ b/manual/running.html
@@ -490,6 +490,10 @@ org.apache.tools.ant.Executor implementation specified here.
   <td>number, seconds since the epoch (midnight 1970-01-01)</td>
   <td>The value to use as current time and date for &lt;tstamp&gt;</td>
 </tr>
+<tr>
+  <td><code>ant.tstamp.now.iso</code></td>
+  <td>ISO-8601 timestamp string like <code>1972-04-17T08:07:00Z</code></td>
+</tr>
 </table>
 
 <p>

http://git-wip-us.apache.org/repos/asf/ant/blob/7a7307bc/src/main/org/apache/tools/ant/MagicNames.java
----------------------------------------------------------------------
diff --git a/src/main/org/apache/tools/ant/MagicNames.java b/src/main/org/apache/tools/ant/MagicNames.java
index 5f6aec9..cfd4cc7 100644
--- a/src/main/org/apache/tools/ant/MagicNames.java
+++ b/src/main/org/apache/tools/ant/MagicNames.java
@@ -308,5 +308,18 @@ public final class MagicNames {
      * @since Ant 1.10.2
      */
     public static final String TSTAMP_NOW = "ant.tstamp.now";
+
+    /**
+     * Magic property that can be set to contain a value for tstamp's
+     * "now" in order to make builds that use the task create
+     * reproducible results.
+     *
+     * <p>The value is expected to be in ISO time format
+     * (<i>1972-04-17T08:07</i>)</p>
+     *
+     * Value: {@value}
+     * @since Ant 1.10.2
+     */
+    public static final String TSTAMP_NOW_ISO = "ant.tstamp.now.iso";
 }
 

http://git-wip-us.apache.org/repos/asf/ant/blob/7a7307bc/src/main/org/apache/tools/ant/taskdefs/Tstamp.java
----------------------------------------------------------------------
diff --git a/src/main/org/apache/tools/ant/taskdefs/Tstamp.java b/src/main/org/apache/tools/ant/taskdefs/Tstamp.java
index 98dd779..3f5c172 100644
--- a/src/main/org/apache/tools/ant/taskdefs/Tstamp.java
+++ b/src/main/org/apache/tools/ant/taskdefs/Tstamp.java
@@ -19,6 +19,7 @@
 package org.apache.tools.ant.taskdefs;
 
 import java.text.SimpleDateFormat;
+import java.time.Instant;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.HashMap;
@@ -26,9 +27,12 @@ import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 import java.util.NoSuchElementException;
+import java.util.Optional;
 import java.util.StringTokenizer;
 import java.util.TimeZone;
 import java.util.Vector;
+import java.util.function.BiFunction;
+import java.util.function.Function;
 
 import org.apache.tools.ant.BuildException;
 import org.apache.tools.ant.Location;
@@ -111,16 +115,45 @@ public class Tstamp extends Task {
      * Return the {@link Date} instance to use as base for DSTAMP, TSTAMP and TODAY.
      */
     protected Date getNow() {
-        String magicNow = getProject().getProperty(MagicNames.TSTAMP_NOW);
-        if (magicNow != null && magicNow.length() > 0) {
+        Optional<Date> now = getNow(
+            MagicNames.TSTAMP_NOW_ISO,
+            s -> Date.from(Instant.parse(s)),
+            (k, v) -> "magic property " + k + " ignored as '" + v + "' is not in valid
ISO pattern"
+        );
+        if (now.isPresent()) {
+            return now.get();
+        }
+
+        now = getNow(
+            MagicNames.TSTAMP_NOW,
+            s -> new Date(1000 * Long.parseLong(s)),
+            (k, v) -> "magic property " + k + " ignored as " + v + " is not a valid number"
+        );
+        if (now.isPresent()) {
+            return now.get();
+        }
+
+        return new Date();
+    }
+
+    /**
+     * Checks and returns a Date if the specified property is set.
+     * @param propertyName name of the property to check
+     * @param map convertion of the property value as string to Date
+     * @param log supplier of the log message containg the property name and value if
+     *     the convertion fails
+     * @return Optional containing the Date or null
+     */
+    protected Optional<Date> getNow(String propertyName, Function<String, Date>
map, BiFunction<String, String, String> log) {
+        String property = getProject().getProperty(propertyName);
+        if (property != null && property.length() > 0) {
             try {
-                return new Date(1000 * Long.parseLong(magicNow));
-            } catch (NumberFormatException ex) {
-                log("magic property " + MagicNames.TSTAMP_NOW + " ignored as "
-                    + magicNow + " is not a valid number");
+                return Optional.ofNullable(map.apply(property));
+            } catch (Exception e) {
+                log(log.apply(propertyName, property));
             }
         }
-        return new Date();
+        return Optional.empty();
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/ant/blob/7a7307bc/src/tests/antunit/taskdefs/tstamp-test.xml
----------------------------------------------------------------------
diff --git a/src/tests/antunit/taskdefs/tstamp-test.xml b/src/tests/antunit/taskdefs/tstamp-test.xml
index c9bebf8..c358262 100644
--- a/src/tests/antunit/taskdefs/tstamp-test.xml
+++ b/src/tests/antunit/taskdefs/tstamp-test.xml
@@ -24,4 +24,21 @@
     <tstamp/>
     <au:assertPropertyEquals name="DSTAMP" value="19700102"/>
   </target>
+
+  <target name="testMagicPropertyIso">
+    <local name="ant.tstamp.now.iso"/>
+    <property name="ant.tstamp.now.iso" value="1972-04-17T08:07:00Z"/>
+    <tstamp/>
+    <au:assertPropertyEquals name="DSTAMP" value="19720417"/>
+  </target>
+
+  <target name="testMagicPropertyBoth">
+    <local name="ant.tstamp.now"/>
+    <local name="ant.tstamp.now.iso"/>
+    <property name="ant.tstamp.now" value="100000"/>
+    <property name="ant.tstamp.now.iso" value="1972-04-17T08:07:22Z"/>
+    <tstamp/>
+    <!-- 'iso' overrides 'simple' -->
+    <au:assertPropertyEquals name="DSTAMP" value="19720417"/>
+  </target>
 </project>


Mime
View raw message