如何从phing目标返回值?

如何从phing目标返回值?,phing,Phing,我希望有这样一个foreach任务,它迭代目录“a”中的所有文件/目录- 可以在生成文件中访问 我有一个目标,检查我的生产代码库是否与预期的git版本有任何差异- <target name="compare_prod_with_expected_revision"> <input propertyname="box.git_version"> Enter git version of the production codebase:

我希望有这样一个foreach任务,它迭代目录“a”中的所有文件/目录-

可以在生成文件中访问

我有一个目标,检查我的生产代码库是否与预期的git版本有任何差异-

<target name="compare_prod_with_expected_revision">
        <input propertyname="box.git_version">
            Enter git version of the production codebase:
        </input>
        <exec command="git reset --hard ${box.git_version}" dir="${dir.scratchpad}" />
        <!-- Scratchpad brought to revision ${box.git_version} -->

        <echo>Verifying whether production is at revision ${box.git_version}..</echo>
        <exec command="diff -arq --exclude='.git' ${dir.scratchpad}/${dir.subdir} ${dir.destination}/${dir.subdir}" outputProperty="diffList"/><!-- #TODO ignore.swp files in this step. Diff says .swp files present in production code. But doing ls -a there does not show the same. -->
        <php function="strlen" returnProperty="productionDeviationFromExpectedBranch"><!-- #TODO - find how to not show this step during build process. Put it in a target and set hidden="true" -->
            <param value="${diffList}"/>
        </php>
        <if>
            <equals arg1="${productionDeviationFromExpectedBranch}" arg2="0" />
            <then>
                <echo>Verified production is at revision ${box.git_version}</echo>
            </then>
            <else>
                <echo>Differences -  </echo>
                <echo>${diffList}</echo>
            </else>
        </if>
    </target>

输入生产代码库的git版本:
正在验证产品是否位于修订版${box.git_version}。。
已验证的产品版本为${box.git_version}
差异-
${diffList}

现在,我想
phingcall
这个目标,并想访问它设置的一些属性。

我想我理解你的目的,同时我觉得你选择的不是做这件事的最佳工具

正如您在问题中提到的,关于phing的官方文件明确了任务(目标):

目标是项目组件(但不是其他目标)的集合,这些组件在其项目中指定了唯一的名称。目标通常执行特定任务,或者调用执行特定任务的其他目标,因此目标有点像函数(但目标没有返回值)

目标应该是应用程序的组件,它们执行特定的任务、原子任务。它可以是初始化任务、配置获取、编译步骤、资产准备和转储、部署任务、清理任务等。目标没有标准意义上的“输出”,但目标执行的结果是执行本身的成功:成功或失败。

我们不应该试图在这样的项目目标中加入太多的逻辑,因为不打算做复杂的计算,做繁重的逻辑决策等等。我的意思是,Phing可以做到,这样的事情是可能的,但这种设置会很庞大,不可读,并且很难扩展/重新考虑

使用Phing,您可以轻松定义条件执行和逻辑流的分支,您可以定义任务的执行顺序(依赖项)——这就是它简洁而优雅的原因。使目标尽可能简单,将项目分割成小的、已完成的逻辑任务

根据我所从事的项目,最大的目标可能是初始化阶段和配置抓取。下面是一些示例,为了理解它可能包含的内容,我从真实项目中选取了它:

<target name="init_configuration">
    <echo msg="Define initial configuration for the deployment..." />
    <if>
        <not>
            <isset property="host" />
        </not>
        <then>
            <property name="host" value="dev" override="true" />
            <echo message="The value of hostname has been set to ${host}" />
        </then>
        <else>
            <echo message="The value of hostname is ${host}" />
        </else>
    </if>
    <if>
        <not>
            <isset property="version" />
        </not>
        <then>
            <property name="version" value="1.0.0" override="true" />
            <echo message="The value of version has been set to ${version}" />
        </then>
        <else>
            <echo message="The value of version is ${version}" />
        </else>
    </if>

    <property name="host_credital_file" value="config/hosts/${host}.properties" />
    <property file="${host_credital_file}" />
    <available file="${host_credital_file}" property="hostfilefound" value="true"/>
    <fail unless="hostfilefound" message="Missing Hostfile configuration file (${host_credital_file})!" />

    <echo msg="Configuration is done" />
</target>

其他目标是极其简单的,它们通常是–1-5行长
,并且只做小用途、小任务。这可能是与Phing合作时的最佳建议


你试图把这个逻辑放在Phing的肩膀上是可能的,但它会非常庞大

考虑一下要点:在您的示例中,使用简单的bash脚本可以更快、更容易、更可读地完成相同的事情。甚至可以用PHP编写小型CLI实用程序,这样做既优雅又快速。之后,在Phing中,您将保留参数化目标,该目标将从CLI执行此“修订差异脚本”

Phing对于它的设计目的来说是一个很好的工具,但它不可能是所有用途的最佳选择。只是不要把太多的责任和逻辑放在其中

作为一个解决方案,对于更复杂的事情最好将Phing与一些专门的东西结合起来:bash脚本、phpcli、nodeJS(+Grunt、Gulp等)。。。只需在以后添加Phing目标的呼叫

目标“子任务”应检查文件/文件夹的对应项是否存在于另一个目录“B”(我基本上比较的是目录A和B),如果不存在,则返回以下任一项-

  • 旗帜
  • 文件名
您可以比较两个目录,而无需像下面这样使用
foreach
任务:

<phingcall target="--console-return-property">
    <property name="command" value="ps auxwww"/>
    <property name="checkreturn" value="true"/>
    <property name="logoutput" value="false"/>
    <property name="outputProperty" value="ps_output"/>
</phingcall>

这是我设法使目标具有类似函数的行为的方法:

<target name="-console-cmd-return-property" hidden="true">
    <exec command="${command}" checkreturn="${checkreturn}" logoutput="${logoutput}" outputProperty="${outputProperty}"/>
</target>

它的调用方式如下:

<phingcall target="--console-return-property">
    <property name="command" value="ps auxwww"/>
    <property name="checkreturn" value="true"/>
    <property name="logoutput" value="false"/>
    <property name="outputProperty" value="ps_output"/>
</phingcall>


当然,它之所以有效,是因为它依赖于现有的
exec
,而且它不是通用的…

如果您只想捕获输出,您可以使用这里提到的ExecTask来实现这一点。希望这对您有所帮助。@RameshDahiya,我想获取变量中目标的输出,而不是exectask的输出。我想在函数中有一个重复的逻辑。。
<target name="-console-cmd-return-property" hidden="true">
    <exec command="${command}" checkreturn="${checkreturn}" logoutput="${logoutput}" outputProperty="${outputProperty}"/>
</target>
<phingcall target="--console-return-property">
    <property name="command" value="ps auxwww"/>
    <property name="checkreturn" value="true"/>
    <property name="logoutput" value="false"/>
    <property name="outputProperty" value="ps_output"/>
</phingcall>