How to show PHP code in TeamCity build

Use case: As a developer I want to see PHP source code in TeamCity build.

It is quite useful sometimes to observe code that used for build and tests of a project.

1. Lets create an observable HTML-presentation of PHP code

We will use a remarkable tool PHP_CodeBrowser. This program generates html-files based on php code, highlight syntax and pack results in a user friendly view. Let’s install PHP_CodeBrowser as a PEAR-extension on our TeamCity server.

pear channel-discover pear.phpqatools.org
pear install --alldeps phpqatools/PHP_CodeBrowser

After installation we get a new program phpcb. Let’s test it in command line:

C:\>phpcb
[Error] Missing log or source argument.
[Error] Missing output argument.

Now we can create an archive from our example php project. Run phpcb with some parameters.

C:\>phpcb 
-s c:\inetpub\wwwroot\php\ConverterPHP2JS 
-o c:\temp\ConverterPHP2JS 
-i c:\inetpub\wwwroot\php\ConverterPHP2JS\tests

Parameters here are: -s PHP-source code, -o html-output, -i ignored folders and files. In the out we will get a user friendly compact code view.

 

2. Make an ant-build task

The following task can be used to create html-code view. It can be added to TeamCity as a separate task or be added somewhere in the build script. In any case we need a special folder to keep the generated results. This folder should be cleaned while every build.

<target name="CodeBrowser php">
  <delete dir="c:\\inetpub\\wwwroot\\codebrowser\\ConverterPHP2JS"/>
  <exec executable="phpcb.bat" failonerror="true">
    <arg value="-s"/>
    <arg value="${basedir}\\"/>
    <arg value="-o"/>
    <arg value="c:\\inetpub\\wwwroot\\codebrowser\\ConverterPHP2JS"/>
    <arg value="-i"/>
    <arg value="${basedir}/gui/extjs,${basedir}/tests"/>
  </exec>
</target>

 

 3. Integration with TeamCity. Add a new tab to build details

First of all we will make that folder we create on the previous step as a build artefact for TeamCity. Go to project properties General Settings -> Artefact paths. Points TeamCity to reate a new artefact (a zip-archive in fact) from that folder where generated html-failes are. We just need to add the following line to artefacts paths:
c:\inetpub\wwwroot\codebrowser\converterphp2js => browser.zip

After the artefact with html files inside is created we can show code browser as a separated tab in TeamCity. Go to Administration->Integrations->Report Tabs->Create new report tab and set parameters for the new tab

Rebuild the project and we get a new tab with browsable php source code.