Jump To …

build.xml

<?xml version="1.0" encoding="UTF-8"?>

Phing Drupal Template is a XML build file for the Phing build system with targets and configuration designed for Drupal projects.

The aim of the project is to provide a build file which can be used with a contiuous integration server to ensure clean code - check for compilation errors, run unit tests, lint code and verify that code standards are upheld.

The template has been developed with the Jenkins job template for Drupal projects and the Drupal Jenkins demo. It may work with other continuous integration systems.

Usage

The template can be used in several ways:

  • As a starting point for your own build files. Download build.xml to the root directory of your Drupal project and modify it as needed.

  • As the actual build file for your project. You can configure most aspects of the build according to your project through a properties file. This works whether you have your entire Drupal project in VCS or use drush_make. Download the entire project and place it in a subfolder or use it as a git submodule. Using the build subfolder from the root directory is recommended. Copy the build.default.properties file to the root directory, rename it build.properties and modify it according to your project. Leave out properties where you want to use the default values.

Requirements

Using the template requires a range of PEAR packages present. They can be installed as follows:

pear channel-discover pear.phing.info
pear channel-discover pear.pdepend.org
pear channel-discover pear.phpmd.org
pear channel-discover pear.phpunit.de
pear channel-discover components.ez.no
pear channel-discover pear.symfony-project.com

pear install phing/phing-2.4.12
pear install  -a phpmd/PHP_PMD
pear install phpunit/phpcpd
pear install phpunit/phploc
pear install PHPDocumentor
pear install PHP_CodeSniffer
pear install HTTP_Request2
pear install -a phpunit/PHP_CodeBrowser

The following must be available from the command line:

Credits

The build file is heavily inspired by Sebastian Bergmans wonderful template for Jenkins Jobs for PHP Projects and began as a Phing port of his suggested Apache Ant build file.

Many of the same tools are used: phploc, phpcpd, PHPMD, phpDocumentor and PHP_CodeBrowser.

A couple of additional external tools are downloaded and used during the build process:

  • Phing Drush Task: A custom task for running drush from Phing
  • Phing PHPLoc: A custom task for running phploc from Phing
  • jslint4java: Supports execution of jslint from the command line
  • jslintĀ“: A more humane version of Douglas Crockfords JSLint.
  • csslint: A tool to help point out problems with CSS code.
  • Mozilla Rhino: An implementation of Javascript in Java.
  • Coder: Drupal module for performing code reviews.

This documentation has been generated by phrocco - a PHP port of the literate documentation generator Docco.

Contributors

The Phing Drupal template is developed by Reload! - a Drupal development agency located in Copenhagen, Denmark.

Drush Make and Simpletest support has been sponsored by DBC.

Fork me on GitHub

Do not use any later versions than 2.4.12 due to a bug in Phing.

<project name="phing-drupal" default="build" phingVersion="2.4.11">

Main targets

Build project

Do a complete build of the project by verifying code consistency and and performing static analysis of the code.

This is the default build target.

Do not run docs for now. Generating documentation eats up memory and build time and is largely obsolete when using GitHub.

  <target name="build" depends="init,
                                clean,
                                verify,
                                analyze" />

Verify code consistency

Make sure that our code is clean and functional.

  <target name="verify" depends="init,
                                 clean,
                                 lint-php-custom,
                                 lint-js-custom,
                                 lint-css-custom,
                                 check-php-debug,
                                 check-js-debug,
                                 simpletest"/>

Analyze code

Perform static analysis of the code to generate statistics, identify potential problems and opportunities for refactorings and make sure that the code complies with coding standards.

  <target name="analyze" depends="init,
                                  clean,
                                  phploc,
                                  phpmd,
                                  phpcpd">

Moved inside target to support properties in target name

    <phingcall target="coder-review-d${drupal.version}">

Run the target as if it was executed from the Drupal root directory.

      <property name="project.basedir" value="${project.drupal.dir}"/>

We already have a clean environment so avoid further cleaning.

      <property name="project.cleaned" value="1"/>

Make will run again as a part of the init target. We already have a working site so skip that.

      <property name="project.make.skip" value="1"/>
    </phingcall>
  </target>

Generate documentation

Generate HTML documentation and code browser for the project.

  <target name="docs" depends="init,
                               clean,
                               phpdoc,
                               phpcb" />

Individual targets

These targets can be executed individually or grouped unless explicitly stated as a part of the task.

As a rule targets without descriptions should not be executed directly.

PHP linting

Check files for syntax errors.

  <target name="lint-php"
          description="Check all PHP files for syntax errors using PHPLint"
          depends="init">
    <phplint haltonfailure="true">
      <fileset refid="src.php" />
    </phplint>
  </target>

  <target name="lint-php-custom"
          description="Check custom PHP files for syntax errors using PHPLint"
          depends="init">
    <phplint haltonfailure="true">
      <fileset refid="src.php.custom" />
    </phplint>
  </target>

Javascript linting

Checks code against jslint to assure a coding standard is followed and detect potential problems. By default jslintĀ“ is used.

  <target name="lint-js"
          description="Check all Javascript files using JSlint"
          depends="init, setup-jslint4java, setup-jslint">
    <foreach target="jslint-file" param="filename" absparam="absfilename">
      <fileset refid="src.js"/>
    </foreach>
  </target>

  <target name="lint-js-custom"
          description="Check custom Javascript files using JSlint"
          depends="init, setup-jslint4java, setup-jslint">
    <foreach target="jslint-file" param="filename" absparam="absfilename">
      <fileset refid="src.js.custom"/>
    </foreach>
  </target>

