<?xml version="1.0" encoding="utf-8"?>
<project name="ext" default=".help">
    <!--
    The build-impl.xml file imported here contains the guts of the build process. It is
    a great idea to read that file to understand how the process works, but it is best to
    limit your changes to this file.
    -->
    <import file="${basedir}/.sencha/package/build-impl.xml"/>

    <!--
    The following targets can be provided to inject logic before and/or after key steps
    of the build process:

        The "init-local" target is used to initialize properties that may be personalized
        for the local machine.

            <target name="-before-init-local"/>
            <target name="-after-init-local"/>

        The "clean" target is used to clean build output from the build.dir.

            <target name="-before-clean"/>
            <target name="-after-clean"/>

        The general "init" target is used to initialize all other properties, including
        those provided by Sencha Cmd.

            <target name="-before-init"/>
            <target name="-after-init"/>
        
        The "build" target performs the call to Sencha Cmd to build the application.

            <target name="-before-build"/>
            <target name="-after-build"/>
    -->

    <target name="-before-init">
        <property name="pkg.excludes"
                  value="all-licenses/**/*,
                         examples/**/*,
                         welcome/**/*,
                         build/welcome/**/*,
                         build/classic/**/*,
                         build/modern/**/*,
                         build/packages/**/*,
                         build/index.html,
                         test/**/*,
                         classic/classic/test/**/*,
                         modern/modern/test/**/*,
                         release-notes-*,
                         build/temp/**/*,
                         build/examples/**/*,
                         ext-all*.js,
                         ext-debug.js,
                         ext.js,
                         ext-modern*.js,
                         classic/*/test/**/*,
                         modern/*/test/**/*,
                         packages/*/test/**/*,
                         test_internal/**/*,
                         bootstrap*.js"/>

        <condition property="repo.dev.mode" value="true">
            <available file="${basedir}/../attic" type="dir"/>
        </condition>
    </target>
    
    <target name="-after-init" depends="apply-production-settings"/> 

    <macrodef name="x-set-license">
        <attribute name="license" default="${ext.license.name}"/>
        <attribute name="version" default="${build.version}"/>
        <sequential>
            <replaceregexp replace="ext.license.name=@{license}"
                           file="${basedir}/cmd/sencha.cfg" byline="true">
                <regexp pattern="ext.license.name=(.*)"/>
            </replaceregexp>

            <replaceregexp replace="$ext-version: '@{version}'"
                           file="${package.dir}/classic/theme-base/sass/etc/all.scss">
                <regexp pattern="\$ext-version: '(.*)'"/>
            </replaceregexp>
            <replaceregexp replace="$ext-version: '@{version}'"
                           file="${package.dir}/modern/theme-base/sass/etc/all.scss">
                <regexp pattern="\$ext-version: '(.*)'"/>
            </replaceregexp>

            <local name="watermark.setting"/>

            <condition property="watermark.beta.setting" value="$ext-beta: true !default;">
                <equals arg1="@{license}" arg2="beta"/>
            </condition>

            <property name="watermark.beta.setting" value="$ext-beta: false !default;"/>

            <condition property="watermark.setting" value="$ext-trial: true !default;">
                <equals arg1="@{license}" arg2="trial"/>
            </condition>

            <property name="watermark.setting" value="$ext-trial: false !default;"/>

            <replaceregexp replace="${watermark.setting}"
                           file="${package.dir}/classic/theme-base/sass/etc/all.scss">
                <regexp pattern="\$ext-trial: (.*) !default;"/>
            </replaceregexp>
            <replaceregexp replace="${watermark.setting}"
                           file="${package.dir}/modern/theme-base/sass/etc/all.scss">
                <regexp pattern="\$ext-trial: (.*) !default;"/>
            </replaceregexp>
            <replaceregexp replace="${watermark.beta.setting}"
                           file="${package.dir}/classic/theme-base/sass/etc/all.scss">
                <regexp pattern="\$ext-beta: (.*) !default;"/>
            </replaceregexp>
            <replaceregexp replace="${watermark.beta.setting}"
                           file="${package.dir}/modern/theme-base/sass/etc/all.scss">
                <regexp pattern="\$ext-beta: (.*) !default;"/>
            </replaceregexp>

            <if>
                <available file="${basedir}/all-licenses/@{license}" type="dir"/>
                <then>
                    <copy todir="${basedir}/licenses" overwrite="true">
                        <fileset dir="${basedir}/all-licenses/@{license}" includes="**/*"/>
                    </copy>
                </then>
            </if>

        </sequential>
    </macrodef>

    <target name="beta" depends="init">
        <x-set-license license="beta"/>
    </target>

    <target name="commercial" depends="init">
        <x-set-license license="commercial"/>
    </target>

    <target name="gpl" depends="init">
        <x-set-license license="gpl"/>
    </target>

    <target name="trial" depends="init">
        <x-set-license license="trial"/>
    </target>

    <target name="dev" depends="init">
        <x-set-license license="dev"/>
    </target>

    <target name="init-version-properties" if="repo.dev.mode">
        <property name="build.number" value="12345"/>
        <property name="version.major" value="6"/>
        <property name="version.minor" value="0"/>
        <property name="version.patch" value="0"/>
        <property name="version.build" value="${build.number}"/>
        <property name="version.release" value="${version.major}.${version.minor}.${version.patch}"/>
        <property name="version.full" value="${version.release}.${version.build}"/>
        <property name="ext.version.number" value="${version.full}"/>
        <property name="ext.license.name" value="dev"/>
    </target>

    <target name="detect-git-hash" if="repo.dev.mode">
        <x-git-current-hash property="git.current.hash"/>
    </target>

    <target name="generate-version-properties"
            depends="detect-git-hash,init-version-properties"
            if="repo.dev.mode">
        <propertyfile file="${package.dir}/version.properties">
            <entry operation="="
                   key="version.major"
                   value="${version.major}"/>
            <entry operation="="
                   key="version.minor"
                   value="${version.minor}"/>
            <entry operation="="
                   key="version.patch"
                   value="${version.patch}"/>
            <entry operation="="
                   key="version.build"
                   value="${version.build}"/>
            <entry operation="="
                   key="version.release"
                   value="${version.release}"/>
            <entry operation="="
                   key="version.full"
                   value="${version.full}"/>
            <entry operation="="
                   key="git.current.hash"
                   value="${git.current.hash}"/>
        </propertyfile>
    </target>

    <target name="load-version-properties" depends="generate-version-properties">
        <property file="${package.dir}/version.properties"/>
    </target>
    
    <target name="set-build-production">
        <property name="build.production" value="1"/>
        
        <replace file="${basedir}/package.json"
                 token="&quot;local&quot;: true"
                 value="&quot;local&quot;: false"/>
    </target>
    
    <target name="apply-production-settings" if="build.production">
        <x-property-file file="${package.dir}/.sencha/package/sencha.cfg">
            <entry type="string" key="skip.examples" operation="=" value="1"/>
        </x-property-file>
    </target>

    <target name="copy-license" depends="init,load-version-properties">
        <tstamp>
            <!-- sets DSTAMP=yyyyMMdd, TSTAMP=hhmm -->
            <format property="THIS_YEAR" pattern="yyyy"/>
            <format property="tstamp.datetime" pattern="yyyy-MM-dd HH:mm:ss"/>
            <format property="tstamp.pretty" pattern="MMMM d, yyyy"/>
        </tstamp>
        <property name="product.name" value="Ext JS ${package.version}"/>

        <!--
        Ext JS is distrubted under GPL and Commercial licenses as well as a Beta license.
        This target allows the package build to be leveraged while swapping out the files
        with license information.
        -->

        <condition property="ext.license"
                   value="${package.dir}/../all-licenses/${ext.license.name}">
            <available file="${package.dir}/../all-licenses/${ext.license.name}" type="dir"/>
        </condition>

        <property name="ext.license"  value="${package.licenses.dir}"/>

        <echo> loading file-header.txt </echo>
        <!-- Load the appropriate license file header -->
        <local name="file-header"/>
        <loadfile property="file-header" srcfile="${ext.license}/file-header.txt">
            <filterchain>
                <expandproperties/>
            </filterchain>
        </loadfile>

        <echo> expanding file-header.txt </echo>
        <copy file="${ext.license}/file-header.txt"
              tofile="${package.licenses.dir}/file-header.txt.final"
              overwrite="true">
            <filterchain>
                <expandproperties/>
            </filterchain>
        </copy>

        <rename src="${package.licenses.dir}/file-header.txt.final"
                dest="${package.licenses.dir}/file-header.txt"
                replace="true"/>

        <!--
        Create a JS/CSS compatible file with header inside a "/* */" block.
        -->
        <echo file="${package.licenses.dir}/file-header.js"
              message="/*&#10;${file-header}&#10;*/&#10;"/>

        <fixcrlf file="${package.licenses.dir}/file-header.js"/>

        <!--
        Copy in the appropriate license.txt file
        -->
        <mkdir dir="${package.licenses.dir}"/>
        <copy file="${ext.license}/license.txt"
              tofile="${package.licenses.dir}/license.txt.final"
              overwrite="true">
            <filterchain>
                <expandproperties/>
            </filterchain>
        </copy>

        <rename src="${package.licenses.dir}/license.txt.final"
                dest="${package.licenses.dir}/license.txt"
                replace="true"/>

        <copy file="${package.licenses.dir}/license.txt"
              tofile="${package.dir}/LICENSE" overwrite="true"/>

        <!--
        Lay down the file header so we can append the rest from the compiler.
        -->
        <for list="ext,ext-all,ext-all-rtl,ext-modern,ext-modern-all"
             param="file">
            <sequential>
                <for list=".js,-debug.js" param="sfx">
                    <sequential>
                        <concat destfile="${build.dir}/@{file}@{sfx}.tmp" overwrite="true">
                            <fileset file="${package.licenses.dir}/file-header.js"/>
                            <fileset file="${build.dir}/@{file}@{sfx}"/>
                        </concat>
                        <delete>
                            <fileset file="${build.dir}/@{file}@{sfx}"/>
                        </delete>
                        <move file="${build.dir}/@{file}@{sfx}.tmp" tofile="${build.dir}/@{file}@{sfx}"/>
                    </sequential>
                </for>
            </sequential>
        </for>
    </target>

    <target name="bootstrap-classic" depends="init"
            description="Build Ext JS Bootstrap (Classic)">
        <x-echo>=========================================</x-echo>
        <x-echo>Building framework bootstrap (Classic)</x-echo>
        <x-echo>=========================================</x-echo>
        <x-sencha-command 
                dir="${package.dir}/classic/classic"
                inheritall="false">
            ant
                bootstrap
        </x-sencha-command>
    </target>
    
    <target name="bootstrap-modern" depends="init"
            description="Build Ext JS Bootstrap (Modern)">
        <x-echo>=========================================</x-echo>
        <x-echo>Building framework bootstrap (Modern)</x-echo>
        <x-echo>=========================================</x-echo>
        <x-sencha-command 
                dir="${package.dir}/modern/modern"
                inheritall="false">
            ant
                bootstrap
        </x-sencha-command>
    </target>
    
    <target name="bootstrap" depends="bootstrap-classic,bootstrap-modern"
            description="Build Ext JS Bootstrap"/>
    
    <target name="build-examples-index">
        <copy todir="${build.dir}/examples/" overwrite="true">
            <fileset dir="${basedir}/examples/">
                <include name="index.html"/>
                <include name="main.css"/>
                <include name="main.js"/>
                <include name="examples.js"/>
            </fileset>
        </copy>

        <copy todir="${build.dir}/examples/resources/" overwrite="true">
            <fileset dir="${basedir}/examples/resources/" includes="**/*"/>
        </copy>
    </target>
    
    <target name="build-non-app-examples">
        <for param="example">
            <dirset dir="${package.dir}/examples/classic" includes="*"/>
            <sequential>
                <if>
                    <not>
                        <available file="@{example}/app.json"/>
                    </not>
                    <then>
                        <local name="example.name"/>
                        <basename property="example.name" file="@{example}"/>
                        <copy todir="${build.dir}/examples/classic/${example.name}" overwrite="true">
                            <fileset dir="@{example}" includes="**/*"/>
                        </copy>
                    </then>
                </if>
            </sequential>
        </for>
    </target>
    
    <target name="prep-build-folder" depends="init,build-examples-index,build-non-app-examples">
        <copy todir="${build.dir}/welcome" overwrite="true">
            <fileset dir="${basedir}/welcome"/>
        </copy>

        <copy file="${basedir}/index.html"
              tofile="${build.dir}/index.html"
              overwrite="true"/>
        <replace file="${build.dir}/index.html"
                 token="build/examples/classic/index.html"
                 value="examples/classic/index.html"/>
        <replace file="${build.dir}/examples/classic/shared/include-ext.js"
                 token="Ext.devMode = 2;"
                 value="Ext.devMode = 0;"/>
        <replace file="${build.dir}/examples/classic/shared/include-ext.js"
                 token="Ext.devMode = 1;"
                 value="Ext.devMode = 0;"/>
    </target>
    
    <target name="-before-pkg" depends="init,copy-license">
        <if>
            <available file="${basedir}/release-notes.html"/>
            <then>
                <copy file="${basedir}/release-notes.html"
                      tofile="${build.dir}/release-notes.html"
                      overwrite="true"/>
            </then>
        </if>
    </target>

    <!--
    ******************************************************************
    Targets to easily run builds for specific items
    ******************************************************************
    -->

    <target name="build-classic"
            description="Build the Classic Theme (needed by unit tests)"
            depends="init">
        <x-sencha-command dir="${package.dir}/classic/theme-classic" inheritall="false">
            package
                build
        </x-sencha-command>
    </target>

    <target name="build-crisp"
            description="Build the Crisp Theme"
            depends="init">
        <x-sencha-command dir="${package.dir}/classic/theme-crisp" inheritall="false">
            package
                build
        </x-sencha-command>
    </target>

    <target name="build-gray"
            description="Build the Gray Theme"
            depends="init">
        <x-sencha-command dir="${package.dir}/classic/theme-gray" inheritall="false">
            package
                build
        </x-sencha-command>
    </target>

    <target name="build-neptune"
            description="Build the Neptune Theme"
            depends="init">
        <x-sencha-command dir="${package.dir}/classic/theme-neptune" inheritall="false">
            package
                build
        </x-sencha-command>
    </target>

    <target name="build-triton"
            description="Build the Triton Theme"
            depends="init">
        <x-sencha-command dir="${package.dir}/classic/theme-triton" inheritall="false">
            package
                build
        </x-sencha-command>
    </target>

    <target name="themes"
            description="Build all theme packages"
            depends="build-neptune,build-crisp,build-triton,build-classic,build-gray"/>

    <!--
    ******************************************************************
    Targets used to produce deployment builds
    ******************************************************************
    -->

    <target name="docs" depends="init" description="Builds docs for Ext JS and sub-packages">
        <mkdir dir="${package.build.dir}/docs"/>
        <exec executable="jsduck">
            <arg value="--output=${package.build.dir}/docs"/>
            <arg value="--config=${package.dir}/docs/config.json"/>
        </exec>
    </target>

    <!-- FIXME core should be the only package here, but we still have dependencies in the examples -->
    <property name="static.packages" value="core,amf,charts,soap"/>

    <target name="zip-impl" depends="init">
        <property name="staging.dir" value="${workspace.dir}/staging"/>
        <property name="display.version" value="${package.version}"/>
        <property name="folder.display.version" value="${display.version}"/>
        <property name="zip.display.version" value="${display.version}"/>
        <property name="ext.staging.dir" value="${staging.dir}/${package.name}-${folder.display.version}"/>
        <property name="ext.zip" value="${pkg.build.dir}/${package.name}-${zip.display.version}-${ext.license.name}.zip"/>

        <delete dir="${staging.dir}"/>
        <delete file="${ext.zip}"/>
        
        <mkdir dir="${ext.staging.dir}"/>
        <unzip src="${pkg.build.dir}/${pkg.file.name}" dest="${ext.staging.dir}"/>
        <copy todir="${ext.staging.dir}">
            <fileset dir="${package.dir}" includes="examples/**/*,build/examples/**/*"/>
        </copy>
        <copy todir="${ext.staging.dir}">
            <fileset dir="${package.dir}" includes="examples/**/*,build/examples/**/*"/>
        </copy>
        <if>
            <equals arg1="${repo.dev.mode}" arg2="true"/>
            <then>
                <copy todir="${ext.staging.dir}/.sencha/workspace">
                    <fileset dir="${package.dir}/../deployment_workspace" 
                             includes="**/*"/>
                </copy>
            </then>
        </if>

        <copy todir="${ext.staging.dir}">
            <fileset dir="${package.dir}"
                     includes="welcome/**/*,
                               build/**/*,
                               packages/core/test/specs*/**/*,
                               packages/core/test/resources/**/*,
                               packages/charts/test/specs*/**/*,
                               classic/classic/test/*.*,
                               classic/classic/test/resources/**/*,
                               classic/classic/test/local/**/*,
                               classic/classic/test/specs*/**/*,
                               modern/modern/test/*.*,
                               modern/modern/test/local/**/*,
                               modern/modern/test/specs*/**/*"/>
        </copy>
        <delete dir="${ext.staging.dir}/build/temp"/>
        <delete dir="${ext.staging.dir}/build/ext"/>

        <replace file="${ext.staging.dir}/examples/classic/shared/include-ext.js"
                 token="Ext.devMode = 2;"
                 value="Ext.devMode = 1;"/>

        <zip destfile="${ext.zip}" basedir="${staging.dir}" level="9"/>
    </target>

    <target name="zip" depends="set-build-production,build,prep-build-folder,zip-impl"
            description="Build package and create distribution ZIP file">
    </target>

    <target name="sass-rebuild" depends="init" if="false">
        <x-process-sub-packages>
            <x-sub-build dir="@{pkg-dir}"
                         target="sass"
                         inherit-version="${build.subpkgs.inherit.version}"/>
        </x-process-sub-packages>
        <x-process-examples>
            <x-sencha-command dir="@{example-dir}" inheritall="false">
                app
                each
                ant
                sass
            </x-sencha-command>
        </x-process-examples>
    </target>

    <target name="quick-build" depends="init,js,prep-build-folder,pkg"/>

    <target name="zip-only" depends="quick-build,zip-impl">
    </target>
    
    <macrodef name="x-sandbox">
        <attribute name="file"/>
        <attribute name="outfile" default="@{file}"/>
        <attribute name="jsPrefix"/>
        <attribute name="cssPrefix"/>
        <sequential>
            <concat destfile="@{outfile}">
                <header trimleading="yes" filtering="no">
                    (function(Ext) {
                    Ext.sandboxName = '@{jsPrefix}';
                    Ext.isSandboxed = true;
                    Ext.buildSettings = { baseCSSPrefix: "@{cssPrefix}", scopeResetCSS: true };
                </header>
                <filelist files="@{file}"/>
                <footer trimleading="yes" filtering="no">
                    })(this.@{jsPrefix} || (this.@{jsPrefix} = {}));
                </footer>
            </concat>
        </sequential>
    </macrodef>
    
    <target name="-after-build">
        <for list="ext-all,ext-all-rtl,ext-modern-all" param="file">
            <sequential>
                <for list=".js,-debug.js" param="sfx">
                    <sequential>
                        <x-sandbox file="${build.dir}/@{file}@{sfx}"
                                   outfile="${build.dir}/@{file}-sandbox@{sfx}"
                                   jsPrefix="Ext6"
                                   cssPrefix="x6-"/>
                    </sequential>
                </for>
            </sequential>
        </for>
    </target>

    <!--
    ******************************************************************
    Targets for Test
    ******************************************************************
    -->
    <!--
    <target name="test-ext" depends="bootstrap,build-classic,test-run"/>

    <target name="test-all" depends="test-ext"/>

    <target name="coverage-run" depends="init">
        <x-sencha-command dir="${package.dir}">
            <![CDATA[
                config
                    -prop
                    cmd-test.specs.test-json=${package.dir}/test/specs/coverageTest.json
                    -prop
                    cmd-test.coverage.results.dir=${workspace.dir}/test/coverage-results
                then
                package
                    test
                        run
            ]]>
        </x-sencha-command>
    </target>

    <target name="coverage-report" depends="init">
        <x-shell dir="${workspace.dir}/test">
            node istanbul_report.js ${workspace.dir}/test/coverage-results ${workspace.dir}
        </x-shell>
    </target>

    <target name="coverage" depends="coverage-run,coverage-report"/>
    -->

</project>
