private List <GenerateResult> GenerateInternal(TemplateOption option, List <Table> tables, CodeGenerateHandler handler) { var result = new List <GenerateResult>(); var references = new List <Reference>(); foreach (var table in tables) { references.AddRange(table.ForeignKeys); } var tparts = option.Partitions.Where(s => s.Loop == PartitionLoop.Tables).ToList(); var nparts = option.Partitions.Where(s => s.Loop == PartitionLoop.None).ToList(); var engine = new VelocityEngine(); foreach (var table in tables) { var context = new VelocityContext(); context.Put("Tables", tables); context.Put("References", references); context.Put("Current", table); context.Put("Profile", option.Profile); foreach (var part in option.Partitions) { var props = new ExtendedProperties(); var info = new FileInfo(part.FilePath); props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file"); props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, info.DirectoryName); props.AddProperty(RuntimeConstants.COUNTER_INITIAL_VALUE, "0"); engine.Init(props); using (var writer = new StringWriter()) { engine.MergeTemplate(info.Name, "gb2312", context, writer); var r = new GenerateResult(part, writer.ToString()); if (option.WriteToDisk && r.WriteToDisk) { PartitionWriter.Write(r, table, option.Profile, option.OutputDirectory); } result.Add(r); } } } return(result); }
public List <GenerateResult> GenerateFiles(TemplateOption option, List <Table> tables, CodeGenerateHandler handler) { try { return(GenerateInternal(option, tables, handler)); } catch (Exception exp) { MessageBoxHelper.ShowError(exp.Message); return(null); } }
private List <GenerateResult> GenerateInternal(TemplateOption option, List <Table> tables, CodeGenerateHandler handler) { var result = new List <GenerateResult>(); var _tables = ProxyBuilder.Rebuild(tables); var _profile = ProxyBuilder.Rebuild(option.Profile); var _guids = new GuidDispatcher(); var _reference = new List <dynamic>(); foreach (var table in _tables) { _reference.AddRange(table.ForeignKeys); } var assemblyList = option.DynamicAssemblies; assemblyList.AddRange(ProxyBuilder.GetAssemblyList()); var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "templates\\T4"); var host = new TemplateHost(path, _tables, _reference, assemblyList, _guids); var engine = new Engine(); host.Profile = _profile; var tparts = option.Partitions.Where(s => s.Loop == PartitionLoop.Tables).ToList(); var nparts = option.Partitions.Where(s => s.Loop == PartitionLoop.None).ToList(); var count = tparts.Count * tables.Count + nparts.Count; var index = 0; var calc = new Func <int, int>(i => { return((int)((i / (count * 1.0)) * 100)); }); foreach (var table in _tables) { if (Processor.IsCancellationRequested()) { break; } foreach (var part in tparts) { if (Processor.IsCancellationRequested()) { break; } host.TemplateFile = part.FilePath; if (handler != null) { handler(table.Name, calc(++index)); } host.Current = table; var content = engine.ProcessTemplate(part.Content, host); var r = ProcessPartitionCodeFile(part, host, table, _profile, option, content); if (r != null) { result.Add(r); } } } foreach (var part in nparts) { if (Processor.IsCancellationRequested()) { break; } if (handler != null) { handler("全局", calc(++index)); } host.TemplateFile = part.FilePath; var content = engine.ProcessTemplate(part.Content, host); var r = ProcessPartitionCodeFile(part, host, null, _profile, option, content); if (r != null) { result.Add(r); } } if (option.WriteToDisk) { ResourceWriter.Write(option.Template, option.Profile, option.OutputDirectory); } return(result); }
private List <GenerateResult> GenerateInternal(TemplateOption option, List <Table> tables, CodeGenerateHandler handler) { var result = new List <GenerateResult>(); var references = new List <Reference>(); foreach (var table in tables) { references.AddRange(table.ForeignKeys); } var tparts = option.Partitions.Where(s => s.Loop == PartitionLoop.Tables).ToList(); var nparts = option.Partitions.Where(s => s.Loop == PartitionLoop.None).ToList(); var count = tparts.Count * tables.Count + nparts.Count; var index = 0; var calc = new Func <int, int>(i => { return((int)((i / (count * 1.0)) * 100)); }); InitializeNamespaces(); foreach (var table in tables) { var model = new { Tables = tables, References = references, Current = table, Profile = option.Profile }; if (Processor.IsCancellationRequested()) { break; } foreach (var part in tparts) { if (Processor.IsCancellationRequested()) { break; } if (handler != null) { handler(table.Name, calc(++index)); } var info = new FileInfo(part.FilePath); var content = ReplaceIncludeTemplate(info.DirectoryName, part.Content); var code = RazorEngine.Razor.Parse(content, model); var r = ProcessPartitionCodeFile(part, table, option.Profile, option, code); if (r != null) { result.Add(r); } } } foreach (var part in nparts) { if (Processor.IsCancellationRequested()) { break; } if (handler != null) { handler("全局", calc(++index)); } var model = new { Tables = tables, References = references, Profile = option.Profile }; var info = new FileInfo(part.FilePath); var content = ReplaceIncludeTemplate(info.DirectoryName, part.Content); var code = RazorEngine.Razor.Parse(content, model); var r = ProcessPartitionCodeFile(part, null, option.Profile, option, code); if (r != null) { result.Add(r); } } if (option.WriteToDisk) { ResourceWriter.Write(option.Template, option.OutputDirectory); } return(result); }
public List <GenerateResult> GenerateFiles(TemplateOption option, List <Table> tables, CodeGenerateHandler handler) { var scope = new SingleAppDomainScope(); try { return(GenerateInternal(option, tables, handler)); } catch (Exception exp) { MessageBoxHelper.ShowError(exp.Message); return(null); } finally { scope.Dispose(); } }