jslint a file

No need to run init or setup targets here. This target should only be called from parent lint-js targets.

  <target name="jslint-file">
    <echo>Linting file: ${absfilename}</echo>

Execute jslint4java and return the result in checkstyle format

    <exec command="java -jar ${jslint4java.file}
                   --jslint ${jslint.file} --report checkstyle ${absfilename}"
          outputProperty="report" />

Print the result to a file. Replace / with - in path to create legal filenames in the format checkstyle-jslint-dir1-dir2-file.js.xml.

    <php function="str_replace" returnProperty="filename.normalized">
      <param value="/" />
      <param value="-" />
      <param value="${filename}" />
    </php>
    <delete file="${project.logdir}/checkstyle-jslint-${filename.normalized}.xml" />
    <append text="${report}"  destFile="${project.logdir}/checkstyle-jslint-${filename.normalized}.xml" />
  </target>

Setup jslint4java

  <target name="setup-jslint4java"
          depends="init"
          unless="project.jslint4java.setup">
    <property name="jslint4java.dir"
              value="${project.toolsdir}/jslint4java" />
    <php function="basename" returnProperty="jslint4java.basename">
      <param value="${jslint4java.url}" />

We assume that the version of jslint4java used is a distribution where the filename ends in -dist.zip

      <param value="-dist.zip" />
    </php>

Download and unpack jslint4java

    <mkdir dir="${jslint4java.dir}" />
    <php function="basename" returnProperty="jslint4java.zipfile">
      <param value="${jslint4java.url}" />
    </php>
    <httpget url="${jslint4java.url}"
             dir="${jslint4java.dir}"
             proxy="${phing.httpget.proxy}" />
    <unzip file="${jslint4java.dir}/${jslint4java.zipfile}"
           todir="${jslint4java.dir}" />

Other targets use this property to determine the location of the jslint4java.jar file

    <property name="jslint4java.file"
 value="${jslint4java.dir}/${jslint4java.basename}/${jslint4java.basename}.jar"/>

Set property to prevent unnecessary additional invocations of this target

    <property name="project.jslint4java.setup" value="true" />
  </target>

Setup jslint

  <target name="setup-jslint"
          depends="init"
          unless="project.jslint.setup">
    <phingcall target="setup-git-repo">
      <property name="repo.dir" value="${project.toolsdir}/jslint"/>
      <property name="repo.url" value="${jslint.repository.url}" />
    </phingcall>

Other targets use this property to determine the location of the jslint file

    <property name="jslint.file"
              value="${project.toolsdir}/jslint/${jslint.file}"
              override="true" />

Set property to prevent unnecessary additional invocations of this target

    <property name="project.jslint.setup" value="true" />
  </target>

CSS linting

Checks code against CSS Lint to help point out problems with CSS code. It does basic syntax checking as well as applying a set of rules to the code that look for problematic patterns or signs of inefficiency.

  <target name="lint-css"
          description="Check all CSS files using CSS Lint"
          depends="init, setup-rhino, setup-csslint">
    <foreach target="csslint-file" param="filename" absparam="absfilename">
      <fileset refid="src.css"/>
    </foreach>
  </target>

  <target name="lint-css-custom"
          description="Check custom CSS files using CSS Lint"
          depends="init, setup-rhino, setup-csslint">
    <foreach target="csslint-file" param="filename" absparam="absfilename">
      <fileset refid="src.css.custom"/>
    </foreach>
  </target>

csslint a file

No need to run init or setup targets here. This target should only be called from parent lint-css targets.

  <target name="csslint-file">
    <echo>Linting file: ${absfilename}</echo>

Run csslint through Rhino and return the result in checkstyle format

    <exec command="java -jar ${rhino.jar} ${csslint.rhino.file}
                   --format=checkstyle-xml --rules=${csslint.rules}
                   ${absfilename}"
          outputProperty="report" />

Print the result to a file. Replace / with - in path to create legal filenames in the format checkstyle-csslint-dir1-dir2-file.css.xml.

    <php function="str_replace" returnProperty="filename.normalized">
      <param value="/" />
      <param value="-" />
      <param value="${filename}" />
    </php>

    <property name="csslint.report.file" value="${project.logdir}/checkstyle-csslint-${filename.normalized}.xml" />
    <delete file="${csslint.report.file}" />
    <append text="${report}"  destFile="${csslint.report.file}" />

Cleanup the break rules property. Hyphens are removed to support both input and output rule format. csslint-rule becomes CsslintRule. Seperators (Commas and multiple whitespace characters) are reduced to a pipe to be used in a regular expression.

         <php expression="str_replace('-', '', '${csslint.rules.break}')"
              returnProperty="csslint.rules.break"/>
         <php expression="preg_replace('/(\s+|\s*,\s*)/', '|', '${csslint.rules.break}')"
              returnProperty="csslint.rules.break"/>

If any rules which require the build to break are defined then look for them.

    <if>
      <not>
        <equals arg1="${csslint.rules.break}" arg2="" />
      </not>
      <then>

