Ejemplo n.º 1
0
        private void DoBuild()
        {
            int finish = 0;
            int total  = listBox2.Items.Count;

            //遍历选中的表,一张表对应生成一个代码文件
            foreach (object item in listBox2.Items)
            {
                SOTable table     = item as SOTable;
                string  className = table.Name.RemovePrefix(tablePrefix, prefixLevel).Replace(" ", "");

                List <SOColumn> columnList = table.ColumnList;//可能传入的是从PDObject对象转换过来的SODatabase对象
                if (columnList == null || columnList.Count == 0)
                {
                    columnList = DbSchemaHelper.Instance.CurrentSchema.GetTableColumnList(table);
                }

                //生成代码文件
                TableHost host = new TableHost();
                host.Table        = table;
                host.ColumnList   = columnList;
                host.TemplateFile = templateFile;
                host.SetValue("NameSpace", nameSpace);
                host.SetValue("ClassName", className);
                host.SetValue("TablePrefix", tablePrefix);
                host.SetValue("ColumnPrefix", columnPrefix);
                host.SetValue("PrefixLevel", prefixLevel);

                Engine engine = new Engine();

                string outputContent = engine.ProcessTemplate(File.ReadAllText(templateFile), host);
                //string outputFile = Path.Combine(outputPath,string.Format("{0}.cs", className));
                string outputFile = Path.Combine(outputPath, string.Format("{0}{1}", className, host.FileExtention));

                StringBuilder sb = new StringBuilder();
                if (host.ErrorCollection.HasErrors)
                {
                    foreach (CompilerError err in host.ErrorCollection)
                    {
                        sb.AppendLine(err.ToString());
                    }
                    outputContent = outputContent + Environment.NewLine + sb.ToString();
                    outputFile    = outputFile + ".error";
                }

                if (Directory.Exists(outputPath) == false)
                {
                    Directory.CreateDirectory(outputPath);
                }
                File.WriteAllText(outputFile, outputContent, Encoding.UTF8);

                finish = finish + 1;
                int percent = ConvertUtil.ToInt32(finish * 100 / total, 0);

                backgroundWorker1.ReportProgress(percent, table);
            }//end build code foreach
        }
