<project name="ext-50-plugin" basedir=".">
    <!--
    IMPORTANT: This file is private and should not be edited.
    -->
    <import file="${cmd.config.dir}/plugin.xml"/>

    <target name="init-properties" 
            depends="cmd-root-plugin.init-properties">
        <!--init the defualt app path here-->
        <property name="args.path" location="${basedir}/../../"/>
        <property name="templates.dir"
                  location="${framework.config.dir}/templates"/>
        <property name="templates.common.dir"
                  location="${cmd.config.dir}/templates"/>
    </target>

    <!--
        Copies select files from the base sdk directory into the  new application
        directory structure
    -->
    <target name="copy-framework-to-workspace-impl"
            depends="cmd-root-plugin.copy-framework-to-workspace-impl">
        <property name="framework.target.dir" location="${framework.path}"/>
        <copy todir="${framework.target.dir}" overwrite="true">
            <fileset dir="${framework.dir}"
                     includes=".sencha/**/*,
                               src/**/*,
                               overrides/**/*,
                               build/*.js,
                               cmd/**/*,
                               locale/**/*,
                               *.js,
                               *.json,
                               build.xml,
                               license.txt,
                               licenses/**/*,
                               version.properties,
                               packages/**/*"/>
        </copy>

        <copy todir="${framework.target.dir}/src/ux" overwrite="true">
            <fileset dir="${framework.dir}/examples/ux"
                     includes="**/*"/>
        </copy>
    </target>

    <target name="generate-starter-app" unless="skip.starter.app">
        <property name="templates.app.starter.dir" value="${templates.dir}/StarterApp"/>

        <x-generate dir="${templates.app.starter.dir}" 
                    todir="${args.path}"
                    store="${args.path}/${senchadir}/app/codegen.json"
                    basedir="${args.path}">
            <param name="name" value="${args.name}"/>
            <param name="appName" value="${args.name}"/>
            <!-- These are needed for the theme template-->
            <param name="themeName" value="${args.themeName}"/>
            <param name="controllerName" value="${args.controllerName}"/>
            <param name="controllerFileName" value="${args.controllerName}"/>
            <param name="viewName" value="${args.viewName}"/>
            <param name="viewFileName" value="${args.viewName}"/>
            <param name="frameworkName" value="${framework.name}"/>
            <param name="frameworkKey" value="${framework.key}"/>
            <param name="frameworkPath" value="${framework.path}"/>
            <param name="packagesRelPath" value="${packages.extract.path}"/>
            <param name="senchadir" value="${senchadir}"/>
            <param name="uniqueId" value="${app.id}"/>
            <!-- placeholders for mvc structures -->
            <param name="appModels" value=""/>
            <param name="appViews" value=""/>
            <param name="appControllers" value=""/>
            <param name="appStores" value=""/>
            <param name="controllerNamespace" value="${args.name}.controller"/>
            <param name="modelNamespace" value="${args.name}.model"/>
            <param name="viewNamespace" value="${args.name}.view"/>
        </x-generate>
        
        <mkdir dir="${args.path}/app/model"/>
        <mkdir dir="${args.path}/app/store"/>
        <mkdir dir="${args.path}/resources"/>
        <mkdir dir="${args.path}/overrides"/>
        <mkdir dir="${args.path}/sass"/>
        <mkdir dir="${args.path}/sass/src"/>
        <mkdir dir="${args.path}/sass/var"/>
        <mkdir dir="${args.path}/sass/etc"/>
    </target>

    <!--
        Generates an application structure from templates
        CLI args available as:
        args.name => the application name
        args.path => the target path of the new application
    -->
    <target name="generate-app-impl">
        <x-guid property="app.json.id"/>
        <property name="app.id" value="${app.json.id}"/>

        <!--default these from args if new app-->
        <property name="app.dir" value="${args.path}"/>
        <property name="app.name" value="${args.name}"/>

        <property name="app.cmd.version" value="3.0.0.250"/>
        
        <local name="app.sencha.cfg"/>
        <property name="app.sencha.cfg" location="${app.dir}/.sencha/app/sencha.cfg"/>
        
        <local name="workspace.sencha.cfg"/>
        <property name="workspace.sencha.cfg" location="${workspace.dir}/.sencha/workspace/sencha.cfg"/>
        
        <x-get-relative-path from="${framework.dir}" to="${args.path}"
                             property="framework.path"/>
        
        <x-get-relative-path from="${workspace.packages.dir}" to="${args.path}"
                             property="packages.extract.path"/>
        
        <if>
            <isset property="args.upgrade"/>
            <then>
                <x-upgrade appcfg="${app.sencha.cfg}"
                           workspacecfg="${workspace.sencha.cfg}"/>
                <if>
                    <equals arg1="${workspace.build.dir}" arg2="${workspace.dir}/build/${app.name}"/>
                    <then>
                        <x-property-file file="${workspace.config.dir}/sencha.cfg">
                            <entry type="string" key="workspace.build.dir" operation="="
                                   value="$${workspace.dir}/build"/>
                        </x-property-file>
                    </then>
                </if>
            </then>
        </if>
        
        <!--first, evaluate the shared templates-->
        <x-generate dir="${templates.common.dir}/SenchaCommon"
                    todir="${args.path}/${senchadir}/app"
                    store="${args.path}/${senchadir}/app/codegen.json"
                    basedir="${args.path}"
                    original="${legacy.template.path}/App">
            <param name="senchadir" value="${senchadir}"/>
        </x-generate>

        <x-generate dir="${templates.common.dir}/app" todir="${args.path}"
                    store="${args.path}/${senchadir}/app/codegen.json"
                    basedir="${args.path}"
                    original="${legacy.template.path}/App">
            <param name="senchadir" value="${senchadir}"/>
        </x-generate>
        
        <!--now, evaluate the plugin-specific templates-->
        <x-generate dir="${templates.dir}/App" 
                    todir="${args.path}"
                    store="${args.path}/${senchadir}/app/codegen.json"
                    basedir="${args.path}"
                    original="${legacy.template.path}/App"
                    keepMergeFiles="false">
            <param name="name" value="${args.name}"/>
            <param name="appName" value="${args.name}"/>
            <!-- These are needed for the theme template-->
            <param name="themeName" value="${args.themeName}"/>
            <param name="controllerName" value="${args.controllerName}"/>
            <param name="controllerFileName" value="${args.controllerName}"/>
            <param name="viewName" value="${args.viewName}"/>
            <param name="viewFileName" value="${args.viewName}"/>
            <param name="frameworkName" value="${framework.name}"/>
            <param name="frameworkKey" value="${framework.key}"/>
            <param name="frameworkPath" value="${framework.path}"/>
            <param name="packagesRelPath" value="${packages.extract.path}"/>
            <param name="senchadir" value="${senchadir}"/>
            <param name="uniqueId" value="${app.id}"/>
            <!-- placeholders for mvc structures -->
            <param name="appModels" value=""/>
            <param name="appViews" value=""/>
            <param name="appControllers" value=""/>
            <param name="appStores" value=""/>
            <param name="controllerNamespace" value="${args.name}.controller"/>
            <param name="modelNamespace" value="${args.name}.model"/>
            <param name="viewNamespace" value="${args.name}.view"/>
        </x-generate>
        
        <if>
            <x-is-merge-conflict file="${app.sencha.cfg}"/>
            <then>
                <!-- we must be an upgrade because you can't have a conflict otherwise -->
                
                <echo file="${app.sencha.cfg}" append="true"><![CDATA[
<<<<<<< Generated
app.sass.namespace=$${app.name}
app.sass.etcpath=$${app.dir}/sass/etc/all.scss
app.sass.varpath=$${app.dir}/sass/src/var
app.sass.srcpath=$${app.dir}/sass/src
app.theme=ext-theme-classic
app.cmd.version=${cmd.version}
=======
>>>>>>> Custom]]>
                </echo>
            </then>
            <else>
                <if>
                    <not>
                        <isset property="args.upgrade"/>
                    </not>
                    <then>
                        <antcall target="generate-starter-app"/>
                    </then>
                </if>
                
                <x-property-file file="${app.sencha.cfg}">
                    <!--apply defaults if not present-->
                    <entry type="string" key="app.sass.namespace" operation="="
                           default="$${app.name}"/>
                    <entry type="string" key="app.sass.etcpath" operation="="
                           default="$${app.dir}/sass/etc/all.scss"/>
                    <entry type="string" key="app.sass.varpath" operation="="
                           default="$${app.dir}/sass/src/var"/>
                    <entry type="string" key="app.sass.srcpath" operation="="
                           default="$${app.dir}/sass/src"/>
                    <entry type="string" key="app.theme" operation="="
                           default="ext-theme-classic"/>
                </x-property-file>
            </else>
        </if>
        
        <antcall target="after-upgrade"/>

        <if>
            <isset property="merge.conflicts"/>
            <then>
                <x-ant-call target="-after-generate-app"/>
                <fail>
                    Upgrade encountered ${merge.conflicts} merge conflicts. Please resolve all merge conflicts then run 'sencha app refresh'.
                </fail>
            </then>
            <elseif>
                <x-is-true value="${args.refresh}"/>
                <then>
                    <antcall target="refresh-generated-app"/>
                </then>
            </elseif>
        </if>
    </target>

    <!--
        Adds a new theme template application
        args.name => the theme name
        args.cssPath => the path to the css file
    -->
    <target name="generate-theme-impl">
        <local name="new.package.dir"/>
        <property name="new.package.dir" 
                  location="${workspace.packages.dir}/${args.name}"/>

        <x-sencha-command dir="${workspace.packages.dir}" inheritAll="false">
            generate
                package
                    --framework=ext
                    --type=theme
                    --extend=${args.extend}
                    --name=${args.name}
        </x-sencha-command>
    </target>

    <!--
        Adds a controller class to the application
        CLI args available as:
        args.name => the application name
        args.path => the target path of the new application
    -->
    <target name="generate-controller-impl">
        <x-generate dir="${templates.dir}/Controller"
                    todir="${args.path}/app/controller"
                    store="${args.path}/${senchadir}/app/codegen.json"
                    basedir="${args.path}">
            <param name="appName" value="${app.name}"/>
            <param name="name" value="${args.name}"/>
            <param name="controllerName" value="${args.name}"/>
            <param name="controllerFileName" value="${args.filename}"/>
            <param name="controllerNamespace" value="${app.name}.controller"/>
        </x-generate>
    </target>

    <!--
        Adds a profile class to the application
        CLI args available as:
        args.name => the application name
        args.path => the target path of the new application
    -->
    <target name="generate-profile-impl">
        <x-generate dir="${templates.dir}/Profile" todir="${args.path}/app/profile"
                    store="${args.path}/${senchadir}/app/codegen.json"
                    basedir="${args.path}">
            <param name="appName" value="${app.name}"/>
            <param name="name" value="${args.name}"/>
            <param name="profileName" value="${args.name}"/>
            <param name="profileFileName" value="${args.filename}"/>
        </x-generate>
    </target>

    <!--
        Adds a model class to the application
        CLI args available as:
        args.name => the model name
        args.path => the target path of the application
        args.fields => the field definitions to add
        -fields=name:val,other:otherval,...
    -->
    <target name="generate-model-impl">
        <property name="section" value="model"/>

        <x-generate dir="${templates.dir}/Model" todir="${args.path}/app/model"
                    store="${args.path}/${senchadir}/app/codegen.json"
                    basedir="${args.path}">
            <param name="appName" value="${app.name}"/>
            <param name="baseClass" value="${args.base}"/>
            <param name="name" value="${args.name}"/>
            <param name="modelFileName" value="${args.filename}"/>
            <param name="modelNamespace" value="${app.name}.model"/>
            <param name="fields" value="${args.fields}"/>
        </x-generate>
    </target>

    <!--
        Adds a model class to the application
        CLI args available as:
        args.name => the model name
        args.path => the target path of the application
        args.fields => the field definitions to add
        -fields=name:val,other:otherval,...
    -->
    <target name="generate-view-impl">
        <property name="section" value="model"/>

        <x-generate dir="${templates.dir}/View" todir="${args.path}/app/view"
                    store="${args.path}/${senchadir}/app/codegen.json"
                    basedir="${args.path}">
            <param name="appName" value="${app.name}"/>
            <param name="name" value="${args.name}"/>
            <param name="viewName" value="${args.name}"/>
            <param name="viewFileName" value="${args.filename}"/>
            <param name="viewMvcName" value="${args.mvcName}"/>
            <param name="viewBaseClass" value="${args.viewBaseClass}"/>
            <param name="enableViewJson" value="${args.view.json}"/>
        </x-generate>
        <if>
            <x-is-false value="${args.view.json}"/>
            <then>
                <delete file="${args.path}/app/view/${args.filename}.view.json"/>
            </then>
        </if>
    </target>

    <target name="app-refresh-impl">
        <ant dir="${app.dir}"
             inheritall="true"
             inheritrefs="true"
             target="refresh">
        </ant>
    </target>

    <target name="app-upgrade-impl">
        <x-echo>Upgrading to Ext JS 5.x</x-echo>
        <if>
            <not>
                <isset property="${workspace.packages.dir}"/>
            </not>
            <then>
                <property name="workspace.packages.dir" 
                          value="${workspace.dir}/packages"/>
                <propertyfile file="${workspace.config.dir}/sencha.cfg">
                    <entry key="workspace.packages.dir"
                           operation="="
                           default="$${workspace.dir}/packages"
                           type="string"/>
                </propertyfile>
            </then>
        </if>

        <x-script-def name="x-app-upgrade" 
                      src="${framework.config.dir}/app-upgrade.js">
            <script src="${cmd.config.dir}/ant/JSON.js"/>
            <script src="${cmd.config.dir}/ant/ant-util.js"/>
        </x-script-def>
        <x-app-upgrade/>
    </target>


    <target name="app-explain">
        <ant dir="${app.dir}"
             inheritall="true"
             inheritrefs="true"
             target="explain">
            <property name="args.targetName" value="${args.targetName}"/>
        </ant>
    </target>
</project>