Return value when internally calling target with phing/phingcall
Asked Answered
S

2

2

I am calling a target by means of phingcall command. I want to pass back a status variable from the called target or at least change the existing value from the calling target. Goal: I want to branch in my main target controlling logic if the sub target fails which I indicate with a property. The code below does not work. Any idea how to make it work or an altertive approach for my goal?

Thanks, Juergen

<target name="main">
    <echo>target a</echo>
    <echo>${bOk}</echo>
    <exec command="echo 1" outputProperty="bOk" />
    <echo>bOk is 1: ${bOk}</echo>
    <phingcall inheritRefs="true" target="sub">
    </phingcall>
    <echo>bOk should now be 0: ${bOk}</echo>
</target>

<target name="sub">
    <echo>target b</echo>
    <echo>bOk is 1: ${bOk}</echo>
    <exec command="echo 0" outputProperty="bOk" />
    <echo>bOk now is 0: ${bOk}</echo>
</target>

The problem here is that

   <echo>bOk should now be 0: ${bOk}</echo>

echos

   bOk should now be 0: 1
Sideslip answered 22/7, 2011 at 8:47 Comment(0)
S
3

Even with the great help of #phing IRC I couldn't solve the problem. I decided to write a custom task to account for data passing between targets:

<?php

require_once "phing/Task.php";

class rvGlobalTask extends Task {

    private static $bOk = 1;
    private $sMode = null;
    private $bValue = null;
    private $outputProperty = null;

    public function setSMode( $sMode ) {
        $this->sMode = $sMode;
    }
    public function setBValue( $bValue ) {
        $this->bValue = $bValue;
    }
    public function setOutputProperty( $outputProperty ) {
        $this->outputProperty = $outputProperty;
    }

    public function main() {
        if ( $this->sMode == "set" ) {
            rvGlobalTask::$bOk = $this->bValue;
        } else {
            $this->project->setProperty(
                $this->outputProperty,
                rvGlobalTask::$bOk
            );
        }
    }
}
?>

This works fine for my problem. Perhaps someone else finds this useful as well.

Sideslip answered 22/7, 2011 at 19:3 Comment(1)
For clarity, here is the setter: <rv sMode="set" bValue="${bOk}" /> and the getter: <rv sMode="get" outputProperty="bOk" />Sideslip
P
2

Here's how you use an ExecTask to capture output.

<?xml version="1.0" encoding="UTF-8"?>
<project name="example" default="check-composer">

    <!-- set a property to contain the output -->
    <property name="whichComposer" value="" />

    <!-- check if composer (getcomposer.org) is installed globally -->
    <target name="check-composer">
        <!-- put the output of "which" in our property -->
        <exec command="which composer" outputProperty="whichComposer" />

        <!-- act on what we found out -->
        <if>
            <contains string="${whichComposer}" substring="composer" />
            <then>
                <echo>Composer installed at ${whichComposer}</echo>
            </then>
            <else>
                <echo message="better install composer. ${whichComposer}"/>
            </else>
        </if>
    </target>

</project>
Poling answered 14/9, 2012 at 21:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.