<?xml version="1.0"?>
<!--

  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

-->
<project name="asjsPublisher" default="main" basedir=".">
    <property file="${basedir}/build.properties" 
        description="Properties for the ASJS Publisher" />

    <condition property="isWindows">
        <os family="windows" />
    </condition>
    
    <basename property="PROJECT_NAME" 
        file="${project.main.file}" suffix=".as" />
    
    <dirname property="DIR_PROJECT" 
        file="${project.main.file}/.." />

    <property name="DIR_CLOSURE_THIRD_PARTY" 
        location="${DIR_CLOSURE_LIBRARY}/third_party/closure/goog" />
    <property name="DIR_CLOSURE_SCRIPTS" 
        location="${DIR_CLOSURE_LIBRARY}/closure/bin/build" />
    
    <property name="DIR_INTERMEDIATE_APPLICATION" 
        location="${DIR_PROJECT}/js-intermediate" />
    <property name="DIR_RELEASE_APPLICATION" 
        location="${DIR_PROJECT}/js-release" />

    <property name="FILE_DEPS" 
        location="${DIR_INTERMEDIATE_APPLICATION}/deps.js" />
    <property name="FILE_HTML_TEMPLATE" 
        location="index.html.template" />
    <property name="FILE_OUTPUT_MAIN" 
        location="${DIR_INTERMEDIATE_APPLICATION}/${PROJECT_NAME}.js" />

    <macrodef name="depsWriter">
        <sequential>
            <exec executable="python" failonerror="true" logError="true">
                <arg value="${DIR_CLOSURE_SCRIPTS}/depswriter.py" />
                <arg line="--root=${DIR_INTERMEDIATE_APPLICATION}" />
                <arg line="--root_with_prefix='${DIR_INTERMEDIATE_APPLICATION}/goog goog'" />
                <arg line="--root_with_prefix='${DIR_INTERMEDIATE_APPLICATION}/goog_thirdParty goog_thirdParty'" />
                <arg line="--root_with_prefix='${DIR_INTERMEDIATE_APPLICATION}/VanillaSDK VanillaSDK'" />
                <arg line="--output_file=${FILE_DEPS}" />
            </exec>
        </sequential>
    </macrodef>

    <macrodef name="closureBuilder">
        <sequential>
            <exec executable="python" failonerror="true" logError="true">
                <arg value="${DIR_CLOSURE_SCRIPTS}/closurebuilder.py" />
                <arg line="--compiler_jar=${DIR_CLOSURE_COMPILER}/compiler.jar" />
                <arg line="--root=${DIR_INTERMEDIATE_APPLICATION}" />
                <arg line="--output_mode=compiled" />
                <arg line="--output_file=${DIR_RELEASE_APPLICATION}/${PROJECT_NAME}.js" />
                <arg line="--compiler_flags=--compilation_level=ADVANCED_OPTIMIZATIONS" />
                <arg line="--compiler_flags=--js=${FILE_DEPS}" />
                <arg line="--compiler_flags=--warning_level=VERBOSE" />
                <arg line="--namespace=${PROJECT_NAME}" />
                <arg line="--compiler_flags=--jscomp_error=checkTypes" />
            </exec>
        </sequential>
    </macrodef>

    <target name="main" description="Compiles framework files" 
        depends="clean,createDirs,prepareOutput,falconJx,calculateDependencies,
            closureBuilding,createIntermediateOutput,createReleaseOutput" />
    
    <target name="createDirs">
        <echo>Create 'intermediate' and 'release' directory structure</echo>
        <mkdir dir="${DIR_INTERMEDIATE_APPLICATION}" />
        <mkdir dir="${DIR_RELEASE_APPLICATION}" />
    </target>

    <target name="prepareOutput">
        <echo>
            Creating 'intermediate' and 'release' target directory requirements
        </echo>
        <copy todir="${DIR_INTERMEDIATE_APPLICATION}/goog">
            <fileset dir="${DIR_CLOSURE_LIBRARY}/closure/goog" />
        </copy>
        <copy todir="${DIR_INTERMEDIATE_APPLICATION}/goog_thirdParty">
            <fileset dir="${DIR_CLOSURE_LIBRARY}/third_party/closure/goog" />
        </copy>
        <copy todir="${DIR_INTERMEDIATE_APPLICATION}/VanillaSDK">
            <fileset dir="${DIR_FRAMEWORK}"/>
        </copy>
    </target>

    <target name="ifWindows" if="isWindows">
        <property name="FILE_SUFFIX" value=".bat" />
    </target>

    <target name="ifOtherOS" unless="isWindows">
        <property name="FILE_SUFFIX" value="" />
    </target>

    <target name="falconJx" depends="ifWindows,ifOtherOS">
        <echo>Compiling the AS project into intermediate JS</echo>
        <exec executable="${FILE_FALCONJX_COMPILE_SCRIPT}${FILE_SUFFIX}">
            <arg value="${DIR_PROJECT}/src/${PROJECT_NAME}.as"/>
            <arg value="-output=${FILE_OUTPUT_MAIN}"/>
        </exec>
        <!-- 
            # Making sure that the main class' name doesn't get renamed 
              during the compile, which would invalidate the call to it 
              in the project's HTML
        -->
        <echo file="${FILE_OUTPUT_MAIN}" append="true">
