/// <summary> /// Generate /// </summary> /// <param name="simpleSchemaJson"></param> /// <returns></returns> public SchemaResult GenerateDao(string simpleSchemaJson, DirectoryInfo compileTo = null, bool keepSource = false, string tempDir = ".\\tmp", string partialsDir = null) { try { bool compile = compileTo != null; SchemaResult result = new SchemaResult("Generation completed"); dynamic rehydrated = JsonConvert.DeserializeObject <dynamic>(simpleSchemaJson); if (rehydrated["nameSpace"] == null)// || rehydrated["schemaName"] == null) { result.ExceptionMessage = "Please specify nameSpace"; result.Message = string.Empty; result.Success = false; } else if (rehydrated["schemaName"] == null) { result.ExceptionMessage = "Please specify schemaName"; result.Message = string.Empty; result.Success = false; } else { string nameSpace = (string)rehydrated["nameSpace"]; string schemaName = (string)rehydrated["schemaName"]; result.Namespace = nameSpace; result.SchemaName = schemaName; List <dynamic> foreignKeys = new List <dynamic>(); SetSchema(schemaName, false); ProcessTables(rehydrated, foreignKeys); ProcessXrefs(rehydrated, foreignKeys); foreach (dynamic fk in foreignKeys) { AddColumn(fk.ForeignKeyTable, new Column(fk.ReferencingColumn, DataTypes.Long)); SetForeignKey(fk.PrimaryTable, fk.ForeignKeyTable, fk.ReferencingColumn); } DirectoryInfo daoDir = new DirectoryInfo(tempDir); if (!daoDir.Exists) { daoDir.Create(); } DaoGenerator generator = GetDaoGenerator(compileTo, keepSource, partialsDir, compile, result, nameSpace, daoDir); generator.Generate(CurrentSchema, daoDir.FullName, partialsDir); result.DaoAssembly = generator.DaoAssemblyFile; } return(result); } catch (Exception ex) { SchemaResult r = new SchemaResult(ex.Message); r.StackTrace = ex.StackTrace ?? ""; r.Success = false; return(r); } }
private DaoGenerator GetDaoGenerator(DirectoryInfo compileTo, bool keepSource, string partialsDir, bool compile, SchemaResult result, string nameSpace, DirectoryInfo daoDir) { RazorBaseTemplate.DefaultInspector = (s) => { }; // turn off output to console DaoGenerator generator = new DaoGenerator(nameSpace); if (compile) { if (!compileTo.Exists) { compileTo.Create(); } generator.GenerateComplete += (gen, s) => { List <DirectoryInfo> daoDirs = new List <DirectoryInfo>(); daoDirs.Add(daoDir); if (!string.IsNullOrEmpty(partialsDir)) { daoDirs.Add(new DirectoryInfo(partialsDir)); } gen.DaoAssemblyFile = Compile(daoDirs.ToArray(), gen, nameSpace, compileTo); if (CompilerErrors != null) { result.Success = false; result.Message = string.Empty; foreach (CompilerError err in GetErrors()) { result.Message = string.Format("{0}\r\nFile=>{1}\r\n{2}:::Line {3}, Column {4}::{5}", result.Message, err.FileName, err.ErrorNumber, err.Line, err.Column, err.ErrorText); } } else { result.Message = string.Format("{0}\r\n{1}", result.Message, "Dao compiled"); result.Success = true; } if (!keepSource) { daoDir.Delete(true); daoDir.Refresh(); if (daoDir.Exists) { daoDir.Delete(); } } }; } return(generator); }
private FileInfo Compile(DirectoryInfo[] dirs, DaoGenerator generator, string nameSpace, DirectoryInfo copyTo) { string[] referenceAssemblies = DaoGenerator.DefaultReferenceAssemblies.ToArray(); for (int i = 0; i < referenceAssemblies.Length; i++) { string assembly = referenceAssemblies[i]; string binPath = Path.Combine(BinDir, assembly); referenceAssemblies[i] = File.Exists(binPath) ? binPath : assembly; } CompilerResults results = generator.Compile(dirs, string.Format("{0}.dll", nameSpace), referenceAssemblies); if (results.Errors.Count > 0) { CompilerErrors = results.Errors; return(null); } else { CompilerErrors = null; DirectoryInfo bin = new DirectoryInfo(BinDir); if (!bin.Exists) { bin.Create(); } FileInfo dll = new FileInfo(results.CompiledAssembly.CodeBase.Replace("file:///", "")); string binFile = Path.Combine(bin.FullName, dll.Name); string copy = Path.Combine(copyTo.FullName, dll.Name); if (File.Exists(binFile)) { BackupFile(binFile); } dll.CopyTo(binFile, true); if (!binFile.ToLowerInvariant().Equals(copy.ToLowerInvariant())) { if (File.Exists(copy)) { BackupFile(copy); } dll.CopyTo(copy); } return(new FileInfo(copy)); } }
private FileInfo Compile(DirectoryInfo dir, DaoGenerator generator, string nameSpace, DirectoryInfo copyTo) { return(Compile(new DirectoryInfo[] { dir }, generator, nameSpace, copyTo)); }