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); }
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 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}", 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.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 }
public void DoBuildCode() { textEditorControl1.SaveFile(gbTemplateFile.Text); TableHost host = new TableHost(); host.Table = this.Table; host.TemplateFile = gbTemplateFile.Text; List<SOColumn> columnList = new List<SOColumn>(); foreach (DataGridViewRow row in dataGridView1.Rows) { if (row.Cells[0].FormattedValue.ToString().ToLower() == "true") { SOColumn c = row.DataBoundItem as SOColumn; columnList.Add(c); } } host.ColumnList = columnList; Engine engine = new Engine(); string outputContent = engine.ProcessTemplate(File.ReadAllText(host.TemplateFile), host); StringBuilder sb = new StringBuilder(); if (host.ErrorCollection.HasErrors) { foreach (CompilerError err in host.ErrorCollection) { sb.AppendLine(err.ToString()); } outputContent = outputContent + Environment.NewLine + sb.ToString(); } textEditorControl2.Text = outputContent; tabControl1.SelectedTab = tabPage2; textEditorControl2.Refresh(); }