2 // ConditionFunctionExpression.cs
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
7 // (C) 2006 Marek Sieradzki
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.Collections.Generic;
31 using System.Reflection;
34 namespace Microsoft.Build.BuildEngine {
35 internal sealed class ConditionFunctionExpression : ConditionExpression {
37 List <ConditionFactorExpression> args;
40 static Dictionary <string, MethodInfo> functions;
42 static ConditionFunctionExpression ()
44 Type t = typeof (ConditionFunctionExpression);
45 string [] names = new string [] { "Exists", "HasTrailingSlash" };
47 functions = new Dictionary <string, MethodInfo> (StringComparer.OrdinalIgnoreCase);
48 foreach (string name in names)
49 functions.Add (name, t.GetMethod (name, BindingFlags.NonPublic | BindingFlags.Static));
52 public ConditionFunctionExpression (string name, List <ConditionFactorExpression> args)
58 public override bool BoolEvaluate (Project context)
60 if (!functions.ContainsKey (name))
61 throw new InvalidOperationException ("Unknown function named: " + name);
63 if (functions [name] == null)
64 throw new InvalidOperationException ("Unknown function named: " + name);
66 MethodInfo mi = functions [name];
67 object [] argsArr = new object [args.Count + 1];
69 foreach (ConditionFactorExpression cfe in args)
70 argsArr [i++] = cfe.StringEvaluate (context);
71 argsArr [i] = context;
73 return (bool) mi.Invoke (null, argsArr);
76 public override float NumberEvaluate (Project context)
78 throw new NotSupportedException ();
81 public override string StringEvaluate (Project context)
83 throw new NotSupportedException ();
86 public override bool CanEvaluateToBool (Project context)
88 return functions.ContainsKey (name);
91 public override bool CanEvaluateToNumber (Project context)
96 public override bool CanEvaluateToString (Project context)
101 #pragma warning disable 0169
103 // FIXME imported projects
104 static bool Exists (string file, Project context)
106 string directory = null;
108 if (context.FullFileName != String.Empty)
109 directory = Path.GetDirectoryName (context.FullFileName);
111 if (!Path.IsPathRooted (file) && directory != null && directory != String.Empty)
112 file = Path.Combine (directory, file);
114 return File.Exists (file) || Directory.Exists (file);
117 static bool HasTrailingSlash (string file, Project context)
124 int len = file.Length;
128 return file [len - 1] == '\\' || file [len - 1] == '/';
132 #pragma warning restore 0169