CSS Lint reports checkstyle errors using the format net.csslint.RuleName. Load all checkstyle reports and look for errors with such a source from the provided rules.

        <loadfile property="csslint.break.errors"
                  file="${csslint.report.file}">
          <filterchain>
            <linecontainsregexp>
                <regexp pattern="(net\.csslint\.(${csslint.rules.break}))"
                        ignoreCase="true" />
              </linecontainsregexp>
          </filterchain>
        </loadfile>

Break if any errors from the provided rules are detected!

        <if>
          <not>
            <equals arg1="${csslint.break.errors}" arg2="" />
          </not>
          <then>
            <fail message="CSS error detected in file ${absfilename}" />
          </then>
        </if>
      </then>
    </if>
  </target>

Setup csslint

   <target name="setup-csslint"
           depends="init"
           unless="project.csslint.setup">
     <phingcall target="setup-git-repo">
       <property name="repo.dir"
                 value="${project.toolsdir}/csslint"/>
       <property name="repo.url"
                 value="${csslint.repository.url}" />
       <property name="repo.revision"
                 value="${csslint.repository.revision}" />
     </phingcall>

Other targets use this property to determine the location of the csslint rhino file

     <property name="csslint.rhino.file"
               value="${project.toolsdir}/csslint/release/csslint-rhino.js" />

Set property to prevent unnecessary additional invocations of this target

     <property name="project.csslint.setup" value="true" />
   </target>

Debug code detection

Code should not call functions which are usually used for debugging. This belongs on developer environments - not VCS. This goes for mentioning them in comments as well.

  <target name="check-php-debug"
          description="Check custom PHP code for debug statements"
          depends="init">
    <phingcall target="check-debug">
      <property name="debug.language" value="PHP" override="true" />
      <property name="debug.pattern" value="(var_dump\(|dsm\(|dpm\()"
                override="true" />
      <property name="debug.fileset" value="src.php.custom"/>
    </phingcall>
  </target>

  <target name="check-js-debug"
          description="Check custom Javascript code for debug statements">
    <phingcall target="check-debug">
      <property name="debug.language" value="Javascript" override="true" />
      <property name="debug.pattern" value="(console\.log\()" override="true" />
      <property name="debug.fileset" value="src.js.custom"/>
    </phingcall>
  </target>

Check a fileset for debug code

  <target name="check-debug"
          depends="init">
    <php function="strtolower" returnProperty="debug.language.lower">
      <param value="${debug.language}" />
    </php>
    <property name="debug.output"
              value="${project.logdir}/debug_${debug.language.lower}.txt"
              override="true" />
    <delete file="${debug.output}"/>
    <append text="" destFile="${debug.output}" />

    <foreach target="check-debug-file" param="filename"
             absparam="absfilename">
      <fileset refid="${debug.fileset}"/>
    </foreach>

    <loadfile property="debug.lines" file="${debug.output}" />

Break if debug code is detected!

    <if>
      <not>
        <equals arg1="${debug.lines}" arg2="" />
      </not>
      <then>
        <fail message="${debug.language} debug code detected:${line.separator}
                       ${debug.lines}" />
      </then>
    </if>
  </target>

Check an individual file for debug code

No need to run init here. This target should only be called through parent check-debug target.

  <target name="check-debug-file">
    <echo>Checking file for debug statements: ${absfilename}</echo>
    <loadfile property="debug.lines" file="${absfilename}">
      <filterchain>
        <linecontainsregexp>
            <regexp pattern="${debug.pattern}" />
          </linecontainsregexp>
      </filterchain>
    </loadfile>
    <if>
      <not>
        <equals arg1="${debug.lines}" arg2="" />
      </not>
      <then>
        <append text="${filename}:${line.separator}
                      ${debug.lines}${line.separator}
                      ${line.separator}"
                destFile="${debug.output}"/>
      </then>
    </if>
  </target>

Detect code mess

Uses PHPMD to detect code mess and look for potential problems.

  <target name="phpmd"
          description="Generate pmd.xml using PHPMD"
          depends="init">

We do not use the unusedcode ruleset as Drupal hook implementations usually are declared with all arguements but may not use them all.

    <phpmd rulesets="codesize,naming,design">
      <fileset refid="src.php.custom" />
      <formatter type="xml" outfile="${project.logdir}/pmd.xml"/>
    </phpmd>
  </target>

Detect potential copy/pasting

Uses phpcpd to detect duplicate code. This indicates potential refactorings.

  <target name="phpcpd"
          description="Generate pmd-cpd.xml using phpcpd"
          depends="init">
    <phpcpd>
      <fileset refid="src.php.custom" />
      <formatter type="pmd" outfile="${project.logdir}/pmd-cpd.xml"/>
    </phpcpd>
  </target>

Generate code statistics

Measures the size of the project using phploc and generates statistics.

  <target name="phploc"
          description="Generate phploc.csv using phploc"
          depends="init, setup-phing-phploc">

Suffixes should be the same as included in the src.php filesets

    <phploc reportType="csv"
            reportName="phploc" reportDirectory="${project.logdir}"
            suffixes="php,module,inc,install,profile,test" countTests="true">
      <fileset refid="src.php.custom" />
    </phploc>
  </target>

Setup Phing phploc integration

  <target name="setup-phing-phploc"
          depends="init" >

