private void TypeScript(string path, string name, string selector, string htmlRoot, string feture) { var file = $"{selector}.component.ts"; if (File.Exists(Path.Combine(path, file))) { Console.WriteLine($"File already exists: {file}"); return; } StringBuilderIndented builder = new StringBuilderIndented(); string templateUrl = $"\"{htmlRoot}/{feture}/{selector}.html\","; builder .AppendLine("import { Component, OnInit } from \"@angular/core\";") .AppendLine() .AppendLine("@Component({") .IncrementIndent() .AppendLine($"selector: \"{selector}\",") .AppendLine("templateUrl: " + templateUrl.ToLower()) .AppendLine($"styles: [require(\"./{selector}.scss\")]") .DecrementIndent() .AppendLine("})") .AppendLine($"export class {name.GetName()}Component implements OnInit {{") .IncrementIndent() .AppendLine("constructor() { }") .AppendLine() .AppendLine("ngOnInit(): void { }") .DecrementIndent() .AppendLine("}"); WriteFile(path, file, builder); }
internal void Run(string path, List<string> services) { path = path.ToLower(); foreach (string service in services) { var file = Path.Combine(path, $"{service.GetSelector()}.service.ts").ToLower(); if (File.Exists(Path.Combine(path, file))) { Console.WriteLine($"File already exists: {file}"); return; } StringBuilderIndented builder = new StringBuilderIndented(); builder .AppendLine("import { Injectable } from \"@angular/core\"; ") .AppendLine("import { Http, Response } from \"@angular/http\"; ") .AppendLine() .AppendLine("@Injectable()") .AppendLine($"export class {service.GetName()}Service {{") .IncrementIndent() .AppendLine("constructor(private http: Http) { }") .DecrementIndent() .AppendLine("}"); WriteFile(path, file, builder); Console.WriteLine($"Generated component {service.GetName()}"); } }
private void GenerateEntity(CSharpConfig config, ItemGroup group, Item item) { string path = Path.Combine(config.EntrityPath, group.Name, "Auto"); string file = $"{item.Name}Entity.cs"; var nameSpace = new List<string> { "System", "XCommon.Patterns.Repository.Entity", "System.Runtime.Serialization" }; nameSpace.AddRange(item.Properties.Where(c => c.NameGroup != group.Name).Select(c => c.NameGroup)); StringBuilderIndented builder = new StringBuilderIndented(); builder .GenerateFileMessage() .ClassInit($"{item.Name}Entity", "EntityBase", $"{config.EntrityNameSpace}.{group.Name}", ClassVisility.Public, true, nameSpace.ToArray()); foreach (var column in item.Properties) { builder .AppendLine($"public {column.Type} {column.Name} {{ get; set; }}") .AppendLine(); } builder .AppendLine() .AppendLine("[IgnoreDataMember]") .AppendLine("public override Guid Key") .AppendLine("{") .IncrementIndent() .AppendLine("get") .AppendLine("{") .IncrementIndent() .AppendLine($"return {item.NameKey};") .DecrementIndent() .AppendLine("}") .AppendLine("set") .AppendLine("{") .IncrementIndent() .AppendLine($"{item.NameKey} = value;") .DecrementIndent() .AppendLine("}") .DecrementIndent() .AppendLine("}") .ClassEnd(); WriteFile(path, file, builder); }
internal void Run(TypeScriptResource config, IndexExport index) { if (config.Resources == null || config.Resources.Count <= 0) return; StringBuilderIndented builder = new StringBuilderIndented(); builder .AppendLine("import { Injectable } from \"@angular/core\";"); if (config.LazyLoad) { builder .AppendLine("import { Http, Response } from \"@angular/http\";") .AppendLine("import { Observable } from \"rxjs/Observable\";"); } builder .AppendLine("import { Map } from \"../entity\";") .AppendLine(); Resources = new List<GeneratorResourceEntity>(); GetResouces(config); BuildLanguageSuported(builder); BuildInterface(builder); if (!config.LazyLoad) BuildClass(config, builder); BuidService(config, builder); if (!Directory.Exists(config.Path)) Directory.CreateDirectory(config.Path); var file = config.File.GetSelector() + ".service.ts"; WriteFile(config.Path.ToLower(), file, builder); Index.Run(config.Path); index.Run(config.Path); Console.WriteLine("Generate resource typescript - OK"); }
private void GenerateValidateTest(CSharpConfig config, ItemGroup group, Item item) { string path = Path.Combine(config.UnitTestPath, group.Name); string file = $"{item.Name}Test.cs"; if (File.Exists(Path.Combine(path, file))) return; var className = $"{item.Name}Test"; var nameSpace = new List<string> { "System.Linq", "System.Collections.Generic", "FluentAssertions", "Xunit", "XCommon.Patterns.Ioc", "XCommon.Application.Executes", "XCommon.Patterns.Specification.Validation", "XCommon.Patterns.Specification.Query" }; nameSpace.Add($"{config.EntrityNameSpace}.{group.Name}"); nameSpace.Add($"{config.EntrityNameSpace}.{group.Name}.Filter"); nameSpace.Add($"{config.DataBase.NameSpace}.{group.Name}"); nameSpace.Add($"{config.UnitTestNameSpace}.{group.Name}.DataSource"); StringBuilderIndented builder = new StringBuilderIndented(); builder .ClassInit(className, "BaseTest", $"{config.UnitTestNameSpace}.{group.Name}", ClassVisility.Public, false, nameSpace.ToArray()); builder .AppendLine("[Inject]") .AppendLine($"protected ISpecificationValidation<{item.Name}Entity> SpecificationValidation {{ get; set; }}") .AppendLine() .AppendLine("[Inject]") .AppendLine($"protected ISpecificationQuery<{item.Name}, {item.Name}Filter> SpecificationQuery {{ get; set; }}") .AppendLine() .AppendLine($"[Theory(DisplayName = \"{item.Name} (Validate)\")]") .AppendLine($"[MemberData(nameof({item.Name}DataSource.EntityValidation), MemberType = typeof({item.Name}DataSource))]") .AppendLine($"public void Validate{item.Name}({item.Name}Entity data, bool expected, string message)") .AppendLine("{") .IncrementIndent() .AppendLine("Execute execute = new Execute();") .AppendLine("bool result = SpecificationValidation.IsSatisfiedBy(data, execute);") .AppendLine() .AppendLine("result.Should().Be(expected, message);") .AppendLine("expected.Should().Be(!execute.HasErro, message);") .DecrementIndent() .AppendLine("}") .AppendLine() .AppendLine($"[Theory(DisplayName = \"{item.Name} (Load) \")]") .AppendLine($"[MemberData(nameof({item.Name}DataSource.EntityFilter), MemberType = typeof({item.Name}DataSource))]") .AppendLine($"public void ValidateLoad(List<{item.Name}> source, {item.Name}Filter filter, int expected, string message)") .AppendLine("{") .IncrementIndent() .AppendLine("var result = SpecificationQuery.Build(source, filter);") .AppendLine() .AppendLine("result.Count().Should().Be(expected, message);") .DecrementIndent() .AppendLine("}") .ClassEnd(); WriteFile(path, file, builder); }
private void Sass(string path, string selector, List<string> styleInclude) { var file = $"{selector}.scss"; if (File.Exists(Path.Combine(path, file))) { Console.WriteLine($"File already exists: {file}"); return; } StringBuilderIndented builder = new StringBuilderIndented(); styleInclude.ForEach(c => builder.AppendLine(c)); if (styleInclude.Count > 0) builder.AppendLine(); builder .AppendLine($".{selector} {{") .AppendLine("}"); WriteFile(path, file, builder); }
private void ProcessMapColumns(StringBuilderIndented builder, Item item) { foreach (var property in item.Properties) { builder .Append($"entity.Property(e => e.{property.Name})"); using (builder.Indent()) { if (!property.Nullable) builder .AppendLine() .Append(".IsRequired()"); if (property.Type == "string" && !string.IsNullOrEmpty(property.Size) && property.Size != "MAX") builder .AppendLine() .Append($".HasMaxLength({property.Size})"); if (property.Key) builder .AppendLine() .Append(".ValueGeneratedNever()"); } builder .Append(";") .AppendLine() .AppendLine(); } }
private void GenerateEntity(CSharpConfig config) { foreach (var group in config.DataBaseItems) { foreach (var item in group.Items) { string path = Path.Combine(config.DataBase.Path, group.Name); string file = $"{item.Name}.cs"; var nameSpace = new List<string> { "System", "System.Collections.Generic" }; nameSpace.AddRange(item.Relationships.Where(c => c.ItemGroupFK != group.Name).Select(c => $"{config.DataBase.NameSpace}.{c.ItemGroupFK}").Distinct()); nameSpace.AddRange(item.Relationships.Where(c => c.ItemGroupPK != group.Name).Select(c => $"{config.DataBase.NameSpace}.{c.ItemGroupPK}").Distinct()); nameSpace.AddRange(item.Properties.Where(c => c.NameGroup != group.Name).Select(c => c.NameGroup)); StringBuilderIndented builder = new StringBuilderIndented(); builder .GenerateFileMessage() .ClassInit(item.Name, null, $"{config.DataBase.NameSpace}.{group.Name}", ClassVisility.Public, nameSpace.ToArray()); foreach (var property in item.Properties) { builder .AppendLine($"public {property.Type} {property.Name} {{ get; set; }}") .AppendLine(); } foreach (var relationShip in item.Relationships.Where(c => c.RelationshipType == ItemRelationshipType.Single)) { string relationShipName = ProcessRelationShipName(config, relationShip, relationShip.ItemPK); builder .AppendLine($"public virtual {relationShip.ItemPK} {relationShipName} {{ get; set; }}") .AppendLine(); } foreach (var relationShip in item.Relationships.Where(c => c.RelationshipType == ItemRelationshipType.Many)) { string relationShipName = ProcessRelationShipName(config, relationShip, relationShip.ItemFK); builder .AppendLine($"public virtual ICollection<{relationShip.ItemFK}> {relationShipName} {{ get; set; }}") .AppendLine(); } builder .ClassEnd(); WriteFile(path, file, builder); } } }
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("}"); }
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 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 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 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 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 GenerateValidateDataSource(CSharpConfig config, ItemGroup group, Item item) { string path = Path.Combine(config.UnitTestPath, group.Name, "DataSource"); string file = $"{item.Name}DataSource.cs"; if (File.Exists(Path.Combine(path, file))) return; var className = $"{item.Name}DataSource"; var nameSpace = new List<string> { "System", "XCommon.Util", "System.Collections.Generic" }; nameSpace.Add($"{config.EntrityNameSpace}.{group.Name}"); nameSpace.Add($"{config.DataBase.NameSpace}.{group.Name}"); nameSpace.Add($"{config.EntrityNameSpace}.{group.Name}.Filter"); StringBuilderIndented builder = new StringBuilderIndented(); builder .ClassInit(className, string.Empty, $"{config.UnitTestNameSpace}.{group.Name}.DataSource", ClassVisility.Public, false, nameSpace.ToArray()); builder .AppendLine($"public static List<{item.Name}> Source") .AppendLine("{") .IncrementIndent() .AppendLine("get") .AppendLine("{") .IncrementIndent() .AppendLine($"List<{item.Name}> result = new List<{item.Name}>();") .AppendLine() .AppendLine("return result;") .DecrementIndent() .AppendLine("}") .DecrementIndent() .AppendLine("}") .AppendLine($"public static IEnumerable<object[]> EntityValidation") .AppendLine("{") .IncrementIndent() .AppendLine("get") .AppendLine("{") .IncrementIndent() .AppendLine($"PairList<{item.Name}Entity, bool> result = new PairList<{item.Name}Entity, bool>();") .AppendLine() .AppendLine("result.Add(null, false, \"Null value\");") .AppendLine($"result.Add(new {item.Name}Entity(), false, \"Default value\");") .AppendLine() .AppendLine("return result.Cast();") .DecrementIndent() .AppendLine("}") .DecrementIndent() .AppendLine("}") .AppendLine($"public static IEnumerable<object[]> EntityFilter") .AppendLine("{") .IncrementIndent() .AppendLine("get") .AppendLine("{") .IncrementIndent() .AppendLine($"PairList<List<{item.Name}>, {item.Name}Filter, int> result = new PairList<List<{item.Name}>, {item.Name}Filter, int>();") .AppendLine() .AppendLine($"result.Add(Source, new {item.Name}Filter(), 0, \"Default filter\");") .AppendLine() .AppendLine("return result.Cast();") .DecrementIndent() .AppendLine("}") .DecrementIndent() .AppendLine("}") .ClassEnd(); 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 BuildLanguageSuported(StringBuilderIndented builder) { builder .AppendLine("export class ApplicationCulture {") .IncrementIndent() .AppendLine("public Name: string;") .AppendLine("public Description: string;") .DecrementIndent() .AppendLine("}") .AppendLine(); }
private void BuildLanguageSuportedContructor(TypeScriptResource config, StringBuilderIndented builder) { foreach (var culture in config.Cultures) { builder.AppendLine($"this.Languages.push({{ Name: \"{culture.Name}\", Description: \"{culture.Description}\" }});"); } }
private void ProcessMapRelationShips(CSharpConfig config, StringBuilderIndented builder, Item item) { foreach (var relationShip in item.Relationships.Where(c => c.RelationshipType == ItemRelationshipType.Single)) { string relationShipNamePK = ProcessRelationShipName(config, relationShip, relationShip.ItemPK); string relationShipNameFK = ProcessRelationShipName(config, relationShip, relationShip.ItemFK); builder .AppendLine("entity") .IncrementIndent() .AppendLine($".HasOne(d => d.{relationShipNamePK})") .AppendLine($".WithMany(p => p.{relationShipNameFK})") .AppendLine($".HasForeignKey(d => d.{relationShip.PropertyFK});") .DecrementIndent() .AppendLine(); } }
private void GenerateContext(CSharpConfig config) { string path = config.DataBase.Path; string file = $"{config.DataBase.ContextName}.cs"; List<string> nameSpaces = new List<string> { "System", "Microsoft.EntityFrameworkCore", "Microsoft.EntityFrameworkCore.Infrastructure", "XCommon.Patterns.Ioc", "XCommon.Application" }; nameSpaces.AddRange(config.DataBaseItems.Select(c => $"{config.DataBase.NameSpace}.{c.Name}")); nameSpaces.AddRange(config.DataBaseItems.Select(c => $"{config.DataBase.NameSpace}.{c.Name}.Map")); StringBuilderIndented builder = new StringBuilderIndented(); builder .GenerateFileMessage() .ClassInit(config.DataBase.ContextName, "DbContext", config.DataBase.NameSpace, ClassVisility.Public, nameSpaces.ToArray()) .AppendLine() .AppendLine("private IApplicationSettings AppSettings => Kernel.Resolve<IApplicationSettings>();") .AppendLine(); foreach (var item in config.DataBaseItems.SelectMany(c => c.Items)) { builder .AppendLine($"public DbSet<{item.Name}> {item.Name} {{ get; set; }}") .AppendLine(); } builder .AppendLine("protected override void OnConfiguring(DbContextOptionsBuilder options)") .AppendLine("{") .IncrementIndent() .AppendLine("if (AppSettings.UnitTest)") .AppendLine("{") .IncrementIndent() .AppendLine($"options") .IncrementIndent() .AppendLine($".UseInMemoryDatabase(\"{config.DataBase.ContextName}\")") .AppendLine(".ConfigureWarnings(config => config.Ignore(InMemoryEventId.TransactionIgnoredWarning));") .DecrementIndent() .AppendLine() .AppendLine("return;") .DecrementIndent() .AppendLine("}") .AppendLine() .AppendLine($"options.UseSqlServer(AppSettings.ConnectionString);") .AppendLine() .DecrementIndent() .AppendLine("}") .AppendLine() .AppendLine("protected override void OnModelCreating(ModelBuilder modelBuilder)") .AppendLine("{"); using (builder.Indent()) { builder .AppendLine(); foreach (var item in config.DataBaseItems.SelectMany(c => c.Items)) { builder.AppendLine($"{item.Name}Map.Map(modelBuilder, AppSettings.UnitTest);"); } } builder .AppendLine("}") .ClassEnd(); WriteFile(path, file, builder); }
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 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 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(); } } }