using CloudBuilder.CodeGenerator.Config; using CloudBuilder.CodeGenerator.Data; using CloudBuilder.Core.Application.Builders; using CloudBuilder.Core.Authorization; using CloudBuilder.Core.DatabaseAccessor; using CloudBuilder.Core.DatabaseAccessor.Entity; using CloudBuilder.Core.Extensions; using CloudBuilder.Core.Service; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using RazorEngineCore; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace CloudBuilder.CodeGenerator.Utility { public class RazorEngineHelper { public static string CompiledTemplate(string template, ModelData model) { ToolInitConfig toolInitConfig = ToolInitConfig.GetInstance(); IRazorEngine razorEngine = new RazorEngine(); List args = new List(); if (toolInitConfig.AssemblyReferences != null) { foreach (var reference in toolInitConfig.AssemblyReferences) { if (string.IsNullOrEmpty(reference.AssemblyFileName)) continue; if (!File.Exists(reference.AssemblyFileName)) continue; args.Add(reference.AssemblyFileName); } } template = toolInitConfig.Using + template; try { IRazorEngineCompiledTemplate compiledTemplate = razorEngine.Compile(template, builder => { //builder.AddAssemblyReferenceByName("System.Security"); // by name foreach (string arg in args) { builder.AddAssemblyReference(Assembly.LoadFrom(arg)); } Assembly[] assemblies = CloudBuilderBuilder.GetAssemblies(); foreach (Assembly assembly in assemblies) { builder.AddAssemblyReference(assembly); } builder.AddAssemblyReference(typeof(System.IO.File)); // by type //builder.AddAssemblyReference(Assembly.Load("source")); // by reference }); return compiledTemplate.Run(model); } catch (Exception ex) { throw ex; } return null; } public static ClassInfo[] PackIServiceClassInfo(Assembly assembly) { if (assembly == null) return null; var baseServiceType = typeof(IService); var typeServices = assembly.GetTypes() .Where(type => type != baseServiceType && type.HasImplementedRawGeneric(baseServiceType)) .Where(x => x.IsClass).ToList(); List classInfos = new List(); List usings = new List(); foreach (var implementType in typeServices) { ClassInfo classInfo = new ClassInfo(); classInfo.ClassName = implementType.Name; classInfo.InterfaceName = implementType.GetInterfaces().FirstOrDefault().Name; if (!usings.Contains(implementType.GetInterfaces().FirstOrDefault().Namespace)) { usings.Add(implementType.GetInterfaces().FirstOrDefault().Namespace); } var tors = implementType.GetConstructors(); ClassConstructorInfo constructorInfo = new ClassConstructorInfo(); List parameters = new List(); foreach (var item in tors) { ParameterInfo[] Parameters = item.GetParameters(); MethodParameter methodParameter; foreach (var pi in Parameters) { methodParameter = new MethodParameter(); methodParameter.ParameterName = pi.Name; methodParameter.ParameterTypeName = pi.ParameterType.Name; if (!usings.Contains(pi.ParameterType.Namespace)) { usings.Add(pi.ParameterType.Namespace); } parameters.Add(methodParameter); } } constructorInfo.Parameters = parameters.ToArray(); classInfo.ClassConstructorInfos = new ClassConstructorInfo[] { constructorInfo }; List classMethodInfos = new List(); ClassMethodInfo classMethodInfo; MethodInfo[] infos = implementType.GetMethods(); foreach (var item in infos) { var permissionOperate = item.GetCustomAttribute(); var allowAnonymous = item.GetCustomAttribute(); if (permissionOperate == null && allowAnonymous == null) continue; classMethodInfo = new ClassMethodInfo(); classMethodInfo.MethodName = item.Name; var httpGet = item.GetCustomAttribute(); if (httpGet != null) classMethodInfo.HttpName = "[HttpGet]"; if (permissionOperate != null) { classMethodInfo.AuthorizeName = null; classMethodInfo.PermissionOperateAttribute = permissionOperate; } else { classMethodInfo.PermissionOperate = null; } var unitOfWork = item.GetCustomAttribute(); if (unitOfWork == null) classMethodInfo.UnitOfWorkName = null; classMethodInfo.MethodReturnName = item.ReturnType.Name; bool isValueType = MethodParameter.CheckValueType(item.ReturnType); classMethodInfo.IsValueType = isValueType; if (!isValueType && !usings.Contains(item.ReturnType.Namespace)) { usings.Add(item.ReturnType.Namespace); } classMethodInfo.MethodReturnName = classMethodInfo.MethodReturnName == "Object" ? "dynamic" : classMethodInfo.MethodReturnName; ParameterInfo[] methodParameters = item.GetParameters(); List methodParameterLs = new List(); MethodParameter methodParameter; foreach (var pi in methodParameters) { methodParameter = new MethodParameter(); methodParameter.ParameterName = pi.Name; methodParameter.ParameterTypeName = pi.ParameterType.Name; if (!usings.Contains(pi.ParameterType.Namespace)) { usings.Add(pi.ParameterType.Namespace); } methodParameter.IsValueType = MethodParameter.CheckValueType(pi.ParameterType); if (methodParameter.IsValueType) methodParameter.ParameterTypeName = methodParameter.ParameterTypeName; methodParameterLs.Add(methodParameter); } classMethodInfo.Parameters = methodParameterLs.ToArray(); classMethodInfos.Add(classMethodInfo); } classInfo.ClassMethodInfos = classMethodInfos.ToArray(); classInfo.Usings = usings.ToArray(); classInfos.Add(classInfo); } return classInfos.ToArray(); } public static ClassInfo[] PackTypeScriptClassInfo(Assembly assembly) { var typeDatas = assembly.GetTypes() .Where(x => x.GetCustomAttribute() != null) .Where(x => x.IsClass).ToList(); List dataInfos = new List(); ClassInfo dataInfo; List classPropertyInfos = new List(); ClassPropertyInfo classPropertyInfo; List baseTypes = new List(); foreach (var implementType in typeDatas) { PackClassInfo(dataInfos, implementType); } return dataInfos.ToArray(); } public static void PackClassInfo(List dataInfos, Type implementType) { ClassInfo dataInfo = new ClassInfo(); dataInfo.ClassName = implementType.Name; if (implementType.BaseType != typeof(ViewEntityBase) && implementType.BaseType != typeof(object) && implementType.BaseType != null && !implementType.BaseType.Name.StartsWith("EntityBase")) { dataInfo.ImplementTypeName = implementType.BaseType.Name; PackClassInfo(dataInfos, implementType.BaseType); } PropertyInfo[] properties = implementType.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance); List baseTypes = new List(); List classPropertyInfos = new List(); ClassPropertyInfo classPropertyInfo; ToolInitConfig toolInitConfig = ToolInitConfig.GetInstance(); foreach (var item in properties) { classPropertyInfo = new ClassPropertyInfo(); classPropertyInfo.PropertyName = item.Name; Type type = item.PropertyType; bool isContinue = false; foreach (string exculdePropertyType in toolInitConfig.ExculdePropertyTypes) { if (item.PropertyType.Name.Contains(exculdePropertyType)) { classPropertyInfo.PropertyTypeName = "any"; isContinue = true; break; } } if (isContinue) { classPropertyInfos.Add(classPropertyInfo); continue; } if (item.PropertyType.BaseType != null && item.PropertyType.BaseType.Name == "Array") { string typeName = item.PropertyType.FullName.Replace("[]", string.Empty); type = item.PropertyType.Assembly.GetType(typeName); classPropertyInfo.IsValueType = MethodParameter.CheckValueType(type); classPropertyInfo.PropertyTypeName = type.Name + "[]"; } else if (item.PropertyType.Name == "List`1") { string typeName = item.PropertyType.FullName.Replace("[[", "|").Replace(",", "|").Split('|')[1]; type = item.PropertyType.Assembly.GetType(typeName); classPropertyInfo.IsValueType = MethodParameter.CheckValueType(type); classPropertyInfo.PropertyTypeName = type.Name + "[]"; } else { classPropertyInfo.IsValueType = MethodParameter.CheckValueType(item.PropertyType); classPropertyInfo.PropertyTypeName = item.PropertyType.Name; } if (!classPropertyInfo.IsValueType) { if (!baseTypes.Contains(type.Name)) baseTypes.Add(type.Name); PackClassInfo(dataInfos, type); } classPropertyInfos.Add(classPropertyInfo); } dataInfo.ClassProperties = classPropertyInfos.ToArray(); if (baseTypes.Count > 0) dataInfo.Usings = baseTypes.ToArray(); FieldInfo[] fields = implementType.GetFields(); List fieldInfos = new List(); ClassFieldInfo classFieldInfo; foreach (var item in fields) { if (!item.IsStatic) continue; bool IsValueType = MethodParameter.CheckValueType(item.FieldType); if (!IsValueType) continue; classFieldInfo = new ClassFieldInfo(); classFieldInfo.IsValueType = IsValueType; classFieldInfo.FieldName = item.Name; classFieldInfo.FieldTypeName = item.FieldType.Name; classFieldInfo.FieldValue = Convert.ToString(item.GetValue(null)); fieldInfos.Add(classFieldInfo); } dataInfo.ClassFields = fieldInfos.ToArray(); dataInfos.Add(dataInfo); } } }