aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Prebuild/src/Core/Utilities/Helper.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Prebuild/src/Core/Utilities/Helper.cs')
-rw-r--r--Prebuild/src/Core/Utilities/Helper.cs654
1 files changed, 0 insertions, 654 deletions
diff --git a/Prebuild/src/Core/Utilities/Helper.cs b/Prebuild/src/Core/Utilities/Helper.cs
deleted file mode 100644
index 9a0d131..0000000
--- a/Prebuild/src/Core/Utilities/Helper.cs
+++ /dev/null
@@ -1,654 +0,0 @@
1#region BSD License
2/*
3Copyright (c) 2004-2005 Matthew Holmes (matthew@wildfiregames.com), Dan Moorehead (dan05a@gmail.com)
4
5Redistribution and use in source and binary forms, with or without modification, are permitted
6provided that the following conditions are met:
7
8* Redistributions of source code must retain the above copyright notice, this list of conditions
9 and the following disclaimer.
10* Redistributions in binary form must reproduce the above copyright notice, this list of conditions
11 and the following disclaimer in the documentation and/or other materials provided with the
12 distribution.
13* The name of the author may not be used to endorse or promote products derived from this software
14 without specific prior written permission.
15
16THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
17BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
22IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23*/
24#endregion
25
26using System;
27using System.Collections;
28using System.Diagnostics;
29using System.IO;
30using System.Runtime.InteropServices;
31using System.Text.RegularExpressions;
32using System.Collections.Specialized;
33using System.Xml;
34using Prebuild.Core.Nodes;
35
36namespace Prebuild.Core.Utilities
37{
38 /// <summary>
39 ///
40 /// </summary>
41 public class Helper
42 {
43 #region Fields
44
45 private static Stack dirStack;
46 private static Regex varRegex;
47 static bool checkForOSVariables;
48
49 /// <summary>
50 ///
51 /// </summary>
52 public static bool CheckForOSVariables
53 {
54 get
55 {
56 return checkForOSVariables;
57 }
58 set
59 {
60 checkForOSVariables = value;
61 }
62 }
63
64 #endregion
65
66 #region Constructors
67
68 /// <summary>
69 /// Initializes the <see cref="Helper"/> class.
70 /// </summary>
71 static Helper()
72 {
73 dirStack = new Stack();
74 //m_VarRegex = new Regex(@"\${(?<var>[\w|_]+)}");
75 }
76
77 #endregion
78
79 #region Properties
80
81 /// <summary>
82 ///
83 /// </summary>
84 public static Stack DirStack
85 {
86 get
87 {
88 return dirStack;
89 }
90 }
91
92 /// <summary>
93 ///
94 /// </summary>
95 public static Regex VarRegex
96 {
97 get
98 {
99 return varRegex;
100 }
101 set
102 {
103 varRegex = value;
104 }
105 }
106
107 #endregion
108
109 #region Public Methods
110
111 #region String Parsing
112 #region Inner Classes and Delegates
113 /// <summary>
114 ///
115 /// </summary>
116 public delegate string StringLookup(string key);
117
118 #endregion
119
120 /// <summary>
121 /// Gets a collection of StringLocationPair objects that represent the matches
122 /// </summary>
123 /// <param name="target">The target.</param>
124 /// <param name="beforeGroup">The before group.</param>
125 /// <param name="afterGroup">The after group.</param>
126 /// <param name="includeDelimitersInSubstrings">if set to <c>true</c> [include delimiters in substrings].</param>
127 /// <returns></returns>
128 public static StringCollection FindGroups(string target, string beforeGroup, string afterGroup, bool includeDelimitersInSubstrings)
129 {
130 if( beforeGroup == null )
131 {
132 throw new ArgumentNullException("beforeGroup");
133 }
134 if( afterGroup == null )
135 {
136 throw new ArgumentNullException("afterGroup");
137 }
138 StringCollection results = new StringCollection();
139 if(target == null || target.Length == 0)
140 {
141 return results;
142 }
143
144 int beforeMod = 0;
145 int afterMod = 0;
146 if(includeDelimitersInSubstrings)
147 {
148 //be sure to not exlude the delims
149 beforeMod = beforeGroup.Length;
150 afterMod = afterGroup.Length;
151 }
152 int startIndex = 0;
153 while((startIndex = target.IndexOf(beforeGroup,startIndex)) != -1) {
154 int endIndex = target.IndexOf(afterGroup,startIndex);//the index of the char after it
155 if(endIndex == -1)
156 {
157 break;
158 }
159 int length = endIndex - startIndex - beforeGroup.Length;//move to the first char in the string
160 string substring = substring = target.Substring(startIndex + beforeGroup.Length - beforeMod,
161 length - afterMod);
162
163 results.Add(substring);
164 //results.Add(new StringLocationPair(substring,startIndex));
165 startIndex = endIndex + 1;
166 //the Interpolate*() methods will not work if expressions are expandded inside expression due to an optimization
167 //so start after endIndex
168
169 }
170 return results;
171 }
172
173 /// <summary>
174 /// Replaces the groups.
175 /// </summary>
176 /// <param name="target">The target.</param>
177 /// <param name="beforeGroup">The before group.</param>
178 /// <param name="afterGroup">The after group.</param>
179 /// <param name="lookup">The lookup.</param>
180 /// <returns></returns>
181 public static string ReplaceGroups(string target, string beforeGroup, string afterGroup, StringLookup lookup) {
182 if( target == null )
183 {
184 throw new ArgumentNullException("target");
185 }
186 //int targetLength = target.Length;
187 StringCollection strings = FindGroups(target,beforeGroup,afterGroup,false);
188 if( lookup == null )
189 {
190 throw new ArgumentNullException("lookup");
191 }
192 foreach(string substring in strings)
193 {
194 target = target.Replace(beforeGroup + substring + afterGroup, lookup(substring) );
195 }
196 return target;
197 }
198
199 /// <summary>
200 /// Replaces ${var} statements in a string with the corresonding values as detirmined by the lookup delegate
201 /// </summary>
202 /// <param name="target">The target.</param>
203 /// <param name="lookup">The lookup.</param>
204 /// <returns></returns>
205 public static string InterpolateForVariables(string target, StringLookup lookup)
206 {
207 return ReplaceGroups(target, "${" , "}" , lookup);
208 }
209
210 /// <summary>
211 /// Replaces ${var} statements in a string with the corresonding environment variable with name var
212 /// </summary>
213 /// <param name="target"></param>
214 /// <returns></returns>
215 public static string InterpolateForEnvironmentVariables(string target)
216 {
217 return InterpolateForVariables(target, new StringLookup(Environment.GetEnvironmentVariable));
218 }
219
220 #endregion
221
222 /// <summary>
223 /// Translates the value.
224 /// </summary>
225 /// <param name="translateType">Type of the translate.</param>
226 /// <param name="translationItem">The translation item.</param>
227 /// <returns></returns>
228 public static object TranslateValue(Type translateType, string translationItem)
229 {
230 if(translationItem == null)
231 {
232 return null;
233 }
234
235 try
236 {
237 string lowerVal = translationItem.ToLower();
238 if(translateType == typeof(bool))
239 {
240 return (lowerVal == "true" || lowerVal == "1" || lowerVal == "y" || lowerVal == "yes" || lowerVal == "on");
241 }
242 else if(translateType == typeof(int))
243 {
244 return (Int32.Parse(translationItem));
245 }
246 else
247 {
248 return translationItem;
249 }
250 }
251 catch(FormatException)
252 {
253 return null;
254 }
255 }
256
257 /// <summary>
258 /// Deletes if exists.
259 /// </summary>
260 /// <param name="file">The file.</param>
261 /// <returns></returns>
262 public static bool DeleteIfExists(string file)
263 {
264 string resFile = null;
265 try
266 {
267 resFile = ResolvePath(file);
268 }
269 catch(ArgumentException)
270 {
271 return false;
272 }
273
274 if(!File.Exists(resFile))
275 {
276 return false;
277 }
278
279 File.Delete(resFile);
280 return true;
281 }
282
283 static readonly char seperator = Path.DirectorySeparatorChar;
284
285 // This little gem was taken from the NeL source, thanks guys!
286 /// <summary>
287 /// Makes a relative path
288 /// </summary>
289 /// <param name="startPath">Path to start from</param>
290 /// <param name="endPath">Path to end at</param>
291 /// <returns>Path that will get from startPath to endPath</returns>
292 public static string MakePathRelativeTo(string startPath, string endPath)
293 {
294 string tmp = NormalizePath(startPath, seperator);
295 string src = NormalizePath(endPath, seperator);
296 string prefix = "";
297
298 while(true)
299 {
300 if((String.Compare(tmp, 0, src, 0, tmp.Length) == 0))
301 {
302 string ret;
303 int size = tmp.Length;
304 if(size == src.Length)
305 {
306 return "./";
307 }
308 if((src.Length > tmp.Length) && src[tmp.Length - 1] != seperator)
309 {
310 }
311 else
312 {
313 ret = prefix + endPath.Substring(size, endPath.Length - size);
314 ret = ret.Trim();
315 if(ret[0] == seperator)
316 {
317 ret = "." + ret;
318 }
319
320 return NormalizePath(ret);
321 }
322
323 }
324
325 if(tmp.Length < 2)
326 {
327 break;
328 }
329
330 int lastPos = tmp.LastIndexOf(seperator, tmp.Length - 2);
331 int prevPos = tmp.IndexOf(seperator);
332
333 if((lastPos == prevPos) || (lastPos == -1))
334 {
335 break;
336 }
337
338 tmp = tmp.Substring(0, lastPos + 1);
339 prefix += ".." + seperator.ToString();
340 }
341
342 return endPath;
343 }
344
345 /// <summary>
346 /// Resolves the path.
347 /// </summary>
348 /// <param name="path">The path.</param>
349 /// <returns></returns>
350 public static string ResolvePath(string path)
351 {
352 string tmpPath = NormalizePath(path);
353 if(tmpPath.Length < 1)
354 {
355 tmpPath = ".";
356 }
357
358 tmpPath = Path.GetFullPath(tmpPath);
359 if(!File.Exists(tmpPath) && !Directory.Exists(tmpPath))
360 {
361 throw new ArgumentException("Path could not be resolved: " + tmpPath);
362 }
363
364 return tmpPath;
365 }
366
367 /// <summary>
368 /// Normalizes the path.
369 /// </summary>
370 /// <param name="path">The path.</param>
371 /// <param name="separatorCharacter">The separator character.</param>
372 /// <returns></returns>
373 public static string NormalizePath(string path, char separatorCharacter)
374 {
375 if(path == null || path == "" || path.Length < 1)
376 {
377 return "";
378 }
379
380 string tmpPath = path.Replace('\\', '/');
381 tmpPath = tmpPath.Replace('/', separatorCharacter);
382 return tmpPath;
383 }
384
385 /// <summary>
386 /// Normalizes the path.
387 /// </summary>
388 /// <param name="path">The path.</param>
389 /// <returns></returns>
390 public static string NormalizePath(string path)
391 {
392 return NormalizePath(path, Path.DirectorySeparatorChar);
393 }
394
395 /// <summary>
396 /// Ends the path.
397 /// </summary>
398 /// <param name="path">The path.</param>
399 /// <param name="separatorCharacter">The separator character.</param>
400 /// <returns></returns>
401 public static string EndPath(string path, char separatorCharacter)
402 {
403 if(path == null || path == "" || path.Length < 1)
404 {
405 return "";
406 }
407
408 if(!path.EndsWith(separatorCharacter.ToString()))
409 {
410 return (path + separatorCharacter);
411 }
412
413 return path;
414 }
415
416 /// <summary>
417 /// Ends the path.
418 /// </summary>
419 /// <param name="path">The path.</param>
420 /// <returns></returns>
421 public static string EndPath(string path)
422 {
423 return EndPath(path, Path.DirectorySeparatorChar);
424 }
425
426 /// <summary>
427 /// Makes the file path.
428 /// </summary>
429 /// <param name="path">The path.</param>
430 /// <param name="name">The name.</param>
431 /// <param name="ext">The ext.</param>
432 /// <returns></returns>
433 public static string MakeFilePath(string path, string name, string ext)
434 {
435 string ret = EndPath(NormalizePath(path));
436
437 if( name == null )
438 {
439 throw new ArgumentNullException("name");
440 }
441
442 ret += name;
443 if(!name.EndsWith("." + ext))
444 {
445 ret += "." + ext;
446 }
447
448 //foreach(char c in Path.GetInvalidPathChars())
449 //{
450 // ret = ret.Replace(c, '_');
451 //}
452
453 return ret;
454 }
455
456 /// <summary>
457 /// Makes the file path.
458 /// </summary>
459 /// <param name="path">The path.</param>
460 /// <param name="name">The name.</param>
461 /// <returns></returns>
462 public static string MakeFilePath(string path, string name)
463 {
464 string ret = EndPath(NormalizePath(path));
465
466 if( name == null )
467 {
468 throw new ArgumentNullException("name");
469 }
470
471 ret += name;
472
473 //foreach (char c in Path.GetInvalidPathChars())
474 //{
475 // ret = ret.Replace(c, '_');
476 //}
477
478 return ret;
479 }
480
481 /// <summary>
482 ///
483 /// </summary>
484 /// <param name="path"></param>
485 /// <returns></returns>
486 public static string MakeReferencePath(string path)
487 {
488 string ret = EndPath(NormalizePath(path));
489
490 //foreach (char c in Path.GetInvalidPathChars())
491 //{
492 // ret = ret.Replace(c, '_');
493 //}
494
495 return ret;
496 }
497
498 /// <summary>
499 /// Sets the current dir.
500 /// </summary>
501 /// <param name="path">The path.</param>
502 public static void SetCurrentDir(string path)
503 {
504 if( path == null )
505 {
506 throw new ArgumentNullException("path");
507 }
508 if(path.Length < 1)
509 {
510 return;
511 }
512
513 Environment.CurrentDirectory = path;
514 }
515
516 /// <summary>
517 /// Checks the type.
518 /// </summary>
519 /// <param name="typeToCheck">The type to check.</param>
520 /// <param name="attr">The attr.</param>
521 /// <param name="inter">The inter.</param>
522 /// <returns></returns>
523 public static object CheckType(Type typeToCheck, Type attr, Type inter)
524 {
525 if(typeToCheck == null || attr == null)
526 {
527 return null;
528 }
529
530 object[] attrs = typeToCheck.GetCustomAttributes(attr, false);
531 if(attrs == null || attrs.Length < 1)
532 {
533 return null;
534 }
535 if( inter == null )
536 {
537 throw new ArgumentNullException("inter");
538 }
539
540 if(typeToCheck.GetInterface(inter.FullName) == null)
541 {
542 return null;
543 }
544
545 return attrs[0];
546 }
547
548 /* A bit of overhead for simple group parsing, there are problems with Regex in Mono
549 public static string ParseValue(string val)
550 {
551 if(val == null || val.Length < 1 || !CheckForOSVariables)
552 return val;
553
554 string tmp = val;
555 Match m = m_VarRegex.Match(val);
556 while(m.Success)
557 {
558 if(m.Groups["var"] == null)
559 continue;
560
561 Capture c = m.Groups["var"].Captures[0];
562 if(c == null)
563 continue;
564
565 string var = c.Value;
566 string envVal = Environment.GetEnvironmentVariable(var);
567 if(envVal == null)
568 envVal = "";
569
570 tmp = tmp.Replace("${" + var + "}", envVal);
571 m = m.NextMatch();
572 }
573
574 return tmp;
575 }*/
576
577 /// <summary>
578 /// Attributes the value.
579 /// </summary>
580 /// <param name="node">The node.</param>
581 /// <param name="attr">The attr.</param>
582 /// <param name="def">The def.</param>
583 /// <returns></returns>
584 public static string AttributeValue(XmlNode node, string attr, string def)
585 {
586 if( node == null )
587 {
588 throw new ArgumentNullException("node");
589 }
590 if(node.Attributes[attr] == null)
591 {
592 return def;
593 }
594 string val = node.Attributes[attr].Value;
595 if(!CheckForOSVariables)
596 {
597 return val;
598 }
599
600 return InterpolateForEnvironmentVariables(val);
601 }
602
603 /// <summary>
604 /// Parses the boolean.
605 /// </summary>
606 /// <param name="node">The node.</param>
607 /// <param name="attr">The attr.</param>
608 /// <param name="defaultValue">if set to <c>true</c> [default value].</param>
609 /// <returns></returns>
610 public static bool ParseBoolean(XmlNode node, string attr, bool defaultValue)
611 {
612 if( node == null )
613 {
614 throw new ArgumentNullException("node");
615 }
616 if(node.Attributes[attr] == null)
617 {
618 return defaultValue;
619 }
620 return bool.Parse(node.Attributes[attr].Value);
621 }
622
623 /// <summary>
624 /// Enums the attribute value.
625 /// </summary>
626 /// <param name="node">The node.</param>
627 /// <param name="attr">The attr.</param>
628 /// <param name="enumType">Type of the enum.</param>
629 /// <param name="def">The def.</param>
630 /// <returns></returns>
631 public static object EnumAttributeValue(XmlNode node, string attr, Type enumType, object def)
632 {
633 if( def == null )
634 {
635 throw new ArgumentNullException("def");
636 }
637 string val = AttributeValue(node, attr, def.ToString());
638 return Enum.Parse(enumType, val, true);
639 }
640
641 /// <summary>
642 ///
643 /// </summary>
644 /// <param name="assemblyName"></param>
645 /// <param name="projectType"></param>
646 /// <returns></returns>
647 public static string AssemblyFullName(string assemblyName, ProjectType projectType)
648 {
649 return assemblyName + (projectType == ProjectType.Library ? ".dll" : ".exe");
650 }
651
652 #endregion
653 }
654}