【问题标题】:How to beautify the output of phing?如何美化phing的输出?
【发布时间】:2012-07-27 09:25:24
【问题描述】:

Phing,默认情况下,甚至使用任何内置记录器(phing.listener.NoBannerLogger、phing.listener.AnsiColorLogger、phing.listener.XmlLogger 和 phing.listener.HtmlColorLogger)都有非常详细的输出。

我的用例是使用 Phing 作为预提交挂钩来运行测试。因此,我不关心日志 phing 中可能提供给我的所有信息。 我只是将它用作运行测试的多平台工具。

例子:

Buildfile: /private/var/workspace/www/me_com/build.xml

SBKSWWW > main:

   [delete] Deleting /private/var/workspace/www/me_com/temp/pre-commit-hook/changed_files
   [delete] Deleting directory /private/var/workspace/www/me_com/temp/pre-commit-hook
    [mkdir] Created dir: /private/var/workspace/www/me_com/temp/pre-commit-hook
  [phplint] Parse error: parse error in ./www/MyTest.php on line 2
[phpcodesniffer] 2 files where checked
[phpcodesniffer] No syntax errors detected

BUILD FINISHED

Total time: 0.3430 seconds

其中许多行对于我的用例来说确实是多余且无用的。实际上我什至不运行原始含义的“构建”。

我想让 phing 日志看起来像这样:

 ✔ Commited code matches coding standards
 ✘ Commited code has syntax errors!
   Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE in MyTest.php on line 2

如果您认为我出于自己的目的使用了不好的工具,也请告诉我,我很高兴知道还有别的东西。

【问题讨论】:

    标签: php phing pre-commit-hook


    【解决方案1】:

    从 Phing 2.11.0 开始,您将使用 phing.listener.SilentLogger

    【讨论】:

      【解决方案2】:

      您可以尝试使用phing.listener.XmlLogger 并将其通过xsltproc 与您自己的样式表一起使用。

      给定一个基本的build.xml 文件,它只会对您的 PHP 进行 lints:

      <?xml version="1.0" ?>
      <project name="Example" basedir=".">
      
          <target name="lint" description="PHP syntax check">
              <phplint>
                  <fileset dir="src">
                      <include name="**/*.php"/>
                  </fileset>
              </phplint>
          </target>
      
      </project>
      

      加上parse.xsl:

      <?xml version="1.0" encoding="UTF-8"?>
      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      
          <xsl:output method="text"
              omit-xml-declaration="yes"/>
      
          <xsl:template match="/build">
              <xsl:variable name="newline"><xsl:text>
      </xsl:text></xsl:variable>
      
              <xsl:for-each select="target">
                  <xsl:text>Task: </xsl:text>
                  <xsl:value-of select="concat(@name, $newline)" />
      
                  <xsl:choose>
                      <xsl:when test="task/message[@priority = 'error']">
                          <xsl:value-of select="concat(task/message[@priority = 'error'], $newline)"/>
                      </xsl:when>
                      <xsl:otherwise>
                          <xsl:text>No errors</xsl:text>
                          <xsl:value-of select="$newline" />
                      </xsl:otherwise>
                  </xsl:choose>
              </xsl:for-each>
          </xsl:template>
      
      </xsl:stylesheet>
      

      phing -logger phing.listener.XmlLogger lint | xsltproc parse.xsl - 调用会给你一些干净的东西,比如:

      Task: lint
      Fatal error: Only variables can be passed by reference in Request.class.php on line 128
      

      【讨论】:

        【解决方案3】:

        你在第 2 行 mytest 上有一个语法错误

        它基本上是在传递不适当的代码或没有像 {}

        那样正确关闭时出现

        你能发布 mytest 的第 2 行吗

        【讨论】:

        • 您是否阅读了整个问题?我不是在问错误,而是在问工具。错误是故意存在的。