<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
 * Phing build file for Yii 2.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @link http://www.yiiframework.com/
 * @copyright 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */
-->
<project name="yii" basedir="." default="help">
  <!-- task definitions -->
  <taskdef name="yii-init-build" classname="YiiInitTask" classpath="tasks" />
  <!--
  <taskdef name="yii-pear" classname="YiiPearTask" classpath="tasks"/>
  -->

  <!-- init yii.version, yii.revision and yii.winbuild -->
  <yii-init-build />

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

  <property name="pkgname" value="${phing.project.name}-${yii.version}.${yii.revision}"/>
  <property name="docname" value="${phing.project.name}-docs-${yii.version}.${yii.revision}"/>
  <property name="pearname" value="${phing.project.name}-${yii.release}.tgz" />

  <!-- directory definitions -->
  <property name="build.base.dir" value="release"/>
  <property name="build.dist.dir" value="${build.base.dir}/dist"/>
  <property name="build.src.dir" value="${build.base.dir}/${pkgname}"/>
  <property name="build.pear.src.dir" value="${build.src.dir}/framework" />
  <property name="build.doc.dir" value="${build.base.dir}/${docname}"/>
  <property name="build.web.dir" value="${build.base.dir}/web"/>

  <tstamp>
    <format property="DATE" pattern="%b %e %Y" />
  </tstamp>

  <if>
    <equals arg1="${yii.winbuild}" arg2="true"/>
    <then>
      <property name="build" value="build"/>
    </then>
    <else>
      <property name="build" value="php build"/>
    </else>
  </if>

  <!-- source files in the framework -->
  <fileset dir=".." id="framework">
    <exclude name="**/.gitignore"/>
    <exclude name="**/*.bak"/>
    <exclude name="**/*~"/>
    <include name="framework/**/*"/>
    <include name="requirements/**/*"/>
    <include name="demos/**/*"/>
    <include name="CHANGELOG"/>
    <include name="UPGRADE"/>
    <include name="LICENSE"/>
    <include name="README"/>
  </fileset>

  <!-- doc files -->
  <fileset dir="../docs" id="docs">
    <exclude name="**/.gitignore"/>
    <exclude name="**/*.bak"/>
    <exclude name="**/*~"/>
    <include name="guide/**/*"/>
    <include name="blog/**/*"/>
  </fileset>

  <fileset dir="../docs/guide" id="docs-guide">
    <exclude name="**/.gitignore"/>
    <exclude name="**/*.bak"/>
    <exclude name="**/*~"/>
    <include name="**/*"/>
  </fileset>

  <fileset dir="../docs/blog" id="docs-blog">
    <exclude name="**/.gitignore"/>
    <exclude name="**/*.bak"/>
    <exclude name="**/*~"/>
    <include name="**/*"/>
  </fileset>

  <fileset dir="." id="writables">
    <include name="${build.src.dir}/**/runtime" />
    <include name="${build.src.dir}/**/assets" />
    <include name="${build.src.dir}/demos/**/data" />
  </fileset>

  <fileset dir="." id="executables">
    <include name="${build.src.dir}/**/yii" />
  </fileset>

  <target name="src" depends="sync">
    <echo>Building package ${pkgname}...</echo>
    <echo>Copying files to build directory...</echo>
    <copy todir="${build.src.dir}">
      <fileset refid="framework"/>
    </copy>

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

    <echo>Generating source release file...</echo>
    <mkdir dir="${build.dist.dir}" />
    <if>
      <equals arg1="${yii.winbuild}" arg2="true"/>
      <then>
        <tar destfile="${build.dist.dir}/${pkgname}.tar.gz" compression="gzip">
          <fileset dir="${build.base.dir}">
            <include name="${pkgname}/**/*"/>
          </fileset>
        </tar>
      </then>
      <else>
        <exec command="tar czpf ${pkgname}.tar.gz ${pkgname}" dir="${build.base.dir}"/>
        <move file="${build.base.dir}/${pkgname}.tar.gz" todir="${build.dist.dir}" />
      </else>
    </if>
    <zip destfile="${build.dist.dir}/${pkgname}.zip">
      <fileset dir="${build.base.dir}">
        <include name="${pkgname}/**/*"/>
      </fileset>
    </zip>
  </target>

  <target name="doc" depends="sync">
    <echo>Building documentation...</echo>

    <echo>Building Guide PDF...</echo>
    <exec command="${build} guideLatex" dir="." passthru="true" />
    <exec command="${pdflatex} guide.tex -interaction=nonstopmode -max-print-line=120" dir="commands/guide" passthru="true"/>
    <exec command="${pdflatex} guide.tex -interaction=nonstopmode -max-print-line=120" dir="commands/guide" passthru="true"/>
    <exec command="${pdflatex} guide.tex -interaction=nonstopmode -max-print-line=120" dir="commands/guide" passthru="true"/>
    <move file="commands/guide/guide.pdf" tofile="${build.doc.dir}/yii-guide-${yii.version}.pdf" />

    <echo>Building Blog PDF...</echo>
    <exec command="${build} blogLatex" dir="." passthru="true" />
    <exec command="${pdflatex} blog.tex -interaction=nonstopmode -max-print-line=120" dir="commands/blog" passthru="true"/>
    <exec command="${pdflatex} blog.tex -interaction=nonstopmode -max-print-line=120" dir="commands/blog" passthru="true"/>
    <exec command="${pdflatex} blog.tex -interaction=nonstopmode -max-print-line=120" dir="commands/blog" passthru="true"/>
    <move file="commands/blog/blog.pdf" tofile="${build.doc.dir}/yii-blog-${yii.version}.pdf" />

    <echo>Building API...</echo>
    <exec command="${build} api ${build.doc.dir}" dir="." passthru="true" />

    <!--
    <echo>Building API CHM...</echo>
    <exec command="${hhc} ${build.doc.dir}/api/manual.hhp" />
    <move file="${build.doc.dir}/api/manual.chm" tofile="${build.doc.dir}/yii-api-${yii.version}.chm" />
    <delete>
      <fileset dir="${build.doc.dir}/api">
        <include name="manual.*" />
      </fileset>
    </delete>
    -->

    <echo>Generating doc release file...</echo>
    <mkdir dir="${build.dist.dir}" />
    <tar destfile="${build.dist.dir}/${docname}.tar.gz" compression="gzip">
      <fileset dir="${build.base.dir}">
        <include name="${docname}/**/*"/>
      </fileset>
    </tar>
    <zip destfile="${build.dist.dir}/${docname}.zip">
      <fileset dir="${build.base.dir}">
        <include name="${docname}/**/*"/>
      </fileset>
    </zip>
  </target>

  <target name="web" depends="sync">

    <echo>Building online API...</echo>
    <mkdir dir="${build.web.dir}/common/data/${yii.version}" />
    <exec command="${build} api ${build.web.dir}/common/data/${yii.version} online" dir="." passthru="true" />

    <echo>Copying tutorials...</echo>
    <copy todir="${build.web.dir}/common/data/${yii.version}/tutorials/guide">
      <fileset refid="docs-guide"/>
    </copy>
    <copy todir="${build.web.dir}/common/data/${yii.version}/tutorials/blog">
      <fileset refid="docs-blog"/>
    </copy>

	<echo>Copying release text files...</echo>
    <mkdir dir="${build.web.dir}/frontend/www/files" />
	<copy file="../CHANGELOG" tofile="${build.web.dir}/frontend/www/files/CHANGELOG-${yii.version}.txt" />
	<copy file="../UPGRADE" tofile="${build.web.dir}/frontend/www/files/UPGRADE-${yii.version}.txt" />

	<echo>

