Subversion Repositories DevTools

Rev

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

<project name="envchange-task">

        <!--    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.
        -->

        <!-- "REPLACE_ANT_WINDOWS" below gets replaced by ant-windows version when
                 this package is released. -->
        <using name="ant-windows" version="REPLACE_ANT_WINDOWS"/>


        <!-- Invoke another copy of Ant and run the specified target.
                 This allows modification of the environment by:
                  - "<env>" elements as per the standard "<exec>" Ant task.  It's best not to use
                    this for modifying Path or PATH.
                  - attribute "pathreplacement=" - replace the Path/PATH environment variable
                    with the given string.  String can contain multiple extra paths
                    separated by either ";" or ":".
                  - attributes "pathinsertions=" & "pathadditions=" - prepend / append the given
                    string to the Path/PATH environment variable.  String can contain multiple
                    extra paths separated by either ";" or ":".

                 This is used for e.g. ensuring the Borland compiler can find the Borland
                 linker (ilink32).

                 Note "using.nocache=true" is set on the new Ant to flag that all ant-using
                 caching has already been done on the first Ant, so no need to do it all again.
         -->
        <macrodef name="envchange">
                <attribute name="target"/>
                <attribute name="pathreplacement" default="NOT_SUPPLIED"/>
                <attribute name="pathinsertions" default="NOT_SUPPLIED"/>
                <attribute name="pathadditions" default="NOT_SUPPLIED"/>

                <!-- Any elements inside <envchange> implicitly are part of <envchange-elements>.
                         This is to pick up <env> elements for <exec>. -->
                <element name="envchange-elements" implicit="yes" optional="yes"/>
                <sequential>
                        <!-- Replacing the path completely? -->
                        <if>
                                <not><equals arg1="@{pathreplacement}" arg2="NOT_SUPPLIED"/></not>
                                <then>
                                        <property name="envchange.newpath" value="@{pathreplacement}"/>
                                </then>
                                <else>
                                        <!-- Changing existing path - Get existing Path/PATH env var -->
                                        <if>
                                                <isset property="windows"/>
                                                <then>
                                                        <property name="envchange.oldpath" value="${env.Path}"/>
                                                </then>
                                                <else>
                                                        <property name="envchange.oldpath" value="${env.PATH}"/>
                                                </else>
                                        </if>

                                        <!-- Add specified additions to old path, or just use old path
                                                 if no additions were specified -->
                                        <if>
                                                <equals arg1="@{pathinsertions}" arg2="NOT_SUPPLIED"/>
                                                <then>
                                                        <property name="tmppath" value="${envchange.oldpath}"/>
                                                </then>
                                                <else>
                                                        <property name="tmppath" value="@{pathinsertions};${envchange.oldpath}"/>
                                                </else>
                                        </if>
                                        <if>
                                                <equals arg1="@{pathadditions}" arg2="NOT_SUPPLIED"/>
                                                <then>
                                                        <property name="envchange.newpath" value="${tmppath}"/>
                                                </then>
                                                <else>
                                                        <property name="envchange.newpath" value="${tmppath};@{pathadditions}"/>
                                                </else>
                                        </if>
                                </else>
                        </if>
                        <if>
                                <isset property="windows"/>
                                <then>

                                        <!-- Note we can't use executable=ant.bat because we don't get error codes
                                             propagated back to detect build failures.  This is an inconsistency in
                                             Windows - that you'll get the error code of the batch file's last command
                                             even if it's something irrelevant like a goto!  However if you run a batch
                                             file in an existing shell then an error code will be maintained.  e.g.
                                             if from the cmd line you echo %ERRRORLEVEL%.  So that's why we need to
                                             exec cmd.exe instead and call the batch file from there.
                                         -->
                                    <exec executable="cmd.exe" failonerror="true">
                                        <arg value="/c"/>
                                                <arg value="call ant.bat -f ${ant.file} -Dusing.nocache=true @{target}"/>
                                                <env key="Path" path="${envchange.newpath}"/>   <!-- Windows: use "Path" env var -->
                                                <envchange-elements/>
                                </exec>
                        </then>
                           <else>
                                        <!-- Not Windows - so invoke ant directly -->
                                    <exec executable="ant" failonerror="true">
                                                <arg value="-f"/>
                                                <arg value="${ant.file}"/>
                                                <arg value="-Dusing.nocache=true"/>
                                                <arg value="@{target}"/>
                                                <env key="PATH" path="${envchange.newpath}"/>   <!-- Unix: use "PATH" env var -->
                                                <envchange-elements/>
                                </exec>
                           </else>
                    </if>
                </sequential>
        </macrodef>

</project>