Skip to content

Commit

Permalink
Extended tstamp task. (#995)
Browse files Browse the repository at this point in the history
  • Loading branch information
siad007 authored and mrook committed Nov 20, 2018
1 parent 4732b93 commit b9ccb58
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 9 deletions.
8 changes: 4 additions & 4 deletions classes/phing/tasks/system/TstampCustomFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ public function setTimezone($timezone)
* @param TstampTask $tstamp reference to task
* @throws BuildException
*/
public function execute(TstampTask $tstamp)
public function execute(TstampTask $tstamp, $d, $location)
{
if (empty($this->propertyName)) {
throw new BuildException("property attribute must be provided");
throw new BuildException("property attribute must be provided", $location);
}

if (empty($this->pattern)) {
throw new BuildException("pattern attribute must be provided");
throw new BuildException("pattern attribute must be provided", $location);
}

$oldlocale = "";
Expand All @@ -76,7 +76,7 @@ public function execute(TstampTask $tstamp)
date_default_timezone_set($this->timezone);
}

$value = strftime($this->pattern);
$value = strftime($this->pattern, $d);
$tstamp->prefixProperty($this->propertyName, $value);

if (!empty($this->locale)) {
Expand Down
41 changes: 37 additions & 4 deletions classes/phing/tasks/system/TstampTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,19 @@ public function addFormat(TstampCustomFormat $cf)
*/
public function main()
{
$d = $this->getNow();

foreach ($this->customFormats as $cf) {
$cf->execute($this);
$cf->execute($this, $d, $this->getLocation());
}

$dstamp = strftime('%Y%m%d');
$dstamp = strftime('%Y%m%d', $d);
$this->prefixProperty('DSTAMP', $dstamp);

$tstamp = strftime('%H%M');
$tstamp = strftime('%H%M', $d);
$this->prefixProperty('TSTAMP', $tstamp);

$today = strftime('%B %d %Y');
$today = strftime('%B %d %Y', $d);
$this->prefixProperty('TODAY', $today);
}

Expand All @@ -90,4 +92,35 @@ public function prefixProperty($name, $value)
{
$this->getProject()->setNewProperty($this->prefix . $name, $value);
}

protected function getNow(): int
{
$property = $this->getProject()->getProperty('phing.tstamp.now.iso');

if ($property !== null && $property !== '') {
try {
$dateTime = new DateTime($property);
} catch (Exception $e) {
$this->log('magic property phing.tstamp.now.iso ignored as ' . $property . ' is not a valid number');
$dateTime = new DateTime();
}

return $dateTime->getTimestamp();
}

$property = $this->getProject()->getProperty('phing.tstamp.now');

$dateTime = (new DateTime())->getTimestamp();

if ($property !== null && $property !== '') {
$dateTime = DateTime::createFromFormat('U', $property);
if ($dateTime === false) {
$this->log('magic property phing.tstamp.now ignored as ' . $property . ' is not a valid number');
} else {
$dateTime = $dateTime->getTimestamp();
}
}

return $dateTime;
}
}
6 changes: 5 additions & 1 deletion docs/guide/en/source/appendixes/coretasks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4472,7 +4472,11 @@ file defines a task in the format: name=path.to.Task -->
filenames, or used to replace placeholder tags inside documents to indicate, for
example, the release date. The best place for this task is probably in an initialization
target.</para>

<para>the magic property phing.tstamp.now 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 phing.tstamp.now.iso you could also specify that value in DateTime compatible
format. If you specify a value in an invalid format an INFO message will be logged and the value will be
ignored.</para>
<table>
<title>Attributes</title>
<tgroup cols="5">
Expand Down
72 changes: 72 additions & 0 deletions test/classes/phing/tasks/system/TstampTaskTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://phing.info>.
*/

/**
* Tests the Tstamp Task
*
* @author Siad Ardroumli <siad.ardroumli@gmail.com>
* @package phing.tasks.system
*/
class TstampTaskTest extends BuildFileTest
{
/** @var TstampTask */
private $tstamp;

public function setUp()
{
$this->configureProject(
PHING_TEST_BASE . '/etc/tasks/system/TstampTest.xml'
);

$this->tstamp = new TstampTask();
$this->tstamp->setProject($this->project);
}

public function testMagicProperty()
{
$this->executeTarget(__FUNCTION__);
$this->assertPropertyEquals('DSTAMP', 19700102);
}

public function testMagicPropertyIso()
{
$this->executeTarget(__FUNCTION__);
$this->assertPropertyEquals('DSTAMP', 19720417);
}

public function testMagicPropertyBoth()
{
$this->executeTarget(__FUNCTION__);
$this->assertPropertyEquals('DSTAMP', 19720417);
}

public function testMagicPropertyIsoCustomFormat()
{
$this->executeTarget(__FUNCTION__);
$this->assertPropertyEquals('tstamp.test', '1972-04-17');
}

public function testPrefix()
{
$this->tstamp->setPrefix('prefix');
$this->tstamp->main();
$prop = $this->project->getProperty('prefix.DSTAMP');
$this->assertNotNull($prop);
}
}
43 changes: 43 additions & 0 deletions test/etc/tasks/system/TstampTest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
~ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
~ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
~ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
~ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
~ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
~ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
~ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
~ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
~ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
~ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
~
~ This software consists of voluntary contributions made by many individuals
~ and is licensed under the LGPL. For more information please see
~ <http://phing.info>.
-->

<project name="TstampTest" default="testMagicProperty">
<target name="testMagicProperty">
<property name="phing.tstamp.now" value="100000"/>
<tstamp/>
</target>

<target name="testMagicPropertyIso">
<property name="phing.tstamp.now.iso" value="1972-04-17T08:07:00Z"/>
<tstamp/>
</target>

<target name="testMagicPropertyIsoCustomFormat">
<property name="phing.tstamp.now.iso" value="1972-04-17T08:07:00Z"/>
<tstamp>
<format property="tstamp.test" pattern="%Y-%m-%d" />
</tstamp>
</target>

<target name="testMagicPropertyBoth">
<property name="phing.tstamp.now" value="100000"/>
<property name="phing.tstamp.now.iso" value="1972-04-17T08:07:22Z"/>
<tstamp/>
</target>
</project>

0 comments on commit b9ccb58

Please sign in to comment.