<![CDATA[
// Ensures the symbol will be visible after compiler renaming.
goog.exportSymbol('${PROJECT_NAME}', ${PROJECT_NAME});
]]>
        </echo>
    </target>

    <target name="calculateDependencies">
        <echo>Calculating dependencies and writing them to 'deps.js'</echo>
        <depsWriter />
    </target>

    <target name="closureBuilding">
        <echo>Creating combined and compiled output JS</echo>
        <closureBuilder />
    </target>

    <target name="createIntermediateOutput">
        <echo>Creating 'intermediate' playback project</echo>
        <copy file="${FILE_HTML_TEMPLATE}" 
            tofile="${DIR_INTERMEDIATE_APPLICATION}/${PROJECT_NAME}.html" 
            overwrite="true" />
        <move file="${DIR_INTERMEDIATE_APPLICATION}/goog/base.js" 
            todir="${DIR_INTERMEDIATE_APPLICATION}" />
        <echo file="${DIR_INTERMEDIATE_APPLICATION}/${PROJECT_NAME}.html"><![CDATA[
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" >
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

    <script type="text/javascript" src="./base.js"></script>
    <script type="text/javascript">
        goog.require("${PROJECT_NAME}");
    </script>
</head>
<body>
    <script type="text/javascript">
        new ${PROJECT_NAME}();
    </script>
</body>
</html>
        ]]></echo>
    </target>

    <target name="createReleaseOutput">
        <echo>Creating 'release' playback project</echo>
        <copy file="${FILE_HTML_TEMPLATE}" 
            tofile="${DIR_RELEASE_APPLICATION}/${PROJECT_NAME}.html" 
            overwrite="true" />
        <echo file="${DIR_RELEASE_APPLICATION}/${PROJECT_NAME}.html"><![CDATA[
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" >
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

    <script type="text/javascript" src="./${PROJECT_NAME}.js"></script>
</head>
<body>
    <script type="text/javascript">
        new ${PROJECT_NAME}();
    </script>
</body>
</html>
        ]]></echo>
    </target>

    <!--

        CLEANUP

    -->

    <target name="clean" description="Clean up previous runs" 
        depends="cleanIntermediate,cleanRelease">
        <echo>Delete 'intermediate' and 'release' directories</echo>
    </target>

    <target name="cleanIntermediate">
        <delete failonerror="false" includeemptydirs="true">
            <fileset dir="${DIR_INTERMEDIATE_APPLICATION}" />
		</delete>
    </target>

    <target name="cleanRelease">
        <delete failonerror="false" includeemptydirs="true">
            <fileset dir="${DIR_RELEASE_APPLICATION}" />
		</delete>
    </target>

</project>
