From 3689238cb9ca77cbd7fa34dbd15003af87266e36 Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Mon, 4 Dec 2017 18:44:33 +0200 Subject: Added Code Generation Library. --- .../Visual_Studio/Tango.CodeGeneration/Helper.cs | 171 +++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 Software/Visual_Studio/Tango.CodeGeneration/Helper.cs (limited to 'Software/Visual_Studio/Tango.CodeGeneration/Helper.cs') 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 + { + /// + /// Gets a code template by the code object type. + /// + /// The type. + /// + public static String GetTemplate(Type type) + { + return GetTemplate(type.Name); + } + + /// + /// Gets a code template by name. + /// + /// The name. + /// + 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(); + } + } + + /// + /// Parses the template using Razor Engine. + /// + /// The template. + /// The model. + /// + public static String ParseTemplate(String template, object model) + { + return Engine.Razor.RunCompile(template, Guid.NewGuid().ToString(), model.GetType(), model).Replace("
", "").Replace("
", ""); + } + + /// + /// Indents the c sharp code. + /// + /// The code. + /// + 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(); + } + } +} -- cgit v1.3.1