Add prebuild source files
This commit is contained in:
parent
cbcd8b2bd3
commit
4f5e7942e1
86 changed files with 18185 additions and 0 deletions
152
src/Core/Utilities/CommandLineCollection.cs
Normal file
152
src/Core/Utilities/CommandLineCollection.cs
Normal file
|
@ -0,0 +1,152 @@
|
|||
#region BSD License
|
||||
|
||||
/*
|
||||
Copyright (c) 2004-2005 Matthew Holmes (matthew@wildfiregames.com), Dan Moorehead (dan05a@gmail.com)
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted
|
||||
provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice, this list of conditions
|
||||
and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions
|
||||
and the following disclaimer in the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
* The name of the author may not be used to endorse or promote products derived from this software
|
||||
without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
|
||||
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Prebuild.Core.Utilities;
|
||||
|
||||
/// <summary>
|
||||
/// The CommandLine class parses and interprets the command-line arguments passed to
|
||||
/// prebuild.
|
||||
/// </summary>
|
||||
public class CommandLineCollection : IEnumerable<KeyValuePair<string, string>>
|
||||
{
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a new CommandLine instance and set some internal variables.
|
||||
/// </summary>
|
||||
public CommandLineCollection(string[] args)
|
||||
{
|
||||
m_RawArgs = args;
|
||||
|
||||
Parse();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
|
||||
/// <summary>
|
||||
/// Gets the parameter associated with the command line option
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Returns null if option was not specified,
|
||||
/// null string if no parameter was specified, and the value if a parameter was specified
|
||||
/// </remarks>
|
||||
public string this[string index]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (m_Arguments.ContainsKey(index)) return m_Arguments[index];
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
|
||||
private void Parse()
|
||||
{
|
||||
if (m_RawArgs.Length < 1)
|
||||
return;
|
||||
|
||||
var idx = 0;
|
||||
string lastArg = null;
|
||||
|
||||
while (idx < m_RawArgs.Length)
|
||||
{
|
||||
var arg = m_RawArgs[idx];
|
||||
|
||||
if (arg.Length > 2 && arg[0] == '/')
|
||||
{
|
||||
arg = arg.Substring(1);
|
||||
lastArg = arg;
|
||||
m_Arguments[arg] = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
if (lastArg != null)
|
||||
{
|
||||
m_Arguments[lastArg] = arg;
|
||||
lastArg = null;
|
||||
}
|
||||
}
|
||||
|
||||
idx++;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Methods
|
||||
|
||||
/// <summary>
|
||||
/// Wases the passed.
|
||||
/// </summary>
|
||||
/// <param name="arg">The arg.</param>
|
||||
/// <returns></returns>
|
||||
public bool WasPassed(string arg)
|
||||
{
|
||||
return m_Arguments.ContainsKey(arg);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Fields
|
||||
|
||||
// The raw OS arguments
|
||||
private readonly string[] m_RawArgs;
|
||||
|
||||
// Command-line argument storage
|
||||
private readonly Dictionary<string, string> m_Arguments = new();
|
||||
|
||||
#endregion
|
||||
|
||||
#region IEnumerable Members
|
||||
|
||||
/// <summary>
|
||||
/// Returns an enumerator that can iterate through a collection.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// An <see cref="T:System.Collections.IDictionaryEnumerator" />
|
||||
/// that can be used to iterate through the collection.
|
||||
/// </returns>
|
||||
public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
|
||||
{
|
||||
return m_Arguments.GetEnumerator();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
65
src/Core/Utilities/CurrentDirectory.cs
Normal file
65
src/Core/Utilities/CurrentDirectory.cs
Normal file
|
@ -0,0 +1,65 @@
|
|||
#region BSD License
|
||||
|
||||
/*
|
||||
Copyright (c) 2004-2005 Matthew Holmes (matthew@wildfiregames.com), Dan Moorehead (dan05a@gmail.com)
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted
|
||||
provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice, this list of conditions
|
||||
and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions
|
||||
and the following disclaimer in the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
* The name of the author may not be used to endorse or promote products derived from this software
|
||||
without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
|
||||
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Prebuild.Core.Utilities;
|
||||
|
||||
/// <summary>
|
||||
/// </summary>
|
||||
public class CurrentDirectory
|
||||
{
|
||||
#region Fields
|
||||
|
||||
private readonly Stack<string> m_Stack = new();
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Methods
|
||||
|
||||
/// <summary>
|
||||
/// Pushes this instance.
|
||||
/// </summary>
|
||||
public void Push()
|
||||
{
|
||||
m_Stack.Push(Environment.CurrentDirectory);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Pops this instance.
|
||||
/// </summary>
|
||||
public void Pop()
|
||||
{
|
||||
if (m_Stack.Count < 1) return;
|
||||
|
||||
var cwd = m_Stack.Pop();
|
||||
Helper.SetCurrentDir(cwd);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
474
src/Core/Utilities/Helper.cs
Normal file
474
src/Core/Utilities/Helper.cs
Normal file
|
@ -0,0 +1,474 @@
|
|||
#region BSD License
|
||||
|
||||
/*
|
||||
Copyright (c) 2004-2005 Matthew Holmes (matthew@wildfiregames.com), Dan Moorehead (dan05a@gmail.com)
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted
|
||||
provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice, this list of conditions
|
||||
and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions
|
||||
and the following disclaimer in the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
* The name of the author may not be used to endorse or promote products derived from this software
|
||||
without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
|
||||
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Specialized;
|
||||
using System.IO;
|
||||
using System.Xml;
|
||||
using Prebuild.Core.Nodes;
|
||||
|
||||
namespace Prebuild.Core.Utilities;
|
||||
|
||||
/// <summary>
|
||||
/// </summary>
|
||||
public class Helper
|
||||
{
|
||||
#region Fields
|
||||
|
||||
/// <summary>
|
||||
/// </summary>
|
||||
public static bool CheckForOSVariables { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Methods
|
||||
|
||||
#region String Parsing
|
||||
|
||||
public delegate string StringLookup(string key);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a collection of StringLocationPair objects that represent the matches
|
||||
/// </summary>
|
||||
/// <param name="target">The target.</param>
|
||||
/// <param name="beforeGroup">The before group.</param>
|
||||
/// <param name="afterGroup">The after group.</param>
|
||||
/// <param name="includeDelimitersInSubstrings">if set to <c>true</c> [include delimiters in substrings].</param>
|
||||
/// <returns></returns>
|
||||
public static StringCollection FindGroups(string target, string beforeGroup, string afterGroup,
|
||||
bool includeDelimitersInSubstrings)
|
||||
{
|
||||
if (beforeGroup == null) throw new ArgumentNullException("beforeGroup");
|
||||
if (afterGroup == null) throw new ArgumentNullException("afterGroup");
|
||||
var results = new StringCollection();
|
||||
if (target == null || target.Length == 0) return results;
|
||||
|
||||
var beforeMod = 0;
|
||||
var afterMod = 0;
|
||||
if (includeDelimitersInSubstrings)
|
||||
{
|
||||
//be sure to not exlude the delims
|
||||
beforeMod = beforeGroup.Length;
|
||||
afterMod = afterGroup.Length;
|
||||
}
|
||||
|
||||
var startIndex = 0;
|
||||
while ((startIndex = target.IndexOf(beforeGroup, startIndex)) != -1)
|
||||
{
|
||||
var endIndex = target.IndexOf(afterGroup, startIndex); //the index of the char after it
|
||||
if (endIndex == -1) break;
|
||||
var length = endIndex - startIndex - beforeGroup.Length; //move to the first char in the string
|
||||
var substring = target.Substring(startIndex + beforeGroup.Length - beforeMod,
|
||||
length - afterMod);
|
||||
|
||||
results.Add(substring);
|
||||
//results.Add(new StringLocationPair(substring,startIndex));
|
||||
startIndex = endIndex + 1;
|
||||
//the Interpolate*() methods will not work if expressions are expandded inside expression due to an optimization
|
||||
//so start after endIndex
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Replaces the groups.
|
||||
/// </summary>
|
||||
/// <param name="target">The target.</param>
|
||||
/// <param name="beforeGroup">The before group.</param>
|
||||
/// <param name="afterGroup">The after group.</param>
|
||||
/// <param name="lookup">The lookup.</param>
|
||||
/// <returns></returns>
|
||||
public static string ReplaceGroups(string target, string beforeGroup, string afterGroup, StringLookup lookup)
|
||||
{
|
||||
if (target == null) throw new ArgumentNullException("target");
|
||||
//int targetLength = target.Length;
|
||||
var strings = FindGroups(target, beforeGroup, afterGroup, false);
|
||||
if (lookup == null) throw new ArgumentNullException("lookup");
|
||||
foreach (var substring in strings)
|
||||
target = target.Replace(beforeGroup + substring + afterGroup, lookup(substring));
|
||||
return target;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Replaces ${var} statements in a string with the corresonding values as detirmined by the lookup delegate
|
||||
/// </summary>
|
||||
/// <param name="target">The target.</param>
|
||||
/// <param name="lookup">The lookup.</param>
|
||||
/// <returns></returns>
|
||||
public static string InterpolateForVariables(string target, StringLookup lookup)
|
||||
{
|
||||
return ReplaceGroups(target, "${", "}", lookup);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Replaces ${var} statements in a string with the corresonding environment variable with name var
|
||||
/// </summary>
|
||||
/// <param name="target"></param>
|
||||
/// <returns></returns>
|
||||
public static string InterpolateForEnvironmentVariables(string target)
|
||||
{
|
||||
return InterpolateForVariables(target, Environment.GetEnvironmentVariable);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Translates the value.
|
||||
/// </summary>
|
||||
/// <param name="translateType">Type of the translate.</param>
|
||||
/// <param name="translationItem">The translation item.</param>
|
||||
/// <returns></returns>
|
||||
public static object TranslateValue(Type translateType, string translationItem)
|
||||
{
|
||||
if (translationItem == null) return null;
|
||||
|
||||
try
|
||||
{
|
||||
var lowerVal = translationItem.ToLower();
|
||||
if (translateType == typeof(bool))
|
||||
return lowerVal == "true" || lowerVal == "1" || lowerVal == "y" || lowerVal == "yes" ||
|
||||
lowerVal == "on";
|
||||
if (translateType == typeof(int))
|
||||
return int.Parse(translationItem);
|
||||
return translationItem;
|
||||
}
|
||||
catch (FormatException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes if exists.
|
||||
/// </summary>
|
||||
/// <param name="file">The file.</param>
|
||||
/// <returns></returns>
|
||||
public static bool DeleteIfExists(string file)
|
||||
{
|
||||
string resFile = null;
|
||||
try
|
||||
{
|
||||
resFile = ResolvePath(file);
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!File.Exists(resFile)) return false;
|
||||
|
||||
File.Delete(resFile);
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool DeleteFolderIfExists(string file)
|
||||
{
|
||||
string resFile = null;
|
||||
try
|
||||
{
|
||||
resFile = ResolvePath(file);
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!Directory.Exists(resFile)) return false;
|
||||
|
||||
Directory.Delete(resFile, true);
|
||||
return true;
|
||||
}
|
||||
|
||||
private static readonly char seperator = Path.DirectorySeparatorChar;
|
||||
|
||||
// This little gem was taken from the NeL source, thanks guys!
|
||||
/// <summary>
|
||||
/// Makes a relative path
|
||||
/// </summary>
|
||||
/// <param name="startPath">Path to start from</param>
|
||||
/// <param name="endPath">Path to end at</param>
|
||||
/// <returns>Path that will get from startPath to endPath</returns>
|
||||
public static string MakePathRelativeTo(string startPath, string endPath)
|
||||
{
|
||||
var tmp = NormalizePath(startPath, seperator);
|
||||
var src = NormalizePath(endPath, seperator);
|
||||
var prefix = "";
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (string.Compare(tmp, 0, src, 0, tmp.Length) == 0)
|
||||
{
|
||||
string ret;
|
||||
var size = tmp.Length;
|
||||
if (size == src.Length) return "./";
|
||||
if (src.Length > tmp.Length && src[tmp.Length - 1] != seperator)
|
||||
{
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = prefix + endPath.Substring(size, endPath.Length - size);
|
||||
ret = ret.Trim();
|
||||
if (ret[0] == seperator) ret = "." + ret;
|
||||
|
||||
return NormalizePath(ret);
|
||||
}
|
||||
}
|
||||
|
||||
if (tmp.Length < 2) break;
|
||||
|
||||
var lastPos = tmp.LastIndexOf(seperator, tmp.Length - 2);
|
||||
var prevPos = tmp.IndexOf(seperator);
|
||||
|
||||
if (lastPos == prevPos || lastPos == -1) break;
|
||||
|
||||
tmp = tmp.Substring(0, lastPos + 1);
|
||||
prefix += ".." + seperator;
|
||||
}
|
||||
|
||||
return endPath;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resolves the path.
|
||||
/// </summary>
|
||||
/// <param name="path">The path.</param>
|
||||
/// <returns></returns>
|
||||
public static string ResolvePath(string path)
|
||||
{
|
||||
var tmpPath = NormalizePath(path);
|
||||
if (tmpPath.Length < 1) tmpPath = ".";
|
||||
|
||||
tmpPath = Path.GetFullPath(tmpPath);
|
||||
if (!File.Exists(tmpPath) && !Directory.Exists(tmpPath))
|
||||
throw new ArgumentException("Path could not be resolved: " + tmpPath);
|
||||
|
||||
return tmpPath;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Normalizes the path.
|
||||
/// </summary>
|
||||
/// <param name="path">The path.</param>
|
||||
/// <param name="separatorCharacter">The separator character.</param>
|
||||
/// <returns></returns>
|
||||
public static string NormalizePath(string path, char separatorCharacter)
|
||||
{
|
||||
if (path == null || path == "" || path.Length < 1) return "";
|
||||
|
||||
var tmpPath = path.Replace('\\', '/');
|
||||
tmpPath = tmpPath.Replace('/', separatorCharacter);
|
||||
return tmpPath;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Normalizes the path.
|
||||
/// </summary>
|
||||
/// <param name="path">The path.</param>
|
||||
/// <returns></returns>
|
||||
public static string NormalizePath(string path)
|
||||
{
|
||||
return NormalizePath(path, Path.DirectorySeparatorChar);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ends the path.
|
||||
/// </summary>
|
||||
/// <param name="path">The path.</param>
|
||||
/// <param name="separatorCharacter">The separator character.</param>
|
||||
/// <returns></returns>
|
||||
public static string EndPath(string path, char separatorCharacter)
|
||||
{
|
||||
if (path == null || path == "" || path.Length < 1) return "";
|
||||
|
||||
if (!path.EndsWith(separatorCharacter.ToString())) return path + separatorCharacter;
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ends the path.
|
||||
/// </summary>
|
||||
/// <param name="path">The path.</param>
|
||||
/// <returns></returns>
|
||||
public static string EndPath(string path)
|
||||
{
|
||||
return EndPath(path, Path.DirectorySeparatorChar);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Makes the file path.
|
||||
/// </summary>
|
||||
/// <param name="path">The path.</param>
|
||||
/// <param name="name">The name.</param>
|
||||
/// <param name="ext">The ext.</param>
|
||||
/// <returns></returns>
|
||||
public static string MakeFilePath(string path, string name, string ext)
|
||||
{
|
||||
var ret = EndPath(NormalizePath(path));
|
||||
|
||||
if (name == null) throw new ArgumentNullException("name");
|
||||
|
||||
ret += name;
|
||||
if (!name.EndsWith("." + ext)) ret += "." + ext;
|
||||
|
||||
//foreach(char c in Path.GetInvalidPathChars())
|
||||
//{
|
||||
// ret = ret.Replace(c, '_');
|
||||
//}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Makes the file path.
|
||||
/// </summary>
|
||||
/// <param name="path">The path.</param>
|
||||
/// <param name="name">The name.</param>
|
||||
/// <returns></returns>
|
||||
public static string MakeFilePath(string path, string name)
|
||||
{
|
||||
var ret = EndPath(NormalizePath(path));
|
||||
|
||||
if (name == null) throw new ArgumentNullException("name");
|
||||
|
||||
ret += name;
|
||||
|
||||
//foreach (char c in Path.GetInvalidPathChars())
|
||||
//{
|
||||
// ret = ret.Replace(c, '_');
|
||||
//}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <returns></returns>
|
||||
public static string MakeReferencePath(string path)
|
||||
{
|
||||
var ret = EndPath(NormalizePath(path));
|
||||
|
||||
//foreach (char c in Path.GetInvalidPathChars())
|
||||
//{
|
||||
// ret = ret.Replace(c, '_');
|
||||
//}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the current dir.
|
||||
/// </summary>
|
||||
/// <param name="path">The path.</param>
|
||||
public static void SetCurrentDir(string path)
|
||||
{
|
||||
if (path == null) throw new ArgumentNullException("path");
|
||||
if (path.Length < 1) return;
|
||||
|
||||
Environment.CurrentDirectory = path;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks the type.
|
||||
/// </summary>
|
||||
/// <param name="typeToCheck">The type to check.</param>
|
||||
/// <param name="attr">The attr.</param>
|
||||
/// <param name="inter">The inter.</param>
|
||||
/// <returns></returns>
|
||||
public static object CheckType(Type typeToCheck, Type attr, Type inter)
|
||||
{
|
||||
if (typeToCheck == null || attr == null) return null;
|
||||
|
||||
var attrs = typeToCheck.GetCustomAttributes(attr, false);
|
||||
if (attrs == null || attrs.Length < 1) return null;
|
||||
if (inter == null) throw new ArgumentNullException("inter");
|
||||
|
||||
if (typeToCheck.GetInterface(inter.FullName) == null) return null;
|
||||
|
||||
return attrs[0];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attributes the value.
|
||||
/// </summary>
|
||||
/// <param name="node">The node.</param>
|
||||
/// <param name="attr">The attr.</param>
|
||||
/// <param name="def">The def.</param>
|
||||
/// <returns></returns>
|
||||
public static string AttributeValue(XmlNode node, string attr, string def)
|
||||
{
|
||||
if (node == null) throw new ArgumentNullException("node");
|
||||
if (node.Attributes[attr] == null) return def;
|
||||
var val = node.Attributes[attr].Value;
|
||||
if (!CheckForOSVariables) return val;
|
||||
|
||||
return InterpolateForEnvironmentVariables(val);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parses the boolean.
|
||||
/// </summary>
|
||||
/// <param name="node">The node.</param>
|
||||
/// <param name="attr">The attr.</param>
|
||||
/// <param name="defaultValue">if set to <c>true</c> [default value].</param>
|
||||
/// <returns></returns>
|
||||
public static bool ParseBoolean(XmlNode node, string attr, bool defaultValue)
|
||||
{
|
||||
if (node == null) throw new ArgumentNullException("node");
|
||||
if (node.Attributes[attr] == null) return defaultValue;
|
||||
return bool.Parse(node.Attributes[attr].Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enums the attribute value.
|
||||
/// </summary>
|
||||
/// <param name="node">The node.</param>
|
||||
/// <param name="attr">The attr.</param>
|
||||
/// <param name="enumType">Type of the enum.</param>
|
||||
/// <param name="def">The def.</param>
|
||||
/// <returns></returns>
|
||||
public static object EnumAttributeValue(XmlNode node, string attr, Type enumType, object def)
|
||||
{
|
||||
if (def == null) throw new ArgumentNullException("def");
|
||||
var val = AttributeValue(node, attr, def.ToString());
|
||||
return Enum.Parse(enumType, val, true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// </summary>
|
||||
/// <param name="assemblyName"></param>
|
||||
/// <param name="projectType"></param>
|
||||
/// <returns></returns>
|
||||
public static string AssemblyFullName(string assemblyName, ProjectType projectType)
|
||||
{
|
||||
return assemblyName + (projectType == ProjectType.Library ? ".dll" : ".exe");
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
250
src/Core/Utilities/Log.cs
Normal file
250
src/Core/Utilities/Log.cs
Normal file
|
@ -0,0 +1,250 @@
|
|||
#region BSD License
|
||||
|
||||
/*
|
||||
Copyright (c) 2004-2005 Matthew Holmes (matthew@wildfiregames.com), Dan Moorehead (dan05a@gmail.com)
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted
|
||||
provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice, this list of conditions
|
||||
and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions
|
||||
and the following disclaimer in the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
* The name of the author may not be used to endorse or promote products derived from this software
|
||||
without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
|
||||
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace Prebuild.Core.Utilities;
|
||||
|
||||
/// <summary>
|
||||
/// </summary>
|
||||
public enum LogType
|
||||
{
|
||||
/// <summary>
|
||||
/// </summary>
|
||||
None,
|
||||
|
||||
/// <summary>
|
||||
/// </summary>
|
||||
Info,
|
||||
|
||||
/// <summary>
|
||||
/// </summary>
|
||||
Warning,
|
||||
|
||||
/// <summary>
|
||||
/// </summary>
|
||||
Error
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// </summary>
|
||||
[Flags]
|
||||
public enum LogTargets
|
||||
{
|
||||
/// <summary>
|
||||
/// </summary>
|
||||
None = 0,
|
||||
|
||||
/// <summary>
|
||||
/// </summary>
|
||||
Null = 1,
|
||||
|
||||
/// <summary>
|
||||
/// </summary>
|
||||
File = 2,
|
||||
|
||||
/// <summary>
|
||||
/// </summary>
|
||||
Console = 4
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Summary description for Log.
|
||||
/// </summary>
|
||||
public class Log : IDisposable
|
||||
{
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Log" /> class.
|
||||
/// </summary>
|
||||
/// <param name="target">The target.</param>
|
||||
/// <param name="fileName">Name of the file.</param>
|
||||
public Log(LogTargets target, string fileName)
|
||||
{
|
||||
m_Target = target;
|
||||
|
||||
if ((m_Target & LogTargets.File) != 0)
|
||||
m_Writer = new StreamWriter(fileName, false);
|
||||
else if ((m_Target & LogTargets.Console) != 0)
|
||||
// Prevents null reference exceptions when outputing to the log file.
|
||||
// This seems to only happen when running on a network drive.
|
||||
m_Writer = Console.Out;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Fields
|
||||
|
||||
private TextWriter m_Writer;
|
||||
private readonly LogTargets m_Target = LogTargets.Null;
|
||||
private bool disposed;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Methods
|
||||
|
||||
/// <summary>
|
||||
/// Writes this instance.
|
||||
/// </summary>
|
||||
public void Write()
|
||||
{
|
||||
Write(string.Empty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the specified MSG.
|
||||
/// </summary>
|
||||
/// <param name="msg">The MSG.</param>
|
||||
public void Write(string msg)
|
||||
{
|
||||
if ((m_Target & LogTargets.Null) != 0) return;
|
||||
|
||||
if ((m_Target & LogTargets.Console) != 0) Console.WriteLine(msg);
|
||||
if ((m_Target & LogTargets.File) != 0 && m_Writer != null) m_Writer.WriteLine(msg);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the specified format.
|
||||
/// </summary>
|
||||
/// <param name="format">The format.</param>
|
||||
/// <param name="args">The args.</param>
|
||||
public void Write(string format, params object[] args)
|
||||
{
|
||||
Write(string.Format(format, args));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the specified type.
|
||||
/// </summary>
|
||||
/// <param name="type">The type.</param>
|
||||
/// <param name="format">The format.</param>
|
||||
/// <param name="args">The args.</param>
|
||||
public void Write(LogType type, string format, params object[] args)
|
||||
{
|
||||
if ((m_Target & LogTargets.Null) != 0) return;
|
||||
|
||||
var str = "";
|
||||
switch (type)
|
||||
{
|
||||
case LogType.Info:
|
||||
str = "[I] ";
|
||||
break;
|
||||
case LogType.Warning:
|
||||
str = "[!] ";
|
||||
break;
|
||||
case LogType.Error:
|
||||
str = "[X] ";
|
||||
break;
|
||||
}
|
||||
|
||||
Write(str + format, args);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the exception.
|
||||
/// </summary>
|
||||
/// <param name="type">The type.</param>
|
||||
/// <param name="ex">The ex.</param>
|
||||
public void WriteException(LogType type, Exception ex)
|
||||
{
|
||||
if (ex != null)
|
||||
{
|
||||
Write(type, ex.Message);
|
||||
//#if DEBUG
|
||||
m_Writer.WriteLine("Exception @{0} stack trace [[", ex.TargetSite.Name);
|
||||
m_Writer.WriteLine(ex.StackTrace);
|
||||
m_Writer.WriteLine("]]");
|
||||
//#endif
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Flushes this instance.
|
||||
/// </summary>
|
||||
public void Flush()
|
||||
{
|
||||
if (m_Writer != null) m_Writer.Flush();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IDisposable Members
|
||||
|
||||
/// <summary>
|
||||
/// Performs application-defined tasks associated with freeing, releasing, or
|
||||
/// resetting unmanaged resources.
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Dispose objects
|
||||
/// </summary>
|
||||
/// <param name="disposing">
|
||||
/// If true, it will dispose close the handle
|
||||
/// </param>
|
||||
/// <remarks>
|
||||
/// Will dispose managed and unmanaged resources.
|
||||
/// </remarks>
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!disposed)
|
||||
if (disposing)
|
||||
if (m_Writer != null)
|
||||
{
|
||||
m_Writer.Close();
|
||||
m_Writer = null;
|
||||
}
|
||||
|
||||
disposed = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// </summary>
|
||||
~Log()
|
||||
{
|
||||
Dispose(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Closes and destroys this object
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Same as Dispose(true)
|
||||
/// </remarks>
|
||||
public void Close()
|
||||
{
|
||||
Dispose();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue