<!--
 各个组件的公用构建文件
 @author yiminghe@gmaill.com, lifesinger@gmail.com
 @refer http://ant.apache.org/manual/Tasks/subant.html
-->
<project name="common-build" basedir="../" xmlns:ac="antlib:net.sf.antcontrib">
    <description>Common Build File</description>

    <!-- 各个组件公用的属性 -->
    <property name="kissy.dir" location="${ant.file.common-build}/../../../"/>
    <property name="kissy-tools.dir" location="${kissy.dir}/../kissy-tools/"/>
    <property name="kissy-tools.module-compiler" location="${kissy-tools.dir}/module-compiler/module-compiler.jar"/>
    <property name="kissy.build.dir" location="${kissy.dir}/build/"/>
    <property name="kissy.src.dir" location="${kissy.dir}/src/"/>
    
    <import file="../version.xml"/>
    
    
    <property name="charset" value="utf-8"/>
    <property name="js-css.files" value="**/*.js,**/*.css"/>
    <property name="js-css.min.files" value="**/*-min.js,**/*-min.css"/>
    <property name="copyright.files" value="**/*.css, **/*.js"/>

    <!-- component src dir -->
    <property name="component.work.dir" location="./"/>
    <property name="component.src.dir" location="./src/"/>
    <property name="component.meta.dir" location="./meta/"/>
    <property name="component.src.dir.tmp" location="./tmp/"/>
    <property name="component.files" value="**/*.*"/>
    <property name="bom" value="&#xFEFF;"/>

    <!-- 需要组件覆盖的属性 -->
    <property name="component.name" value="component.name.need.override"/>

    <!-- 本 xml 需要的属性 -->
    <property name="component.build.dir" value="${kissy.build.dir}/${component.name}/"/>
    <property name="component.build.dir.tmp" value="${kissy.build.dir}/${component.name}/tmp/"/>
    <tstamp>
        <format property="timestamp.isoformat" pattern="yyyyMMddHHmmss" locale="en"/>
    </tstamp>

    <!-- 清空与复制等准备工作 -->
    <target name="prepare">
        <mkdir dir="${component.build.dir}"/>
        <copy todir="${component.build.dir}">
            <fileset dir="${component.src.dir.tmp}" includes="${component.files}"/>
        </copy>
    </target>

    <target name="concat">
        <concat destfile="${component.src.dir.tmp}/${component.name}-pkg.js"
                encoding="${charset}" outputencoding="${charset}">
            <fileset dir="${component.src.dir}" includes="**/*.js"/>
            <filterchain>
                <deletecharacters chars="${bom}"/>
            </filterchain>
        </concat>
        <copy todir="${component.src.dir.tmp}">
            <fileset dir="${component.work.dir}" includes="assets/**/*.css,assets/**/*.swf"/>
        </copy>
    </target>

    <!-- 压缩代码 -->
    <target name="compress-css">
        <!-- 用 YUICompressor 压缩 CSS -->
        <apply executable="java" verbose="true"
               dest="${component.build.dir}">
            <fileset dir="${component.build.dir}" excludes="**/*-min.css" includes="**/*.css"/>
            <arg line="-jar"/>
            <arg path="${kissy-tools.dir}/yuicompressor/yuicompressor.jar"/>
            <arg line="--charset ${charset}"/>
            <srcfile/>
            <arg line="-o"/>
            <targetfile/>
            <mapper type="regexp" from="^(.*)\.(css|js)$" to="\1-min.\2"/>
        </apply>
    </target>

    <!-- 压缩代码 -->
    <target name="compress" depends="compress-css">
        <!-- remove console.log to prevent memory leak in chrome! -->
        <replaceregexp match="^\s*(S|KISSY)\.(log|error)\s*\(.+$"
                       replace=""
                       flags="g"
                       byline="true"
                       encoding="${charset}">
            <fileset dir="${component.build.dir}" includes="**/*.js"/>
        </replaceregexp>

        <!-- 用 Closure Compiler 压缩 JS -->
        <apply executable="java" verbose="true" dest="${component.build.dir}">
            <fileset dir="${component.build.dir}" excludes="**/*-min.js" includes="**/*.js"/>
            <arg line="-jar"/>
            <arg path="${kissy-tools.dir}/closure-compiler/compiler.jar"/>
            <arg line="--charset ${charset}"/>
            <arg value="--warning_level"/>
            <arg value="QUIET"/>
            <arg value="--js"/>
            <srcfile/>
            <arg value="--js_output_file"/>
            <targetfile/>
            <mapper type="regexp" from="^(.*)\.(css|js)$" to="\1-min.\2"/>
        </apply>

        <!-- 还原 debug 版本 -->
        <move todir="${component.build.dir}">
            <fileset dir="${component.src.dir.tmp}" includes="${component.files}"/>
        </move>
    </target>

    <target name="extract-dependency">

        <mkdir dir="${component.meta.dir}"/>

        <delete file="${component.meta.dir}/modules.js"/>

        <java classname="com.taobao.f2e.ExtractDependency" fork="true">

            <arg value="-baseUrls"/>
            <!--
            换行被转成空格 xml spec
            http://stackoverflow.com/questions/2004386/
            how-to-save-newlines-in-xml-attribute-->
            <arg value="${component.src.dir}"/>


            <arg value="-encodings"/>
            <arg value="${charset}"/>

            <arg value="-nameMap"/>
            <arg value="(${component.name})(?:/.*)?||$1"/>

            <arg value="-outputEncoding"/>
            <arg value="utf-8"/>

            <arg value="-compact"/>
            <arg value="true"/>

            <arg value="-output"/>
            <arg value="${component.meta.dir}/modules.js"/>

            <classpath>
                <pathelement path="${kissy-tools.module-compiler}"/>
                <pathelement path="${java.class.path}"/>
            </classpath>
        </java>
    </target>

    <!-- 对 JS 文件 ASCII 化 -->
    <target name="native2ascii" depends="compress">
        <mkdir dir="${component.build.dir.tmp}"/>
        <move todir="${component.build.dir.tmp}">
            <fileset dir="${component.build.dir}" includes="**/*-min.js"/>
        </move>
        <native2ascii encoding="${charset}"
                      src="${component.build.dir.tmp}"
                      dest="${component.build.dir}"
                      includes="**/*.js"/>
        <delete dir="${component.build.dir.tmp}"/>
    </target>

    <!-- 替换版本信息以及给文件加上版权信息以及加上 bom -->
    <target name="copyright">
        <tstamp>
            <format property="stamp.time" pattern="MMM d HH:mm" locale="en,UK"/>
            <format property="build.year" pattern="yyyy"/>
        </tstamp>

        <mkdir dir="${component.build.dir.tmp}"/>
        <move todir="${component.build.dir.tmp}">
            <fileset dir="${component.build.dir}" includes="${copyright.files}"/>
        </move>

        <ac:for param="file">
            <path>
                <fileset dir="${component.build.dir.tmp}"/>
            </path>
            <sequential>
                <!--如果不是压缩文件，就加上 bom 头，否则 ie 诡异问题-->
                <ac:var name="copyright.bomOrEmpty" value=""/>
                <ac:var name="copyright.file.name" unset="true"/>
                <basename property="copyright.file.name" file="@{file}"/>
                <ac:if>
                    <not>
                        <contains string="${copyright.file.name}" substring="-min." casesensitive="false"/>
                    </not>
                    <then>
                        <ac:var name="copyright.bomOrEmpty" value="${bom}"/>
                    </then>
                </ac:if>
                <concat destfile="@{file}.tmp" encoding="${charset}" outputencoding="${charset}">
                    <!-- ie need bom , odd -->
                    <header filtering="no" trimleading="yes">${copyright.bomOrEmpty}/*
                        Copyright ${build.year}, KISSY UI Library v${version}
                        MIT Licensed
                        build time: ${stamp.time}
                        */
                    </header>
                    <fileset file="@{file}"/>
                    <filterchain>
                        <deletecharacters chars="${bom}"/>
                    </filterchain>
                </concat>
                <move file="@{file}.tmp" tofile="@{file}"/>
            </sequential>
        </ac:for>

        <move todir="${component.build.dir}">
            <fileset dir="${component.build.dir.tmp}"/>
        </move>

        <delete dir="${component.build.dir.tmp}"/>

        <replaceregexp match="@VERSION@"
                       replace="${version}"
                       flags="g"
                       encoding="${charset}"
                       byline="true">
            <fileset dir="${component.build.dir}" includes="${js-css.files}"/>
        </replaceregexp>

        <replaceregexp match="@DEBUG@"
                       replace=""
                       flags="g"
                       byline="true"
                       encoding="${charset}">
            <fileset dir="${component.build.dir}" includes="${js-css.min.files}"/>
        </replaceregexp>

        <replaceregexp match="@TIMESTAMP@"
                       replace="${timestamp.isoformat}"
                       flags="g"
                       byline="true"
                       encoding="${charset}">
            <fileset dir="${component.build.dir}" includes="${js-css.files}"/>
        </replaceregexp>

    </target>


    <target name="fix-cr-lf">
        <fixcrlf srcdir="${component.build.dir}"
                 tablength="4"
                 includes="${js-css.files}"
                 encoding="${charset}"/>
    </target>

    <target name="process" depends="prepare,compress,native2ascii,copyright,fix-cr-lf">

        <!-- 1.2 不需要 pkg ,pkg 移到外围 -->
        <move encoding="${charset}" todir="${kissy.build.dir}/">
            <fileset dir="${component.build.dir}" includes="**/*-pkg.js,**/*-pkg-min.js"/>
            <mapper type="regexp" from="(.*)-pkg(.*)" to="\1\2"/>
        </move>

        <delete dir="${component.src.dir.tmp}"/>

        <!-- 空组件文件夹直接删除 -->
        <condition property="empty.component.build.dir">
            <length length="0">
                <fileset dir="${component.build.dir}"/>
            </length>
        </condition>

        <ac:if>
            <isset property="empty.component.build.dir"/>
            <ac:then>
                <delete dir="${component.build.dir}"/>
            </ac:then>
        </ac:if>

    </target>

    <target name="build" depends="concat,process,extract-dependency">

    </target>
    
    <target name="clean">
        <delete dir="${component.build.dir}"/>
    </target>
</project>