Question

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.

Was it helpful?

Solution

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}" />

`

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top