public void WriteUsingStatements() { if (UsingNamespaces.Any()) { AppendLineIfNeeded(); foreach (var usingNamespace in UsingNamespaces.OrderBy(usingNamespace => usingNamespace)) { WriteIndentation(); Write($"using {usingNamespace};"); WriteNewLine(); } appendLine = true; } }
internal static CompilationUnitSyntax CreateCompilationUnitSyntax(string nameSpace, IReadOnlyCollection <HassState> entities, IReadOnlyCollection <HassServiceDomain> services) { var orderedEntities = entities.OrderBy(x => x.EntityId).ToArray(); var orderedServiceDomains = services.OrderBy(x => x.Domain).ToArray(); var code = CompilationUnit() .AddUsings(UsingDirective(ParseName("System"))) .AddUsings(UsingDirective(ParseName("System.Collections.Generic"))) .AddUsings(UsingNamespaces.OrderBy(s => s).Select(u => UsingDirective(ParseName(u))).ToArray()); var namespaceDeclaration = NamespaceDeclaration(ParseName(nameSpace)).NormalizeWhitespace(); namespaceDeclaration = namespaceDeclaration.AddMembers(EntitiesGenerator.Generate(orderedEntities).ToArray()); namespaceDeclaration = namespaceDeclaration.AddMembers(ServicesGenerator.Generate(orderedServiceDomains).ToArray()); namespaceDeclaration = namespaceDeclaration.AddMembers(ExtensionMethodsGenerator.Generate(orderedServiceDomains, entities).ToArray()); code = code.AddMembers(namespaceDeclaration); code = code.NormalizeWhitespace(Tab.ToString(), eol: "\n"); return(code); }
public SortedDictionary <string, string> GenerateCode() { this.sb = new StringBuilder(4096); this.cw = new CodeWriter(sb, 4); this.generateQueue = new Queue <Type>(); this.visited = new HashSet <Type>(); foreach (var fromType in this.Assembly.GetTypes()) { if (fromType.IsAbstract) { continue; } if (fromType.IsSubclassOf(typeof(ServiceRequest)) || fromType.IsSubclassOf(typeof(ServiceResponse)) || fromType.IsSubclassOf(typeof(Row)) || fromType.GetCustomAttribute <ScriptIncludeAttribute>() != null) { EnqueueType(fromType); } } Dictionary <Type, string> generatedCode = new Dictionary <Type, string>(); while (generateQueue.Count > 0) { var type = generateQueue.Dequeue(); if (type.Assembly != this.Assembly) { continue; } GenerateCodeFor(type); generatedCode[type] = sb.ToString(); sb.Clear(); } sb.Clear(); sb.AppendLine(); var ordered = generatedCode.Keys.OrderBy(x => GetNamespace(x)).ThenBy(x => x.Name); var byNameSpace = ordered.ToLookup(x => GetNamespace(x)); var byOwnerType = ordered.ToLookup(x => (x.IsNested ? x.DeclaringType : null)); var outputted = new HashSet <Type>(); var result = new SortedDictionary <string, string>(); foreach (var ns in byNameSpace.ToArray().OrderBy(x => x.Key)) { Action <Type> outputType = delegate(Type type) { var filename = ns.Key + "." + MakeFriendlyName(type) + ".cs"; foreach (var rn in RootNamespaces) { if (filename.StartsWith(rn + ".")) { filename = filename.Substring(rn.Length + 1); } } result.Add(filename, sb.ToString()); }; foreach (var owner in byOwnerType) { bool skip = false; sb.Clear(); sb.AppendLine(); cw.Indented("namespace "); sb.AppendLine(ns.Key); cw.InBrace(delegate { foreach (var usingNamespace in UsingNamespaces.OrderBy(x => x)) { cw.Indented("using "); sb.Append(usingNamespace); sb.AppendLine(";"); } sb.AppendLine(); if (owner.Key == null) { skip = true; return; } if (GetNamespace(owner.Key) != ns.Key) { skip = true; return; } if (outputted.Contains(owner.Key)) { skip = true; return; } outputted.Add(owner.Key); if (!generatedCode.ContainsKey(owner.Key)) { return; } string code = generatedCode[owner.Key].TrimEnd(); code = code.Substring(0, code.Length - 1).TrimEnd(); cw.IndentedMultiLine(code); cw.Block(delegate { sb.AppendLine(); foreach (var subType in owner) { cw.IndentedMultiLine(generatedCode[subType]); outputted.Add(subType); } }); cw.IndentedLine("}"); sb.AppendLine(); }); if (skip) { continue; } outputType(owner.Key); } foreach (var type in ns) { if (outputted.Contains(type)) { continue; } sb.Clear(); sb.AppendLine(); cw.Indented("namespace "); sb.AppendLine(ns.Key); cw.InBrace(() => { foreach (var usingNamespace in UsingNamespaces.OrderBy(x => x)) { cw.Indented("using "); sb.Append(usingNamespace); sb.AppendLine(";"); } sb.AppendLine(); cw.IndentedMultiLine(generatedCode[type]); }); outputType(type); outputted.Add(type); } } return(result); }