Helmut,

Below is some code that demonstrates how to get at project settings from
within a script-task.  I wanted the capability to read all of my properties
in from a file (comma separated values). The problem I had was that
properties values aren't evaluated recursively.  That is, if the value in my
file was "${buildDir}\src", NAnt wouldn't evaluate the ${buildDir} portion.

Maybe this will be helpful....  Noel


    <target name="initProperties">
        <!-- Read each line of a comma delimited file: properties.csv  -->
        <!-- Line format: variable, value                              -->
        <!-- Create the NAnt properties x and y where                  -->
        <!--       x = variable                                        -->
        <!--       y = value                                           -->
        <!--                                                           -->
        <!-- override with the environment variable if set             -->
        <foreach item="Line" in="properties.csv" delim="," trim="Both"
property="x,y">
            <if test="${environment::variable-exists(x)}">
                <!-- if the variable (x) is defined in the environment, -->
                <!-- then override the value (y) with the environment value
-->
                <property name="y" value="${environment::get-variable(x)}"
/>
            </if>
            <!-- Create the NAnt property 'variable' with value of 'value'
-->
            <property name="${x}" value="${dbs::expandValue(y,2)}" />
            <echo message="Set property '${x}' to ${property::get-value(x)}"
/>
        </foreach>
    </target>
    
    <script language="C#" prefix="dbs" mainclass="dbsHelper" >
        <code>
            <![CDATA[
                [Function("expandValue")]
                public static string expandValue(string value, int depth)
                {
                    return dbsHelper.expandValue(value, depth);
                }
                
                class dbsHelper 
                {
                    static Project _project;
                    
                    public static void ScriptMain(Project project) 
                    {
                        _project = project;
                    }
                    
                    public static string expandValue(string value, 
                               int depth)
                    {
                        if (value.IndexOf('$') < 0 || depth == 0)
                            return value;
                            
                        value = _project.ExpandProperties(value,
                                        Location.UnknownLocation);
                        
                        if (depth > 0)
                            value = expandValue(value, depth-1);
                            
                        return value;
                    }
                }
            ]]>
        </code>
    </script>

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Helmut Dipper
Sent: Friday, January 20, 2006 8:19 AM
To: [email protected]
Subject: [NAnt-users] retrieving current project in custom functions

Hello,

sometimes it may be necessary to use the current project
in custom functions defined via script-task, e.g. for the
method GetPropertyValue from class Nant.Core.ExpressionEvaluator.
How can the current project be retrieved?
Using ScriptMain is no solution for nested builds.
Or how can property values be retrieved otherwise?
Passing properties as parameters to functions is no
solution if the name of the property is known only at run time.

Best regards
Helmut Dipper

-- 
Helmut Dipper                     | AED-SICAD Aktiengesellschaft
phone: +49 89 45026 320           | Postfach 830754
  fax: +49 89 45026 316           | D-81707 Muenchen
email: [EMAIL PROTECTED] | http://www.aed-sicad.com


-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
NAnt-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/nant-users



-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
NAnt-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/nant-users

Reply via email to