Ejemplo n.º 2
0
        private void BuildCodeByTableSchema(object item)
        {
            SOTable         table      = item as SOTable;
            List <SOColumn> columnList = table.ColumnList;//可能传入的是从PDObject对象转换过来的SODatabase对象

            if (columnList == null || columnList.Count == 0)
            {
                columnList = DbSchemaHelper.Instance.CurrentSchema.GetTableColumnList(table);
            }

            //生成代码文件
            TableHost host = new TableHost();

            host.Table        = table;
            host.ColumnList   = columnList;
            host.TemplateFile = templateFile;

            foreach (object obj in listBox3.Items)
            {
                string[] ss = obj.ToString().Split('|');

                host.SetValue(ss[0], ss[1].Replace("[<->]", "|"));
            }

            Engine engine = new Engine();

            string fileName  = string.Empty;
            string separator = txtFileNamePrefix.Text.Trim();

            if (separator != "")
            {
                fileName = string.Format("{0}{1}", table.Name.RemovePrefix(separator, 10), host.FileExtention);
            }
            else
            {
                fileName = string.Format("{0}{1}", table.Name, host.FileExtention);
            }

            string outputContent = engine.ProcessTemplate(File.ReadAllText(templateFile), host);
            string outputFile    = Path.Combine(outputPath, fileName);

            StringBuilder sb = new StringBuilder();

            if (host.ErrorCollection.HasErrors)
            {
                foreach (CompilerError err in host.ErrorCollection)
                {
                    sb.AppendLine(err.ToString());
                }
                outputContent = outputContent + Environment.NewLine + sb.ToString();
                outputFile    = outputFile + ".error";
            }

            if (Directory.Exists(outputPath) == false)
            {
                Directory.CreateDirectory(outputPath);
            }
            File.WriteAllText(outputFile, outputContent, Encoding.UTF8);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 生成一个表
        /// </summary>
        /// <param name="table">表对象</param>
        /// <param name="templateItem">模板列表</param>
        /// <param name="savedFileName">保存到的文件名</param>
        /// <param name="paramData">参数数据</param>
        private String BuildTable(SOTable table, TemplateInfo templateItem, out String savedFileName)
        {
            List <SOColumn> columnList = table.ColumnList;//可能传入的是从PDObject对象转换过来的SODatabase对象

            //生成代码文件
            TableHost host = new TableHost(this.NameRuleConfig, this.TypeMapConfigList, table);

            host.Table        = table;
            host.ColumnList   = columnList;
            host.TemplateFile = templateItem.FilePath;

            // 额外参数追加
            if (this.ParamList != null && this.ParamList.Count > 0)
            {
                foreach (var item in this.ParamList)
                {
                    host.SetValue(item.ParamName, item.ParamValue);
                }
            }

            Engine engine          = new Engine();
            string templateContent = File.ReadAllText(templateItem.FilePath);
            var    outputContent   = engine.ProcessTemplate(templateContent, host);

            savedFileName = Path.Combine(this.SavePath, string.Format("{0}{1}", table.Name, host.FileExtention));
            if (String.IsNullOrWhiteSpace(host.OutputFileName) == false)
            {
                savedFileName = Path.Combine(this.SavePath, host.OutputFileName);
            }

            StringBuilder sb = new StringBuilder();

            if (host.ErrorCollection != null && host.ErrorCollection.HasErrors)
            {
                foreach (CompilerError err in host.ErrorCollection)
                {
                    sb.AppendLine(err.ToString());
                }

                return(sb.ToString());
            }

            if (Directory.Exists(this.SavePath) == false)
            {
                Directory.CreateDirectory(this.SavePath);
            }

            File.WriteAllText(savedFileName, outputContent, host.FileEncoding);

            return(String.Empty);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 生成
        /// </summary>
        private string Generate(string templateFile, string toDic)
        {
            string result = string.Empty;

            try
            {
                //遍历选中的表,一张表对应生成一个代码文件
                foreach (object item in listBox2.Items)
                {
                    SOTable table     = item as SOTable;
                    string  className = table.Name;
                    if (cbDeleteTablePrifix.Checked)
                    {
                        className = table.Name.RemovePrefix(tablePrefix, prefixLevel).Replace(" ", "");
                    }
                    if (cbClassNamePascal.Checked)
                    {
                        className = className.InitialToUpperMulti();
                    }
                    if (cbClassNameRemovePlural.Checked)
                    {
                        className = className.EndsWith("s") ? className.TrimEnd('s') : className.Trim();
                    }
                    if (cbAddSuffix.Checked)
                    {
                        className = txtClassPrefix.Text.Trim() + className + txtClassSuffix.Text.Trim();
                    }

                    List <SOColumn> columnList = table.ColumnList;//可能传入的是从PDObject对象转换过来的SODatabase对象
                    if (columnList == null || columnList.Count == 0)
                    {
                        columnList = DbSchemaHelper.Instance.CurrentSchema.GetTableColumnList(table);
                    }

                    //生成代码文件
                    TableHost host = new TableHost();
                    host.Table        = table;
                    host.ColumnList   = columnList;
                    host.TemplateFile = templateFile;
                    host.SetValue("NameSpace", nameSpace);
                    host.SetValue("ClassName", className);
                    host.SetValue("TablePrefix", tablePrefix);
                    //host.SetValue("ColumnPrefix", columnPrefix);
                    host.SetValue("PrefixLevel", prefixLevel);

                    Engine engine           = new Engine();
                    string templateContent  = string.Empty;
                    var    templateFileInfo = new FileInfo(templateFile);
                    if (dicTemp.ContainsKey(templateFile))
                    {
                        templateContent = dicTemp[templateFile];
                    }
                    else
                    {
                        templateContent = File.ReadAllText(templateFile);
                        dicTemp.Add(templateFile, templateContent);
                    }

                    var outputContent = engine.ProcessTemplate(templateContent, host);
                    var extName       = templateFileInfo.Name.Replace(templateFileInfo.Extension, ""); //模板名称

                    var    fileNameFormat = new StringBuilder("{0}");
                    string outputFile     = string.Empty;

                    if (cbTemplateName.Checked || cbClassNameIsFileName.Checked)
                    {
                        if (cbClassNameIsFileName.Checked && cbTemplateName.Checked)
                        {
                            outputFile = Path.Combine(toDic, string.Format("{0}{1}{2}", className, extName, host.FileExtention)); //类名和模板名作为文件名
                        }
                        else
                        {
                            if (cbClassNameIsFileName.Checked)
                            {
                                outputFile = Path.Combine(toDic, string.Format("{0}{1}", className, host.FileExtention)); //类名作为文件名
                            }
                            else if (cbTemplateName.Checked)
                            {
                                outputFile = Path.Combine(toDic, string.Format("{0}{1}", extName, host.FileExtention)); //模板名作为文件名
                            }
                        }
                    }
                    else
                    {
                        outputFile = Path.Combine(toDic, string.Format("{0}{1}", table.Name, host.FileExtention)); //表名作为文件名
                    }


                    StringBuilder sb = new StringBuilder();
                    if (host.ErrorCollection != null && host.ErrorCollection.HasErrors)
                    {
                        foreach (CompilerError err in host.ErrorCollection)
                        {
                            sb.AppendLine(err.ToString());
                        }
                        outputContent = outputContent + Environment.NewLine + sb.ToString();
                        outputFile    = outputFile + ".error";
                    }

                    if (Directory.Exists(toDic) == false)
                    {
                        Directory.CreateDirectory(toDic);
                    }
                    File.WriteAllText(outputFile, outputContent, Encoding.UTF8);
                    result = table.Name + "生成成功";
                    Config.Console(string.Format("根据模板文件“{0}”生成“{1}”成功!", templateFile, outputFile));
                    percent += 1;

                    backgroundWorkerGenerate.ReportProgress(percent, table.Name);
                }
            }
            catch (Exception ex)
            {
                Config.ConsoleException(ex);
                result = ex.Message;
                this.backgroundWorkerGenerate.CancelAsync(); //中止
            }

            return(result);
        }
Ejemplo n.º 5
0
        private void DoBuild()
        {
            int finish = 0;
            int total  = listBox2.Items.Count;

            //遍历选中的表,一张表对应生成一个代码文件
            foreach (object item in listBox2.Items)
            {
                SOTable table     = item as SOTable;
                string  className = table.Name;
                if (cbDeleteTablePrifix.Checked)
                {
                    className = table.Name.RemovePrefix(tablePrefix, prefixLevel).Replace(" ", "");
                }
                if (cbClassNamePascal.Checked)
                {
                    className = className.InitialToUpperMulti();
                }
                if (cbClassNameRemovePlural.Checked)
                {
                    className = className.EndsWith("s") ? className.TrimEnd('s') : className.Trim();
                }
                if (cbAddSuffix.Checked)
                {
                    className = txtClassPrefix.Text.Trim() + className + txtClassSuffix.Text.Trim();
                }

                templateFile = gbTemplateFile.Text;

                List <SOColumn> columnList = table.ColumnList;//可能传入的是从PDObject对象转换过来的SODatabase对象
                if (columnList == null || columnList.Count == 0)
                {
                    columnList = DbSchemaHelper.Instance.CurrentSchema.GetTableColumnList(table);
                }

                //生成代码文件
                TableHost host = new TableHost();
                host.Table        = table;
                host.ColumnList   = columnList;
                host.TemplateFile = templateFile;
                host.SetValue("NameSpace", nameSpace);
                host.SetValue("ClassName", className);
                host.SetValue("TablePrefix", tablePrefix);
                //host.SetValue("ColumnPrefix", columnPrefix);
                host.SetValue("PrefixLevel", prefixLevel);

                Engine engine          = new Engine();
                string templateContent = string.Empty;
                if (dicTemp.ContainsKey(templateFile))
                {
                    templateContent = dicTemp[templateFile];
                }
                else
                {
                    templateContent = File.ReadAllText(templateFile);
                    dicTemp.Add(templateFile, templateContent);
                }

                var outputContent = engine.ProcessTemplate(templateContent, host);
                //string outputFile = Path.Combine(outputPath, string.Format("{0}.cs", className));
                string outputFile = Path.Combine(outputPath, string.Format("{0}{1}", table.Name, host.FileExtention));
                if (cbClassNameIsFileName.Checked)
                {
                    outputFile = Path.Combine(outputPath, string.Format("{0}{1}", className, host.FileExtention));
                }

                StringBuilder sb = new StringBuilder();
                if (host.ErrorCollection != null && host.ErrorCollection.HasErrors)
                {
                    foreach (CompilerError err in host.ErrorCollection)
                    {
                        sb.AppendLine(err.ToString());
                    }
                    outputContent = outputContent + Environment.NewLine + sb.ToString();
                    outputFile    = outputFile + ".error";
                }

                if (Directory.Exists(outputPath) == false)
                {
                    Directory.CreateDirectory(outputPath);
                }
                File.WriteAllText(outputFile, outputContent, Encoding.UTF8);

                finish = finish + 1;
                int percent = ConvertUtil.ToInt32(finish * 100 / total, 0);

                backgroundWorker1.ReportProgress(percent, table);
            }//end build code foreach
        }