Esempio n. 1
0
        private void ParseAndOutputByTables(IDatabase database, ProjectConfig config, List <string> filterTables = null, List <string> withoutTables = null)
        {
            try
            {
                config.Database = database;
                List <Table> tables = null;


                string msg = "";
                tables = DbLoader.GetTableInfos(database, config, filterTables, withoutTables, out msg);
                OnLogReport(msg);



                OutputContext context = new OutputContext();

                config.LastOutputContext = context;

                context.Tables = tables;
                // context.Mappers = mapperDict;
                context.ProjectConfig = config;
                context.ParserConfig  = _parseConfig;
                var    enabledTemplates = config.GeneraterConfigs.Where(p => p.Enabled == true);
                string templateContent = "", templateKey = "", modelName = "", outputFileName = "", stepmsg = "", outputContent = "";

                //清空上一次的生成数据
                string projectDir = DbLoader.GetProjectDirectory(config);// config.Database.Config.CodeGenBaseDirectory;// DbLoader.GetProjectDirectory(config);

                try
                {
                    var files = Directory.GetFiles(projectDir, "", SearchOption.AllDirectories);
                    foreach (var item in files)
                    {
                        if (item != "")
                        {
                            System.IO.File.Delete(item);
                        }
                    }
                    var dirs = Directory.GetDirectories(projectDir);
                    foreach (var dir in dirs)
                    {
                        Directory.Delete(dir);
                    }
                }
                catch (Exception)
                {
                }

                OnLogReport("EnabledTemplates count:" + enabledTemplates.Count() + " -> (" + enabledTemplates.Select(p => p.Name).Join(", ") + ")");

                foreach (var template in enabledTemplates)
                {
                    //CheckCancellation();
                    try
                    {
                        templateContent = GetTemplate(template);
                        if (string.IsNullOrEmpty(templateContent))
                        {
                            database.LogHelper.Debug("TemplateContent is empty,skip at :" + template.TemplateFileName);
                            continue;
                            //throw new ArgumentException("模板内容不能为空!");
                        }
                        context.GeneraterConfig = template;
                        // result.FileName = template.TemplateFileName;
                        templateKey = DbLoader.GetTemplateKey(template.Name, template.TemplateFileName);
                        string reallyOutputFileName = "";

                        var outType = template.OutputType;
                        OnLogReport("Starting generate template:" + template.Name + " , outputType:" + template.OutputType.ToString() + " , templateKey:" + templateKey);

                        if (outType == OutputType.Table)
                        {
                            foreach (Table table in context.Tables)
                            {
                                try
                                {
                                    modelName              = table.ClassName;
                                    context.ModelName      = modelName;
                                    context.OutputFileName = DbLoader.FormatFileName(config, template, modelName, modelName);

                                    table.CurrentOutputContext = context;
                                    var Model = table;
                                    outputContent        = parser.Parse <Table>(templateContent, templateKey, Model);
                                    reallyOutputFileName = context.RealOutputFileName;
                                    parser.OutputResult(reallyOutputFileName, outputContent, template.Encoding, template.Append);


                                    //result.ParseResult.Add( parser.ParseAndOutput<Table>(outputFileName, templateContent, templateKey, table, template.Encoding, template.Append));
                                    stepmsg = "template = " + template.Name + " , table = " + table.Name + string.Format(" , Generate in Template {0}, Table Model {1} is OK : {2}", template.Name, table.Name, reallyOutputFileName);


                                    OnLogReport(stepmsg);
                                }
                                catch (Exception ex11)
                                {
                                    database.LogHelper.Error(ex11);

                                    stepmsg = "template = " + template.Name + " , table = " + table.Name + " , Error: " + ex11.Message;
                                    OnLogReport(stepmsg);
                                }
                            }
                        }
                        else if (outType == OutputType.OutputContext)
                        {
                            modelName = template.Name; //string.IsNullOrEmpty(context.Owner) ? context.ProjectConfig.Name : context.Owner;
                            context.OutputFileName = DbLoader.FormatFileName(config, template, modelName, modelName);
                            //outputFileName = context.RealOutputFileName;// DbLoader.GetOutputFileName(config, template, modelName);
                            outputContent = parser.Parse <OutputContext>(templateContent, templateKey, context);
                            parser.OutputResult(context.RealOutputFileName, outputContent, template.Encoding, template.Append);
                            //result.ParseResult.Add(outputContent);

                            //result.ParseResult.Add(parser.ParseAndOutput<OutputContext>(context.RealOutputFileName, templateContent, templateKey, context, template.Encoding, template.Append));

                            stepmsg = string.Format(" Template {0}, OutputContext Model {1} is OK", template.Name, modelName);
                            //result.StepResult.Add(stepmsg);
                            OnLogReport(stepmsg);
                        }



                        stepmsg = string.Format("  {0} End", template.Name);
                        OnLogReport(stepmsg);
                    }
                    catch (Exception ex)
                    {
                        //result.ErrorMessages.Add(ex.ToFriendlyString());
                        //result.ErrorMessages.Add(ErrorDetail.CreateByContent(ex.Message));
                        database.LogHelper.Error(ex);

                        stepmsg = "Error: " + ex.Message;
                        OnLogReport(stepmsg);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("[ParseAndOutputByTables Error]:" + ex + ex.StackTrace + ex.Source, ex.InnerException);
            }
        }