예제 #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);
            }
        }
예제 #2
0
        //private static object oLock = new object();
        //private static bool HasGen = false;

        public string Generate(IDatabase database)
        {
            string outputdir = "";

            //if (HasGen == false)
            //{
            //    lock (oLock)
            //    {
            try
            {
                var config = database.Config;

                //ProjectConfig projectConfig = new ProjectConfig();
                //projectConfig.Enable = config.EnableCodeGen;
                //projectConfig.ClassNameMode = config.CodeGenClassNameMode;
                //projectConfig.PropertyNameMode = config.CodeGenPropertyNameMode;
                //projectConfig.Name = config.CodeGenProjectName;
                //projectConfig.NameSpace = config.CodeGenNameSpace;
                //projectConfig.TableFilter = config.CodeGenTableFilter;

                //foreach (var temp in config.CodeGenTemplates)
                //{
                //    GeneraterConfig genConfig = new GeneraterConfig();
                //    genConfig.Name = temp.Name;
                //    genConfig.OutputFileExtension = temp.OutputFileExtension;
                //    genConfig.FilePrefix = temp.FilePrefix;
                //    genConfig.FileSuffix = temp.FileSuffix;
                //    genConfig.FileNameFormat = temp.FileNameFormat;
                //    genConfig.Enabled = temp.Enabled;
                //    genConfig.TemplateFileName = temp.TemplateFileName;
                //    //genConfig.Template = temp.Template;
                //    genConfig.OutputDirectory = temp.OutputDirectory;
                //    genConfig.Append = temp.Append;
                //    genConfig.Encoding = temp.Encoding;
                //    genConfig.OutputType = temp.OutputType;

                //    projectConfig.GeneraterConfigs.Add(genConfig);


                //}


                var projectConfig = DbLoader.ConvertDatabaseConfigToProjectConfig(database);
                database.LogHelper.Write(projectConfig.ToString());

                var generater = GeneratorHelper.NewGenerator(database);

                generater.ClearCache(projectConfig);
                List <string> filterTables = new List <string>();
                if (config.AutoMigrateOnContainTable != null && config.AutoMigrateOnContainTable != "")
                {
                    filterTables = config.AutoMigrateOnContainTable.ToUpper().Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList();
                }


                List <string> withoutTables = new List <string>();
                if (database.Config.AutoMigrateWithoutTable != null && database.Config.AutoMigrateWithoutTable != "")
                {
                    withoutTables = database.Config.AutoMigrateWithoutTable.ToUpper().Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList();
                }

                generater.Run(database, projectConfig, filterTables, withoutTables);



                outputdir = DbLoader.GetProjectDirectory(projectConfig);
                if (System.IO.Directory.Exists(outputdir))
                {
                    return(outputdir);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("CodeGenEngine发生错误:" + ex, ex.InnerException);
            }

            outputdir = System.IO.Path.Combine(PathHelper.GetBaseDirectory(), "generate");
            return(outputdir);
        }