/// <summary> /// Creates type tree that represents UserType base on the specified UserType and user type factory. /// </summary> /// <param name="userType">The user type.</param> /// <param name="factory">The user type factory.</param> /// <returns>Type tree that represents UserType</returns> internal static UserTypeTree Create(UserType userType, UserTypeFactory factory) { // Check arguments if (userType == null) { throw new ArgumentNullException(nameof(userType)); } // If user type is template or declared in template user type, // we need to force template because of possible template types used from "parent" type. var type = userType; while (type != null) { var templateType = type as TemplateUserType; if (templateType != null) { return(new TemplateTypeTree(userType, factory)); } type = type.DeclaredInType; } // Check if user type is enumeration var enumType = userType as EnumUserType; if (enumType != null) { return(new EnumTreeType(enumType)); } // We are now certain that it is regular user type return(new UserTypeTree(userType)); }
public IActionResult Login([FromServices] IAppLogCreator appLogCreator, [FromServices] IAppLogRetriever appLogRetriever, String username, String password) { IUser user = UserTypeFactory.CreateUser(username, password); if (user != null && user.GetType() == typeof(Administrator)) { Administrator AdminUser = (Administrator)user; AdminUser.IsLogin = true; _session = Session.getInstance; _session.setCurrentUser(AdminUser); new AdminMapper().Update(AdminUser).Save().Commit(); return(RedirectToAction("Profile", "Admin")); } else if (user != null && user.GetType() == typeof(Household)) { Household householduser = (Household)user; householduser.IsLogin = true; _session = Session.getInstance; _session.setCurrentUser(householduser); new HouseholdMapper().Update(householduser).Save().Commit(); appLogRetriever.SetHouseholdId(householduser._id); appLogCreator.SetHouseholdId(householduser._id); appLogCreator.AddLog(this, "ACTION*/-LOGIN", DateTime.Now); return(RedirectToAction("Profile", "Household")); } else { return(View("Index")); } }
public IActionResult Create(string street, int postalCode, string unitNo, string surname, string contactNo, string email, string password) { _session = Session.getInstance; if (_session.IsLogin()) { try { string[] UnitNoArray = unitNo.Split("#"); string username = postalCode.ToString() + "-" + UnitNoArray[1]; UserTypeFactory.CreateHousehold(username, password, email, street, postalCode, unitNo, surname, contactNo); return(RedirectToAction(nameof(DashBoard))); } catch { return(View()); } } else { return(RedirectToAction("Index", "Home")); } }
/// <summary> /// Initializes members from the specified CodeGen configuration. /// </summary> /// <param name="xmlConfig">The CodeGen configuration.</param> /// <param name="logger">The logger.</param> /// <param name="errorLogger">The error logger.</param> private void Initialize(XmlConfig xmlConfig, TextWriter logger, TextWriter errorLogger) { stopwatch = System.Diagnostics.Stopwatch.StartNew(); this.logger = logger; this.errorLogger = errorLogger; this.xmlConfig = xmlConfig; xmlModules = xmlConfig.Modules; typeNames = xmlConfig.Types; includedFiles = xmlConfig.IncludedFiles; referencedAssemblies = xmlConfig.ReferencedAssemblies; generationOptions = xmlConfig.GetGenerationFlags(); int nameLimit = xmlConfig.GenerateAssemblyWithRoslyn ? 1000 : 250; if (xmlConfig.GenerateAssemblyWithILWriter && !string.IsNullOrEmpty(xmlConfig.GeneratedAssemblyName)) { nameLimit = 10000; codeWriter = new ManagedILCodeWriter(Path.GetFileNameWithoutExtension(xmlConfig.GeneratedAssemblyName), generationOptions, nameLimit); } else { codeWriter = new CSharpCodeWriter(generationOptions, nameLimit); } userTypeFactory = new UserTypeFactory(xmlConfig.Transformations, codeWriter.Naming); userTypes = new List <UserType>(); }
protected override void PersistUpdatedItem(IUserType entity) { var userTypeFactory = new UserTypeFactory(); var userTypeDto = userTypeFactory.BuildDto(entity); Database.Update(userTypeDto); }
protected override void PersistNewItem(IUserType entity) { var userTypeFactory = new UserTypeFactory(); var userTypeDto = userTypeFactory.BuildDto(entity); var id = Convert.ToInt32(Database.Insert(userTypeDto)); entity.Id = id; }
/// <summary> /// Generates the code for user type and creates a file for it. /// </summary> /// <param name="userType">The user type.</param> /// <param name="factory">The user type factory.</param> /// <param name="outputDirectory">The output directory where code file will be stored.</param> /// <param name="errorOutput">The error output.</param> /// <param name="generationFlags">The user type generation flags.</param> /// <param name="generatedFiles">The list of already generated files.</param> /// <returns>Tuple of generated code and filename</returns> private static Tuple <string, string> GenerateCode(UserType userType, UserTypeFactory factory, string outputDirectory, TextWriter errorOutput, UserTypeGenerationFlags generationFlags, ConcurrentDictionary <string, string> generatedFiles) { Symbol symbol = userType.Symbol; if (symbol != null && symbol.Tag == SymTagEnum.SymTagBaseType) { // ignore Base (Primitive) types. return(Tuple.Create("", "")); } bool allParentsAreNamespaces = true; for (UserType parentType = userType.DeclaredInType; parentType != null && allParentsAreNamespaces; parentType = parentType.DeclaredInType) { allParentsAreNamespaces = parentType is NamespaceUserType; } if (userType is NamespaceUserType || !allParentsAreNamespaces) { return(Tuple.Create("", "")); } string classOutputDirectory = outputDirectory; string nameSpace = (userType.DeclaredInType as NamespaceUserType)?.FullClassName ?? userType.Namespace; if (!string.IsNullOrEmpty(nameSpace)) { classOutputDirectory = Path.Combine(classOutputDirectory, nameSpace.Replace(".", "\\").Replace(":", ".")); } Directory.CreateDirectory(classOutputDirectory); bool isEnum = userType is EnumUserType; string filename = string.Format(@"{0}\{1}{2}.exported.cs", classOutputDirectory, userType.ConstructorName, isEnum ? "_enum" : ""); int index = 1; while (true) { if (generatedFiles.TryAdd(filename.ToLowerInvariant(), filename)) { break; } filename = string.Format(@"{0}\{1}{2}_{3}.exported.cs", classOutputDirectory, userType.ConstructorName, isEnum ? "_enum" : "", index++); } using (TextWriter output = new StreamWriter(filename)) using (StringWriter stringOutput = new StringWriter()) { userType.WriteCode(new IndentedWriter(stringOutput, generationFlags.HasFlag(UserTypeGenerationFlags.CompressedOutput)), errorOutput, factory, generationFlags); string text = stringOutput.ToString(); output.WriteLine(text); return(Tuple.Create(text, filename)); } }
/// <summary> /// Gets the user service /// </summary> /// <returns></returns> private IUserService GetUserService() { IAccountProvider accountProvider = new AspNetAccountProvider( HttpContext.GetOwinContext().GetUserManager <ApplicationUserManager>(), HttpContext.GetOwinContext().Authentication); IUserService userService = UserTypeFactory.GetUserService(new UserServiceUoW(User.Identity.GetUserId(), accountProvider)); return(userService); }
protected override IEnumerable <IUserType> PerformGetByQuery(IQuery <IUserType> query) { var userTypeFactory = new UserTypeFactory(); var sqlClause = GetBaseQuery(false); var translator = new SqlTranslator <IUserType>(sqlClause, query); var sql = translator.Translate(); var dtos = Database.Fetch <UserTypeDto>(sql); return(dtos.Select(userTypeFactory.BuildEntity).ToArray()); }
/// <summary> /// Initializes members from the specified CodeGen configuration. /// </summary> /// <param name="xmlConfig">The CodeGen configuration.</param> /// <param name="logger">The logger.</param> /// <param name="errorLogger">The error logger.</param> private void Initialize(XmlConfig xmlConfig, TextWriter logger, TextWriter errorLogger) { stopwatch = System.Diagnostics.Stopwatch.StartNew(); this.logger = logger; this.errorLogger = errorLogger; this.xmlConfig = xmlConfig; xmlModules = xmlConfig.Modules; typeNames = xmlConfig.Types; includedFiles = xmlConfig.IncludedFiles; referencedAssemblies = xmlConfig.ReferencedAssemblies; generationOptions = xmlConfig.GetGenerationFlags(); userTypeFactory = new UserTypeFactory(xmlConfig.Transformations); userTypes = new List <UserType>(); }
/// <summary> /// Initializes a new instance of the <see cref="TemplateTypeInstance"/> class. /// </summary> /// <param name="templateSpecialization">The user type that is either template specialization user type or declared in one.</param> /// <param name="factory">The user type factory.</param> public TemplateTypeInstance(UserType templateSpecialization, UserTypeFactory factory) : base(templateSpecialization) { // Get all "parent" types UserType type = templateSpecialization; List <UserType> declaredInList = new List <UserType>(); while (type != null) { declaredInList.Add(type); type = type.DeclaredInType; } declaredInList.Reverse(); DeclaredInTypeHierarchy = declaredInList.ToArray(); // Extract all template types and check if we can instantiate this instance CanInstantiate = true; SpecializedArguments = new TypeInstance[DeclaredInTypeHierarchy.Length][]; for (int j = 0; j < DeclaredInTypeHierarchy.Length; j++) { // Check if current type in hierarchy is template type SpecializedTemplateUserType templateType = DeclaredInTypeHierarchy[j] as SpecializedTemplateUserType; if (templateType == null) { continue; } // Try to find specialized arguments for template type IReadOnlyList <Symbol> arguments = templateType.TemplateArgumentsAsSymbols; TypeInstance[] specializedArguments = new TypeInstance[arguments.Count]; for (int i = 0; i < arguments.Count; i++) { TypeInstance ti = factory.GetSymbolTypeInstance(templateSpecialization, arguments[i]); specializedArguments[i] = ti; if (ti.ContainsUndefinedType()) { CanInstantiate = false; } } SpecializedArguments[j] = specializedArguments; } }
protected override IUserType PerformGet(int id) { var sql = GetBaseQuery(false); sql.Where(GetBaseWhereClause(), new { Id = id }); var dto = Database.FirstOrDefault <UserTypeDto>(sql); if (dto == null) { return(null); } var userTypeFactory = new UserTypeFactory(); var userType = userTypeFactory.BuildEntity(dto); return(userType); }
// GET: Users public ActionResult Index() { Mapper.CreateMap <ApplicationUser, UserViewModel>(); IAccountProvider accountProvider = new AspNetAccountProvider( HttpContext.GetOwinContext().GetUserManager <ApplicationUserManager>(), HttpContext.GetOwinContext().Authentication); IUserService userService = UserTypeFactory.GetUserService(new UserServiceUoW(User.Identity.GetUserId(), accountProvider)); IList <UserViewModel> listOfUsers = Mapper.Map <IList <ApplicationUser>, IList <UserViewModel> >( userService.GetUsers().ToList()); UserListViewModel userListViewModel = new UserListViewModel { Users = listOfUsers }; userService.UpdateUserTenants(); return(View(userListViewModel)); }
protected override IEnumerable <IUserType> PerformGetAll(params int[] ids) { var userTypeFactory = new UserTypeFactory(); var sql = GetBaseQuery(false); if (ids.Any()) { sql.Where("umbracoUserType.id in (@ids)", new { ids = ids }); } else { sql.Where <UserTypeDto>(x => x.Id >= 0); } var dtos = Database.Fetch <UserTypeDto>(sql); return(dtos.Select(userTypeFactory.BuildEntity).ToArray()); }
/// <summary> /// Generates the code for user type in single file. /// </summary> /// <param name="codeWriter">The code writer used to output generated code.</param> /// <param name="output">The output text writer.</param> /// <param name="userType">The user type.</param> /// <param name="factory">The user type factory.</param> /// <param name="errorOutput">The error output.</param> /// <param name="generationFlags">The user type generation flags.</param> /// <returns><c>true</c> if code was generated for the type; otherwise <c>false</c></returns> private static bool GenerateCodeInSingleFile(ICodeWriter codeWriter, StringBuilder output, UserType userType, UserTypeFactory factory, TextWriter errorOutput, UserTypeGenerationFlags generationFlags) { Symbol symbol = userType.Symbol; if (symbol != null && symbol.Tag == CodeTypeTag.BuiltinType) { // Ignore built-in types. return(false); } if (userType.DeclaredInType != null) { return(false); } codeWriter.WriteUserType(userType, output); return(true); }
/// <summary> /// Initializes a new instance of the <see cref="TemplateUserTypeFactory"/> class. /// </summary> /// <param name="originalFactory">The original user type factory.</param> /// <param name="templateType">The template user type.</param> public TemplateUserTypeFactory(UserTypeFactory originalFactory, TemplateUserType templateType) : base(originalFactory) { TemplateType = templateType; OriginalFactory = originalFactory; }
/// <summary> /// Initializes a new instance of the <see cref="TemplateArgumentTreeType"/> class. /// </summary> /// <param name="templateArgumentNumber">The template type argument number.</param> /// <param name="templateSpecialization">The template specialization user type.</param> /// <param name="factory">The user type factory.</param> public TemplateArgumentTreeType(int templateArgumentNumber, UserType templateSpecialization, UserTypeFactory factory) : base(templateSpecialization, factory) { ArgumentNumber = templateArgumentNumber; }
/// <summary> /// Initializes a new instance of the <see cref="TemplateTypeTree"/> class. /// </summary> /// <param name="templateSpecialization">The template specialization user type.</param> /// <param name="factory">The user type factory.</param> public TemplateTypeTree(UserType templateSpecialization, UserTypeFactory factory) : base(templateSpecialization) { // Get all "parent" types UserType type = templateSpecialization; List <UserType> declaredInList = new List <UserType>(); while (type != null) { declaredInList.Add(type); type = type.DeclaredInType; } declaredInList.Reverse(); DeclaredInTypeHierarchy = declaredInList.ToArray(); // Extract all template types and check if we can instantiate this instance CanInstantiate = true; SpecializedArguments = new TypeTree[DeclaredInTypeHierarchy.Length][]; for (int j = 0; j < DeclaredInTypeHierarchy.Length; j++) { // Check if current type in hierarchy is template type TemplateUserType templateType = DeclaredInTypeHierarchy[j] as TemplateUserType; if (templateType == null) { continue; } // Try to find specialized arguments for template type IReadOnlyList <Symbol> arguments = templateType.TemplateArgumentsAsSymbols; TypeTree[] specializedArguments = new TypeTree[arguments.Count]; for (int i = 0; i < arguments.Count; i++) { UserType userType; factory.GetUserType(arguments[i], out userType); if (userType != null) { specializedArguments[i] = UserTypeTree.Create(userType, factory); TemplateTypeTree templateTypeTree = specializedArguments[i] as TemplateTypeTree; if (templateTypeTree != null && !templateTypeTree.CanInstantiate) { CanInstantiate = false; } } else { // TODO: Check why do we go one more round trip through module for getting argument symbol Symbol symbol = templateSpecialization.Symbol.Module.GetSymbol(arguments[i].Name); if (symbol.Tag != SymTagEnum.SymTagBaseType) { // Base Types (Primitive Types) can be used for specialization CanInstantiate = false; } // #fixme can't deal with it specializedArguments[i] = templateType.GetSymbolTypeTree(arguments[i], factory); } } SpecializedArguments[j] = specializedArguments; } }
/// <summary> /// Generates the code for user type and creates a file for it. /// </summary> /// <param name="codeWriter">The code writer used to output generated code.</param> /// <param name="userType">The user type.</param> /// <param name="factory">The user type factory.</param> /// <param name="outputDirectory">The output directory where code file will be stored.</param> /// <param name="errorOutput">The error output.</param> /// <param name="generationFlags">The user type generation flags.</param> /// <param name="generatedFiles">The list of already generated files.</param> /// <returns>Tuple of generated code and filename</returns> private static Tuple <string, string> GenerateCode(ICodeWriter codeWriter, UserType userType, UserTypeFactory factory, string outputDirectory, TextWriter errorOutput, UserTypeGenerationFlags generationFlags, ConcurrentDictionary <string, string> generatedFiles) { Symbol symbol = userType.Symbol; if (symbol != null && symbol.Tag == CodeTypeTag.BuiltinType) { // Ignore built-in types. return(Tuple.Create("", "")); } bool allParentsAreNamespaces = true; for (UserType parentType = userType.DeclaredInType; parentType != null && allParentsAreNamespaces; parentType = parentType.DeclaredInType) { allParentsAreNamespaces = parentType is NamespaceUserType; } if (userType is NamespaceUserType || !allParentsAreNamespaces) { return(Tuple.Create("", "")); } string classOutputDirectory = outputDirectory; string nameSpace = (userType.DeclaredInType as NamespaceUserType)?.FullTypeName ?? userType.Namespace; if (!string.IsNullOrEmpty(nameSpace)) { classOutputDirectory = Path.Combine(classOutputDirectory, nameSpace.Replace(".", "\\").Replace(":", ".")); } if (!generationFlags.HasFlag(UserTypeGenerationFlags.DontSaveGeneratedCodeFiles)) { Directory.CreateDirectory(classOutputDirectory); } bool isEnum = userType is EnumUserType; string filename = string.Format(@"{0}\{1}{2}.exported.cs", classOutputDirectory, userType.ConstructorName, isEnum ? "_enum" : ""); int index = 1; while (true) { if (generatedFiles.TryAdd(filename.ToLowerInvariant(), filename)) { break; } filename = string.Format(@"{0}\{1}{2}_{3}.exported.cs", classOutputDirectory, userType.ConstructorName, isEnum ? "_enum" : "", index++); } StringBuilder stringOutput = new StringBuilder(); codeWriter.WriteUserType(userType, stringOutput); string text = stringOutput.ToString(); if (!generationFlags.HasFlag(UserTypeGenerationFlags.DontSaveGeneratedCodeFiles)) { File.WriteAllText(filename, text); } return(Tuple.Create(text, filename)); }
/// <summary> /// Generates user types using the specified XML configuration. /// </summary> /// <param name="xmlConfig">The XML configuration.</param> /// <param name="logger">The logger text writer. If set to null, Console.Out will be used.</param> /// <param name="errorLogger">The error logger text writer. If set to null, Console.Error will be used.</param> public static void Generate(XmlConfig xmlConfig, TextWriter logger = null, TextWriter errorLogger = null) { var sw = System.Diagnostics.Stopwatch.StartNew(); XmlModule[] xmlModules = xmlConfig.Modules; XmlType[] typeNames = xmlConfig.Types; XmlIncludedFile[] includedFiles = xmlConfig.IncludedFiles; XmlReferencedAssembly[] referencedAssemblies = xmlConfig.ReferencedAssemblies; UserTypeGenerationFlags generationOptions = xmlConfig.GetGenerationFlags(); ConcurrentDictionary <string, string> generatedFiles = new ConcurrentDictionary <string, string>(); var syntaxTrees = new ConcurrentBag <SyntaxTree>(); string currentDirectory = Directory.GetCurrentDirectory(); string outputDirectory = currentDirectory + "\\output\\"; // Check logger and error logger if (errorLogger == null) { errorLogger = Console.Error; } if (logger == null) { logger = Console.Out; } // Create output directory Directory.CreateDirectory(outputDirectory); // Verify that included files exist if (!string.IsNullOrEmpty(xmlConfig.GeneratedAssemblyName)) { foreach (var file in includedFiles) { if (!File.Exists(file.Path)) { throw new FileNotFoundException("", file.Path); } } } // Loading modules ConcurrentDictionary <Module, XmlModule> modules = new ConcurrentDictionary <Module, XmlModule>(); ConcurrentDictionary <XmlModule, Symbol[]> globalTypesPerModule = new ConcurrentDictionary <XmlModule, Symbol[]>(); logger.Write("Loading modules..."); Parallel.ForEach(xmlModules, (xmlModule) => { Module module = Module.Open(xmlModule); modules.TryAdd(module, xmlModule); }); logger.WriteLine(" {0}", sw.Elapsed); // Enumerating symbols logger.Write("Enumerating symbols..."); Parallel.ForEach(modules, (mm) => { XmlModule xmlModule = mm.Value; Module module = mm.Key; string moduleName = xmlModule.Name; string nameSpace = xmlModule.Namespace; List <Symbol> symbols = new List <Symbol>(); foreach (var type in typeNames) { Symbol[] foundSymbols = module.FindGlobalTypeWildcard(type.NameWildcard); if (foundSymbols.Length == 0) { errorLogger.WriteLine("Symbol not found: {0}", type.Name); } else { symbols.AddRange(foundSymbols); } } symbols.AddRange(module.GetAllTypes()); globalTypesPerModule.TryAdd(xmlModule, symbols.ToArray()); }); List <Symbol> allSymbols = new List <Symbol>(); Symbol[][] symbolsPerModule = globalTypesPerModule.Select(ss => ss.Value).ToArray(); int maxSymbols = symbolsPerModule.Max(ss => ss.Length); for (int i = 0; i < maxSymbols; i++) { for (int j = 0; j < symbolsPerModule.Length; j++) { if (i < symbolsPerModule[j].Length) { allSymbols.Add(symbolsPerModule[j][i]); } } } logger.WriteLine(" {0}", sw.Elapsed); #if false // Initialize symbol fields and base classes logger.Write("Initializing symbol values..."); Parallel.ForEach(Partitioner.Create(allSymbols), (symbol) => { var fields = symbol.Fields; var baseClasses = symbol.BaseClasses; }); logger.WriteLine(" {0}", sw.Elapsed); #endif logger.Write("Deduplicating symbols..."); // Group duplicated symbols Dictionary <string, List <Symbol> > symbolsByName = new Dictionary <string, List <Symbol> >(); Dictionary <Symbol, List <Symbol> > duplicatedSymbols = new Dictionary <Symbol, List <Symbol> >(); foreach (var symbol in allSymbols) { List <Symbol> symbols; if (!symbolsByName.TryGetValue(symbol.Name, out symbols)) { symbolsByName.Add(symbol.Name, symbols = new List <Symbol>()); } bool found = false; foreach (var s in symbols.ToArray()) { if (s.Size != 0 && symbol.Size != 0 && s.Size != symbol.Size) { #if DEBUG logger.WriteLine("{0}!{1} ({2}) {3}!{4} ({5})", s.Module.Name, s.Name, s.Size, symbol.Module.Name, symbol.Name, symbol.Size); #endif continue; } if (s.Size == 0 && symbol.Size != 0) { List <Symbol> duplicates; if (!duplicatedSymbols.TryGetValue(s, out duplicates)) { duplicatedSymbols.Add(s, duplicates = new List <Symbol>()); } duplicatedSymbols.Remove(s); duplicates.Add(s); duplicatedSymbols.Add(symbol, duplicates); symbols.Remove(s); symbols.Add(symbol); } else { List <Symbol> duplicates; if (!duplicatedSymbols.TryGetValue(s, out duplicates)) { duplicatedSymbols.Add(s, duplicates = new List <Symbol>()); } duplicates.Add(symbol); } found = true; break; } if (!found) { symbols.Add(symbol); } } // Unlink duplicated symbols if two or more are named the same foreach (var symbols in symbolsByName.Values) { if (symbols.Count <= 1) { continue; } foreach (var s in symbols.ToArray()) { List <Symbol> duplicates; if (!duplicatedSymbols.TryGetValue(s, out duplicates)) { continue; } symbols.AddRange(duplicates); duplicatedSymbols.Remove(s); } } // Extracting deduplicated symbols Dictionary <string, Symbol[]> deduplicatedSymbols = new Dictionary <string, Symbol[]>(); Dictionary <Symbol, string> symbolNamespaces = new Dictionary <Symbol, string>(); foreach (var symbols in symbolsByName.Values) { if (symbols.Count != 1) { foreach (var s in symbols) { symbolNamespaces.Add(s, modules[s.Module].Namespace); } } else { Symbol symbol = symbols.First(); List <Symbol> duplicates; if (!duplicatedSymbols.TryGetValue(symbol, out duplicates)) { duplicates = new List <Symbol>(); } duplicates.Insert(0, symbol); deduplicatedSymbols.Add(symbol.Name, duplicates.ToArray()); foreach (var s in duplicates) { symbolNamespaces.Add(s, xmlConfig.CommonTypesNamespace); } } } var globalTypes = symbolsByName.SelectMany(s => s.Value).ToArray(); logger.WriteLine(" {0}", sw.Elapsed); logger.WriteLine(" Total symbols: {0}", globalTypesPerModule.Sum(gt => gt.Value.Length)); logger.WriteLine(" Unique symbol names: {0}", symbolsByName.Count); logger.WriteLine(" Dedupedlicated symbols: {0}", globalTypes.Length); // Initialize GlobalCache with deduplicatedSymbols GlobalCache.Update(deduplicatedSymbols); // Collecting types logger.Write("Collecting types..."); var factory = new UserTypeFactory(xmlConfig.Transformations); List <UserType> userTypes = new List <UserType>(); foreach (var module in modules.Keys) { userTypes.Add(factory.AddSymbol(module.GlobalScope, new XmlType() { Name = "ModuleGlobals" }, modules[module].Namespace, generationOptions)); } ConcurrentBag <Symbol> simpleSymbols = new ConcurrentBag <Symbol>(); Dictionary <Tuple <string, string>, List <Symbol> > templateSymbols = new Dictionary <Tuple <string, string>, List <Symbol> >(); Parallel.ForEach(Partitioner.Create(globalTypes), (symbol) => { string symbolName = symbol.Name; // TODO: Add configurable filter // if (symbolName.StartsWith("$") || symbolName.StartsWith("__vc_attributes") || symbolName.Contains("`anonymous-namespace'") || symbolName.Contains("`anonymous namespace'") || symbolName.Contains("::$") || symbolName.Contains("`")) { return; } // Do not handle template referenced arguments if (symbolName.Contains("&")) { // TODO: Convert this to function pointer return; } // TODO: C# doesn't support lengthy names if (symbolName.Length > 160) { return; } // TODO: For now remove all unnamed-type symbols string scopedClassName = symbol.Namespaces.Last(); if (scopedClassName.StartsWith("<") || symbolName.Contains("::<")) { return; } // Check if symbol contains template type. if (SymbolNameHelper.ContainsTemplateType(symbolName)) { List <string> namespaces = symbol.Namespaces; string className = namespaces.Last(); var symbolId = Tuple.Create(symbolNamespaces[symbol], SymbolNameHelper.CreateLookupNameForSymbol(symbol)); lock (templateSymbols) { if (templateSymbols.ContainsKey(symbolId) == false) { templateSymbols[symbolId] = new List <Symbol>() { symbol } } ; else { templateSymbols[symbolId].Add(symbol); } } // TODO: // Do not add physical types for template specialization (not now) // do if types contains static fields // nested in templates } else { simpleSymbols.Add(symbol); } }); logger.WriteLine(" {0}", sw.Elapsed); // Populate Templates logger.Write("Populating templates..."); foreach (List <Symbol> symbols in templateSymbols.Values) { Symbol symbol = symbols.First(); string symbolName = SymbolNameHelper.CreateLookupNameForSymbol(symbol); XmlType type = new XmlType() { Name = symbolName }; userTypes.AddRange(factory.AddSymbols(symbols, type, symbolNamespaces[symbol], generationOptions)); } logger.WriteLine(" {0}", sw.Elapsed); // Specialized class logger.Write("Populating specialized classes..."); foreach (Symbol symbol in simpleSymbols) { userTypes.Add(factory.AddSymbol(symbol, null, symbolNamespaces[symbol], generationOptions)); } logger.WriteLine(" {0}", sw.Elapsed); // To solve template dependencies. Update specialization arguments once all the templates has been populated. logger.Write("Updating template arguments..."); foreach (TemplateUserType templateUserType in userTypes.OfType <TemplateUserType>()) { foreach (TemplateUserType specializedTemplateUserType in templateUserType.SpecializedTypes) { if (!specializedTemplateUserType.UpdateTemplateArguments(factory)) { #if DEBUG logger.WriteLine("Template user type cannot be updated: {0}", specializedTemplateUserType.Symbol.Name); #endif } } } logger.WriteLine(" {0}", sw.Elapsed); // Post processing user types (filling DeclaredInType) logger.Write("Post processing user types..."); var namespaceTypes = factory.ProcessTypes(userTypes, symbolNamespaces).ToArray(); userTypes.AddRange(namespaceTypes); logger.WriteLine(" {0}", sw.Elapsed); // Code generation and saving it to disk logger.Write("Saving code to disk..."); if (!generationOptions.HasFlag(UserTypeGenerationFlags.SingleFileExport)) { // Generate Code Parallel.ForEach(userTypes, (symbolEntry) => { Tuple <string, string> result = GenerateCode(symbolEntry, factory, outputDirectory, errorLogger, generationOptions, generatedFiles); string text = result.Item1; string filename = result.Item2; if (xmlConfig.GenerateAssemblyWithRoslyn && !string.IsNullOrEmpty(xmlConfig.GeneratedAssemblyName) && !string.IsNullOrEmpty(text)) { lock (syntaxTrees) { syntaxTrees.Add(CSharpSyntaxTree.ParseText(text, path: filename, encoding: System.Text.UTF8Encoding.Default)); } } }); } else { string filename = string.Format(@"{0}\everything.exported.cs", outputDirectory); HashSet <string> usings = new HashSet <string>(); foreach (var symbolEntry in userTypes) { foreach (var u in symbolEntry.Usings) { usings.Add(u); } } generatedFiles.TryAdd(filename.ToLowerInvariant(), filename); using (StringWriter stringOutput = new StringWriter()) using (TextWriter masterOutput = !xmlConfig.DontSaveGeneratedCodeFiles ? new StreamWriter(filename, false /* append */, System.Text.Encoding.UTF8, 16 * 1024 * 1024) : TextWriter.Null) { foreach (var u in usings.OrderBy(s => s)) { masterOutput.WriteLine("using {0};", u); if (xmlConfig.GenerateAssemblyWithRoslyn && !string.IsNullOrEmpty(xmlConfig.GeneratedAssemblyName)) { stringOutput.WriteLine("using {0};", u); } } masterOutput.WriteLine(); if (xmlConfig.GenerateAssemblyWithRoslyn) { stringOutput.WriteLine(); } ObjectPool <StringWriter> stringWriterPool = new ObjectPool <StringWriter>(() => new StringWriter()); Parallel.ForEach(userTypes, (symbolEntry) => { var output = stringWriterPool.GetObject(); output.GetStringBuilder().Clear(); GenerateCodeInSingleFile(output, symbolEntry, factory, errorLogger, generationOptions); string text = output.ToString(); if (!string.IsNullOrEmpty(text)) { lock (masterOutput) { masterOutput.WriteLine(text); if (xmlConfig.GenerateAssemblyWithRoslyn && !string.IsNullOrEmpty(xmlConfig.GeneratedAssemblyName)) { stringOutput.WriteLine(text); } } } stringWriterPool.PutObject(output); }); if (xmlConfig.GenerateAssemblyWithRoslyn && !string.IsNullOrEmpty(xmlConfig.GeneratedAssemblyName)) { syntaxTrees.Add(CSharpSyntaxTree.ParseText(stringOutput.ToString(), path: filename, encoding: UTF8Encoding.Default)); } } } logger.WriteLine(" {0}", sw.Elapsed); // Compiling the code string binFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); if (xmlConfig.GenerateAssemblyWithRoslyn && !string.IsNullOrEmpty(xmlConfig.GeneratedAssemblyName)) { List <MetadataReference> references = new List <MetadataReference> { MetadataReference.CreateFromFile(typeof(object).Assembly.Location), MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location) }; references.AddRange(xmlConfig.ReferencedAssemblies.Select(r => MetadataReference.CreateFromFile(r.Path))); foreach (var includedFile in includedFiles) { syntaxTrees.Add(CSharpSyntaxTree.ParseText(File.ReadAllText(includedFile.Path), path: includedFile.Path, encoding: System.Text.UTF8Encoding.Default)); } CSharpCompilation compilation = CSharpCompilation.Create( Path.GetFileNameWithoutExtension(xmlConfig.GeneratedAssemblyName), syntaxTrees: syntaxTrees, references: references, options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, platform: Platform.X64)); logger.WriteLine("Syntax trees: {0}", syntaxTrees.Count); string dllFilename = Path.Combine(outputDirectory, xmlConfig.GeneratedAssemblyName); string pdbFilename = Path.Combine(outputDirectory, Path.GetFileNameWithoutExtension(dllFilename) + ".pdb"); using (var dllStream = new FileStream(dllFilename, FileMode.Create)) using (var pdbStream = new FileStream(pdbFilename, FileMode.Create)) { var result = compilation.Emit(dllStream, !xmlConfig.DisablePdbGeneration ? pdbStream : null); if (!result.Success) { IEnumerable <Diagnostic> failures = result.Diagnostics.Where(diagnostic => diagnostic.IsWarningAsError || diagnostic.Severity == DiagnosticSeverity.Error); errorLogger.WriteLine("Compile errors (top 1000):"); foreach (var diagnostic in failures.Take(1000)) { errorLogger.WriteLine(diagnostic); } } else { logger.WriteLine("DLL size: {0}", dllStream.Position); logger.WriteLine("PDB size: {0}", pdbStream.Position); } } logger.WriteLine("Compiling: {0}", sw.Elapsed); } // Check whether we should generate assembly if (!xmlConfig.GenerateAssemblyWithRoslyn && !string.IsNullOrEmpty(xmlConfig.GeneratedAssemblyName)) { var codeProvider = new CSharpCodeProvider(); var compilerParameters = new CompilerParameters() { IncludeDebugInformation = !xmlConfig.DisablePdbGeneration, OutputAssembly = outputDirectory + xmlConfig.GeneratedAssemblyName, }; compilerParameters.ReferencedAssemblies.AddRange(AppDomain.CurrentDomain.GetAssemblies().Where(a => !a.IsDynamic).Select(a => a.Location).ToArray()); //compilerParameters.ReferencedAssemblies.AddRange(referencedAssemblies); const string MicrosoftCSharpDll = "Microsoft.CSharp.dll"; if (!compilerParameters.ReferencedAssemblies.Cast <string>().Where(a => a.Contains(MicrosoftCSharpDll)).Any()) { compilerParameters.ReferencedAssemblies.Add(MicrosoftCSharpDll); } compilerParameters.ReferencedAssemblies.Add(Path.Combine(binFolder, "CsDebugScript.Engine.dll")); compilerParameters.ReferencedAssemblies.Add(Path.Combine(binFolder, "CsDebugScript.CommonUserTypes.dll")); var filesToCompile = generatedFiles.Values.Union(includedFiles.Select(f => f.Path)).ToArray(); var compileResult = codeProvider.CompileAssemblyFromFile(compilerParameters, filesToCompile); if (compileResult.Errors.Count > 0) { errorLogger.WriteLine("Compile errors (top 1000):"); foreach (CompilerError err in compileResult.Errors.Cast <CompilerError>().Take(1000)) { errorLogger.WriteLine(err); } } logger.WriteLine("Compiling: {0}", sw.Elapsed); } // Generating props file if (!string.IsNullOrEmpty(xmlConfig.GeneratedPropsFileName)) { using (TextWriter output = new StreamWriter(outputDirectory + xmlConfig.GeneratedPropsFileName, false /* append */, System.Text.Encoding.UTF8, 16 * 1024 * 1024)) { output.WriteLine(@"<?xml version=""1.0"" encoding=""utf-8""?>"); output.WriteLine(@"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"); output.WriteLine(@" <ItemGroup>"); foreach (var file in generatedFiles.Values) { output.WriteLine(@" <Compile Include=""{0}"" />", file); } output.WriteLine(@" </ItemGroup>"); output.WriteLine(@"</Project>"); } } logger.WriteLine("Total time: {0}", sw.Elapsed); }
/// <summary> /// Initializes a new instance of the <see cref="SingleClassInheritanceWithInterfacesTypeInstance"/> class. /// </summary> /// <param name="baseClassUserType">The base class user type.</param> /// <param name="factory">The user type factory.</param> public SingleClassInheritanceWithInterfacesTypeInstance(UserType baseClassUserType, UserTypeFactory factory) : base(factory.CodeNaming) { BaseClassUserType = UserTypeInstance.Create(baseClassUserType, factory); }
/// <summary> /// Generates the code for user type in single file. /// </summary> /// <param name="output">The output text writer.</param> /// <param name="userType">The user type.</param> /// <param name="factory">The user type factory.</param> /// <param name="errorOutput">The error output.</param> /// <param name="generationFlags">The user type generation flags.</param> /// <returns><c>true</c> if code was generated for the type; otherwise <c>false</c></returns> private static bool GenerateCodeInSingleFile(TextWriter output, UserType userType, UserTypeFactory factory, TextWriter errorOutput, UserTypeGenerationFlags generationFlags) { Symbol symbol = userType.Symbol; if (symbol != null && symbol.Tag == CodeTypeTag.BuiltinType) { // Ignore built-in types. return(false); } if (userType.DeclaredInType != null) { return(false); } userType.WriteCode(new IndentedWriter(output, generationFlags.HasFlag(UserTypeGenerationFlags.CompressedOutput)), errorOutput, factory, generationFlags); return(true); }
/// <summary> /// Initializes a new instance of the <see cref="SingleClassInheritanceWithInterfacesTypeTree"/> class. /// </summary> /// <param name="baseClassUserType">The base class user type.</param> /// <param name="factory">The user type factory.</param> public SingleClassInheritanceWithInterfacesTypeTree(UserType baseClassUserType, UserTypeFactory factory) { BaseClassUserType = UserTypeTree.Create(baseClassUserType, factory); }
/// <summary> /// Initializes a new instance of the <see cref="TemplateUserTypeFactory"/> class. /// </summary> /// <param name="originalFactory">The original user type factory.</param> /// <param name="templateType">The template user type.</param> public TemplateUserTypeFactory(UserTypeFactory originalFactory, TemplateUserType templateType) : base(originalFactory) { TemplateType = templateType; OriginalFactory = originalFactory; }
private async Task SetAuthenticationToken() { UserType userType = UserTypeFactory.Default(); UserType admin = UserTypeFactory.Default().WithName("Admin"); City city = CityFactory.Default(); await ExecuteDatabaseAction(async (doFestContext) => { await doFestContext.UserTypes.AddAsync(userType); await doFestContext.UserTypes.AddAsync(admin); await doFestContext.Cities.AddAsync(city); await doFestContext.SaveChangesAsync(); CityId = city.Id; }); var userRegisterModel = new RegisterModel { Username = "******", Age = 20, BucketListName = "test bucketlist", City = city.Id, Email = "*****@*****.**", Name = "testtest", Password = "******", Year = 3 }; var userRegisterResponse = await HttpClient.PostAsJsonAsync($"api/v1/auth/register", userRegisterModel); userRegisterResponse.IsSuccessStatusCode.Should().BeTrue(); if (_isAdmin == true) { var userRespository = new UserRepository(dbContext); var userTypeRepository = new UserTypeRepository(userTypeDbContext); var userTypeAdmin = await userTypeRepository.GetByName("Admin"); var user = await userRespository.GetByEmail(userRegisterModel.Email); user.UserTypeId = userTypeAdmin.Id; userRespository.Update(user); await userRespository.SaveChanges(); } var authenticateModel = new LoginModelRequest { Email = userRegisterModel.Email, Password = userRegisterModel.Password }; var userAuthenticateResponse = await HttpClient.PostAsJsonAsync($"api/v1/auth/login", authenticateModel); userAuthenticateResponse.IsSuccessStatusCode.Should().BeTrue(); AuthenticatedUserId = new Guid(); var authenticationResponseContent = await userAuthenticateResponse.Content.ReadAsAsync <LoginModelResponse>(); var stream = authenticationResponseContent.Token; var handler = new JwtSecurityTokenHandler(); var tokenS = handler.ReadToken(stream) as JwtSecurityToken; AuthenticatedUserId = new Guid(tokenS.Claims.First(x => x.Type == "userId").Value); AuthenticationToken = authenticationResponseContent.Token; }