Clone the project

    <phingcall target="setup-git-repo">
      <property name="repo.dir" value="${project.toolsdir}/phing-phploc"/>
      <property name="repo.url" value="${phing.phploc.repository.url}" />
      <property name="repo.revision" value="${phing.phploc.repository.revision}" />
    </phingcall>

Register the custom Phing task

    <taskdef name="phploc" classname="PHPLocTask"
             classpath="${project.toolsdir}/phing-phploc" />
  </target>

Drupal Coder review

Review code using Drupal coder module.

Configuration of which modules and themes to review is done in build.properties. If your modules use a common prefix such as yourproject_module1, yourproject_module2 you can add all modules with a specific prefix.

  <target name="coder-review-d6"
          description="Review code using Drupal 6 Coder module"
          depends="init">
    <echo level="warning">Coder Review is not supported for Drupal 6 yet.
                          Check http://drupal.org/node/858330 for
                          patches/updates.</echo>
  </target>

  <target name="coder-review-d7"
          description="Review code using Drupal 7 Coder module"
          depends="init, clean, site-install">

Setup properties for running Coder Review. For some reason these properties are not passed correctly through to subtargets when defining them within the phingcall.

    <property name="coder.review.command" value="coder-review"/>

Download and enable the Coder Review module

    <phingcall target="enable-module">
      <property name="project" value="coder"/>
      <property name="project.version" value="7.x-1.0"/>
      <property name="module" value="coder_review"/>
    </phingcall>

Perform actual coder review for each style

    <foreach target="coder-review" param="coder.review.type"
             list="comment,i18n,security,sql,style" />
  </target>

Perform coder review

This target requires properties set by calling targets coder-review-d6 or coder-review-d7.

No need to run init here. This target should only be called from parent coder-review-* targets.

  <target name="coder-review"
          depends="setup-phing-drush">

Get a list of modules and themes matching the project prefix. These are the ones we are going to review.

    <drush command="pm-list" pipe="true" returnProperty="projects" />

The project list is piped through a file as this seems to be the only way to handle filtering of values in Phing.

    <delete file="${project.logdir}/projects.txt" />
    <append text="${projects}" destFile="${project.logdir}/projects.txt" />

Build a regular expression to match modules based on the set properties. The propery can contain prefixes separated by comma and/or spaces. The expression should be in the format ^(prefix1|prefix2|prefix3)_.

    <php expression="'^(' . preg_replace('/(\s+|\s*,\s*)/', '|', '${project.code.prefix}') .')_'"
         returnProperty="project.code.prefix.regex"/>

    <loadfile property="project.code.projects"
              file="${project.logdir}/projects.txt">
      <filterchain>
        <linecontainsregexp>
          <regexp pattern="${project.code.prefix.regex}" />
        </linecontainsregexp>

Prefix all lines with a space. We need the space as separator when we strip line breaks

        <prefixlines prefix=" " />
        <striplinebreaks />
      </filterchain>
    </loadfile>

Cleanup the custom code property. Commas and multiple whitespace characters are reduced to a single space.

    <php expression="preg_replace('/(\s+|\s*,\s*)/', ' ', '${project.code.custom}')"
         returnProperty="project.code.custom"/>

Execute coder review and output results in XML format

    <drush command="${coder.review.command}" assume="yes"
           pipe="yes" returnProperty="xml">
      <param>no-empty</param>
      <param>checkstyle</param>
      <param>minor</param>
      <param>${coder.review.type}</param>

Review all the modules and themes matching the project prefix

      <param>${project.code.projects}</param>

Review additional modules which do not match the prefix

      <param>${project.code.custom}</param>
    </drush>

Write XML output to file

    <property name="coderreview.checkstyle.file"
              value="${project.logdir}/checkstyle-${coder.review.type}.xml" />
    <delete file="${coderreview.checkstyle.file}" />
    <append destFile="${coderreview.checkstyle.file}" text="${xml}" />

Convert source from source extract to Category.Type format

    <php function="ucwords" returnProperty="type">
      <param value="${coder.review.type}"/>
    </php>
    <reflexive file="${coderreview.checkstyle.file}">
      <filterchain>
        <replaceregexp>
          <regexp pattern='source=".*"'
                  replace='source="Drupal.CoderReview.${type}"' />
        </replaceregexp>
      </filterchain>
    </reflexive>
  </target>

Review code using PHP_CodeSniffer

The purpose and outcome of this target is the same as the coder-review targets. In general PHP_CodeSniffer is faster to execute but there does not seem to be a complete ruleset which covers all of the Drupal coding standards.

Consequently coder-review-d6/7 and not phpcs is used for the main targets.

    <target name="phpcs"
            description="Generate checkstyle.xml using PHP_CodeSniffer"
            depends="init">

Clone a repository containing Drupal code guidelines for PHP_CodeSniffer.

      <phingcall target="setup-git-repo">
        <property name="repo.dir" value="${project.toolsdir}/drupalcs"/>
        <property name="repo.url" value="${phpcs.drupalcs.repository.url}" />
      </phingcall>

