<?xml version="1.0" encoding="UTF-8"?>
<!--
  PRADO 3.2 build file - Copyright (C) 2013 PradoSoft

  Requirements
  ============
  xdebug >= 2.0.0beta4
-->
<project name="prado" basedir="." default="help">
  <taskdef name="prado-version-check" classname="PradoVersionTask" classpath="buildscripts/phing/tasks"/>
  <taskdef name="prado-quickstart-index" classname="QuickstartIndexTask" classpath="buildscripts/phing/tasks"/>
  <taskdef name="prado-api-index" classname="ManualIndexTask" classpath="buildscripts/phing/tasks"/>
  <taskdef name="phpunitreporttask" classname="PhpUnitReportTask" classpath="buildscripts/phing/tasks"/>

  <!-- generates ${prado.version} and ${prado.revision} -->
  <prado-version-check/>

  <!-- these are required external commands -->
  <property name="php" value="php"/> <!-- PHP parser -->
  <property name="pdflatex" value="pdflatex"/>  <!-- generates PDF from LaTex -->
  <property name="zip" value="zip"/>  <!-- zip compression -->

  <property name="pkgname" value="${phing.project.name}-${prado.version}.r${prado.revision}"/>

  <property name="compact-strip-comments" value="false"/>

  <property name="build.base.dir" value="build"/>
  <property name="build.dist.dir" value="${build.base.dir}/dist"/>
  <property name="build.src.dir" value="${build.base.dir}/${pkgname}"/>
  <property name="build.doc.dir" value="${build.base.dir}/docs"/>
  <property name="build.web.dir" value="${build.base.dir}/web"/>
  <property name="build.test.dir" value="${build.base.dir}/test-reports"/>
  <property name="build.functionaltest.dir" value="${build.base.dir}/functional-test-reports"/>
  <property name="build.snapshot.dir" value="${build.base.dir}/snapshot"/>

  <!--
      All Source Files in framework
  -->
  <fileset dir="." id="framework">
    <exclude name="**/.svn"/>
    <exclude name="**/*.bak"/>
    <exclude name="**/*~"/>
    <exclude name="**/pradolite.php"/><!-- will be generated -->
    <exclude name="**/prado-cli.php"/><!-- will be copied later -->
    <include name="framework/**/*"/>
  </fileset>

  <!--
      Only php source files in framework
  -->
  <fileset dir="." id="framework-php">
    <exclude name="**/pradolite.php"/>
    <exclude name="**/prado-cli.php"/>
    <include name="framework/**/*.php"/>
  </fileset>

  <!--
      Surrounding files
  -->
  <fileset dir="." id="misc">
    <exclude name="**/.svn"/>
    <exclude name="**/*.bak"/>
    <exclude name="**/*~"/>
    <include name="COPYRIGHT"/>
    <include name="HISTORY"/>
    <include name="UPGRADE"/>
    <include name="index.html"/>
    <include name="requirements/*"/>
  </fileset>

  <!--
      Documentation
  -->
  <fileset dir="." id="docs">
    <exclude name="**/.svn"/>
    <exclude name="**/*.bak"/>
    <exclude name="**/*~"/>
    <exclude name="**/latex"/>
    <exclude name="**/sqlmap_tut.pdf"/>
    <include name="docs/specs/*"/>
    <include name="docs/sqlmap/sqlmap.pdf"/>
  </fileset>

  <!--
      Demos
  -->
  <fileset dir="." id="demos">
    <exclude name="**/.svn"/>
    <exclude name="**/*.bak"/>
    <exclude name="**/*~"/>
    <exclude name="**/runtime/*"/>
    <exclude name="**/assets/*"/>
    <exclude name="**/runtime/*/**"/>
    <exclude name="**/assets/*/**"/>
    <exclude name="demos/blog/protected/Data/Blog.db"/>
    <include name="demos/address-book/**/*"/>
    <include name="demos/blog/**/*"/>
    <include name="demos/blog-tutorial/**/*"/>
    <include name="demos/chat/**/*"/>
    <include name="demos/composer/**/*"/>
    <include name="demos/currency-converter/**/*"/>
    <include name="demos/helloworld/**/*"/>
    <include name="demos/northwind-db/**/*"/>
    <include name="demos/personal/**/*"/>
    <include name="demos/quickstart/**/*"/>
    <include name="demos/soap/**/*"/>
    <include name="demos/sqlmap/**/*"/>
    <include name="demos/time-tracker/**/*"/>
  </fileset>

  <!--
      Test tools
    -->
  <fileset dir="." id="test-tools">
    <include name="tests/test_tools/**/*"/>
  </fileset>

  <!--
      Writable files and directories
  -->
  <fileset dir="." id="writables">
    <include name="${build.src.dir}/demos/**/runtime"/>
    <include name="${build.src.dir}/demos/**/assets"/>
    <include name="${build.src.dir}/demos/blog/protected/Data"/>
  </fileset>

  <target name="help">
    <echo>

    Welcome to use PRADO build script!
    ----------------------------------
    You may use the following command format to build a target:

              phing   &lt;target name&gt;

    where &lt;target name&gt; can be one of the following:

    For all PRADO developers:
    - test       : run unit tests (results are under /build/test-reports)
    - coverage   : run unit tests collecting coverage informations
    - lint       : run lint on framework
    - lint-demos : run lint on demos

    For PRADO release manager:
    - dist    : create release files;
    - build   : prepare a directory for distribution;
    - clean   : clean up the build directory;
    - rebuild : clean first and then build;
    - docs    : generate documentation;
    - snapshot: generate nightly snapshot;
    </echo>
  </target>

  <target name="build" description="Building standard PRADO package v${prado.version}.${prado.revision}">
    <echo>Building pradolite.php...</echo>
    <delete file="framework/pradolite.php"/>
    <exec command="${php} build.php" dir="buildscripts/phpbuilder" passthru="true"/>

    <echo>Copying files to build directory...</echo>
    <copy todir="${build.src.dir}">
      <fileset refid="demos"/>
      <fileset refid="docs"/>
      <fileset refid="framework"/>
      <fileset refid="misc"/>
    <fileset refid="test-tools"/>
    </copy>

    <copy file="buildscripts/classtree/ExtensionReadme.html" tofile="${build.src.dir}/editors/ExtensionReadme.html"/>
    <copy file="framework/pradolite.php" tofile="${build.src.dir}/framework/pradolite.php"/>
    <copy file="bin/prado-cli.php" tofile="${build.src.dir}/bin/prado-cli.php"/>

    <echo>Changing file permissions...</echo>
    <chmod mode="0777">
      <fileset refid="writables"/>
    </chmod>

    <echo>Building quickstart demo search indices...</echo>
    <prado-quickstart-index todir="${build.src.dir}/demos/quickstart/protected/index/quickstart"/>
  </target>

  <target name="clean">
    <echo>Cleaning up the mess</echo>
    <delete dir="${build.base.dir}"/>
  </target>

  <target name="rebuild" depends="clean,build"/>

  <target name="api-manual" depends="build">
    <echo>Building API manuals...</echo>
    <delete dir="${build.doc.dir}/manual"/>
    <mkdir dir="${build.doc.dir}/manual"/>
    <apigen
      executable="bin/apigen.php"
      source="${build.src.dir}/framework"
      destination="${build.doc.dir}/manual"
      exclude="*pradolite.php,*prado-cli.php,*3rdParty/*,*Javascripts/source/*,*packages.php,*JSMin.php,*I18N/core/*,*WebControls/assets/*"
      title="PRADO v${prado.version} API Manual"
      deprecated="true"
      todo="true"
      sourcecode="false"
      php="false"
      templateconfig="buildscripts/apigen/pradosoft/config.neon"
    />
    <echo>Cleaning svn directories from API manuals...</echo>
    <delete dir="${build.doc.dir}/manual/resources/.svn" includeemptydirs="true" failonerror="true" />

    <echo>Indexing API manuals...</echo>
    <prado-api-index docdir="${build.doc.dir}/manual" todir="${build.doc.dir}/manual"/>
    <copy file="buildscripts/index/search.php" todir="${build.doc.dir}/manual" />
  </target>

  <target name="docs" depends="build,api-manual">
    <echo>Build PDF + HTML Docs, it may take about 5 mins</echo>

    <echo>Building quickstart.pdf...</echo>
    <delete>
      <fileset dir=".">
        <include name="buildscripts/texbuilder/quickstart/*.aux"/>
      </fileset>
    </delete>
    <exec command="${php} build.php" dir="buildscripts/texbuilder/quickstart" passthru="true"/>
    <!-- use -interaction=nonstopmode to see compilation errors in the shell output -->
    <exec command="${pdflatex} -interaction=batchmode quickstart.tex" dir="buildscripts/texbuilder/quickstart" passthru="true"/>
    <move file="buildscripts/texbuilder/quickstart/quickstart.pdf" todir="${build.doc.dir}"/>

    <exec command="${php} build.php" dir="buildscripts/classtree" passthru="true"/>
  </target>

  <target name="dist" depends="rebuild,docs">
    <echo>Moving doc files to build...</echo>
    <mkdir dir="${build.dist.dir}"/>
    <move file="${build.doc.dir}/quickstart.pdf" todir="${build.src.dir}/docs"/>
    <copy todir="${build.src.dir}/docs/manual">
      <fileset dir="${build.doc.dir}/manual">
        <include name="**/**"/>
      </fileset>
    </copy>

    <echo>Generating compressed distribution files...</echo>
    <if>
      <equals arg1="${prado.winbuild}" arg2="false"/>
      <then>
        <exec command="tar czvf ${pkgname}.tar.gz ${pkgname}" dir="${build.base.dir}"/>
        <move file="${build.base.dir}/${pkgname}.tar.gz" todir="${build.dist.dir}"/>
      </then>
    </if>

    <echo>Generating files for pradosoft.com...</echo>
    <echo>Compiling class docs...</echo>
    <exec command="${php} build.php" dir="buildscripts/classtree" passthru="true"/>
    <move file="buildscripts/classtree/classes.data" todir="${build.web.dir}/protected/Data"/>

    <echo>Moving HTML docs to Web folder...</echo>
    <mkdir dir="${build.web.dir}"/>
    <copy todir="${build.web.dir}/docs/manual">
      <fileset dir="${build.doc.dir}/manual">
        <include name="**/**"/>
      </fileset>
    </copy>

    <echo>
                   Distribution Completed!!!
    </echo>
  </target>

  <target name="test" description="Running unit tests">
    <delete dir="${build.test.dir}"/>
    <mkdir dir="${build.test.dir}"/>
    <phpunit codecoverage="false" haltonfailure="false" haltonerror="false" printsummary="false" bootstrap="./tests/test_tools/phpunit_bootstrap.php">
      <batchtest>
        <fileset dir="tests/unit">
        <include name="**/*Test.php"/>
        </fileset>
      </batchtest>
      <formatter type="xml" todir="${build.test.dir}" outfile="unit-tests.xml"/>
    </phpunit>
    <phpunitreporttask infile="${build.test.dir}/unit-tests.xml" format="frames" todir="${build.test.dir}"/>
  </target>

  <target name="functionaltest" description="Running functional tests">
    <delete dir="${build.functionaltest.dir}"/>
    <mkdir dir="${build.functionaltest.dir}"/>
    <phpunit codecoverage="false" haltonfailure="false" haltonerror="false" printsummary="false" bootstrap="./tests/test_tools/phpunit_bootstrap.php">
      <batchtest>
        <fileset dir="tests/FunctionalTests">
        <include name="**/*TestCase.php"/>
        </fileset>
      </batchtest>
      <formatter type="xml" todir="${build.functionaltest.dir}" outfile="functional-tests.xml"/>
    </phpunit>
    <phpunitreporttask infile="${build.functionaltest.dir}/functional-tests.xml" format="frames" todir="${build.functionaltest.dir}"/>
  </target>

  <target name="coverage" description="Running unit tests with coverage checks">
    <delete dir="${build.test.dir}"/>
    <mkdir dir="${build.test.dir}"/>

    <coverage-setup database="${build.test.dir}/coverage.db">
      <fileset refid="framework-php" />
    </coverage-setup>
    <phpunit codecoverage="true" haltonfailure="false" haltonerror="false" printsummary="false" bootstrap="./tests/test_tools/phpunit_bootstrap.php">
      <batchtest>
        <fileset dir="tests/unit">
        <include name="**/*Test.php"/>
        </fileset>
        <fileset dir="tests/FunctionalTests">
        <include name="**/*TestCase.php"/>
        </fileset>
      </batchtest>
      <formatter type="xml" todir="${build.test.dir}" outfile="unit-tests.xml"/>
      <formatter type="clover" todir="${build.test.dir}" outfile="clover.xml"/>
    </phpunit>
    <phpunitreporttask infile="${build.test.dir}/unit-tests.xml" format="frames" todir="${build.test.dir}"/>
  </target>

  <target name="snapshot" depends="rebuild">
    <echo>Generating compressed distribution files...</echo>
    <if>
      <equals arg1="${prado.winbuild}" arg2="false"/>
      <then>
        <exec command="tar czvf ${pkgname}.tar.gz ${pkgname}" dir="${build.base.dir}"/>
        <move file="${build.base.dir}/${pkgname}.tar.gz" todir="${build.dist.dir}"/>
      </then>
    </if>
    <exec command="zip ${pkgname}.zip -r ${pkgname}" dir="${build.base.dir}"/>
    <move file="${build.base.dir}/${pkgname}.zip" tofile="${build.snapshot.dir}/prado-latest.zip"/>
    <copy file="${build.src.dir}/HISTORY" tofile="${build.snapshot.dir}/changelog-latest.txt"/>
  </target>

  <taskdef name="phplint" classname="PhpLintTask" classpath="buildscripts/phing/tasks"/>
  <taskdef name="xmllint" classname="XmlLintTask" classpath="buildscripts/phing/tasks"/>
  <taskdef name="jslint" classname="JslLintTask" classpath="buildscripts/phing/tasks"/>

  <target name="lint" description="Check syntax of source files">
    <echo>Checking php files..</echo>
    <phplint deprecatedAsError="true">
      <fileset dir="framework">
        <exclude name="**/.svn"/>
        <exclude name="**/*.bak"/>
        <exclude name="**/*~"/>
        <include name="**/*.php"/>
      </fileset>
    </phplint>

    <echo>Checking js files..</echo>
    <jsllint>
      <fileset dir="framework">
        <exclude name="**/.svn"/>
        <exclude name="**/*.bak"/>
        <exclude name="**/*~"/>
        <include name="**/*.js"/>
      </fileset>
    </jsllint>

    <echo>Checking xml files..</echo>
    <xmllint>
      <fileset dir="framework">
        <exclude name="**/.svn"/>
        <exclude name="**/*.bak"/>
        <exclude name="**/*~"/>
        <include name="**/*.xml"/>
      </fileset>
    </xmllint>
  </target>

  <target name="lint-demos" description="Check syntax of demo files">
    <echo>Checking php files..</echo>
    <phplint deprecatedAsError="true">
      <fileset dir="demos">
        <exclude name="**/.svn"/>
        <exclude name="**/*.bak"/>
        <exclude name="**/*~"/>
        <include name="**/*.php"/>
      </fileset>
    </phplint>

    <echo>Checking js files..</echo>
    <jsllint>
      <fileset dir="demos">
        <exclude name="**/.svn"/>
        <exclude name="**/*.bak"/>
        <exclude name="**/*~"/>
        <include name="**/*.js"/>
      </fileset>
    </jsllint>

    <echo>Checking xml files..</echo>
    <xmllint>
      <fileset dir="demos">
        <exclude name="**/.svn"/>
        <exclude name="**/*.bak"/>
        <exclude name="**/*~"/>
        <include name="**/*.xml"/>
      </fileset>
    </xmllint>
  </target>

</project>