//public void PopulateFeature(IEnumerable<ApplicationPart> parts, ControllerFeature feature) public static void GenerateTS() { IEnumerable <string> dlls = System.IO.Directory.EnumerateFiles(@"C:\repos\acorex-backend" + @"\lib").Where(f => f.Contains("Module")); HashSet <Type> modelImports = new HashSet <Type>(); foreach (string dll in dlls) { Assembly module = AssemblyLoadContext.Default.LoadFromAssemblyPath(dll); IEnumerable <global::System.Reflection.TypeInfo> controllers = module.DefinedTypes.Where(c => c.GetMethods().Where(m => m.GetCustomAttributes <WebApiAttribute>().Count() > 0).Count() > 0); foreach (global::System.Reflection.TypeInfo ct in controllers) { string moduleName = module.FullName.Split(",")[0]; StringBuilder sb = new StringBuilder(); StringBuilder header = new StringBuilder(); header.AppendLine("import { Injectable } from '@angular/core';"); //sb.AppendLine("using ACoreX.Core;"); //sb.AppendLine("using ACoreX.Core.Injector;"); header.AppendLine("import { AXHttpService, HttpResult } from 'acorex-ui/acorex-ui';"); //sb.AppendLine("using CRM.Module.Contexts;"); //sb.AppendLine("using Microsoft.AspNetCore.Authorization;"); sb.AppendLine("@Injectable()"); //sb.AppendLine("using System.Threading.Tasks;"); //sb.AppendLine("using ACoreX.Core.WepApi;"); //sb.AppendLine("using ACoreX.Core.Authentication;"); string interfaceName = ct.GetInterfaces()[0].Name; sb.AppendFormat("export class {0}", ct.Name.ToString().Replace("Context", "Service")); sb.AppendLine("{"); sb.AppendLine("constructor(private http:AXHttpService){}"); IEnumerable <MethodInfo> actions = ct.GetMethods().Where(m => m.GetCustomAttributes <WebApiAttribute>().Count() > 0); foreach (MethodInfo a in actions) { WebApiAttribute attr = a.GetCustomAttribute <WebApiAttribute>(); sb.AppendLine(); List <string> paramsList = new List <string>(); List <string> paramsListWithType = new List <string>(); List <string> paramsValue = new List <string>(); foreach (ParameterInfo p in a.GetParameters()) { paramsList.Add(string.Format("{1}{2}", p.ParameterType.GetFriendlyName(), p.Name, p.HasDefaultValue && p.ParameterType.GetFriendlyName() == "string" ? ":" + @"""" + p.DefaultValue + @"""" : p.HasDefaultValue ? ":" + p.DefaultValue.ToString() : "")); paramsListWithType.Add(string.Format("{0}:{1}", p.Name, p.ParameterType.GetFriendlyName())); paramsValue.Add(string.Format("{0}", p.Name)); } string paramsStr = string.Join(",", paramsList); string paramsStrWithType = string.Join(",", paramsListWithType); string valueStr = string.Join(",", paramsValue); sb.AppendFormat("{0}({2}):HttpResult<{1}>", a.Name, a.ReturnType.GetFriendlyName() == "System.Void" ? "any" : a.ReturnType.GetTypeName().Replace("[][]", "[]"), paramsStrWithType); string fileNameMethod = a.ReturnType.GetTypeFileName().Replace("[]", ""); if (fileNameMethod != "string") { modelImports.Add(GetPureType(a.ReturnType)); header.AppendLine("import {" + fileNameMethod + " } from '../models/" + fileNameMethod + "';"); } sb.AppendLine("{return this.http.request({"); sb.AppendFormat(@"url:""{0}"",method:""{1}""{2}", attr.Route, attr.Method.ToString().ToLower(), paramsStr != "" ? ",params:{" + paramsStr : ""); sb.AppendLine("})}"); // sb.AppendLine(); } sb.AppendLine("}"); StringBuilder fileString = header.Append(sb); GenerateNeededTSModels(modelImports); Console.WriteLine(fileString.ToString()); GenerateModel(fileString.ToString(), ct.Name.ToString().Replace("Context", "Service"), module.Location); } } }
public void PopulateFeature(IEnumerable <ApplicationPart> parts, ControllerFeature feature) { IEnumerable <Assembly> all = AppDomain.CurrentDomain.GetAssemblies().Where(c => !c.FullName.Contains("DynamicMethods")); IEnumerable <Assembly> allSdk = all; IEnumerable <Assembly> modulesAssemblies = all.Where(c => c.FullName.Contains(".Module") && !c.FullName.Contains("Contract")); foreach (Assembly module in modulesAssemblies) { string moduleName = module.FullName.Split(",")[0]; StringBuilder sb = new StringBuilder(); sb.AppendLine("using System;"); //sb.AppendLine("using ACoreX.Core;"); //sb.AppendLine("using ACoreX.Core.Injector;"); sb.AppendLine("using Microsoft.AspNetCore.Mvc;"); //sb.AppendLine("using CRM.Module.Contexts;"); //sb.AppendLine("using Microsoft.AspNetCore.Authorization;"); sb.AppendLine("using ACoreX.Infrastructure.Authentication;"); sb.AppendLine("using System.Threading.Tasks;"); //sb.AppendLine("using ACoreX.Core.WepApi;"); //sb.AppendLine("using ACoreX.Core.Authentication;"); sb.AppendLine(); sb.AppendFormat("namespace {0}.Controllers {{", moduleName); IEnumerable <global::System.Reflection.TypeInfo> controllers = module.DefinedTypes.Where(c => c.GetMethods().Where(m => m.GetCustomAttributes <WebApiAttribute>().Count() > 0).Count() > 0); foreach (global::System.Reflection.TypeInfo ct in controllers) { string interfaceName = ct.GetInterfaces()[0].FullName; sb.AppendLine(); sb.AppendLine("[ApiController]"); sb.AppendFormat("public class {0}Controller : ControllerBase {{", ct.Name); sb.AppendLine(); sb.AppendFormat(" private {0} context;", interfaceName); sb.AppendLine(); sb.AppendFormat("public {0}Controller({1} context) {{ ", ct.Name, interfaceName); sb.AppendFormat("this.context = context;", interfaceName); sb.AppendLine("}"); IEnumerable <MethodInfo> actions = ct.GetMethods().Where(m => m.GetCustomAttributes <WebApiAttribute>().Count() > 0); foreach (MethodInfo a in actions) { WebApiAttribute attr = a.GetCustomAttribute <WebApiAttribute>(); if (attr.Authorized) { sb.AppendLine("[Authentication]"); } //else //{ // sb.AppendLine("[AllowAnonymous]"); //} switch (attr.Method) { case WebApiMethod.Get: sb.AppendFormat(@"[HttpGet(""{0}"")]", attr.Route); break; case WebApiMethod.Post: sb.AppendFormat(@"[HttpPost(""{0}"")]", attr.Route); break; case WebApiMethod.Put: sb.AppendFormat(@"[HttpPut(""{0}"")]", attr.Route); break; case WebApiMethod.Delete: sb.AppendFormat(@"[HttpDelete(""{0}"")]", attr.Route); break; case WebApiMethod.Head: sb.AppendFormat(@"[HttpHead(""{0}"")]", attr.Route); break; case WebApiMethod.Patch: sb.AppendFormat(@"[HttpPatch(""{0}"")]", attr.Route); break; default: sb.AppendFormat(@"[HttpGet(""{0}"")]", attr.Route); break; } sb.AppendLine(); List <string> paramsList = new List <string>(); List <string> paramsValue = new List <string>(); foreach (ParameterInfo p in a.GetParameters()) { //paramsList.Add(String.Format("[FromBody]{0} {1}",p.ParameterType.PrettyName(),p.Name)); paramsList.Add(string.Format("{0} {1}{2}", p.ParameterType.GetFriendlyName(), p.Name, p.HasDefaultValue && p.ParameterType.GetFriendlyName() == "string" ? "=" + @"""" + p.DefaultValue + @"""" : p.HasDefaultValue ? "=" + p.DefaultValue.ToString() : "")); paramsValue.Add(string.Format("{0}", p.Name)); } string paramsStr = string.Join(",", paramsList); string valueStr = string.Join(",", paramsValue); sb.AppendFormat("public {3} {1} {0}({2}){{", a.Name, a.ReturnType.GetFriendlyName() == "System.Void" ? "void" : a.ReturnType.GetFriendlyName(), paramsStr, a.ReturnType.BaseType == typeof(Task) ? "async" : ""); sb.AppendFormat("{0} {3} context.{1}({2});", a.ReturnType != typeof(void) ? "return" : "", a.Name, valueStr, a.ReturnType.BaseType == typeof(Task) ? "await" : ""); sb.AppendLine("}"); // sb.AppendLine(); } sb.AppendLine("}"); } sb.AppendLine("}"); SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(sb.ToString()); string assemblyName = $"{moduleName}.Controllers"; List <MetadataReference> references = new List <MetadataReference>(); foreach (Assembly r in allSdk) { references.Add(MetadataReference.CreateFromFile(r.Location)); } references.Add(MetadataReference.CreateFromFile(module.Location)); // CSharpCompilation compilation = CSharpCompilation.Create( assemblyName, syntaxTrees: new[] { syntaxTree }, references: references, options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); using (MemoryStream ms = new MemoryStream()) { EmitResult result = compilation.Emit(ms); if (!result.Success) { IEnumerable <Diagnostic> failures = result.Diagnostics.Where(diagnostic => diagnostic.IsWarningAsError || diagnostic.Severity == DiagnosticSeverity.Error); foreach (Diagnostic diagnostic in failures) { Console.Error.WriteLine("{0}: {1}", diagnostic.Id, diagnostic.GetMessage()); } } else { ms.Seek(0, SeekOrigin.Begin); Assembly assembly = AssemblyLoadContext.Default.LoadFromStream(ms); Type[] candidates = assembly.GetExportedTypes(); foreach (Type candidate in candidates) { feature.Controllers.Add(candidate.GetTypeInfo()); } } } } }