<project name="x-refresh-impl">
    <import file="bootstrap-impl.xml"/>

    <target name="-init-refresh" depends="-detect-app-build-properties">
        <property name="app.bootstrap.base.path"
                  value="${build.bootstrap.metadata.dir}"/>
    </target>

    <target name="-refresh-app-manifest" if="build.enable.appmanifest" depends="-init-refresh">

        <local name="manifest.root.excludes"/>
        <condition property="manifest.root.excludes" value="${app.bootstrap.manifest.exclude}">
            <isset property="app.bootstrap.manifest.exclude"/>
        </condition>

        <condition property="manifest.root.excludes" value="loadOrder">
            <and>
                <x-is-true value="${app.has.js.sdk}"/>
                <not>
                    <isset property="app.watch.enabled"/>
                </not>
                <not>
                    <isset property="app.uses"/>
                </not>
            </and>
        </condition>
        <property name="manifest.root.excludes" value=""/>

        <property name="metadata.bootstrap.base.path" value="${build.bootstrap.metadata.dir}"/>

        <condition property="refresh.file.filter" value="framework,package-core,package-${toolkit.name}">
            <x-is-true value="${framework.isV6}"/>
        </condition>
        <property name="refresh.file.filter" value="framework,package-sencha-core"/>

        <if>
            <x-is-true value="${app.has.js.sdk}"/>
            <then>
                <x-compile refid="${compiler.ref.id}">
                    <![CDATA[
                        include
                            -all
                        and
                        exclude
                            -tag=${refresh.file.filter}
                        and
                        exclude
                            -file=Boot.js
                        and
                        save
                            bootstrap
                    ]]>
                </x-compile>
            </then>
            <else>
                <x-compile refid="${compiler.ref.id}">
                    <![CDATA[
                        include
                            -all
                        and
                        exclude
                            -file=Boot.js
                        and
                        save
                            bootstrap
                    ]]>
                </x-compile>
            </else>
        </if>

        <x-get-relative-path from="${build.bootstrap.page.dir}"
                             to="${build.json.bootstrap.path}"
                             property="build.json.bootstrap.rel.path"/>

<x-file-write file="${app.bootstrap.js}">var Ext = Ext || {};
Ext.manifest = Ext.manifest || "${build.json.bootstrap.rel.path}";
</x-file-write>

        <x-compile refid="${compiler.ref.id}">
            <![CDATA[
                microload
                    -operation=microloader
                    -microloaderPath=${app.microloader.bootstrap}
                    -bootPath=${build.boot.file}
                    +append
                    -out=${app.bootstrap.js}
                and
                microload
                    -operation=manifest
                    -jsonp=Ext.Microloader.setManifest
                    -fashion=${use.fashion}
                    -bootstrap
                    +ignoreDisabled
                    -exclude=${manifest.root.excludes}
                    -tpl={0}
                    -basePath=${metadata.bootstrap.base.path}
                    -out=${build.json.bootstrap.path}
            ]]>
        </x-compile>
    </target>
    
    <target name="-refresh-app-bootstrap" unless="build.enable.appmanifest" depends="-init-refresh">

        <!--regenerate class system metadata-->
        <x-bootstrap file="${app.bootstrap.js}"
                     basedir="${app.bootstrap.base.path}"
                     overrideTpl="${bootstrap.override.tpl}"
                     overrideTplType="${bootstrap.override.tpltype}"
                     includeOverrides="${bootstrap.include.overrides}"
                     includeBoot="${bootstrap.include.boot}"/>

        <!--
        create / overwrite bootstrap.json, which will be used
        by the default development.js microloader
        -->

<x-file-write file="${build.json.bootstrap.path}">
/**
 * This file is generated by Sencha Cmd and should NOT be edited. It is a
 * combination of content from app.json, and all required package's package.json
 * files.  Customizations should be placed in app.json.
 */
</x-file-write>
        <x-compile refid="${compiler.ref.id}">
            <![CDATA[
            microload
                -operation=json
                -bootstrap
                -append
                -tpl={0}
                -basePath=${app.bootstrap.base.path}
                -out=${build.json.bootstrap.path}
            ]]>
        </x-compile>
    </target>
    
    <!--
    Refreshes the application's bootstrap javascript and microloader manifest
    '-detect-app-build-properties' is defined in js-impl.xml
    -->
    <target name="-refresh-app" 
            depends="-detect-app-build-properties">
        <if>
            <equals arg1="development" arg2="${build.environment}"/>
            <then>
                <x-ant-call target="-refresh-app-manifest,-refresh-app-bootstrap"/>
            </then>
        </if>
    </target>
    
    <!--
    Refresh app
    -->
    <target name="-before-refresh"/>
    <target name="-refresh" depends="-refresh-app"/>
    <target name="-after-refresh"/>
</project>