private void ProcessTypes() { foreach (var file in TSClass.Select(c => c.FileName).Distinct().OrderBy(c => c)) { var fileName = file.GetSelector(); var importItems = TSClass.Where(c => c.FileName == file).SelectMany(c => c.Imports).ToList(); StringBuilderIndented builder = new StringBuilderIndented(); builder .GenerateFileMessage(); if (importItems.Any()) { bool imported = false; foreach (var importFile in importItems.Where(c => c.File != file).Select(c => c.File).Distinct()) { imported = true; var classes = importItems.Where(c => c.File == importFile).Select(c => c.Class).Distinct().ToArray(); var import = string.Join(", ", classes); builder .AppendLine($"import {{ {import} }} from \"./{importFile.Replace(".ts", string.Empty)}\";"); } if (imported) { builder .AppendLine(); } } foreach (var className in TSClass.Where(c => c.FileName == file).Select(c => c.Class).Distinct().OrderBy(c => c)) { var tsClass = TSClass .Where(c => c.FileName == file && c.Class == className) .FirstOrDefault(); var generics = tsClass.Properties .Where(c => c.Generic) .Select(c => c.Type) .ToList(); string classNamePrint = tsClass.Class; if (generics.Count > 0) { classNamePrint = classNamePrint.Substring(0, classNamePrint.IndexOf('`')); classNamePrint += "<"; classNamePrint += string.Join(", ", generics); classNamePrint += ">"; } builder .AppendLine($"export interface I{classNamePrint} {{") .IncrementIndent(); foreach (var property in tsClass.Properties) { string nullable = property.Nullable ? "?" : ""; builder .AppendLine($"{property.Name}{nullable}: {property.Type}; "); } builder .DecrementIndent() .AppendLine("}") .AppendLine(); } if (GeneratedEntities.Contains(fileName)) { throw new Exception($"Two file with same name cannot be generated: {fileName}"); } GeneratedEntities.Add(fileName); WriteFile(Config.Path.ToLower(), fileName, builder); } }
private void GenerateEntityMap(CSharpConfig config) { foreach (var group in config.DataBaseItems) { foreach (var item in group.Items) { string path = Path.Combine(config.DataBase.Path, group.Name, "Map"); string file = $"{item.Name}Map.cs"; StringBuilderIndented builder = new StringBuilderIndented(); builder .GenerateFileMessage() .ClassInit($"{item.Name}Map", null, $"{config.DataBase.NameSpace}.{group.Name}.Map", ClassVisility.Internal, "System", "Microsoft.EntityFrameworkCore") .AppendLine("internal static void Map(ModelBuilder modelBuilder, bool unitTest)") .AppendLine("{") .IncrementIndent() .AppendLine($"modelBuilder.Entity<{item.Name}>(entity =>") .AppendLine("{") .IncrementIndent() .AppendLine($"entity.HasKey(e => e.{item.NameKey});") .AppendLine() .AppendLine("if (!unitTest)") .IncrementIndent() .AppendLine($"entity.ToTable(\"{item.Name}\", \"{group.Name}\");") .DecrementIndent() .AppendLine("else") .IncrementIndent() .AppendLine($"entity.ToTable(\"{group.Name}{item.Name}\");") .DecrementIndent() .AppendLine(); ProcessMapColumns(builder, item); ProcessMapRelationShips(config, builder, item); builder .DecrementIndent() .AppendLine("});") .DecrementIndent() .AppendLine("}") .ClassEnd(); WriteFile(path, file, builder); } } }
private void ProcessEnum() { StringBuilderIndented builder = new StringBuilderIndented(); builder .GenerateFileMessage(); foreach (var enumItem in TSEnums) { builder .AppendLine($"export enum {enumItem.Name} {{") .IncrementIndent(); foreach (var values in enumItem.Values) { builder .AppendLine($"{values.Key} = {values.Value},"); } builder .DecrementIndent() .AppendLine("}") .AppendLine(); } WriteFile(Config.Path.ToLower(), "enum.ts", builder); }
private void GenerateFactoryRepository(CSharpConfig config, StringBuilderIndented builder) { builder .AppendLine("private static void RegisterRepository()") .AppendLine("{") .IncrementIndent(); foreach (var table in config.DataBaseItems.SelectMany(c => c.Items)) { builder .AppendLine($"Kernel.Map<I{table.Name}Business>().To<{table.Name}Business>();"); } builder .DecrementIndent() .AppendLine("}"); }
private void GenerateFactoryValidate(CSharpConfig config, StringBuilderIndented builder) { builder .AppendLine("private static void RegisterValidate()") .AppendLine("{") .IncrementIndent(); foreach (var table in config.DataBaseItems.SelectMany(c => c.Items)) { builder .AppendLine($"Kernel.Map<ISpecificationValidation<{table.Name}Entity>>().To<{table.Name}Validate>();"); } builder .DecrementIndent() .AppendLine("}"); }
public void Dispose() => _stringBuilder.DecrementIndent();
private void Html(string path, string selector, bool outlet) { var file = $"{selector}.html"; if (File.Exists(Path.Combine(path, file))) { Console.WriteLine($"File already exists: {file}"); return; } StringBuilderIndented builder = new StringBuilderIndented(); builder .AppendLine($"<div class=\"{selector}\">") .IncrementIndent() .AppendLine($"<h1>Hey! I\"m {selector}</h1>"); if (outlet) { builder .AppendLine("<router-outlet></router-outlet>"); } builder .DecrementIndent() .AppendLine("</div>"); WriteFile(path, file, builder); }
private void BuildInterface(StringBuilderIndented builder) { foreach (var resource in Resources) { builder .AppendLine($"export interface I{resource.ResourceName} {{") .IncrementIndent(); foreach (var value in resource.Values.FirstOrDefault().Properties) { builder .AppendLine($"{value.Key}: string;"); } builder .DecrementIndent() .AppendLine("}") .AppendLine(); } builder .AppendLine("export interface IResource {") .IncrementIndent() .AppendLine("Culture: string;"); foreach (var resource in Resources) { builder .AppendLine($"{resource.ResourceName}: I{resource.ResourceName};"); } builder .DecrementIndent() .AppendLine("}") .AppendLine(); }
private void BuildNormalService(TypeScriptResource config, StringBuilderIndented builder) { builder .AppendLine("switch (this.Language.Name) {") .IncrementIndent(); foreach (var culture in config.Cultures) { builder .AppendLine($"case \"{culture.Name}\":") .IncrementIndent(); foreach (var resource in Resources) { builder .AppendLine($"this.{resource.ResourceName} = new {resource.ResourceName}{GetCultureName(culture.Name)}();"); } builder .AppendLine("break;") .DecrementIndent(); } builder .DecrementIndent() .AppendLine("}"); }
private void BuildLazyService(TypeScriptResource config, StringBuilderIndented builder) { builder .AppendLine("if (this.Resources[this.Language.Name]) {") .IncrementIndent() .AppendLine("let resource: IResource = this.Resources[this.Language.Name];") .AppendLine(); foreach (var resource in Resources) { builder .AppendLine($"this.{resource.ResourceName} = resource.{resource.ResourceName};"); } builder .AppendLine() .AppendLine("return;") .DecrementIndent() .AppendLine("}") .AppendLine() .AppendLine($"this.http.get(`{config.RequestAddress}/${{this.Language.Name}}`)") .IncrementIndent() .AppendLine(".map((response: Response, index: number) => response.json())") .AppendLine(".subscribe((resource: IResource) => {") .IncrementIndent() .AppendLine("this.Resources[resource.Culture] = resource;") .AppendLine(); foreach (var resource in Resources) { builder .AppendLine($"this.{resource.ResourceName} = resource.{resource.ResourceName};"); } builder .DecrementIndent() .AppendLine("});"); }
private string BuidService(TypeScriptResource config, StringBuilderIndented builder) { builder .AppendLine("@Injectable()") .AppendLine("export class ResourceService {") .IncrementIndent() .AppendLine(config.LazyLoad ? "constructor(private http: Http) {" : "constructor() {") .IncrementIndent(); BuildLanguageSuportedContructor(config, builder); builder .AppendLine($"this.SetLanguage(\"{config.CultureDefault.Name}\")") .DecrementIndent() .AppendLine("}") .AppendLine(); if (config.LazyLoad) { builder .AppendLine("private Resources: Map<IResource> = {};"); } builder .AppendLine("public Languages: ApplicationCulture[] = [];") .AppendLine("private Language: ApplicationCulture;") .AppendLine(); foreach (var resource in Resources) { builder.AppendLine($"public {resource.ResourceName}: I{resource.ResourceName};"); } builder .AppendLine() .AppendLine("public GetLanguage(): ApplicationCulture {") .IncrementIndent() .AppendLine("return this.Language;") .DecrementIndent() .AppendLine("}") .AppendLine() .AppendLine("public SetLanguage(language: string): void {") .IncrementIndent() .AppendLine() .AppendLine("if (this.Language && this.Language.Name === language) {") .IncrementIndent() .AppendLine("return;") .DecrementIndent() .AppendLine("}") .AppendLine() .AppendLine("let value = this.Languages.find((item: ApplicationCulture) => item.Name == language);") .AppendLine() .AppendLine("if (!value) {") .IncrementIndent() .AppendLine($"console.warn(`Unknown language: ${{language}}! Set up current culture as the default language: {config.CultureDefault.Name}`);") .AppendLine($"this.SetLanguage(\"{config.CultureDefault.Name}\");") .AppendLine("return;") .DecrementIndent() .AppendLine("}") .AppendLine() .AppendLine("this.Language = value;") .AppendLine(); if (!config.LazyLoad) { BuildNormalService(config, builder); } if (config.LazyLoad) { BuildLazyService(config, builder); } builder .DecrementIndent() .AppendLine("}") .DecrementIndent() .AppendLine("}"); return builder.ToString(); }
private void BuildClass(TypeScriptResource config, StringBuilderIndented builder) { foreach (var culture in config.Cultures) { foreach (var resource in Resources) { builder .AppendLine($"class {resource.ResourceName}{GetCultureName(culture.Name)} implements I{resource.ResourceName} {{") .IncrementIndent(); foreach (var property in resource.Values.Where(c => c.Culture == culture.Name).SelectMany(c => c.Properties)) { builder .AppendLine($"{property.Key}: string = \"{property.Value}\";"); } builder .DecrementIndent() .AppendLine("}") .AppendLine(); } } }