<?xml version="1.0" encoding="UTF-8"?>
<!-- ====================================================================== 
     Dec 8, 2008 2:05:07 PM                                                        

     PyDev 
     Builds the PyDev feature
                   
     cwilliams                                                                
     ====================================================================== -->
<project name="PyDev" default="build">
	<description>
            Builds the PyDev feature
    </description>
	
	
	
	<macrodef name = "git">
	    <attribute name = "command" />
	    <attribute name = "dir" default = "" />
	    <element name = "args" optional = "true" />
	    <sequential>
	        <echo message = "git @{command}" />
	        <exec executable = "${git.executable}" dir = "@{dir}">
	            <arg value = "@{command}" />
	            <args/>
	        </exec>
	    </sequential>
	</macrodef>

	<macrodef name = "git-clone-pull">
	    <attribute name = "repository" />
	    <attribute name = "dest" />
	    <sequential>
	        <git command = "clone">
	            <args>
	                <arg value = "@{repository}" />
	                <arg value = "@{dest}" />
	            </args>
	        </git>
	        <git command = "pull" dir = "@{dest}" />
	    </sequential>
	</macrodef>
	
	<property environment="env" />
	<property file="build_local.properties" />
	<!-- Set is.windows property IF the OS is from the Windows family -->
	<condition property="is.windows" value="true">
		<os family="windows" />
	</condition>
	<!-- Set is.local_build property IF the local_build property is any true value -->
	<condition property="is.local_build" value="true">
		<istrue value="${local_build}"/>
	</condition>
	<!-- sign_jars can be optionally set, but otherwise defaults to local_build
	     Set is.sign_jars property IF the sign_jars property is any true value
	     OR (the sign_jars property is unset AND the local_build property is any true value) -->
	<condition property="is.sign_jars" value="true">
		<and>
			<isset property="sign_jars"/>
			<istrue value="${sign_jars}"/>
		</and>
	</condition>
	<condition property="is.sign_jars" value="true">
		<and>
			<not>
				<isset property="sign_jars"/>
			</not>
			<istrue value="${local_build}"/>
		</and>
	</condition>

    <taskdef resource="net/sf/antcontrib/antcontrib.properties">
        <classpath>
            <pathelement location="ant-contrib/ant-contrib.jar" />
        </classpath>
    </taskdef>


	<!-- ================================= 
          target: build              
         ================================= -->
	<target name="doit" description="Builds the PyDev feature" depends="generate.build.properties,get.revision,generate.base">
		<java jar="${baseLocation}/plugins/${launcher.plugin}.jar" fork="true" failonerror="yes">
			<jvmarg value="-Xmx512m" />
			<jvmarg value="-XX:MaxPermSize=256m" />
			<arg line="-nosplash -application org.eclipse.ant.core.antRunner -buildfile ${baseLocation}/plugins/${builder.plugin}/scripts/build.xml -Dbuilder=${basedir} -Dbuild.revision=${build.revision}" />
		</java>
		<antcall target="categorize" />
	</target>
	
	<!-- ================================= 
          target: categorize              
          
          Note: in http://wiki.eclipse.org/Equinox_p2_Metadata_Generator
          there are newer instructions (so, if at sometime this is not ok, it might be worth
          revisiting that site to update the command line - again).
         ================================= -->
    <target name="categorize" description="categorize the update site">
    	<java jar="${baseLocation}/plugins/${launcher.plugin}.jar" fork="true" failonerror="yes">
    		<!-- regenerates the content.jar with proper categories -->
            <arg line="-application org.eclipse.equinox.p2.publisher.EclipseGenerator -nosplash -consoleLog -updateSite ${deploy.dir} -site file:${deploy.dir}/site.xml -metadataRepository file:${deploy.dir} -metadataRepositoryName &quot;Pydev Update Site&quot; -artifactRepository file:${deploy.dir} -artifactRepositoryName &quot;Pydev Artifacts&quot; -publishArtifacts -publishArtifactRepository -compress -reusePack200Files -noDefaultIUs -vmargs -Xmx256m" />
    	</java>
    </target>

	<!-- ================================= 
          target: generate.build.properties              
         ================================= -->
	<target name="generate.build.properties" description="Takes in properties from build_local.properties and the command line overrides and generates a combined build.properties to use for the build">
		<echoproperties destfile="build.properties">
			<propertyset negate="true">
				<propertyref prefix="java." />
				<propertyref prefix="ant." />
				<propertyref prefix="awt." />
				<propertyref prefix="user." />
				<propertyref prefix="sun." />
			</propertyset>
		</echoproperties>
	</target>
	
	
    <!-- - - - - - - - - - - - - - - - - - 
          target: clean
         - - - - - - - - - - - - - - - - - -->
    <target name="clean" depends="generate.build.properties" description="Clean up temp and build files">
        <delete failonerror="false">
            <fileset dir="${buildDirectory}" defaultexcludes="true">
                <include name="assemble.*" />
                <include name="finalFeaturesVersions.*" />
                <include name="finalPluginsVersions.*" />
                <include name="package.*" />
            </fileset>
        </delete>
        <delete dir="${buildDirectory}/${buildLabel}" failonerror="false" />
        <java jar="${baseLocation}/plugins/${launcher.plugin}.jar" fork="true" failonerror="false">
            <arg line="-application org.eclipse.ant.core.antRunner -buildfile ${baseLocation}/plugins/${builder.plugin}/scripts/build.xml -Dbuilder=${basedir} -nosplash clean" />
        </java>
        <delete dir="${baseLocation}" />
        <delete dir="${buildDirectory}/features/org.eclipse.pde.build.container.feature" failonerror="false" />
        <delete dir="${p2.repo.dir}" failonerror="false" />
        <!-- Do an git checkout of the build directory -->
        <exec executable="/bin/bash" dir="${buildDirectory}" failifexecutionfails="false">
            <arg value="-c" />
            <arg value="${git.executable} checkout -f" />
        </exec>
    </target>
    

	<!-- - - - - - - - - - - - - - - - - - 
          target: clean.after                      
         - - - - - - - - - - - - - - - - - -->
	<target name="build" depends="doit" if="cleanAfter.set">
		<antcall target="clean" />
	</target>

	<!-- - - - - - - - - - - - - - - - - - 
          target: get.revision                      
         - - - - - - - - - - - - - - - - - -->
    <target name="get.revision" unless="build.revision">
	   	<echo message="Retrieving Git timestamp for latest revision" level="info" />
	    	<exec executable="${git.executable}" dir="${basedir}" outputproperty="git.revision.max">
		    	<arg value="log" />
		    	<arg value="--max-count=1" />
		    	<arg value="--pretty=format:'%ct'" />
	    	</exec>
            <exec executable="${python.executable}" dir="${basedir}" outputproperty="python.timestamp">
		    	<arg value="-c" />
		    	<arg value="import time;t=time.gmtime(float(${git.revision.max}));print '\'%d%02d%02d%02d\'' % (t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour)" />
	    	</exec>
    	<property name="build.revision" value="${python.timestamp}" />
        <echo message="Git ct : ${git.revision.max} - Target: ${basedir}" level="info" />
        <echo message="Date ct: ${build.revision} - Target: ${basedir}" level="info" />
    </target>

	<!-- - - - - - - - - - - - - - - - - - 
          target: generate.base                      
         - - - - - - - - - - - - - - - - - -->
    <target name="generate.base" depends="copy.base.windows,copy.base" unless="is.local_build">
        <!-- Workaround for https://bugs.eclipse.org/241430 shouldn't be necessary when we move to 3.5 -->
        <delete failonerror="false">
            <fileset dir="${baseLocation}/configuration/.settings">
                <include name="org.eclipse.equinox.p2.*.repository.prefs" />
            </fileset>
        </delete>
        <!-- Copy the features and plugins over into a dropins folder. Installing makes it so that non-linux plugins/features won't be there! -->
        <mkdir dir="${baseLocation}/dropins/eclipse" />
    </target>

	<!-- - - - - - - - - - - - - - - - - - 
          target: copy.base                      
         - - - - - - - - - - - - - - - - - -->
	<target name="copy.base" unless="is.windows">
		<!-- Copy over the original base eclipse to a new location.
			Have to use exec cp to retain permissions on *nix, Mac -->
		<exec executable="cp">
			<arg line="-R ${vanilla.eclipse} ${baseLocation}" />
		</exec>
	</target>

	<!-- - - - - - - - - - - - - - - - - - 
          target: copy.base.windows with default ant                   
         - - - - - - - - - - - - - - - - - -->
	<!--
	<target name="copy.base.windows" if="is.windows">
		<copy todir="${baseLocation}">
			<fileset dir="${vanilla.eclipse}" includes="**/*" defaultexcludes="false" />
		</copy>
	</target>
	-->
	
	<!-- - - - - - - - - - - - - - - - - - 
          target: copy.base.windows with fast copy (note that fastcopy must be available for it to work).
          If this doesn't properly work, the command above should be used.
         - - - - - - - - - - - - - - - - - -->
	<target name="copy.base.windows" if="is.windows">
		<exec executable="${fastcopy.executable}">
			<arg line="/to=${baseLocation} /cmd=sync /auto_close ${vanilla.eclipse}" />
		</exec>
	</target>

    <target name="create-artifact-symlink">
        <exec executable="unlink">
            <arg value="${env.WORKSPACE}/build-artifacts" />
        </exec>
        <symlink link="${env.WORKSPACE}/build-artifacts" resource="${deploy.dir}" failonerror="false" />
     </target>
</project>
