コード例 #1
0
ファイル: TemplateHost.cs プロジェクト: faib920/codebuilder
        public TemplateHost(string path, dynamic tables, dynamic references, List <string> assemblyList, GuidDispatcher guids)
        {
            this.path  = path;
            Tables     = tables;
            References = references;
            Guids      = guids;

            Initialize();

            if (assemblyList != null)
            {
                assemblyLocationList.AddRange(assemblyList);
            }
        }
コード例 #2
0
        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);
        }