<?xml version="1.0" encoding="UTF-8"?>
<!--

    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="harness" basedir="." default="build">

    <import file="common.xml"/>
    <import file="jdk.xml"/>
    
    <target name="taskdefs" unless="${harness.taskdefs.done}">
        <dirname property="harness.dir" file="${harness.file}"/>
        <taskdef classpath="${harness.dir}/tasks.jar" resource="org/netbeans/nbbuild/taskdefs.properties"/>
        <typedef name="pathfileset" classname="org.netbeans.nbbuild.PathFileSet"
                 classpath="${harness.dir}/tasks.jar"/>
        <typedef name="islocked" classname="org.netbeans.nbbuild.IsLocked" 
                 classpath="${harness.dir}/tasks.jar"/>
        <property name="harness.taskdefs.done" value="true"/>
        <fail message="When using cluster.path property, remove netbeans.dest.dir, enabled.clusters and disabled.clusters properties from platform config, they would be ignored.">
            <condition>
                <and>
                    <isset property="cluster.path.evaluated"/>
                    <or>
                        <isset property="netbeans.dest.dir"/>
                        <isset property="enabled.clusters"/>
                        <isset property="disabled.clusters"/>
                    </or>
                    <not>
                        <isset property="suite.dir"/>
                    </not>
                </and>
            </condition>
        </fail>
    </target>

    <target name="-convert-old-project" depends="-build-dir-init" unless="cluster.path.evaluated">
        <fail unless="netbeans.dest.dir" message="Either cluster.path/cluster.path.evaluated properties or netbeans.dest.dir must be set."/>

        <pathconvert property="old.project.update.tracking">
            <fileset dir="${netbeans.dest.dir}" includes="**/update_tracking/*.xml"/>
        </pathconvert>

        <!-- standalone modules don't specify enabled/disabled clusters -->
        <property name="enabled.clusters" value=""/>
        <property name="disabled.clusters" value=""/>
        <property name="disabled.modules" value=""/>

        <selector id="old.project.included.files">
            <custom classpath="${harness.dir}/tasks.jar" classname="org.netbeans.nbbuild.ModuleSelector">
                <param name="excludeModules" value="${disabled.modules}"/>
                <!-- XXX inc/exc clusters -->
                <param name="includeClusters" value="${enabled.clusters}"/>
                <param name="excludeClusters" value="${disabled.clusters}"/>
                <param name="updateTrackingFiles" value="${old.project.update.tracking}"/>
            </custom>
        </selector>

        <pathconvert property="old.project.clusters.duplicates" pathsep="&#10;">
            <fileset dir="${netbeans.dest.dir}">
                <selector refid="old.project.included.files"/>
            </fileset>
            <chainedmapper>
                <mapper type="regexp" from="(^.+)[/\\]config[/\\]Modules[/\\]" to="\1"/> <!-- #71849 -->
                <filtermapper>
                    <linecontainsregexp negate="true">
                        <regexp pattern="update[/\\]backup[/\\]netbeans[/\\]?$"/>
                    </linecontainsregexp>
                </filtermapper>
            </chainedmapper>
        </pathconvert>
        <tempfile property="old.project.clusters.duplicates.file" destdir="${build.dir}" deleteonexit="true" prefix="duplicates"/>
        <mkdir dir="${build.dir}"/>
        <echo file="${old.project.clusters.duplicates.file}" message="${old.project.clusters.duplicates}"/>

        <pathconvert property="cluster.path.evaluated">
            <union id="old.project.clusters"> <!-- union removes duplicates -->
                <files includesfile="${old.project.clusters.duplicates.file}"/>
            </union>
        </pathconvert>
    </target>

    <target name="common-init" depends="taskdefs,-build-dir-init,-convert-old-project">
        <property name="netbeans.javadoc.dir" location="${build.dir}/javadoc"/>
        <condition property="cluster.path.basedir" value="${suite.dir}" else="${basedir}">
            <isset property="suite.dir"/>
        </condition>
        <convertclusterpath from="${cluster.path.evaluated}" to="cluster.path.final" id="cluster.path.id" basedir="${cluster.path.basedir}"/>
    </target>

    <target name="basic-init" depends="projectized-common.basic-init">
        <condition property="suite.build.dir" value="${suite.dir}/build">
            <isset property="suite.dir"/>
        </condition>
        <!-- Must be done even without build-init, or clean will not work. -->
        <condition property="cluster" value="${suite.build.dir}/cluster"> <!-- #59872 -->
            <isset property="suite.build.dir"/>
        </condition>
        <property name="cluster" location="${build.dir}/cluster"/>

        <condition property="test.dist.dir" value="${suite.build.dir}/testdist">
            <isset property="suite.build.dir"/>
        </condition>
        <property name="test.dist.dir" location="${build.dir}/testdist"/>
        <property name="build.classes.dir" location="${build.dir}/classes"/>

    </target>

    <target name="init" depends="basic-init,files-init,build-init,-javac-init"/>

    <target name="nbm-license-init" unless="license.file">
        <mkdir dir="${build.dir}"/>
        <property name="license.file" location="${build.dir}/no-license.txt"/>
        <echo file="${license.file}">[NO LICENSE SPECIFIED]</echo>
    </target>

    <target name="build-init" depends="basic-init,nbm-license-init">
        <condition property="public.package.jar.dir" value="${suite.build.dir}/public-package-jars">
            <isset property="suite.build.dir"/>
        </condition>
        <property name="public.package.jar.dir" location="${build.dir}/public-package-jars"/>
        <mkdir dir="${public.package.jar.dir}"/>
        <!-- Similar to projectized.xml but does not try to set NBM homepage/distribution, nor is javahelp.excludes predefined. -->
        <parseprojectxml
            project="."
            publicpackagesproperty="public.packages"
            friendsproperty="friends"
            javadocpackagesproperty="module.javadoc.packages"
            moduledependenciesproperty="module.dependencies"
            moduleclasspathproperty="module.classpath"
            publicpackagejardir="${public.package.jar.dir}"
            moduleprocessorclasspathproperty="module.processor.classpath"
            modulerunclasspathproperty="module.run.classpath"
            classpathextensionsproperty="class.path.extensions"
        >
            <testtype name="unit"
                      runtimecp="test.unit.runtime.cp"
                      compilecp="test.unit.compile.cp"
                      compiledep="test.unit.testdep"/>
            <testtype name="qa-functional"
                      runtimecp="test.qa-functional.runtime.cp"
                      compilecp="test.qa-functional.compile.cp"
                      compiledep="test.qa-functional.testdep"/>
        </parseprojectxml>
        <condition property="is.regular">
            <not>
                <or>
                    <isset property="is.autoload"/>
                    <isset property="is.eager"/>
                    <equals arg1="${module.jar.dir}" arg2="core"/>
                    <equals arg1="${module.jar.dir}" arg2="lib"/>
                </or>
            </not>
        </condition>
        <property name="build.compiler.debug" value="true"/>
        <property name="build.compiler.debuglevel" value="source,lines,vars"/>
        <property name="build.compiler.deprecation" value="true"/>
        <property name="build.sysclasspath" value="ignore"/>
        <property name="build.compiler.optimize" value="off"/>
        <property name="build.package.compress" value="false"/>
        <property name="build.package.index" value="false"/>
        <property name="manifest.mf" location="manifest.mf"/>
        <property name="src.dir" location="src"/>
        <property name="nbm" value="${code.name.base.dashes}.nbm"/>
        <property name="nbm.module.author" value=""/>
        <property name="nbm.release.date" value=""/>
        <property name="nbm.needs.restart" value="false"/>
        <property name="nbm.is.global" value=""/>
        <property name="nbm.is.preferredupdate" value=""/>
        <!-- Override if you want to use these: -->
        <property name="nbm.distribution" value=""/>
        <property name="nbm.homepage" value=""/>
        <property name="build.javahelp.dir" location="${build.dir}/javahelp"/>
        <property name="javahelp.base" value="${code.name.base.slashes}/docs"/>
        <property name="javahelp.search" value="JavaHelpSearch"/>
        <pathconvert property="retouche.javac.api">
            <pathfileset>
                <path path="${cluster.path.final}"/>
                <filename name="modules/ext/*javac*api*.jar"/>
            </pathfileset>
        </pathconvert>
        <pathconvert property="retouche.javac.impl">
            <pathfileset>
                <path path="${cluster.path.final}"/>
                <filename name="modules/ext/*javac*impl*.jar"/>
            </pathfileset>
            <fileset dir="${nbjdk.home}/../Classes" erroronmissingdir="false">
                <include name="*.jar"/>
            </fileset>
        </pathconvert>
        <!-- When requires.nb.javac property is true, prepend javac-api on bootclasspath to allow override the default annotation
             processing API located in rt.jar. On Mac, prepend also javac-impl, so that tests are built against correct version of javac. -->
        <condition property="bootclasspath.prepend" value="${retouche.javac.api}:${retouche.javac.impl}">
            <and>
                <os family="mac"/>
                <istrue value="${requires.nb.javac}"/>
            </and>
        </condition>
        <condition property="bootclasspath.prepend" value="${retouche.javac.api}">
            <istrue value="${requires.nb.javac}"/>
        </condition>
    </target>

    <target name="-test-branding" if="branding.token"><!-- #197066 -->
        <property name="test-unit-sys-prop.branding.token" value="${branding.token}"/>
        <property name="test-qa-functional-sys-prop.branding.token" value="${branding.token}"/>
    </target>
    <target name="test-lib-init" depends="init,-test-branding">
        <!-- NbModuleSuite must be made to ignore disabled.modules during a test; see discussion in #136621: -->
        <delete failonerror="false">
            <fileset dir="${suite.build.dir}">
                <include name="cluster/config/Modules/*.xml_hidden"/>
            </fileset>
        </delete>
        <path id="test.unit.lib.cp">
            <pathelement path="${test.unit.lib.cp}"/> <!-- for manual override -->
            <!-- As a fallback for old platforms which do not include the right modules: -->
            <pathfileset include="modules/ext/junit-*.jar">
                <path refid="cluster.path.id"/>
            </pathfileset>
            <fileset dir="${netbeans.home}/..">
                <include name="*/modules/ext/junit-*.jar"/>
            </fileset>
        </path>
    </target>

    <target name="javahelp" depends="init" if="has.javahelp">
        <!-- Similar to projectized.xml but does not fiddle with usersguide/.../ide.css, and finds jhall.jar differently: -->
        <property name="jhall.jar" location="${harness.dir}/antlib/jsearch-2.0_05.jar"/>
        <available property="jhall.jar.exists" file="${jhall.jar}"/>
        <fail unless="jhall.jar.exists">You must set 'jhall.jar' (e.g. in private.properties) to the location of jsearch.jar from a JavaHelp distribution</fail>
        <mkdir dir="${build.javahelp.dir}/${javahelp.base}/${javahelp.search}"/>
        <copy todir="${build.javahelp.dir}">
            <fileset dir="javahelp"/>
        </copy>
        <jhindexer basedir="${build.javahelp.dir}/${javahelp.base}"
                   db="${build.javahelp.dir}/${javahelp.base}/${javahelp.search}">
            <classpath>
                <pathelement location="${jhall.jar}"/>
                <pathelement location="${harness.dir}/tasks.jar"/>
            </classpath>
            <include name="**/*.html"/>
            <include name="**/*.htm"/>  <!-- Fix for Issue #160276 -->
            <exclude name="${javahelp.search}/"/>
            <exclude name="${javahelp.excludes}"/>
        </jhindexer>
        <mkdir dir="${cluster}/${javahelp.jar.dir}"/>
        <jar jarfile="${cluster}/${javahelp.jar}" compress="true">
            <fileset dir="${build.javahelp.dir}"/>
        </jar>
    </target>

    <target name="javadoc" depends="build-init,-javadoc-init">
        <mkdir dir="${netbeans.javadoc.dir}/${code.name.base.dashes}"/>
        <javadoc destdir="${netbeans.javadoc.dir}/${code.name.base.dashes}" packagenames="${module.javadoc.packages}" source="${javac.source}" windowtitle="${javadoc.title}" encoding="UTF-8" charset="UTF-8">
            <classpath refid="processor.cp"/> <!-- #157320 -->
            <sourcepath>
                <pathelement location="${src.dir}"/>
                <pathelement location="${build.dir}/classes-generated"/>
            </sourcepath>
            <doctitle>${javadoc.title}</doctitle>
            <header>${javadoc.header}</header>
        </javadoc>
    </target>

    <target name="javadoc-nb" depends="init,javadoc" if="netbeans.home">
        <nbbrowse file="${netbeans.javadoc.dir}/${code.name.base.dashes}/index.html"/>
    </target>

    <target name="-hide-excluded-modules" if="suite.dir"> <!-- #194563 -->
        <ant dir="${suite.dir}" inheritall="false" target="-hide-excluded-modules"/>
    </target>

    <target name="run" depends="netbeans,-hide-excluded-modules">
        <ant antfile="${harness.dir}/run.xml" target="run">
            <reference refid="cluster.path.id"/>
        </ant>
    </target>

    <target name="-profile-check" if="netbeans.home">
        <condition property="profiler.configured">
            <or>
                <contains string="${run.args.ide}" substring="-agentpath:" casesensitive="true"/>
                <contains string="${run.args.ide}" substring="-javaagent:" casesensitive="true"/>
            </or>
        </condition>
    </target>

    <target name="-do-profile" depends="netbeans,-hide-excluded-modules,-jdk-presetdef-nbprofiledirect">
        <ant antfile="${harness.dir}/run.xml" target="profile">
            <reference refid="cluster.path.id"/>
        </ant>
    </target>
    <target name="-try-profile" depends="-profile-check" if="profiler.configured">
        <antcall target="-do-profile"/>
    </target>

    <target name="profile">
        <ant antfile="${harness.dir}/run.xml" target="-profile-check"/>
        <antcall target="-try-profile"/>
    </target>

    <target name="debug" depends="netbeans,-hide-excluded-modules,-jdk-presetdef-nbjpdastart">
        <ant antfile="${harness.dir}/run.xml" target="debug">
            <reference refid="cluster.path.id"/>
        </ant>
    </target>

    <target name="reload" depends="netbeans">
        <ant antfile="${harness.dir}/run.xml" target="run">
            <property name="run.args" value='--reload "${cluster}/${module.jar}" --nosplash'/>
            <property name="run.args.extra" value=""/>
            <reference refid="cluster.path.id"/>
        </ant>
    </target>

    <target name="reload-in-ide" depends="netbeans">
        <fail unless="netbeans.home">This target only works when run from inside the NetBeans IDE.</fail>
        <nbinstaller module="${cluster}/${module.jar}" action="reinstall"/>
    </target>

    <target name="-copy-testxmlresults">
        <copy todir="${test.xml.results.dir}" flatten="true" failonerror="false">
            <fileset dir="${build.dir}/test" includes="*/results/TEST-*.xml"/>
        </copy>
    </target>

</project>
