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. --- .../Tango.CodeGeneration/CodeObject.cs | 134 +++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 Software/Visual_Studio/Tango.CodeGeneration/CodeObject.cs (limited to 'Software/Visual_Studio/Tango.CodeGeneration/CodeObject.cs') diff --git a/Software/Visual_Studio/Tango.CodeGeneration/CodeObject.cs b/Software/Visual_Studio/Tango.CodeGeneration/CodeObject.cs new file mode 100644 index 000000000..6d7e74960 --- /dev/null +++ b/Software/Visual_Studio/Tango.CodeGeneration/CodeObject.cs @@ -0,0 +1,134 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using System.Web; + +namespace Tango.CodeGeneration +{ + /// + /// Represents a code object base class. + /// + /// + public abstract class CodeObject : ICodeObject + { + /// + /// Initializes a new instance of the class. + /// + public CodeObject() + { + Attributes = new List(); + IndentResult = true; + } + + /// + /// Gets or sets the code object modifier. + /// + public CodeObjectModifier Modifier { get; set; } + + /// + /// Gets or sets the attributes associated with this code object. + /// + public List Attributes { get; set; } + + /// + /// Gets or sets a value indicating whether to indent the resulting code. + /// + public bool IndentResult { get; set; } + + /// + /// Generates the code represented by this code object. + /// + /// + public virtual string GenerateCode() + { + var code = Helper.ParseTemplate(GetTemplate(), this); + + if (IndentResult) + { + code = Helper.IndentCSharpCode(code); + code = RemoveDoubleLines(code); + } + + code = code.Replace(""", "\""); + code = HttpUtility.HtmlDecode(code); + + return code; + } + + /// + /// Removes the double lines. + /// + /// The string. + /// + private String RemoveDoubleLines(string str) + { + String result = String.Empty; + + using (var streamReader = new StringReader(str)) + { + string line = null; + bool previousLineWasBlank = false; + while ((line = streamReader.ReadLine()) != null) + { + if (!string.IsNullOrEmpty(line) || !previousLineWasBlank) + { + result += line +Environment.NewLine; + } + + previousLineWasBlank = string.IsNullOrEmpty(line); + } + } + + return result; + } + + /// + /// Gets the template associated with this code object. + /// + /// + public virtual string GetTemplate() + { + return Helper.GetTemplate(this.GetType()); + } + + /// + /// Returns a that represents this instance. + /// + /// + /// A that represents this instance. + /// + public override string ToString() + { + return GenerateCode(); + } + + /// + /// Gets the modifier string. + /// + /// + public String GetModifierString() + { + return Modifier.ToString().ToLower(); + } + + /// + /// Gets the attributes string. + /// + /// + public String GetAttributesString() + { + if (Attributes != null && Attributes.Count > 0) + { + return String.Join(Environment.NewLine, Attributes); + } + else + { + return null; + } + } + } +} -- cgit v1.3.1