【问题标题】:Checking for existence of a file type in phing检查 phing 中是否存在文件类型
【发布时间】:2014-05-29 15:31:39
【问题描述】:

我正在 Phing 中编写构建脚本,并希望有一个 if 语句来测试目录中是否存在具有给定扩展名的任何文件(在本例中为 .sql 文件)。我尝试使用<available> 执行此操作,如下所示,但没有运气(<then> 永远不会被执行。有人有什么想法吗?

<if>
     <available file="${build.pendingsql}/*" type="file" extension="sql"  />
     <then>

         <!-- Do stuff -->

     </then>
</if>

【问题讨论】:

    标签: php phing


    【解决方案1】:

    你可以做类似的事情(根据你的确切需要调整它, ls 在当前目录中运行):

    <?xml version="1.0" encoding="UTF-8"?>
    <project name="test" default="check">
        <target name="check">
            <exec checkreturn="true" command="ls *.sql" outputProperty="list"/>
            <if>
                <equals arg1="${list}" arg2="" />
                <then>
                    <echo msg="not found"/>
                </then>
                <else>
                    <echo msg="found ${list}"/>
                </else>
            </if>
        </target>
    </project>
    

    【讨论】:

    • 不幸的是 ls *.sql 不返回 "" 更糟糕的是,在 Mac 上它退出 >0。非常感谢,你让我走上了正确的道路......
    【解决方案2】:

    在我的 Mac 上,我最终使用了以下内容:

    <!-- Are there sql files in pending -->
    <exec checkreturn="false" dir="${build.pendingsql}" command="ls *.sql" outputProperty="list"/>
    <echo msg="${list}"/>
    <if>
        <equals arg1="${list}" arg2="ls: *.sql: No such file or directory" />
        <then>
            <echo msg="No SQL patches found"/>
        </then>
        <else>
            <echo msg="SQL patches found"/>
        </else>
    </if>
    

    Bolek 发布的解决方案的主要变化是 checkreturn 需要为 false 以说明 ls 退出 >0 并将 equals 测试更改为“ls: *.sql: No such file or directory”。我意识到这有点 hacky 并且可能会中断(基于输出不同“No such..”消息的语言/系统)但它对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-25
      • 1970-01-01
      • 1970-01-01
      • 2012-02-05
      相关资源
      最近更新 更多