diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2017-12-04 18:44:33 +0200 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2017-12-04 18:44:33 +0200 |
| commit | 3689238cb9ca77cbd7fa34dbd15003af87266e36 (patch) | |
| tree | 3d928df9c9b0b11703e6fb10ddf09a436a405dde /Software/Visual_Studio/Tango.CodeGeneration/Helper.cs | |
| parent | 6c3f942b4e321dc9e7bbca8d95dc81dcb43f7fd2 (diff) | |
| download | Tango-3689238cb9ca77cbd7fa34dbd15003af87266e36.tar.gz Tango-3689238cb9ca77cbd7fa34dbd15003af87266e36.zip | |
Added Code Generation Library.
Diffstat (limited to 'Software/Visual_Studio/Tango.CodeGeneration/Helper.cs')
| -rw-r--r-- | Software/Visual_Studio/Tango.CodeGeneration/Helper.cs | 171 |
1 files changed, 171 insertions, 0 deletions
diff --git a/Software/Visual_Studio/Tango.CodeGeneration/Helper.cs b/Software/Visual_Studio/Tango.CodeGeneration/Helper.cs new file mode 100644 index 000000000..0ca012be9 --- /dev/null +++ b/Software/Visual_Studio/Tango.CodeGeneration/Helper.cs @@ -0,0 +1,171 @@ +using RazorEngine; +using RazorEngine.Templating; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.CodeGeneration +{ + internal static class Helper + { + /// <summary> + /// Gets a code template by the code object type. + /// </summary> + /// <param name="type">The type.</param> + /// <returns></returns> + public static String GetTemplate(Type type) + { + return GetTemplate(type.Name); + } + + /// <summary> + /// Gets a code template by name. + /// </summary> + /// <param name="name">The name.</param> + /// <returns></returns> + public static String GetTemplate(String name) + { + var assembly = Assembly.GetAssembly(typeof(Helper)); + var resourceName = "CodeGenerator.Templates." + name + ".cshtml"; + + using (Stream stream = assembly.GetManifestResourceStream(resourceName)) + using (StreamReader reader = new StreamReader(stream)) + { + return reader.ReadToEnd(); + } + } + + /// <summary> + /// Parses the template using Razor Engine. + /// </summary> + /// <param name="template">The template.</param> + /// <param name="model">The model.</param> + /// <returns></returns> + public static String ParseTemplate(String template, object model) + { + return Engine.Razor.RunCompile(template, Guid.NewGuid().ToString(), model.GetType(), model).Replace("<div>", "").Replace("</div>", ""); + } + + /// <summary> + /// Indents the c sharp code. + /// </summary> + /// <param name="code">The code.</param> + /// <returns></returns> + public static string IndentCSharpCode(string code) + { + const string INDENT_STEP = " "; + + if (string.IsNullOrWhiteSpace(code)) + { + return code; + } + + var result = new StringBuilder(); + var indent = string.Empty; + var lineContent = false; + var stringDefinition = false; + + for (var i = 0; i < code.Length; i++) + { + var ch = code[i]; + + if (ch == '"' && !stringDefinition) + { + result.Append(ch); + stringDefinition = true; + continue; + } + + if (ch == '"' && stringDefinition) + { + result.Append(ch); + stringDefinition = false; + continue; + } + + if (stringDefinition) + { + result.Append(ch); + continue; + } + + if (ch == '{' && !stringDefinition) + { + if (lineContent) + { + result.AppendLine(); + } + + result.Append(indent).Append("{"); + + if (lineContent) + { + result.AppendLine(); + } + + indent += INDENT_STEP; + lineContent = false; + + continue; + } + + if (ch == '}' && !stringDefinition) + { + if (indent.Length != 0) + { + indent = indent.Substring(0, indent.Length - INDENT_STEP.Length); + } + + if (lineContent) + { + result.AppendLine(); + } + + result.Append(indent).Append("}"); + + if (lineContent) + { + result.AppendLine(); + } + + + lineContent = false; + + continue; + } + + if (ch == '\r') + { + continue; + } + + if ((ch == ' ' || ch == '\t') && !lineContent) + { + continue; + } + + if (ch == '\n') + { + lineContent = false; + result.AppendLine(); + + continue; + } + + if (!lineContent) + { + result.Append(indent); + lineContent = true; + } + + result.Append(ch); + } + + return result.ToString(); + } + } +} |
