<?xml version='1.0'?>
<project name="Gitana JavaScript Driver" basedir="." default="package">

    <property name="project.dir" value="."/>

    <property name="js.dir" value="${project.dir}/js"/>
    <property name="lib.dir" value="${project.dir}/lib"/>
    <property name="tests.dir" value="${project.dir}/tests"/>
    <property name="thirdparty.dir" value="${project.dir}/thirdparty"/>

    <property name="build.dir" value="${project.dir}/build"/>

    <property name="prep.dir" value="${build.dir}/prep"/>
    <property name="package.dir" value="${build.dir}/package"/>

    <property name="yui-compressor" value="${project.dir}/tool/yuicompressor-2.4.7.jar"/>

    <!-- load version info from file -->
    <property file="version.properties"/>

    <target name="setup" description="Creates all required directories" depends="clean">
        <mkdir dir="${build.dir}"/>
    </target>

    <target name="prep" description="Creates all required directories" depends="setup">
        <mkdir dir="${prep.dir}"/>
        <mkdir dir="${prep.dir}/js"/>
        <mkdir dir="${prep.dir}/lib"/>
        <mkdir dir="${prep.dir}/tests"/>
        <mkdir dir="${package.dir}"/>
        <mkdir dir="${package.dir}/js"/>
        <mkdir dir="${package.dir}/lib"/>

        <copy todir="${prep.dir}/js">
            <fileset dir="${js.dir}"/>
        </copy>
        <copy todir="${prep.dir}/thirdparty">
            <fileset dir="${thirdparty.dir}"/>
        </copy>
        <copy todir="${prep.dir}/lib">
            <fileset dir="${lib.dir}"/>
        </copy>
        <copy todir="${prep.dir}/tests">
            <fileset dir="${tests.dir}"/>
        </copy>
        <copy todir="${prep.dir}/">
            <fileset dir="${project.dir}">
                <include name="license.txt"/>
            </fileset>
        </copy>
    </target>

    <target name="concat-js" description="Concat all javascript files" depends="prep">

        <!-- Concatenate files for Gitana Driver -->
        <concat destfile="${prep.dir}/js/gitana-standalone.js">
            <filelist dir="${prep.dir}/js">
                <file name="gitana/compatibility.js"/>
            </filelist>
            <filelist dir="${prep.dir}/thirdparty">
                <file name="base.js"/>
                <file name="json2.js"/>
            </filelist>
            <filelist dir="${prep.dir}/js">

                <!-- core -->
                <file name="gitana/Gitana.js"/>
                <file name="gitana/GitanaError.js"/>
                <file name="gitana/http/Http.js"/>
                <file name="gitana/http/OAuth2Http.js"/>
                <file name="gitana/Chain.js"/>
                <file name="gitana/Chainable.js"/>
                <file name="gitana/Response.js"/>
                <file name="gitana/AuthInfo.js"/>
                <file name="gitana/SystemMetadata.js"/>
                <file name="gitana/Timestamp.js"/>
                <file name="gitana/Utils.js"/>

                <!-- async -->
                <file name="gitana/async/Defer.js"/>
                <file name="gitana/async/Promise.js"/>
                <file name="gitana/async/Queue.js"/>

                <!-- common implementation methods -->
                <file name="gitana/methods/GitanaMethods.js"/>

                <!-- auto configuration for cloud hosted platform -->
                <file name="gitana/GitanaAutoConfig.js"/>

                <!-- object factory -->
                <file name="gitana/ObjectFactory.js"/>

                <!-- abstracts -->
                <file name="gitana/AbstractPersistable.js"/>
                <file name="gitana/AbstractMap.js"/>
                <file name="gitana/AbstractObject.js"/>
                <file name="gitana/AbstractSelfableObject.js"/>
                <file name="gitana/AbstractSelfableACLObject.js"/>

                <!-- core -->
                <file name="gitana/DataStore.js"/>
                <file name="gitana/ContainedDataStore.js"/>
                <file name="gitana/BinaryAttachment.js"/>
                <file name="gitana/BinaryAttachmentMap.js"/>
                <file name="gitana/AuditRecord.js"/>
                <file name="gitana/AuditRecordMap.js"/>
                <file name="gitana/Team.js"/>
                <file name="gitana/TeamMap.js"/>
                <file name="gitana/TeamMember.js"/>
                <file name="gitana/TeamMemberMap.js"/>
                <file name="gitana/Activity.js" />
                <file name="gitana/ActivityMap.js" />
                <file name="gitana/Role.js"/>
                <file name="gitana/RoleMap.js"/>

                <!-- DATASTORE: cluster -->
                <file name="gitana/cluster/Cluster.js" />
                <!-- objects -->
                <file name="gitana/cluster/AbstractClusterObject.js" />
                <file name="gitana/cluster/Job.js" />
                <file name="gitana/cluster/JobMap.js" />
                <file name="gitana/cluster/LogEntry.js" />
                <file name="gitana/cluster/LogEntryMap.js" />
                <!-- custom jobs -->
                <file name="gitana/cluster/CopyJob.js" />
                <file name="gitana/cluster/TransferImportJob.js" />
                <file name="gitana/cluster/TransferExportJob.js" />


                <!-- DATASTORE: platform -->
                <file name="gitana/platform/Platform.js" />
                <!-- objects -->
                <file name="gitana/platform/AbstractPlatformDataStore.js" />
                <file name="gitana/platform/AbstractPlatformObject.js" />
                <file name="gitana/platform/AbstractPlatformObjectMap.js" />
                <file name="gitana/platform/Stack.js" />
                <file name="gitana/platform/StackMap.js" />
                <file name="gitana/platform/Client.js" />
                <file name="gitana/platform/ClientMap.js" />
                <file name="gitana/platform/AuthenticationGrant.js" />
                <file name="gitana/platform/AuthenticationGrantMap.js" />
                <file name="gitana/platform/PlatformDataStoreMap.js" />
                <!-- billing -->
                <file name="gitana/platform/BillingProviderConfiguration.js" />
                <file name="gitana/platform/BillingProviderConfigurationMap.js" />
                <!-- projects -->
                <file name="gitana/platform/Project.js" />
                <file name="gitana/platform/ProjectMap.js" />
                <!-- service descriptors -->
                <file name="gitana/platform/Descriptor.js" />
                <file name="gitana/platform/DescriptorMap.js" />
                <!-- scheduled work items -->
                <file name="gitana/platform/ScheduledWork.js" />
                <file name="gitana/platform/ScheduledWorkMap.js" />
                <!-- reports -->
                <file name="gitana/platform/Report.js" />
                <file name="gitana/platform/ReportMap.js" />
                <!-- workflow -->
                <file name="gitana/platform/WorkflowInstance.js" />
                <file name="gitana/platform/WorkflowInstanceMap.js" />
                <file name="gitana/platform/WorkflowModel.js" />
                <file name="gitana/platform/WorkflowModelMap.js" />
                <file name="gitana/platform/WorkflowTask.js" />
                <file name="gitana/platform/WorkflowTaskMap.js" />
                <file name="gitana/platform/WorkflowComment.js" />
                <file name="gitana/platform/WorkflowCommentMap.js" />
                <!-- extends -->
                <file name="gitana/platform/extends/ClientMethods.js" />


                <!-- DATASTORE: application -->
                <file name="gitana/application/Application.js" />
                <file name="gitana/application/ApplicationMap.js" />
                <!-- objects -->
                <file name="gitana/application/AbstractApplicationObject.js" />
                <file name="gitana/application/Settings.js" />
                <file name="gitana/application/SettingsMap.js" />
                <file name="gitana/application/Email.js" />
                <file name="gitana/application/EmailMap.js" />
                <file name="gitana/application/EmailProvider.js" />
                <file name="gitana/application/EmailProviderMap.js" />
                <file name="gitana/application/Registration.js" />
                <file name="gitana/application/RegistrationMap.js" />
                <file name="gitana/application/PageRendition.js" />
                <file name="gitana/application/PageRenditionMap.js" />


                <!-- DATASTORE: warehouse -->
                <file name="gitana/warehouse/Warehouse.js" />
                <file name="gitana/warehouse/WarehouseMap.js" />
                <!-- objects -->
                <file name="gitana/warehouse/AbstractWarehouseObject.js" />
                <file name="gitana/warehouse/AbstractWarehouseObjectMap.js" />
                <file name="gitana/warehouse/AbstractReportableWarehouseObject.js" />
                <file name="gitana/warehouse/Interaction.js" />
                <file name="gitana/warehouse/InteractionMap.js" />
                <file name="gitana/warehouse/InteractionApplication.js" />
                <file name="gitana/warehouse/InteractionApplicationMap.js" />
                <file name="gitana/warehouse/InteractionSession.js" />
                <file name="gitana/warehouse/InteractionSessionMap.js" />
                <file name="gitana/warehouse/InteractionPage.js" />
                <file name="gitana/warehouse/InteractionPageMap.js" />
                <file name="gitana/warehouse/InteractionNode.js" />
                <file name="gitana/warehouse/InteractionNodeMap.js" />
                <file name="gitana/warehouse/InteractionUser.js" />
                <file name="gitana/warehouse/InteractionUserMap.js" />
                <file name="gitana/warehouse/InteractionContinent.js" />
                <file name="gitana/warehouse/InteractionContinentMap.js" />
                <file name="gitana/warehouse/InteractionCountry.js" />
                <file name="gitana/warehouse/InteractionCountryMap.js" />
                <file name="gitana/warehouse/InteractionCity.js" />
                <file name="gitana/warehouse/InteractionCityMap.js" />
                <file name="gitana/warehouse/InteractionRegion.js" />
                <file name="gitana/warehouse/InteractionRegionMap.js" />
                <file name="gitana/warehouse/InteractionPostalCode.js" />
                <file name="gitana/warehouse/InteractionPostalCodeMap.js" />
                <file name="gitana/warehouse/InteractionUserAgent.js" />
                <file name="gitana/warehouse/InteractionUserAgentMap.js" />
                <file name="gitana/warehouse/InteractionOperatingSystem.js" />
                <file name="gitana/warehouse/InteractionOperatingSystemMap.js" />
                <file name="gitana/warehouse/InteractionDevice.js" />
                <file name="gitana/warehouse/InteractionDeviceMap.js" />
                <file name="gitana/warehouse/InteractionReport.js" />
                <file name="gitana/warehouse/InteractionReportMap.js" />
                <file name="gitana/warehouse/InteractionReportEntry.js" />
                <file name="gitana/warehouse/InteractionReportEntryMap.js" />
                <file name="gitana/warehouse/ConversionTrigger.js" />
                <file name="gitana/warehouse/ConversionTriggerMap.js" />


                <!-- DATASTORE: directory -->
                <file name="gitana/directory/Directory.js" />
                <file name="gitana/directory/DirectoryMap.js" />
                <!-- objects -->
                <file name="gitana/directory/AbstractDirectoryObject.js" />
                <file name="gitana/directory/Identity.js" />
                <file name="gitana/directory/IdentityMap.js" />
                <file name="gitana/directory/Connection.js" />
                <file name="gitana/directory/ConnectionMap.js" />


                <!-- DATASTORE: domain -->
                <file name="gitana/domain/Domain.js" />
                <file name="gitana/domain/DomainMap.js" />
                <!-- objects -->
                <file name="gitana/domain/AbstractDomainObject.js" />
                <file name="gitana/domain/DomainPrincipal.js" />
                <file name="gitana/domain/DomainPrincipalMap.js" />
                <!-- extends -->
                <file name="gitana/domain/extends/DomainGroup.js" />
                <file name="gitana/domain/extends/DomainUser.js" />


                <!-- DATASTORE: registrar -->
                <file name="gitana/registrar/Registrar.js" />
                <file name="gitana/registrar/RegistrarMap.js" />
                <!-- objects -->
                <file name="gitana/registrar/AbstractRegistrarObject.js" />
                <file name="gitana/registrar/Meter.js" />
                <file name="gitana/registrar/MeterMap.js" />
                <file name="gitana/registrar/Plan.js" />
                <file name="gitana/registrar/PlanMap.js" />
                <file name="gitana/registrar/Tenant.js" />
                <file name="gitana/registrar/TenantMap.js" />


                <!-- DATASTORE: repository -->
                <file name="gitana/repository/Repository.js" />
                <file name="gitana/repository/RepositoryMap.js" />
                <!-- objects -->
                <file name="gitana/repository/AbstractRepositoryObject.js" />
                <file name="gitana/repository/AbstractNode.js" />
                <file name="gitana/repository/NodeAttachment.js" />
                <file name="gitana/repository/NodeAttachmentMap.js" />
                <file name="gitana/repository/Association.js" />
                <file name="gitana/repository/Branch.js" />
                <file name="gitana/repository/Changeset.js" />
                <file name="gitana/repository/Node.js" />
                <file name="gitana/repository/Release.js" />
                <file name="gitana/repository/BranchMap.js" />
                <file name="gitana/repository/ChangesetMap.js" />
                <file name="gitana/repository/NodeMap.js" />
                <file name="gitana/repository/ReleaseMap.js" />
                <file name="gitana/repository/TraversalResults.js" />
                <!-- types -->
                <file name="gitana/repository/types/Definition.js" />
                <file name="gitana/repository/types/Form.js" />
                <file name="gitana/repository/types/HasFormAssociation.js" />
                <file name="gitana/repository/types/HasTranslationAssociation.js" />
                <file name="gitana/repository/types/Person.js" />


                <!-- DATASTORE: vault -->
                <file name="gitana/vault/Vault.js" />
                <file name="gitana/vault/VaultMap.js" />
                <!-- objects -->
                <file name="gitana/vault/AbstractVaultObject.js" />
                <file name="gitana/vault/Archive.js" />
                <file name="gitana/vault/ArchiveMap.js" />


                <!-- DATASTORE: web host -->
                <file name="gitana/webhost/WebHost.js" />
                <file name="gitana/webhost/WebHostMap.js" />
                <!-- objects -->
                <file name="gitana/webhost/AbstractWebHostObject.js" />
                <file name="gitana/webhost/AutoClientMapping.js" />
                <file name="gitana/webhost/TrustedDomainMappingMap.js" />
                <file name="gitana/webhost/TrustedDomainMapping.js" />
                <file name="gitana/webhost/AutoClientMappingMap.js" />
                <file name="gitana/webhost/DeployedApplication.js" />
                <file name="gitana/webhost/DeployedApplicationMap.js" />

                <!-- utilities -->
                <file name="gitana/Context.js" />

                <!-- application helper -->
                <file name="gitana/AppHelper.js" />

                <!-- async -->
                <file name="gitana/async/Defer.js" />
                <file name="gitana/async/Promise.js" />

                <!-- transaction support -->
                <file name="gitana/Transaction.js" />


            </filelist>
        </concat>

        <!-- wrap Gitana as an UMD module -->
        <antcall target="umd">
            <param name="scriptFile" value="${prep.dir}/js/gitana-standalone.js"/>
            <param name="outputFile" value="${package.dir}/js/gitana.js"/>
            <param name="name" value="gitana" />
            <param name="variableName" value="Gitana" />
            <param name="dependencies" value="[]" />
        </antcall>
        <delete file="${prep.dir}/js/gitana-standalone.js"/>

    </target>

    <target name="compress" description="Compress the javascript" depends="concat-js">
        <echo>Compressing Javascript...</echo>
        <apply executable="java" parallel="false" dest="${build.dir}/package">
            <fileset dir="${build.dir}/package">
                <include name="**/*.js"/>
                <exclude name="**/*-min.js"/>
                <exclude name="**/*.min.js"/>
            </fileset>
            <arg line="-jar"/>
            <arg path="${yui-compressor}"/>
            <arg line="-o"/>
            <targetfile/>
            <srcfile/>
            <mapper type="glob" from="*.js" to="*.min.js"/>
        </apply>
    </target>

    <target name="package" depends="clean,concat-js,compress">
        <mkdir dir="dist"/>
        <copy todir="dist">
            <fileset dir="${package.dir}/js">
                <include name="gitana.js" />
                <include name="gitana.min.js" />
            </fileset>
            <filterchain>
                <tokenfilter>
                    <replacestring from="__VERSION__" to="${version}"/>
                </tokenfilter>
            </filterchain>
        </copy>
    </target>

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

    <target name="umd">

        <!-- load the script -->
        <loadresource property="script">
            <file file="${scriptFile}"/>
        </loadresource>

        <!-- create a temp file holding our UMD wrapped script -->
        <copy file="${prep.dir}/lib/umd/wrapper.js" tofile="${scriptFile}.temp" overwrite="true" filtering="true">
            <filterchain>
                <tokenfilter>
                    <replacestring from="/** BUILD_INSERT_SCRIPT **/" to="${script}"/>
                    <replacestring from="/** BUILD_INSERT_NAME **/" to="${name}"/>
                    <replacestring from="/** BUILD_INSERT_DEPENDENCIES **/" to="${dependencies}"/>
                    <replacestring from="/** BUILD_INSERT_VARIABLE_NAME **/" to="${variableName}"/>
                </tokenfilter>
            </filterchain>
        </copy>
        <concat destfile="${outputFile}">
            <filelist dir="${prep.dir}">
                <file name="js/license.js"/>
            </filelist>
            <filelist>
                <file name="${scriptFile}.temp"/>
            </filelist>
        </concat>
        <delete file="${scriptFile}.temp"/>
    </target>

    <fileset id="test-files" dir="${project.dir}">
        <include name="js/**/*"/>
        <include name="tests/**/*"/>
        <include name="*.html"/>
    </fileset>

</project>