There is no Phing task for PHP Codesniffer in v2.4.6. It's coming for v2.5. Execute while we wait.

      <exec command="phpcs --report=checkstyle
                    --report-file=${project.logdir}/checkstyle-codesniffer.xml
                    --standard=${project.toolsdir}/drupalcs/ruleset.xml
                    --extensions=php,inc
                    --ignore=*/contrib/*,*/*.features.*,*/*.field_group.inc,*/*.layout.*,*/*.pages_default.*,*/*.panels_default.*,*/*strongarm.inc,*/*.views_default.inc
                    ${project.sitesdir}"
            logoutput="true" />
    </target>

Run simpletests

Execution of this target can be skipped by setting the project.simpletest.skip property from the command line or in other targets.

  <target name="simpletest"
          description="Run all unit tests"
          depends="init, setup-phing-drush, site-install"
          unless="project.simpletest.skip">

Enable simpletest module. If using Drupal 6 the module will be downloaded as well.

    <phingcall target="enable-module">
      <property name="module" value="simpletest"/>
    </phingcall>

    <if>
      <isset property="drupal.uri" />
      <then>

Get a list of all available test cases

        <drush command="test-run" uri="${drupal.uri}" root="${project.drupal.dir}" returnProperty="tests" returnGlue="${line.separator}"/>

The project list is piped through a file as this seems to be the only way to handle filtering of values in Phing.

        <delete file="${project.logdir}/tests.txt" />
        <append text="${tests}" destFile="${project.logdir}/tests.txt" />

Build a regular expression to match test groups based on the set properties. The expression should be in the format ^\s?(prefix1|prefix2|prefix3)_.

        <php expression="'^\s?(' . preg_replace('/(\s+|\s*,\s*)/', '|', '${project.code.prefix}') .').*'"
             returnProperty="project.code.prefix.regex"/>

Load the list of tests but keep only the test groups matching our prefixes.

        <loadfile property="project.simpletest.tests"
                  file="${project.logdir}/tests.txt">
          <filterchain>
            <linecontainsregexp>
              <regexp pattern="${project.code.prefix.regex}" ignoreCase="true"/>
            </linecontainsregexp>
          </filterchain>
        </loadfile>

Transform the list of filtered test groups in the form

    Groupname 1             Groupname 1
    Groupname 2             Groupname 2

    into a list of comma separated unique group names `Groupname 1,Groupname 2`.
        <php expression="implode(',', array_unique(preg_split('/(\s{2,}|\r|\n)/', trim('${project.simpletest.tests}', PREG_SPLIT_NO_EMPTY))))"
             returnProperty="project.simpletest.tests"/>

Run the tests and generate JUnit XML reports. This requires Drush 4.5 or newer or a patch.

        <drush command="test-run" uri="${drupal.uri}" root="${project.drupal.dir}" haltonerror="false">
          <param>${project.simpletest.tests}</param>
          <option name="xml">${project.testdir}</option>
          </drush>
      </then>
      <else>
        <echo msg="You must set the drupal.uri property to get simpletests working." />
      </else>
    </if>
    
  </target>

Generate documentation

Generate API Documentation

Uses phpDocumentor to generate documentation.

  <target name="phpdoc"
          description="Generate API documentation using phpDocumentor">
    <mkdir dir="${project.buildir}/api"/>
    <phpdoc title="API Documentation"
            destdir="${project.builddir}/api"
            sourcecode="php"
            output="HTML:Smarty:PHP">
      <fileset refid="src.php" />
    </phpdoc>
  </target>

Generate a code browser

Generate a code browser for PHP files with syntax highlighting and colored error-sections using PHP_CodeBrowser.

  <target name="phpcb"
          description="Aggregate tool output with PHP_CodeBrowser"
          depends="init">
    <mkdir dir="${project.builddir}/code-browser"/>

There is no Phing target for PHP CodeBrowser so do a plain execute.

    <exec command="phpcb  --log ${project.logdir}
                          --source ${project.basedir}
                          --output ${project.builddir}/code-browser"
          logoutput="true" />
  </target>

Helper targets

These targets are used throughout the project and should normally not be executed directly.

Initialization

This target sets up many of the common resources used throughout the build. All other targets except dependencies for this target should depend on this unless specifically stated why.

  <target name="init"
          depends="load-properties, setup-dirs, make, setup-filesets"
          unless="project.initialized">

Set property to prevent target from being executed multiple times

    <property name="project.initialized" value="true"/>
  </target>

Load properties

Loads a set of project specific properties from a .properties file.

These properties contain information regarding the individual project and/or environment such as which version of Drupal you are using, how to create a database and the names of your custom modules.

All available properties are described and set to a default value in build.default.properties. You should create your own properties file by copying the build.default.properties file to the root directory, rename it build.properties and modify it according to your project.

Both property files are loaded so your custom build.properties file should only contain properties where you want to override the default value e.g. set your custom module code prefix or use a special version of one of the build tools.

  <target name="load-properties">
    <php function="dirname" returnProperty="phing.dir">
      <param value="${phing.file}"/>
    </php>

    <property name="project.basedir" value="${phing.dir}" />

Use condition instead of unless property as we cannot unset properties in Phing

    <if>
      <or>

istrue evaluates to true is value is not set we need to check isset as well

        <not><istrue value="${project.properties.loaded}" /></not>
        <not><isset property="project.properties.loaded" /></not>
      </or>
      <then>

By default Jenkins runs Phing from the directory containing the build file. If this file is located in a subdirectory - e.g. when using Phing Drupal as a submodule - we need to reset the project basedir and reload properties.

