/Presentations/MetaProgrammingWithT4/Demos/Demo_007/Include_T4Include.cs

https://bitbucket.org/wcom/wcom-public · C# · 533 lines · 358 code · 84 blank · 91 comment · 31 complexity · a7531ce16e8f9533d0f8329dcf39e3b8 MD5 · raw file

  1. // ############################################################################
  2. // # #
  3. // # ---==> T H I S F I L E I S G E N E R A T E D <==--- #
  4. // # #
  5. // # This means that any edits to the .cs file will be lost when its #
  6. // # regenerated. Changes should instead be applied to the corresponding #
  7. // # text template file (.tt) #
  8. // ############################################################################
  9. // ############################################################################
  10. // @@@ INCLUDING: https://raw.github.com/mrange/T4Include/master/Extensions/BasicExtensions.cs
  11. // @@@ INCLUDE_FOUND: ../Common/Array.cs
  12. // @@@ INCLUDE_FOUND: ../Common/Config.cs
  13. // @@@ INCLUDE_FOUND: ../Common/Log.cs
  14. // @@@ INCLUDING: https://raw.github.com/mrange/T4Include/master/Common/Array.cs
  15. // @@@ INCLUDING: https://raw.github.com/mrange/T4Include/master/Common/Config.cs
  16. // @@@ INCLUDING: https://raw.github.com/mrange/T4Include/master/Common/Log.cs
  17. // @@@ INCLUDE_FOUND: Generated_Log.cs
  18. // @@@ INCLUDING: https://raw.github.com/mrange/T4Include/master/Common/Generated_Log.cs
  19. // ############################################################################
  20. // Certains directives such as #define and // Resharper comments has to be
  21. // moved to top in order to work properly
  22. // ############################################################################
  23. // ReSharper disable InconsistentNaming
  24. // ReSharper disable PartialMethodWithSinglePart
  25. // ReSharper disable PartialTypeWithSinglePart
  26. // ReSharper disable RedundantNameQualifier
  27. // ############################################################################
  28. // ############################################################################
  29. namespace Demo_007
  30. {
  31. // ----------------------------------------------------------------------------------------------
  32. // Copyright (c) Mårten Rånge.
  33. // ----------------------------------------------------------------------------------------------
  34. // This source code is subject to terms and conditions of the Microsoft Public License. A
  35. // copy of the license can be found in the License.html file at the root of this distribution.
  36. // If you cannot locate the Microsoft Public License, please send an email to
  37. // dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  38. // by the terms of the Microsoft Public License.
  39. // ----------------------------------------------------------------------------------------------
  40. // You must not remove this notice, or any other, from this software.
  41. // ----------------------------------------------------------------------------------------------
  42. namespace Source.Extensions
  43. {
  44. using System;
  45. using System.Collections.Generic;
  46. using System.Globalization;
  47. using System.IO;
  48. using System.Reflection;
  49. using Source.Common;
  50. static partial class BasicExtensions
  51. {
  52. public static bool IsNullOrWhiteSpace (this string v)
  53. {
  54. return string.IsNullOrWhiteSpace (v);
  55. }
  56. public static bool IsNullOrEmpty (this string v)
  57. {
  58. return string.IsNullOrEmpty (v);
  59. }
  60. public static T FirstOrReturn<T>(this T[] values, T defaultValue)
  61. {
  62. if (values == null)
  63. {
  64. return defaultValue;
  65. }
  66. if (values.Length == 0)
  67. {
  68. return defaultValue;
  69. }
  70. return values[0];
  71. }
  72. public static T FirstOrReturn<T>(this IEnumerable<T> values, T defaultValue)
  73. {
  74. if (values == null)
  75. {
  76. return defaultValue;
  77. }
  78. foreach (var value in values)
  79. {
  80. return value;
  81. }
  82. return defaultValue;
  83. }
  84. public static string DefaultTo (this string v, string defaultValue = null)
  85. {
  86. return !v.IsNullOrEmpty () ? v : (defaultValue ?? "");
  87. }
  88. public static IEnumerable<T> DefaultTo<T>(
  89. this IEnumerable<T> values,
  90. IEnumerable<T> defaultValue = null
  91. )
  92. {
  93. return values ?? defaultValue ?? Array<T>.Empty;
  94. }
  95. public static T[] DefaultTo<T>(this T[] values, T[] defaultValue = null)
  96. {
  97. return values ?? defaultValue ?? Array<T>.Empty;
  98. }
  99. public static T DefaultTo<T>(this T v, T defaultValue = default (T))
  100. where T : struct, IEquatable<T>
  101. {
  102. return !v.Equals (default (T)) ? v : defaultValue;
  103. }
  104. public static string FormatWith (this string format, CultureInfo cultureInfo, params object[] args)
  105. {
  106. return string.Format (cultureInfo, format ?? "", args.DefaultTo ());
  107. }
  108. public static string FormatWith (this string format, params object[] args)
  109. {
  110. return format.FormatWith (Config.DefaultCulture, args);
  111. }
  112. public static TValue Lookup<TKey, TValue>(
  113. this IDictionary<TKey, TValue> dictionary,
  114. TKey key,
  115. TValue defaultValue = default (TValue))
  116. {
  117. if (dictionary == null)
  118. {
  119. return defaultValue;
  120. }
  121. TValue value;
  122. return dictionary.TryGetValue (key, out value) ? value : defaultValue;
  123. }
  124. public static TValue GetOrAdd<TKey, TValue>(
  125. this IDictionary<TKey, TValue> dictionary,
  126. TKey key,
  127. TValue defaultValue = default (TValue))
  128. {
  129. if (dictionary == null)
  130. {
  131. return defaultValue;
  132. }
  133. TValue value;
  134. if (!dictionary.TryGetValue (key, out value))
  135. {
  136. value = defaultValue;
  137. dictionary[key] = value;
  138. }
  139. return value;
  140. }
  141. public static TValue GetOrAdd<TKey, TValue>(
  142. this IDictionary<TKey, TValue> dictionary,
  143. TKey key,
  144. Func<TValue> valueCreator
  145. )
  146. {
  147. if (dictionary == null)
  148. {
  149. return valueCreator ();
  150. }
  151. TValue value;
  152. if (!dictionary.TryGetValue (key, out value))
  153. {
  154. value = valueCreator ();
  155. dictionary[key] = value;
  156. }
  157. return value;
  158. }
  159. public static void DisposeNoThrow (this IDisposable disposable)
  160. {
  161. try
  162. {
  163. if (disposable != null)
  164. {
  165. disposable.Dispose ();
  166. }
  167. }
  168. catch (Exception exc)
  169. {
  170. Log.Exception ("DisposeNoThrow: Dispose threw: {0}", exc);
  171. }
  172. }
  173. public static TTo CastTo<TTo> (this object value, TTo defaultValue)
  174. {
  175. return value is TTo ? (TTo) value : defaultValue;
  176. }
  177. public static string Concatenate (this IEnumerable<string> values, string delimiter = null, int capacity = 16)
  178. {
  179. values = values ?? Array<string>.Empty;
  180. delimiter = delimiter ?? ", ";
  181. return string.Join (delimiter, values);
  182. }
  183. public static string GetResourceString (this Assembly assembly, string name, string defaultValue = null)
  184. {
  185. defaultValue = defaultValue ?? "";
  186. if (assembly == null)
  187. {
  188. return defaultValue;
  189. }
  190. var stream = assembly.GetManifestResourceStream (name ?? "");
  191. if (stream == null)
  192. {
  193. return defaultValue;
  194. }
  195. using (stream)
  196. using (var streamReader = new StreamReader (stream))
  197. {
  198. return streamReader.ReadToEnd ();
  199. }
  200. }
  201. public static IEnumerable<string> ReadLines (this TextReader textReader)
  202. {
  203. if (textReader == null)
  204. {
  205. yield break;
  206. }
  207. string line;
  208. while ((line = textReader.ReadLine ()) != null)
  209. {
  210. yield return line;
  211. }
  212. }
  213. #if !NETFX_CORE
  214. public static IEnumerable<Type> GetInheritanceChain (this Type type)
  215. {
  216. while (type != null)
  217. {
  218. yield return type;
  219. type = type.BaseType;
  220. }
  221. }
  222. #endif
  223. }
  224. }
  225. }
  226. // ############################################################################
  227. // ############################################################################
  228. namespace Demo_007
  229. {
  230. // ----------------------------------------------------------------------------------------------
  231. // Copyright (c) Mårten Rånge.
  232. // ----------------------------------------------------------------------------------------------
  233. // This source code is subject to terms and conditions of the Microsoft Public License. A
  234. // copy of the license can be found in the License.html file at the root of this distribution.
  235. // If you cannot locate the Microsoft Public License, please send an email to
  236. // dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  237. // by the terms of the Microsoft Public License.
  238. // ----------------------------------------------------------------------------------------------
  239. // You must not remove this notice, or any other, from this software.
  240. // ----------------------------------------------------------------------------------------------
  241. namespace Source.Common
  242. {
  243. static class Array<T>
  244. {
  245. public static readonly T[] Empty = new T[0];
  246. }
  247. }
  248. }
  249. // ############################################################################
  250. // ############################################################################
  251. namespace Demo_007
  252. {
  253. // ----------------------------------------------------------------------------------------------
  254. // Copyright (c) Mårten Rånge.
  255. // ----------------------------------------------------------------------------------------------
  256. // This source code is subject to terms and conditions of the Microsoft Public License. A
  257. // copy of the license can be found in the License.html file at the root of this distribution.
  258. // If you cannot locate the Microsoft Public License, please send an email to
  259. // dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  260. // by the terms of the Microsoft Public License.
  261. // ----------------------------------------------------------------------------------------------
  262. // You must not remove this notice, or any other, from this software.
  263. // ----------------------------------------------------------------------------------------------
  264. namespace Source.Common
  265. {
  266. using System.Globalization;
  267. sealed partial class InitConfig
  268. {
  269. public CultureInfo DefaultCulture = CultureInfo.InvariantCulture;
  270. }
  271. static partial class Config
  272. {
  273. static partial void Partial_Constructed(ref InitConfig initConfig);
  274. public readonly static CultureInfo DefaultCulture;
  275. static Config ()
  276. {
  277. var initConfig = new InitConfig();
  278. Partial_Constructed (ref initConfig);
  279. initConfig = initConfig ?? new InitConfig();
  280. DefaultCulture = initConfig.DefaultCulture;
  281. }
  282. }
  283. }
  284. }
  285. // ############################################################################
  286. // ############################################################################
  287. namespace Demo_007
  288. {
  289. // ----------------------------------------------------------------------------------------------
  290. // Copyright (c) Mårten Rånge.
  291. // ----------------------------------------------------------------------------------------------
  292. // This source code is subject to terms and conditions of the Microsoft Public License. A
  293. // copy of the license can be found in the License.html file at the root of this distribution.
  294. // If you cannot locate the Microsoft Public License, please send an email to
  295. // dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  296. // by the terms of the Microsoft Public License.
  297. // ----------------------------------------------------------------------------------------------
  298. // You must not remove this notice, or any other, from this software.
  299. // ----------------------------------------------------------------------------------------------
  300. namespace Source.Common
  301. {
  302. using System;
  303. using System.Globalization;
  304. static partial class Log
  305. {
  306. static partial void Partial_LogLevel (Level level);
  307. static partial void Partial_LogMessage (Level level, string message);
  308. static partial void Partial_ExceptionOnLog (Level level, string format, object[] args, Exception exc);
  309. public static void LogMessage (Level level, string format, params object[] args)
  310. {
  311. try
  312. {
  313. Partial_LogLevel (level);
  314. Partial_LogMessage (level, GetMessage (format, args));
  315. }
  316. catch (Exception exc)
  317. {
  318. Partial_ExceptionOnLog (level, format, args, exc);
  319. }
  320. }
  321. static string GetMessage (string format, object[] args)
  322. {
  323. format = format ?? "";
  324. try
  325. {
  326. return (args == null || args.Length == 0)
  327. ? format
  328. : string.Format (Config.DefaultCulture, format, args)
  329. ;
  330. }
  331. catch (FormatException)
  332. {
  333. return format;
  334. }
  335. }
  336. }
  337. }
  338. }
  339. // ############################################################################
  340. // ############################################################################
  341. namespace Demo_007
  342. {
  343. // ############################################################################
  344. // # #
  345. // # ---==> T H I S F I L E I S G E N E R A T E D <==--- #
  346. // # #
  347. // # This means that any edits to the .cs file will be lost when its #
  348. // # regenerated. Changes should instead be applied to the corresponding #
  349. // # template file (.tt) #
  350. // ############################################################################
  351. namespace Source.Common
  352. {
  353. using System;
  354. partial class Log
  355. {
  356. public enum Level
  357. {
  358. Success = 1000,
  359. HighLight = 2000,
  360. Info = 3000,
  361. Warning = 10000,
  362. Error = 20000,
  363. Exception = 21000,
  364. }
  365. public static void Success (string format, params object[] args)
  366. {
  367. LogMessage (Level.Success, format, args);
  368. }
  369. public static void HighLight (string format, params object[] args)
  370. {
  371. LogMessage (Level.HighLight, format, args);
  372. }
  373. public static void Info (string format, params object[] args)
  374. {
  375. LogMessage (Level.Info, format, args);
  376. }
  377. public static void Warning (string format, params object[] args)
  378. {
  379. LogMessage (Level.Warning, format, args);
  380. }
  381. public static void Error (string format, params object[] args)
  382. {
  383. LogMessage (Level.Error, format, args);
  384. }
  385. public static void Exception (string format, params object[] args)
  386. {
  387. LogMessage (Level.Exception, format, args);
  388. }
  389. #if !NETFX_CORE
  390. static ConsoleColor GetLevelColor (Level level)
  391. {
  392. switch (level)
  393. {
  394. case Level.Success:
  395. return ConsoleColor.Green;
  396. case Level.HighLight:
  397. return ConsoleColor.White;
  398. case Level.Info:
  399. return ConsoleColor.Gray;
  400. case Level.Warning:
  401. return ConsoleColor.Yellow;
  402. case Level.Error:
  403. return ConsoleColor.Red;
  404. case Level.Exception:
  405. return ConsoleColor.Red;
  406. default:
  407. return ConsoleColor.Magenta;
  408. }
  409. }
  410. #endif
  411. static string GetLevelMessage (Level level)
  412. {
  413. switch (level)
  414. {
  415. case Level.Success:
  416. return "SUCCESS ";
  417. case Level.HighLight:
  418. return "HIGHLIGHT";
  419. case Level.Info:
  420. return "INFO ";
  421. case Level.Warning:
  422. return "WARNING ";
  423. case Level.Error:
  424. return "ERROR ";
  425. case Level.Exception:
  426. return "EXCEPTION";
  427. default:
  428. return "UNKNOWN ";
  429. }
  430. }
  431. }
  432. }
  433. }
  434. // ############################################################################
  435. // ############################################################################
  436. namespace Demo_007.Include
  437. {
  438. static partial class MetaData
  439. {
  440. public const string RootPath = @"https://raw.github.com/";
  441. public const string IncludeDate = @"2013-01-25T15:22:13";
  442. public const string Include_0 = @"mrange/T4Include/master/Extensions/BasicExtensions.cs";
  443. public const string Include_1 = @"https://raw.github.com/mrange/T4Include/master/Common/Array.cs";
  444. public const string Include_2 = @"https://raw.github.com/mrange/T4Include/master/Common/Config.cs";
  445. public const string Include_3 = @"https://raw.github.com/mrange/T4Include/master/Common/Log.cs";
  446. public const string Include_4 = @"https://raw.github.com/mrange/T4Include/master/Common/Generated_Log.cs";
  447. }
  448. }
  449. // ############################################################################