/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnGenerateCode_Click(object sender, RoutedEventArgs e) { if (!this.DocumentHost.ContainsActiveDocument) { MessageBox.Show("Please open a template document."); return; } ucBaseDocument doc = ((ucBaseDocument)this.DocumentHost.SelectedItem); if (doc.DocType == DocumentType.CodeDocument) { MessageBox.Show("Please select a template document."); return; } TextTemplatingEngineHost host = new TextTemplatingEngineHost(); Engine engine = new Engine(); host.Session = new TextTemplatingSession(); host.TemplateFileValue = doc.DocPath == null ? string.Empty : doc.DocPath.ToString(); TextEditor tbox = (TextEditor)doc.Content; string inputTemplate = tbox == null ?string.Empty:tbox.Text;//.Replace("System.Windows.Controls.TextBox: ",string.Empty); List <CustomProperty> cpList = doc.GetParamters(); foreach (CustomProperty cp in cpList) { Parameter par = new Parameter() { Text = cp.Name.Trim(), Value = cp.Value.ToString() }; host.Session.Add(cp.Name.Trim(), par); } string strCodes = engine.ProcessTemplate(inputTemplate, host); string docExt = "." + GetOutputExtension(inputTemplate); CustomProperty tncp = doc.GetProperty("TableName"); string tname = tncp == null ? string.Empty : tncp.Value.ToString(); string dtitle = doc.Title.Substring(4, doc.Title.Length - 7); ucBaseDocument codedoc = new ucBaseDocument() { Title = tname + dtitle == string.Empty ? string.Format("Doc_{0}", DateTime.Now.ToString("yyyyMMdd")) : tname + dtitle + docExt, Content = new TextEditor() { IsReadOnly = false, SyntaxHighlighting = HighlightingManager.Instance.GetDefinition(ToolHelper.GetHighlightingByExtension(GetOutputExtension(inputTemplate).Trim())), Text = strCodes } }; codedoc.DocType = DocumentType.CodeDocument; codedoc.DocChanged = false; codedoc.DocExt = docExt; this.DocumentHost.Items.Add(codedoc); codedoc.Activate(); codedoc.LoadProperties(this.lucDocumentProperties); }
/// <summary> /// /// </summary> /// <param name="tempTreeNode"></param> private void BaseGenerate(PropertyNodeItem sn) { try { CreateTopDirectory(sn); string strTmp = GetNamespace(sn); TextTemplatingEngineHost host = new TextTemplatingEngineHost(); host.TemplateFileValue = AppDomain.CurrentDomain.BaseDirectory + @"\Templates\" + GetPath(sn); Engine engine = new Engine(); host.Session = new TextTemplatingSession(); string inputTemplate = File.ReadAllText(host.TemplateFileValue); Parameter connStrparameter = new Parameter() { Text = "ConnectionStr", Value = dbHelper.SqlConnectionString }; host.Session.Add("ConnectionStr", connStrparameter); Parameter nameSpaceParameter = new Parameter() { Text = "NameSpace", Value = this.txtNameSpaceRoot.Text + strTmp.Replace("Templates." + this.CurSelectedNode.DisplayName, string.Empty) }; host.Session.Add("NameSpace", nameSpaceParameter); Parameter tableNameParameter = new Parameter() { Text = "TableName", Value = string.Empty }; host.Session.Add("TableName", tableNameParameter); string strCodes = engine.ProcessTemplate(inputTemplate, host); string filefullname = this.txtExportPath.Text + @"\" + this.cboxDatabases.Text + @"\" + GetFolder(sn).Replace("Templates", string.Empty) + @"\" + sn.DisplayName.Trim().Replace(".tt", string.Empty) + "." + this.GetOutputExtension(inputTemplate); if (string.IsNullOrEmpty(filefullname)) { return; } FileStream fs; if (!File.Exists(filefullname)) { fs = new FileStream(filefullname, FileMode.Create); } else { fs = new FileStream(filefullname, FileMode.Open); } StreamWriter sw = new StreamWriter(fs); sw.WriteLine(strCodes); sw.Close(); fs.Close(); ++proCount; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
/// <summary> /// /// </summary> /// <param name="tempTreeNode"></param> private void BatchGenerate(PropertyNodeItem sn) { CreateTopDirectory(sn); string strTmp = GetNamespace(sn); for (int i = 0; i < this.listRightTables.Items.Count; i++) { try { TextTemplatingEngineHost host = new TextTemplatingEngineHost(); host.TemplateFileValue = AppDomain.CurrentDomain.BaseDirectory + @"\Templates\" + GetPath(sn); Engine engine = new Engine(); host.Session = new TextTemplatingSession(); string inputTemplate = File.ReadAllText(host.TemplateFileValue); string oName = this.listRightTables.Items[i].ToString(); Parameter connStrparameter = new Parameter() { Text = "ConnectionStr", Value = dbHelper.SqlConnectionString }; host.Session.Add("ConnectionStr", connStrparameter); Parameter nameSpaceParameter = new Parameter() { Text = "NameSpace", Value = this.txtNameSpaceRoot.Text }; //+ strTmp.Replace("Templates." + this.CurSelectedNode.DisplayName, string.Empty) host.Session.Add("NameSpace", nameSpaceParameter); Parameter tableNameParameter = new Parameter() { Text = "TableName", Value = oName.Substring(2) }; host.Session.Add("TableName", tableNameParameter); if ((oName.Substring(0, 1) == "T" && sn.DisplayName.Substring(2, 1) != "T" && sn.DisplayName.Substring(2, 1) != "A" && sn.DisplayName.Substring(2, 1) != "N") || (oName.Substring(0, 1) == "V" && sn.DisplayName.Substring(2, 1) != "V" && sn.DisplayName.Substring(2, 1) != "A" && sn.DisplayName.Substring(2, 1) != "N") ) { continue; } string strCodes = engine.ProcessTemplate(inputTemplate, host); string filefullname = string.Empty;// this.txtExportPath.Text + @"\" + this.cboxDatabases.Text + @"\" + GetFolder(sn).Replace("Templates", string.Empty) + @"\" + this.listRightTables.Items[i].ToString() + "." + this.GetOutputExtension(inputTemplate); if (sn.DisplayName.Substring(2, 1) == "N") { filefullname = this.txtExportPath.Text + @"\" + this.cboxDatabases.Text + @"\" + GetFolder(sn).Replace("Templates", string.Empty) + @"\" + sn.DisplayName.Substring(4, sn.DisplayName.Length - 7) + "." + this.GetOutputExtension(inputTemplate); } else { filefullname = this.txtExportPath.Text + @"\" + this.cboxDatabases.Text + @"\" + GetFolder(sn).Replace("Templates", string.Empty) + @"\" + this.listRightTables.Items[i].ToString().Substring(2) + sn.DisplayName.Substring(4, sn.DisplayName.Length - 7) + "." + this.GetOutputExtension(inputTemplate); } if (string.IsNullOrEmpty(filefullname)) { return; } FileStream fs; if (!File.Exists(filefullname)) { fs = new FileStream(filefullname, FileMode.Create); } else { fs = new FileStream(filefullname, FileMode.Open); } StreamWriter sw = new StreamWriter(fs); sw.WriteLine(strCodes); sw.Close(); fs.Close(); if (sn.DisplayName.Substring(0, 1) == "1") { break; } } catch (Exception ex) { MessageBox.Show(ex.Message); } } proCount += 1; if (this.pbarBatchGenerate.Value != pbarBatchGenerate.Maximum) { Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, proCount }); } }