<?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="starling" default="main" basedir=".">
	<property name="FLEX_HOME" location="${basedir}/../../.."/>

	<property file="${FLEX_HOME}/env.properties"/>
	<property environment="env"/>
	<property file="${FLEX_HOME}/build.properties"/>

    <property name="tests.bin" location="${basedir}/bin-tests"/>

	<target name="main" depends="clean,compile" description="Clean build of starling.swc"/>

	<target name="clean">
		<delete failonerror="false">
			<fileset file="${basedir}/bundles.properties"/>
			<fileset dir="${FLEX_HOME}/frameworks/libs">
				<include name="starling.swc"/>
				<include name="starling.swc.incr"/>
			</fileset>
		</delete>
	</target>

	<target name="compile" description="Compiles starling.swc">
		<echo message="Compiling frameworks/libs/starling.swc"/>
		<!-- Load the <compc> task. We can't do this at the <project> level -->
		<!-- because targets that run before flexTasks.jar gets built would fail. -->
		<taskdef resource="flexTasks.tasks" classpath="${FLEX_HOME}/ant/lib/flexTasks.jar;${FLEX_HOME}/lib/flexTasks.jar"/>
		
		<compc fork="true"
			   output="${FLEX_HOME}/frameworks/libs/starling.swc">
			<jvmarg line="${compc.jvm.args}"/>
			<load-config filename="compile-config.xml" />
			<arg value="+playerglobal.version=${playerglobal.version}" />
			<arg value="+env.PLAYERGLOBAL_HOME=${env.PLAYERGLOBAL_HOME}" />
		</compc>
	</target>

	<target name="test" description="Runs the FlexUnit tests for this project">

		<taskdef resource="flexUnitTasks.tasks">
			<classpath>
				<fileset dir="${env.FLEXUNIT_HOME}/FlexUnit4AntTasks/target">
					<include name="flexUnitTasks*.jar"/>
				</fileset>
			</classpath>
		</taskdef>
		<taskdef resource="flexTasks.tasks" classpath="${FLEX_HOME}/ant/lib/flexTasks.jar;${FLEX_HOME}/lib/flexTasks.jar"/>

		<echo message="Unit tests for '${basedir}'"/>

		<mkdir dir="${tests.bin}"/>

		<copy todir="${tests.bin}/fixtures">
			<fileset dir="${basedir}/Starling-Framework/tests/fixtures"/>
		</copy>

		<mxmlc file="${basedir}/Starling-Framework/tests/src/FlexUnitRunner.as" fork="true" failonerror="true">
			<load-config filename="flexunit-config.xml" />
			<arg value="+playerglobal.version=${playerglobal.version}" />
			<arg value="+env.PLAYERGLOBAL_HOME=${env.PLAYERGLOBAL_HOME}" />
			<library-path dir="${env.FLEXUNIT_HOME}" append="true">
				<include name="FlexUnit4/libs/*.swc"/>
				<include name="FlexUnit4/target/*.swc"/>
				<include name="FlexUnit4CIListener/target/*.swc"/>
			</library-path>
			<output>${tests.bin}/swf/TestStarling.swf</output>
		</mxmlc>

		<flexunit
			player="flash"
			workingDir="${tests.bin}" 
			swf="${tests.bin}/swf/TestStarling.swf"
			haltonfailure="true"
			verbose="true"
			toDir="${tests.bin}"/>

		<delete dir="${tests.bin}"/>
	</target>

	<target name="doc" depends="clean-temp-docs" description="updates starling.swc with asdoc xml">
		<!-- Load the <asdoc> task. We can't do this at the <project> level -->
		<!-- because targets that run before flexTasks.jar gets built would fail. -->
		<taskdef resource="flexTasks.tasks" classpath="${FLEX_HOME}/ant/lib/flexTasks.jar;${FLEX_HOME}/lib/flexTasks.jar"/>

		<condition property="asdoc.jvm.args" value="-Xmx384m">
			<os family="windows"/>
		</condition>

		<condition property="asdoc.jvm.args" value="-Xmx512m">
			<os family="mac"/>
		</condition>

		<condition property="asdoc.jvm.args" value="-Xmx512m">
			<os family="unix"/>
		</condition>


		<!-- Call asdoc to generate dita xml files -->
		<asdoc output="${FLEX_HOME}/tempDoc" lenient="true" failonerror="true" keep-xml="true" skip-xsl="true" fork="true">
			<compiler.source-path path-element="${basedir}/Starling-Framework/starling/src"/>
			<doc-sources path-element="${basedir}/Starling-Framework/starling/src"/>
			<external-library-path dir="${env.PLAYERGLOBAL_HOME}">
				<include name="${playerglobal.version}/playerglobal.swc"/>
			</external-library-path>
			<jvmarg line="${asdoc.jvm.args}"/>
		</asdoc>

		<!-- updates starling.swc with asdoc xml -->
		<zip destfile="${FLEX_HOME}/frameworks/libs/starling.swc" update="true">
			<zipfileset dir="${FLEX_HOME}/tempDoc/tempdita" prefix="docs">
				<include name="*.*"/>
				<exclude name="ASDoc_Config.xml"/>
				<exclude name="overviews.xml"/>
			</zipfileset>
		</zip>
	</target>

	<target name="clean-temp-docs">
		<delete dir="${FLEX_HOME}/tempDoc" failonerror="false" includeEmptyDirs="true"/>
	</target>

</project>
