Merrill,

> *  Is there a better way in NAnt to getting the environment variables
set
> for each <exec>?

Aha, finally I get to give back!

If he's using 0.85, please see the below, the sum of a discussion Ian
MacLean and I had a while ago on this list (BTW, this is still possible
in 0.84, just uglier)... Ian's the mastermind behind the streamlined
code below.

<!-- This snippet only works for NAnt 0.85 (assume and up) -->
<script language="C#" >
            <imports>
                <import namespace="System.Runtime.InteropServices"/>
            </imports>
            <code><![CDATA[                
                [DllImport("kernel32.dll", SetLastError=true)]
                static extern bool SetEnvironmentVariable(string lpName,
string lpValue);
               
                [TaskName("setenv")]
                public class SetEnvTask : Task {               

                private string _name;
                private string _value;
               
                [TaskAttribute("name", Required=true)]
                public string EnvName {
                    get { return _name; }
                    set { _name = value; }
                }
                [TaskAttribute("value", Required=true)]
                public string EnvValue {
                    get { return _value; }
                    set { _value = value; }
                }
               
                protected override void ExecuteTask() {
                    Log(Level.Info, "Setting env var {0} to {1}",
EnvName, EnvValue );
                    if ( ! SetEnvironmentVariable(EnvName, EnvValue) ) {
                        throw new BuildException(string.Format("Error
setting env var {0} to {1}", EnvName, EnvValue ) , Location);
                    }                   
                }       
        }
        ]]></code>
    </script>
         
    <!-- echo current value if it exists -->   
    <if test="${environment::variable-exists('X')}" >
        <echo message="X = ${environment::get-variable('X')}" />
    </if>
       
    <echo message="Setting new value"/>      
    <setenv name="X" value="test"/>
    <echo message="X = ${environment::get-variable('X')}" />       

------------------------------------------------------------------------
------------------------------------------------------------------------
----------------
This way you don't have to call a specific target - just use the custom
<setenv> task. And its simple to set variables between calls to other
tasks.

The above generates the following output:
Target(s) specified: build

   [script] Scanning assembly "00f_4pst" for extensions.
     [echo] Setting new value
   [setenv] Setting env var X to test
     [echo] X = test

build:


This is probably somthing worth considering adding to NAnt. Of course we
would need to use platform #ifdefs as the above is windows specific.

Ian



One tip: if someone is using 
    <echo message="X = ${sys.env.x}"/>
instead of
    <echo message="X = ${environment::get-variable('x')}" /> 

they'll need to refresh sys.env after changing (or prior to checking)
values:
                <sysinfo verbose="true"/>
                <echo message="X = ${sys.env.x}"/> 



-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_ide95&alloc_id396&op=click
_______________________________________________
Nant-users mailing list
Nant-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/nant-users

Reply via email to