private void ProcessEnum() { var 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(); } Writer.WriteFile(Config.TypeScript.Entity.Path.ToLower(), "enum.ts", builder, true); }
private void BuildNormalService(StringBuilderIndented builder) { builder .AppendLine("switch (this.Language.Name) {") .IncrementIndent(); foreach (var culture in Config.TypeScript.Resource.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 WriteResourceClasses(StringBuilderIndented builder) { var resources = Resources[Config.TypeScript.Resource.CultureDefault.Code]; // Write the file header builder .AppendLine("import { Injectable } from '@angular/core';") .AppendLine("import { HttpClient } from '@angular/common/http';") .AppendLine("import { Observable } from 'rxjs';") .AppendLine(""); // Create the resouce class with default values foreach (var resource in resources) { builder .AppendLine($"class {resource.ResourceName}Resource {{") .IncrementIndent(); foreach (var value in resource.Properties) { builder .AppendLine($"{value.Key} = '{value.Value.Replace("'", "\\'")}';"); } builder .DecrementIndent() .AppendLine("}") .AppendLine() .DecrementIndent(); } }
internal static StringBuilderIndented ClassEnd(this StringBuilderIndented builder) { builder .DecrementIndent() .AppendLine("}") .DecrementIndent() .AppendLine("}"); return(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 GenerateFactoryValidate(StringBuilderIndented builder) { builder .AppendLine("private static void RegisterValidate()") .AppendLine("{") .IncrementIndent(); foreach (var table in Config.DataBaseItems.SelectMany(c => c.Tables)) { builder .AppendLine($"Kernel.Map<ISpecificationValidation<{table.Name}Entity>>().To<{table.Name}Validate>();"); } builder .DecrementIndent() .AppendLine("}"); }
public void WriteEntityMap(DataBaseTable item) { if (Config.CSharp.UsingApplicationBase && Config.CSharp.ApplicationClasses.Any(c => c.Name == item.Name && c.Schema == item.Schema)) { return; } var path = Path.Combine(Config.CSharp.EntityFramework.Path, item.Schema, "Map"); var file = $"{item.Name}Map.cs"; var builder = new StringBuilderIndented(); builder .GenerateFileMessage() .ClassInit($"{item.Name}Map", null, $"{Config.CSharp.EntityFramework.NameSpace}.{item.Schema}.Map", ClassVisibility.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.PKName});") .AppendLine() .AppendLine("if (!unitTest)") .IncrementIndent() .AppendLine($"entity.ToTable(\"{item.Name}\", \"{item.Schema}\");") .DecrementIndent() .AppendLine("else") .IncrementIndent() .AppendLine($"entity.ToTable(\"{item.Schema}{item.Name}\");") .DecrementIndent() .AppendLine(); ProcessMapColumns(builder, item); ProcessMapRelationShips(builder, item); builder .DecrementIndent() .AppendLine("});") .DecrementIndent() .AppendLine("}") .ClassEnd(); Writer.WriteFile(path, file, builder, true); }
private void GenerateFactoryRepository(StringBuilderIndented builder) { builder .AppendLine("private static void RegisterRepository()") .AppendLine("{") .IncrementIndent(); foreach (var table in Config.DataBaseItems.SelectMany(c => c.Tables)) { builder .AppendLine($"Kernel.Map<I{table.Name}Business>().To<{table.Name}Business>();"); } builder .DecrementIndent() .AppendLine("}") .AppendLine(); }
private void GenerateFactoryQuery(StringBuilderIndented builder) { builder .AppendLine("private static void RegisterQuery()") .AppendLine("{") .IncrementIndent(); foreach (var table in Config.DataBaseItems.SelectMany(c => c.Tables)) { builder .AppendLine($"Kernel.Map<ISpecificationQuery<{table.Name}, {table.Name}Filter>>().To<{table.Name}Query>();"); } builder .DecrementIndent() .AppendLine("}") .AppendLine(); }
private void BuildLazyService(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.TypeScript.Resource.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 void BuildClass(StringBuilderIndented builder) { foreach (var culture in Config.TypeScript.Resource.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(); } } }
private void Html(string path, string selector, bool outlet) { var file = $"{selector}.html"; var 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>"); Writer.WriteFile(path, file, builder, false); }
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(); var builder = new StringBuilderIndented(); builder .GenerateFileMessage(); if (importItems.Any()) { var 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(); var 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) { var 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); Writer.WriteFile(Config.TypeScript.Entity.Path.ToLower(), fileName, builder, true); } }
private string BuidService(StringBuilderIndented builder) { builder .AppendLine("@Injectable()") .AppendLine("export class ResourceService {") .IncrementIndent() .AppendLine(Config.TypeScript.Resource.LazyLoad ? "constructor(private http: Http) {" : "constructor() {") .IncrementIndent(); BuildLanguageSuportedContructor(builder); builder .AppendLine($"this.SetLanguage('{Config.TypeScript.Resource.CultureDefault.Name}')") .DecrementIndent() .AppendLine("}") .AppendLine(); if (Config.TypeScript.Resource.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.TypeScript.Resource.CultureDefault.Name}`);") .AppendLine($"this.SetLanguage('{Config.TypeScript.Resource.CultureDefault.Name}');") .AppendLine("return;") .DecrementIndent() .AppendLine("}") .AppendLine() .AppendLine("this.Language = value;") .AppendLine(); if (!Config.TypeScript.Resource.LazyLoad) { BuildNormalService(builder); } if (Config.TypeScript.Resource.LazyLoad) { BuildLazyService(builder); } builder .DecrementIndent() .AppendLine("}") .DecrementIndent() .AppendLine("}"); return(builder.ToString()); }
private string WriteService(StringBuilderIndented builder) { // Service declaration builder .AppendLine("@Injectable()") .AppendLine($"export class {Config.TypeScript.Resource.ServiceName}Service {{") .IncrementIndent() .AppendLine(""); var visibility = "public "; var setter = " = "; var finalizer = ";"; var resources = Resources[Config.TypeScript.Resource.CultureDefault.Code]; var count = 0; if (Config.TypeScript.Resource.Extractor) { visibility = ""; setter = ": "; finalizer = ","; builder .AppendLine("public XCommon = {") .IncrementIndent(); } // Resource Properties foreach (var resource in resources) { count++; if (resources.Count == count) { finalizer = ""; } builder .AppendLine($"{visibility}{resource.ResourceName}{setter}new {resource.ResourceName}Resource(){finalizer}"); } if (Config.TypeScript.Resource.Extractor) { builder .DecrementIndent() .AppendLine("}") .AppendLine(); } // Current Culture builder .AppendLine("public CurrentCulture = {") .IncrementIndent() .AppendLine($"Code: '{Config.TypeScript.Resource.CultureDefault.Code}',") .AppendLine($"Name: '{Config.TypeScript.Resource.CultureDefault.Name}'") .DecrementIndent() .AppendLine("};") .AppendLine(); // Available cultures builder .AppendLine("public Cultures = [") .IncrementIndent(); foreach (var culture in Config.TypeScript.Resource.Cultures) { var last = Config.TypeScript.Resource.Cultures.Last().Code.Equals(culture.Code); var comma = last ? string.Empty : ","; builder.AppendLine($"{{ Code: '{culture.Code}', Name: '{culture.Name}' }}{comma}"); } builder .DecrementIndent() .AppendLine("];") .AppendLine(""); // Service constructor builder .AppendLine("constructor(private http: HttpClient) {") .AppendLine("}") .AppendLine(""); // Set culture method builder .AppendLine("public setCulture(cultureCode: string): Observable<boolean> {") .AppendLine("") .IncrementIndent() .AppendLine("const newCulture = this.Cultures.find(c => c.Code === cultureCode);") .AppendLine("") .AppendLine("if (!newCulture) {") .IncrementIndent() .AppendLine("throw new Error(`Invalid culture: ${cultureCode}`);") .DecrementIndent() .AppendLine("}") .AppendLine("") .AppendLine("return new Observable<boolean>(observer => {") .AppendLine("") .IncrementIndent() .AppendLine("if (cultureCode === this.CurrentCulture.Code) {") .IncrementIndent() .AppendLine("observer.next(true);") .AppendLine("observer.complete();") .AppendLine("return;") .DecrementIndent() .AppendLine("}") .AppendLine("") .AppendLine($"this.http.get<any>(`{Config.TypeScript.Resource.RequestAddress}{Config.TypeScript.Resource.JsonPrefix}-${{cultureCode}}.json`)") .IncrementIndent() .AppendLine(".subscribe(res => {") .IncrementIndent(); // Set the properties with the new language foreach (var item in Resources[Config.TypeScript.Resource.CultureDefault.Code]) { if (Config.TypeScript.Resource.Extractor) { builder .AppendLine($"this.XCommon.{item.ResourceName} = res.{item.ResourceName};"); } else { builder .AppendLine($"this.{item.ResourceName} = res.{item.ResourceName};"); } } // Complete the service builder .AppendLine("") .AppendLine("this.CurrentCulture = newCulture;") .AppendLine("") .AppendLine("observer.next(true);") .AppendLine("observer.complete();") .DecrementIndent() .AppendLine("});") .DecrementIndent() .DecrementIndent() .AppendLine("});") .DecrementIndent() .AppendLine("}") .DecrementIndent(); // Close service class builder .DecrementIndent() .AppendLine("}"); return(builder.ToString()); }