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.
31 using System
.Collections
.Generic
;
33 using System
.Reflection
;
36 namespace Microsoft
.Build
.BuildEngine
{
37 internal sealed class ConditionFunctionExpression
: ConditionExpression
{
39 List
<ConditionFactorExpression
> args
;
42 static Dictionary
<string, MethodInfo
> functions
;
44 static ConditionFunctionExpression ()
46 Type t
= typeof (ConditionFunctionExpression
);
47 string [] names
= new string [] { "Exists", "HasTrailingSlash" }
;
49 functions
= new Dictionary
<string, MethodInfo
> ();
50 foreach (string name
in names
)
51 functions
.Add (name
, t
.GetMethod (name
, BindingFlags
.NonPublic
| BindingFlags
.Static
));
54 public ConditionFunctionExpression (string name
, List
<ConditionFactorExpression
> args
)
60 public override bool BoolEvaluate (Project context
)
62 if (!functions
.ContainsKey (name
))
63 throw new InvalidOperationException ();
65 if (functions
[name
] == null)
66 throw new InvalidOperationException ();
68 MethodInfo mi
= functions
[name
];
69 object [] argsArr
= new object [args
.Count
+ 1];
71 foreach (ConditionFactorExpression cfe
in args
)
72 argsArr
[i
++] = cfe
.StringEvaluate (context
);
73 argsArr
[i
] = context
;
75 return (bool) mi
.Invoke (null, argsArr
);
78 public override float NumberEvaluate (Project context
)
80 throw new NotSupportedException ();
83 public override string StringEvaluate (Project context
)
85 throw new NotSupportedException ();
88 public override bool CanEvaluateToBool (Project context
)
90 return functions
.ContainsKey (name
);
93 public override bool CanEvaluateToNumber (Project context
)
98 public override bool CanEvaluateToString (Project context
)
103 #pragma warning disable 0169
105 // FIXME imported projects
106 static bool Exists (string file
, Project context
)
108 string directory
= null;
110 if (context
.FullFileName
!= String
.Empty
)
111 directory
= Path
.GetDirectoryName (context
.FullFileName
);
113 if (!Path
.IsPathRooted (file
) && directory
!= null && directory
!= String
.Empty
)
114 file
= Path
.Combine (directory
, file
);
116 return File
.Exists (file
) || Directory
.Exists (file
);
119 static bool HasTrailingSlash (string file
, Project context
)
126 int len
= file
.Length
;
130 return file
[len
- 1] == '\\' || file
[len
- 1] == '/';
134 #pragma warning restore 0169