NB: This only works if the subdirectory is directly within the Drupal root directory.

        <if>

If build.properties exists then assume we have a project root directory

          <available file="${project.basedir}/../build.properties"/>
          <then>
            <resolvepath propertyName="project.basedir"
                         file="${project.basedir}/../"/>
          </then>
        </if>

By default use default properties file build.default.properties

        <property name="project.properties.file"
                  value="${phing.dir}/build.default.properties" />

Load the default properties. Override in case load-properties are called multiple times.

        <property file="${project.properties.file}" override="true" />

Allow override using build.properties in build file directory

        <available file="${phing.dir}/build.properties"
                   property="project.properties.file"
                   value="${phing.dir}/build.properties" />

Allow override using build.properties in project base directory

        <available file="${project.basedir}/build.properties"
                   property="project.properties.file"
                   value="${project.basedir}/build.properties" />

Load the overriding properties.

        <property file="${project.properties.file}" override="true" />

Set property to prevent unnecessary additional invocations of this target

        <property name="project.properties.loaded" value="true" />
      </then>
    </if>
  </target>

Setup directories

Define working directories - where the individual parts of the build are and should be located. These are used in other targets.

This is part of the initialization of the build. This target should only be called from init target.

  <target name="setup-dirs"
          depends="load-properties">
    <if>
      <isset property="drupal.make.dir"/>
      <then>
        <property name="project.drupal.dir"
                value="${project.basedir}/${drupal.make.dir}" />
      </then>
      <else>
        <property name="project.drupal.dir"
                value="${project.basedir}" />
      </else>
    </if>

    <property name="project.sitesdir"
              value="${project.drupal.dir}/${project.code.dir}" />
    <property name="project.builddir"
              value="${project.basedir}/build" />
    <property name="project.toolsdir"
              value="${project.builddir}/tools" />
    <property name="project.coveragedir"
              value="${project.builddir}/coverage" />
    <property name="project.logdir"
              value="${project.builddir}/logs" />
    <property name="project.testdir"
              value="${project.builddir}/tests" />
  </target>

Drush Make

Download and install the source code for the site using Drush Make.

This target is only executed if the project uses make files as configured in the build.properties file. Execution can also be skipped by setting the project.make.skip property from the command line or in other targets.

This is part of the initialization of the build. This target should only be called from init target.

  <target name="make"
          depends="load-properties, setup-phing-drush"
          if="drupal.make.file">
    <if>
      <or>
        <not><isset property="project.make.skip"/></not>
        <not><istrue value="${project.make.skip}"/></not>
      </or>
      <then>

Delete any prexisting builds

        <delete dir="${project.drupal.dir}"/>

If the make file does not include a core Drupal project we need to download one separately. This should be defined in build.properties.

        <if>
          <and>
            <isset property="drupal.make.nocore"/>
            <istrue value="${drupal.make.nocore}"/>
          </and>
          <then>

Download the appropriate version of Drupal

            <drush command="dl" assume="yes">
              <param>drupal-${drupal.version}</param>
              <option name="drupal-project-rename">${drupal.make.dir}</option>
            </drush>

Make the project in the project code directory. If using a directory inside the sites folder modules can be tested faster and easier when using the minimal profile.

            <drush command="make" assume="yes">
              <param>${drupal.make.file}</param>
              <option name="contrib-destination">${drupal.make.dir}/${project.code.dir}</option>
              <option name="no-core"/>
            </drush>

Copy the install profile bundled with the make file to the appropriate directory.

            <copy todir="${project.drupal.dir}/profiles/${drupal.profile}/">
              <fileset dir="${project.basedir}">
                <exclude name="${drupal.make.dir}/**" />
                <exclude name="build/**" />
                <exclude name="coverage/**" />
                <exclude name="logs/**" />
                <exclude name="tests/**" />
                <exclude name="tools/**" />
              </fileset>
            </copy>
          </then>
          <else>
            <drush command="make" assume="yes">
              <param>${drupal.make.file}</param>
              <param>${drupal.make.dir}</param>
            </drush>
          </else>
        </if>

        <if>
          <isset property="drupal.make.rewritebase" />
          <then>
            <reflexive file="${project.drupal.dir}/.htaccess">
              <filterchain>
                <replaceregexp>
                  <regexp pattern="# RewriteBase [\w/]*" replace="RewriteBase ${drupal.make.rewritebase}"/>
                </replaceregexp>
              </filterchain>
            </reflexive>
          </then>
        </if>

Set property to prevent target from being executed multiple times

        <property name="project.make.skip" value="true"/>
      </then>
      <else>
        <echo>
          Skipping drush make.${line.separator}
          drupal.make.skip has been set to ${project.make.skip}.
        </echo>
      </else>
    </if>
  </target>

Setup file sets

Setup file sets - patterns for different aspects of the source code. These are used in other targets.

This is part of the initialization of the build. This target should only be called from init target.

  <target name="setup-filesets"
          depends="load-properties">

Define pattern sets for future reference

PHP files

    <patternset id="php">
      <include name="**/*.php" />
      <include name="**/*.module" />
      <include name="**/*.install" />
      <include name="**/*.inc" />
      <include name="**/*.profile" />
      <include name="**/*.test" />
    </patternset>

Javascript files

    <patternset id="js">
      <include name="**/*.js" />

