How to return a value from phing ad-hoc task?
Asked Answered
P

1

6

Is there any way to get a return value from a phing ad-hoc task?

For example, I'm trying to get the version number from a JSON string in a file as follows:

    <target name="get-app-version">

    <adhoc-task name="appversion" ><![CDATA[
        class AppversionTask extends Task {

            private $version;

            public function getVersion() {
                return $this->version;
            }
            function main() {
                $manifest = file_get_contents("manifest.json");
                $manifest_json = json_decode($manifest);
                $version = $manifest_json->version;
                $this->log("App version: " . $version);
                $this->version = $version;
            }
        }
    ]]></adhoc-task>
    <appversion output="version" />
    <echo message="${version}" />

</target>

I can only find documentation on setting values, but not getting values. However, the adhoc typdef task seems to show a get syntax, so I'm wondering if there is some way to do this.

Prescience answered 25/5, 2013 at 0:30 Comment(0)
N
12

I am not sure if I understand completely. It sounds like, rather than setting

$this->version

you should instead call

$this->project->setProperty('version', $version);

This will add the 'version' property to your project instance. You won't need to have to set the attribute for your task, unless say, you will want to later change what property name in your project gets set (from 'version' to some other property).

`

<adhoc-task name="appversion" ><![CDATA[
    class AppversionTask extends Task {

        function main() {
            $manifest = file_get_contents("manifest.json");
            $manifest_json = json_decode($manifest);
            $version = $manifest_json->version;
            $this->log("App version: " . $version);
            $this->project->setProperty('version', $version);
        }
    }
]]></adhoc-task>
<appversion />
<!-- The version property should now be set -->
<echo message="${version}" />

`

Noose answered 28/7, 2013 at 5:48 Comment(1)
Wow...I searched all over and couldn't find the ->project part in any documents. That's exactly what I was looking for.Prescience

© 2022 - 2024 — McMap. All rights reserved.