Phing, call a command get its output into a property
Asked Answered
S

4

7

I have a script that can lookup and output or write my current release # to a text file. Now the only problem is how do I get this version number into a PHING property.

Right now my PHING target builds build.zip and built.tar, I would like it to build build-1.0.0.zip or whatever the version script decides the current version is. How can I do this? Will I have to create my own "task"?

Strengthen answered 2/12, 2009 at 23:40 Comment(0)
O
7

You might need to create your own task for this. The task may look something like...

<?php
require_once "phing/Task.php";

class VersionNumberTask extends Task
{
    private $versionprop;

    public function setVersionProp($versionprop)
    {
        $this->versionprop = $versionprop;
    }

    public function init()
    {
    }

    public function main()
    {
        // read the version text file in to a variable
        $version = file_get_contents("version.txt");
        $this->project->setProperty($this->versionprop, $version);
    }
}

Then you would define the task in your build xml

<taskdef classname="VersionNumberTask" name="versiontask" />

Then call the task

<target name="dist">
    <versiontask versionprop="version.number"/>
</target>

At this point, you should be able to access the version number using ${version.number} throughout your build xml.

Hope this helps!

Outofdate answered 4/12, 2009 at 3:31 Comment(0)
R
15

An alternative approach is to use the outputProperty attribute on the ExecTask to provide a property in your build file.

<target name="version">
  <exec command="cat version.txt" outputProperty="version.number" />
  <echo msg="Version: ${version.number}" />
</target>

More information

Ricardaricardama answered 6/4, 2011 at 13:54 Comment(0)
O
7

You might need to create your own task for this. The task may look something like...

<?php
require_once "phing/Task.php";

class VersionNumberTask extends Task
{
    private $versionprop;

    public function setVersionProp($versionprop)
    {
        $this->versionprop = $versionprop;
    }

    public function init()
    {
    }

    public function main()
    {
        // read the version text file in to a variable
        $version = file_get_contents("version.txt");
        $this->project->setProperty($this->versionprop, $version);
    }
}

Then you would define the task in your build xml

<taskdef classname="VersionNumberTask" name="versiontask" />

Then call the task

<target name="dist">
    <versiontask versionprop="version.number"/>
</target>

At this point, you should be able to access the version number using ${version.number} throughout your build xml.

Hope this helps!

Outofdate answered 4/12, 2009 at 3:31 Comment(0)
U
5

An alternative approach that works on both Windows and Linux.

<exec executable="php" outputProperty="version.number">
    <arg value="-r" />
    <arg value="$fh=file('version.txt'); echo trim(array_pop($fh));" />
</exec>
<echo msg="Current version is: ${version.number}"/>

Assumes the last line of the file is simply the version number, and if you would like to update the version number in the file. Try this.

<propertyprompt propertyName="release_version" defaultValue="${version.numver}" promptText="Enter version to be released."/>
<exec executable="php">
    <arg value="-r" />
    <arg value="$file=file_get_contents('version.txt'); $file = str_replace('${version.number}', '${release_version}', $file); file_put_contents('version.txt', $file);" />
</exec>
<echo msg="Version number updated." />
<property name="version.number" value="${release_version}" override="true" />
Utopianism answered 9/4, 2012 at 19:28 Comment(1)
Copied from the CakePHP build script.Utopianism
M
1

Also alternative and best way (my opinion) that works on both Windows and Linux it is use native task LoadFileTask

<loadfile property="myVersion" file="version.txt" />
<echo msg="Current version is: ${myVersion}"/>

also you can use filterchain

<loadfile property="myVersion" file="version.txt">
    <filterchain><striplinebreaks /></filterchain>
</loadfile>
<echo msg="Current version is: ${myVersion}"/>

More information

Moyna answered 26/11, 2013 at 18:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.