Minimized JavaScript files should not be analyzed. In their optimized state they can not be expexted to conform to coding standards.

      <exclude name="**/*.min.js" />
    </patternset>

CSS files

    <patternset id="css">
      <include name="**/*.css" />
    </patternset>

Directories for community contributed code. We exclude these from code analysis as we cannot be held responsible for any problems here.

    <patternset id="contrib">
      <exclude name="**/contrib/**/*.*" />
      <exclude name="**/libraries/**/*.*" />
    </patternset>

We exclude generated files from We exclude these from code analysis as we cannot be held responsible for any problems here.

    <patternset id="generated">
      <exclude name="**/*.features.*" />
      <exclude name="**/*.field_group.inc" />
      <exclude name="**/*.layouts.inc" />
      <exclude name="**/*.pages_default.inc" />
      <exclude name="**/*.panelizer.inc" />
      <exclude name="**/*.panels_default.inc" />
      <exclude name="**/*.strongarm.inc" />
      <exclude name="**/*.views_default.inc" />
    </patternset>

Define file sets for future reference

All PHP files

    <fileset id="src.php" dir="${project.drupal.dir}">
      <patternset refid="php"/>
    </fileset>

Custom PHP files

    <fileset id="src.php.custom" dir="${project.sitesdir}">
      <patternset refid="php"/>
      <patternset refid="contrib"/>
      <patternset refid="generated"/>
    </fileset>

All Javascript files

    <fileset id="src.js" dir="${project.drupal.dir}">
      <patternset refid="js" />
    </fileset>

Custom Javascript files

    <fileset id="src.js.custom" dir="${project.sitesdir}">
      <patternset refid="js" />
      <patternset refid="contrib" />
    </fileset>

All CSS files

    <fileset id="src.css" dir="${project.basedir}">
      <patternset refid="css" />
    </fileset>

Custom CSS files

    <fileset id="src.css.custom" dir="${project.sitesdir}">
      <patternset refid="css" />
      <patternset refid="contrib" />
    </fileset>

  </target>

Setup permissions

Set permissions for certain files correctly after it has been built. This is required to support using the site after it has been built e.g. for conducting tests and site allow it to be rebuilt later on.

By default the built site will be owned by the Jenkins user and allow others to read. We allow read, write and execute access for all to give users such as apache running the webserver the necessary permissions.

    <target name="setup-permissions"
            depends="setup-dirs">

Allow write access to files directory.

      <chmod mode="0777" failonerror="false">
        <fileset dir="${project.drupal.dir}">
          <patternset>
            <include name="sites/default/**/*"/>
          </patternset>
        </fileset>
      </chmod>

If the build uses SQLite then we update permissions to the database file and the directory containing the file.

      <if>
        <contains string="${drupal.db.url}" substring="sqlite"/>
        <then>
          <php expression="dirname(substr('${drupal.db.url}', 7));"
               returnProperty="drupal.sqlite.dir" />
          <php expression="basename(substr('${drupal.db.url}', 7));"
               returnProperty="drupal.sqlite.file" />
          <chmod file="${drupal.sqlite.dir}" mode="0777" failonerror="true"/>
          <chmod file="${drupal.sqlite.file}" mode="0777" failonerror="true"/>
        </then>
      </if>
    </target>

Clean working environment

  <target name="clean"
          description="Clean up and create artifact directories"
          depends="setup-dirs"
          unless="project.cleaned">

Delete any existing artifacts from a previous build. Do not delete builddir. It may contain the build file!

    <delete dir="${project.toolsdir}"/>
    <delete dir="${project.coveragedir}"/>
    <delete dir="${project.logdir}"/>
    <delete dir="${project.testdir}"/>

Verbose. We need to make sure any database is deleted. Reinstalling the site with an existing database causes the build to fail.

    <delete file="${project.drupal.dir}/database.sqlite" verbose="true" />

Remove leftover Drupal simpletest databases

    <delete>
      <fileset dir="${project.basedir}">
        <include name="database.sqlite-simpletest*"/>
      </fileset>
    </delete>

Recreate directories for artifacts

    <mkdir dir="${project.toolsdir}"/>
    <mkdir dir="${project.coveragedir}"/>
    <mkdir dir="${project.logdir}"/>
    <mkdir dir="${project.testdir}"/>

Set property to prevent target from being executed multiple times

    <property name="project.cleaned" value="true"/>
  </target>

Install a Drupal site

This initializes a Drupal site using a installation profile.

Configuration of which installation profile and database to use in done in build.properties.

  <target name="site-install"
          depends="init, setup-phing-drush"
          unless="project.installed">
    <drush command="site-install" assume="yes">
      <option name="db-url">${drupal.db.url}</option>
      <param>${drupal.profile}</param>
    </drush>
    
    <phingcall target="setup-permissions"/>

Set property to prevent target from being executed multiple times

    <property name="project.installed" value="true"/>
  </target>

Download and enable a project/module

  <target name="enable-module"
          depends="setup-phing-drush">

If project is not set then we assume that the module name is also the project name.

    <property name="project" value="${module}" override="no"/>

If the module is not already available then download it

    <drush command="pm-list" returnProperty="modules.available"/>
    <php function="strpos" returnProperty="module.available">
      <param>${modules.available}</param>
      <param>${module}</param>
    </php>
    <if>
      <not><istrue value="${module.available}"/></not>
      <then>

