Hello,

to extend the set of functions I want to define some via
the script-task (see attached file).
Within some implementations I want to use the predefined
functions. If they are realized by "static" methods this
is only a job to find name and class of them.
But if an instance of a class, for example class "Project"
or "ExpressionEvaluator", is necessary I found no way to
retrieve one though they must exist. How can I get the
instance "current project" resp. "current expression"
to apply the commented calls in lines 110-112 of the file?
Especially to retrieve the value of a property
("EvaluateProperty" resp. "GetPropertyValue") should be
possible inside the script-code. How can this be achieved?

Best regards
Helmut Dipper
<project name="fct-extensions">
   <script language="C#" prefix="path">
      <imports>
         <import name="System.Runtime.InteropServices"/>
         <import name="NAnt.Core.Functions"/>
      </imports>
      <code>
         <![CDATA[
         // converts given dos-path to unix format
         [Function("conv_to_unix")]
         public static string conv_to_unix(string path)
         {
            StringBuilder buffer = new StringBuilder(path.Length);

            foreach (char c in path)
            {
               if (c == '\\')
                  buffer.Append('/');
               else
                  buffer.Append(c);
            }

            if (buffer.Length > 1
            && buffer[1] == ':')
            {
               buffer[1] = buffer[0];
               buffer[0] = '/';
            }

            return buffer.ToString();
         }

         // converts given dos-path-list to unix format
         [Function("conv_to_unix_list")]
         public static string conv_to_unix_list(string path_list)
         {
            string delim_dos = ";";
            string delim_unix = ":";

            string[] dos_pathes = path_list.Split(delim_dos.ToCharArray());

            int num = dos_pathes.GetLength(0);

            int ind;
            string[] unix_pathes = new String[num];
            for ( ind = 0 ; ind < num ; ind++ )
               unix_pathes.SetValue(conv_to_unix(dos_pathes[ind]),ind);

            return String.Join(delim_unix,unix_pathes);
         }

         // simplifies given dos-path by eliminating sequences of ".."
         [Function("simplify")]
         public static string simplify(string path)
         {
            string delim = "\\";

            string[] src_parts = path.Split(delim.ToCharArray());
            int src_num = src_parts.GetLength(0);

            string[] tgt_parts = new String[src_num];
            int tgt_num = 0;

            foreach ( string part in src_parts )
            {
               if (tgt_num > 0 && part == "..")
                  tgt_num--;
               else
               {
                  tgt_parts[tgt_num] = part;
                  tgt_num++;
               }
            }

            if (tgt_num == 0)
               return ".";
            else
               return String.Join(delim,tgt_parts,0,tgt_num);
         }

         // combines two pathes and simplifies the result
         [Function("simplify_combined")]
         public static string simplify_combined(string p1, string p2)
         {
            return simplify(PathFunctions.Combine(p1,p2));
         }

         // doubles the separator in the given dos-path
         [Function("double_sep")]
         public static string double_sep(string path)
         {
            StringBuilder buffer = new StringBuilder(2 * path.Length);

            foreach (char c in path)
            {
               if (c == '\\')
                  buffer.Append('\\');

               buffer.Append(c);
            }

            return buffer.ToString();
         }

/*
         // delivers the current projects base-directory
         [Function("project_dir")]
         public static string project_dir()
         {
            //return 
PathFunctions.GetDirectoryName(ProjectFunctions.GetBuildFilePath());
            //return 
ExpressionEvaluator.GetPropertyValue("nant.project.basedir");
            //return 
ExpressionEvaluator.EvaluateProperty("nant.project.basedir");

         }

         // delivers the given relative path transformed to an absolute
         // path which is based on the current projects base-directory
         [Function("project_path")]
         public static string project_path(string path)
         {
            return simplify_combined(project_dir(),path);
         }
*/
         ]]>
      </code>
   </script>


   <script language="C#" prefix="date-time">
      <imports>
         <import name="System.Globalization"/>
      </imports>
      <code>
         <![CDATA[
         // the functions deliver the current date-time
         // in the specified respectivly given format

         // long format for humans
         [Function("cur_human")]
         public static string cur_human()
         {
            DateTime dt = DateTime.Now;

            return dt.ToString("dddd, dd. MMMM yyyy HH:mm:ss");
         }

         // long format for arithmetic and comparison
         [Function("cur_arith")]
         public static string cur_arith()
         {
            DateTime dt = DateTime.Now;

            return dt.ToString("yyyy-MM-dd HH:mm:ss");
         }

         // given format
         [Function("cur")]
         public static string cur(string format)
         {
            DateTime dt = DateTime.Now;

            return dt.ToString(format);
         }
         ]]>
      </code>
   </script>


   <script language="C#" prefix="cmd">
      <code>
         <![CDATA[
         // functions realizing extensions concerning dos-commands

         // combine given command with an extension of
         // environment variable PATH by the given path
         [Function("extended")]
         public static string extended(string path,string cmd)
         {
            return String.Concat("PATH ",path,";%PATH% && ",cmd);
         }
         ]]>
      </code>
   </script>


   <property name="fct-extensions.loaded" value="true"/>
</project>

Reply via email to