Finished building Web files.
Please update yiisite/common/data/versions.php file with the following code:

    '1.1'=>array(
        'version'=>'${yii.version}',
        'revision'=>'${yii.revision}',
        'date'=>'${yii.date}',
        'latest'=>true,
    ),

	</echo>
  </target>

  <target name="sync">
    <echo>Synchronizing code changes for ${pkgname}...</echo>

    <echo>Building autoload map...</echo>
    <exec command="${build} autoload" dir="." passthru="true"/>

    <echo>Building yiilite.php...</echo>
    <exec command="${build} lite" dir="." passthru="true"/>
  </target>

  <target name="message">
    <echo>Extracting i18n messages...</echo>
    <exec command="${build} message ../framework/messages/config.php" dir="." passthru="true"/>
  </target>

  <!--
  <target name="pear" depends="clean,build">
    <echo>Generating pear package for ${phing.project.name}-${yii.release}</echo>
    <mkdir dir="${build.dist.dir}" />
    <yii-pear   pkgdir="${build.pear.src.dir}"
                channel="pear.php.net"
                version="${yii.release}"
                state="stable"
                category="framework"
                package="${phing.project.name}"
                summary="Yii PHP Framework"
                pkgdescription="Yii PHP Framework: Best for Web 2.0 Development"
                notes="http://www.yiiframework.com/files/CHANGELOG-${yii.release}.txt"
                license="BSD"
                />
    <exec command="pear package" dir="${build.pear.src.dir}" passthru="true" />
    <move file="${build.pear.src.dir}/${pearname}" tofile="${build.dist.dir}/${pearname}" />
  </target>
  -->

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

  <target name="help">
    <echo>

    Welcome to use Yii 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:

    - sync    : synchronize yiilite.php and BaseYii.php
    - message : extract i18n messages of the framework
    - src     : build source release
    - doc     : build documentation release (Windows only)
    - clean   : clean up the build

    </echo>
  </target>
</project>
