Subversion Repositories DevTools

Rev

Rev 1978 | Blame | Compare with Previous | Last modification | View Log | RSS feed

<project name="using-macrodef">

    <!--        C O P Y R I G H T   N O T I C E
                        This material is confidential to ERG and may not be disclosed in whole
                        or in part to any third party nor used in any manner whatsoever other
                        than for the purposes expressly consented to by ERG in writing.

                        This material is also copyright and may not be reproduced, stored in a
                        retrieval system or transmitted in any form or by any means in whole or
                        in part without the express written consent of ERG.
        -->


    <!--                                                BUILD ENVIRONMENT NOTES

                GBE_DPKG_CACHE  =       Global Build Environment DPKG Cache
                                                        (This location holds the local set of artefacts acquired from DPKG,
                                                         for use within a build)

                GBE_DPKG                =       Global Build Environment DPKG source

        -->

    <property name="env" environment="env" value="not used" />


    <macrodef name="using">

        <attribute name="name" />
        <attribute name="version" default="$${@{name}}" />
        <attribute name="includes" default="" />
        <attribute name="from" default="${env.GBE_DPKG}" />
        <attribute name="refid" default="@{name}" />
        <attribute name="copy" default="true" />

        <element name="using-fileset"
                 optional="yes"
                 implicit="yes"
                 description="Anything you can put in a fileset." />

        <sequential>

            <property name="using.@{name}.version" value="@{version}" />

            <fail message="The 'using' for package @{name} has already been declared as version @{version}"
                  status="2">
                <condition>
                    <and>
                        <isset property="using.@{name}.version" />
                        <not>
                            <equals arg1="${using.@{name}.version}" arg2="@{version}" />
                        </not>
                    </and>
                </condition>
            </fail>

            <if>
                <isset property="using.@{refid}.basedir" />
                <then>
                    <echo taskname="using" level="verbose">@{name} has already been defined. Skipping import...</echo>
                </then>
                <else>
                    <if>
                        <and>
                            <equals arg1="@{copy}" arg2="true" />
                                                <isset property="env.GBE_DPKG_CACHE"/>
                            <length string="${env.GBE_DPKG_CACHE}" trim="true" when="greater" length="0"/>
                        </and>
                        <then>
                            <property name="using.@{refid}.basedir"
                                      value="${build.repository}/@{name}/@{version}" />
                        </then>
                        <else>
                            <property name="using.@{refid}.basedir" value="@{from}/@{name}/@{version}" />
                        </else>
                    </if>

                    <mkdir dir="${basedir}/build/pkg" />

                    <echo taskname="using">@{name} version @{version}</echo>
                    <if>
                        <and>
                            <equals arg1="@{copy}" arg2="true" />
                            <!-- Only copy from the package archive if the source and target locations are different -->
                            <not>
                                <equals arg1="@{from}/@{name}/@{version}" arg2="${using.@{refid}.basedir}" />
                            </not>
                            <!-- Don't copy if the using.nocache property is true. -->
                            <isfalse value="${using.nocache}" />
                        </and>
                        <then>
                            <mkdir dir="${using.@{refid}.basedir}" />
                            <copy todir="${using.@{refid}.basedir}" verbose="yes">
                                <fileset dir="@{from}/@{name}/@{version}" includes="@{includes}">
                                    <using-fileset />
                                </fileset>
                            </copy>
                        </then>
                        <else>
                            <echo taskname="using">Skipping Copy from ${using.@{refid}.basedir} where Source = Target location</echo>
                        </else>
                    </if>

                    <path id="using.@{refid}.path">
                        <fileset dir="${using.@{refid}.basedir}" includes="@{includes}">
                            <using-fileset />
                        </fileset>
                    </path>

                    <path id="using.@{refid}.classpath">
                        <fileset dir="${using.@{refid}.basedir}" includes="**/*.jar">
                            <using-fileset />
                        </fileset>
                    </path>

                    <!-- Look for an ant script in the dependency named [name].xml - if it exists, import it.
                                                 Otherwise, carry on and ignore the fact that it does not exist.
                                                 Don't do this if we're doing "using" on "ant-using" though, as we've already executed
                                                 ant-using.xml by definition!
                                          -->
                    <if>
                        <not>
                            <equals arg1="@{name}" arg2="ant-using" />
                        </not>
                        <then>
                            <trycatch>
                                <try>
                                    <import file="${using.@{refid}.basedir}/@{name}.xml" optional="true" />
                                </try>
                                <catch>
                                    <if>
                                        <isset property="force.fail.message" />
                                        <then>
                                            <fail message="${force.fail.message}" />
                                        </then>
                                        <else>
                                            <echo taskname="using"
                                                  level="warning"
                                                  message="Import ignored when called within target." />
                                        </else>
                                    </if>
                                </catch>
                            </trycatch>
                        </then>
                    </if>

                </else>
            </if>

        </sequential>

    </macrodef>


    <!-- used to remove the need to add a using line in your ant build file for every
                package listed in your dependency file. Simply call usingall instead and it
                will "detect" all the versions you are using and use them automatically.

                NOTE: This will make a copy of your depends.xml file to determine what
                                packages to use.

                If for some reason you need to use the include and exclude items for some
                pacakges, then don't use this macro.

                In addition, if there are order dependent usings, then list those first, and then
                use usingall to load the others (if they are already loaded then it will ignore them).
        -->
    <macrodef name="usingall">

        <attribute name="dependsFile" default="${ant.project.name}depends.xml" />

        <sequential>
            <copy todir="${basedir}/build" overwrite="true">
                <fileset dir="${basedir}">
                    <include name="@{dependsFile}" />
                </fileset>
            </copy>


            <replaceregexp file="${basedir}/build/@{dependsFile}"
                           match='project.*name="'
                           replace='project name="tmp'
                           byline="true" />

            <replaceregexp file="${basedir}/build/@{dependsFile}"
                           match='property.*name="'
                           replace='property name="use.version.'
                           byline="true" />

            <import file="${basedir}/build/@{dependsFile}" />

            <propertyselector property="use.versions"
                              delimiter=","
                              match="use\.version\.([^\.]*)"
                              select="\1"
                              casesensitive="false" />

            <for list="${use.versions}" delimiter="," param="use.version">
                <sequential>
                    <if>
                        <and>
                            <not>
                                <equals arg1="@{use.version}" arg2="packagename" />
                            </not>
                            <not>
                                <equals arg1="@{use.version}" arg2="packageversion" />
                            </not>
                            <not>
                                <equals arg1="@{use.version}" arg2="releasemanager.releasename" />
                            </not>
                            <not>
                                <equals arg1="@{use.version}" arg2="releasemanager.projectname" />
                            </not>
                        </and>
                        <then>
                            <using name="@{use.version}" />
                        </then>
                    </if>

                </sequential>
            </for>

        </sequential>
    </macrodef>

    <!-- 
          - Determine whether the dependency has been imported via the using or usingall macrodefs
          -->
    <macrodef name="checkUsing">
        <attribute name="name" />
        <attribute name="version" default="using.@{name}.version" />

        <sequential>
            <if>
                <not>
                    <isset property="using.@{name}.basedir" />
                </not>
                <then>
                    <fail message="The required package '@{name}' has not been imported with using macro." />
                </then>
            </if>

            <if>
                <not>
                    <equals arg1="using.@{name}.version" arg2="@{version}" />
                </not>
                <then>
                    <fail message="The required package '@{name}' is version '${using.@{name}.version}' and not '@{version}' as expected." />
                </then>
            </if>
        </sequential>
    </macrodef>

</project>