Download specific version if specified

        <condition property="download" value="${project}-${project.version}">
          <isset property="project.version"/>
        </condition>
        <property name="download" value="${project}" override="false"/>

        <drush command="pm-download" assume="yes">
          <param>${download}</param>
        </drush>
      </then>
    </if>

Enable the module

    <drush command="pm-enable" assume="yes">
      <param>${module}</param>
    </drush>
  </target>

Clone a git repository

  <target name="setup-git-repo">

Only clone if repository does not exist already

    <if>
      <not><available file="${repo.dir}" /></not>
      <then>

Set revision to HEAD if not already defined

        <property name="repo.revision" value="HEAD" override="false"/>

        <echo>Cloning ${repo.url} ${repo.revision} into ${repo.dir}</echo>

The gitclone task does not seem to work. Use exec instead.

        <exec command="git clone ${repo.url} ${repo.dir}" />
        <exec command="git checkout ${repo.revision}" dir="${repo.dir}"/>
      </then>
    </if>
  </target>

Download and apply a patch

  <target name="apply-http-patch">
    <php function="basename" returnProperty="patch.file">
      <param>${patch.url}</param>
    </php>

If patch has already been downloaded then we assume it has also been applied

    <if>
      <not><available file="${project.toolsdir}/${patch.file}"/></not>
      <then>
        <httpget url="${patch.url}"
                 dir="${project.toolsdir}"
                 proxy="${phing.httpget.proxy}" />
        <patch patchfile="${project.toolsdir}/${patch.file}"
               dir="${patch.dir}" haltonfailure="true"/>
      </then>
    </if>
  </target>

Setup Phing Drush integration

  <target name="setup-phing-drush"
          depends="setup-dirs" >

Clone the project

    <phingcall target="setup-git-repo">
      <property name="repo.dir"
                value="${project.toolsdir}/phing-drush"/>
      <property name="repo.url"
                value="${phing.drush.repository.url}" />
      <property name="repo.revision"
                value="${phing.drush.repository.revision}" />
    </phingcall>

Register as custom Phing task

    <taskdef name="drush" classname="DrushTask"
             classpath="${project.toolsdir}/phing-drush" />

Run drush from the project Drupal directory

    <property name="drush.root" value="${project.drupal.dir}"/>
  </target>

Setup Rhino

Mozilla Rhino is an open-source implementation of JavaScript written in Java.

   <target name="setup-rhino"
           depends="setup-dirs"
           unless="project.rhino.setup">
     <property name="rhino.dir"
               value="${project.toolsdir}/rhino" />
     <php function="basename" returnProperty="rhino.basename">
       <param value="${rhino.url}" />

We assume that the version of Rhino used is a distribution where the filename ends in .zip

       <param value=".zip" />
     </php>

Other targets use this property to determine the location of the js.jar file

     <property name="rhino.jar"
               value="${rhino.dir}/${rhino.basename}/js.jar"/>

If the Rhino js.jar file is not available then download and unpack Rhino

     <if>
       <not><available file="${rhino.jar}"/></not>
       <then>
         <mkdir dir="${rhino.dir}" />
         <php function="basename" returnProperty="rhino.zipfile">
           <param value="${rhino.url}" />
         </php>
         <httpget url="${rhino.url}"
                  dir="${rhino.dir}"
                  proxy="${phing.httpget.proxy}"/>
         <unzip file="${rhino.dir}/${rhino.zipfile}"
                todir="${rhino.dir}" />
       </then>
     </if>

Set property to prevent unnecessary additional invocations of this target

     <property name="project.rhino.setup" value="true" />
   </target>

Build documentation using Phrocco

Generate documentation for the build script using Phrocco and prepare for pushing changes to GitHub pages.

To use this task you mush have a version of Phrocco with support for XML parsing installed. The task is primarily for template developers.

   <target name="phrocco"
           depends="clean">

Determine current branch

     <exec command="git status --branch --short"
           outputProperty="phrocco.code.status" />
     <php expression="array_shift(explode('...', trim('${phrocco.code.status}', '# '), 2))"
          returnProperty="phrocco.code.branch"/>

Generate documentation in the root build directory

     <exec command="phrocco -i . -o . -l xml"
           passthru="true" checkreturn="true" />

Checkout the GitHub pages branch

     <exec command="git checkout gh-pages"
           passthru="true" checkreturn="true"/>

We use the documentation for build.xml and root file so rename it to index.html

     <move file="build.html" tofile="index.html" overwrite="true"
           haltonerror="true" />

     <property name="phrocco.commit.mode" value=""/>

If the local documentation branch is ahead then we have other changes which have not been pushed yet. These have probably also been generated by this script so amend these changes.

     <exec command="git status --branch --short"
           outputProperty="phrocco.docs.status" />
     <if>
       <contains string="${phrocco.docs.status}" substring="[ahead"/>
       <then>
         <property name="phrocco.commit.mode" value="--amend"
                   override="true" />
       </then>
     </if>

Commit the changes

     <exec command="git commit --all -m 'Documentation update' ${phrocco.commit.mode}"
           passthru="true" checkreturn="true"/>

Return to the main branch

     <exec command="git checkout ${phrocco.code.branch}"
           passthru="true" checkreturn="true"/>
   </target>

</project>