private void LoadProject() { if (_Project != null) { if (MessageBox.Show("载入新项目将覆盖目前的项目,是否继续?", "询问", MessageBoxButton.YesNo) == MessageBoxResult.No) { return; } } Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); dlg.Filter = "Xml文件|*.xml"; Nullable <bool> result = dlg.ShowDialog(); if (result == true) { projectPath = dlg.FileName; _Project = ZippyCoder.Entity.Project.Load(dlg.FileName); if (!string.IsNullOrEmpty(_Project.Namespace)) { UpdateUI(); projectPath = dlg.FileName; Title = _Project.Namespace; ShowInfo("项目载入成功。"); } else { MessageBox.Show("项目载入失败,请检查该文件的格式项目是否是正确。", "错误", MessageBoxButton.OK, MessageBoxImage.Error); ShowInfo("项目载入失败,请检查该文件的格式项目是否是正确。"); } } }
private void btnOk_Click(object sender, RoutedEventArgs e) { SaveSettings(); string outputPath = tbxPath.Text; if (System.IO.Directory.Exists(outputPath)) { if (MessageBox.Show("生成的代码将覆盖此目录的同名文件,是否继续?", "询问", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes) { return; } ZippyCoder.Entity.Project project = Tag; if (CodeMode == CodeModes.Project) { T4Engine.T4Creator.CreateCode(project, null, TTFile, project.Namespace, tbxNamePattern.Text, tbxFixDel.Text, outputPath, cbxSepDir.IsChecked.Value); } else if (CodeMode == CodeModes.Table) { tipCreate.Text = "正在载入 T4 模板..."; spProgress.Visibility = Visibility.Visible; pbCreate.Maximum = (project.Tables.Where(s => s.IsCoding)).Count(); pbCreate.Value = 0; double value = 0; foreach (ZippyCoder.Entity.Table table in project.Tables) { if (table.IsCoding) { T4Engine.T4Creator.CreateCode(project, table, TTFile, table.Name, tbxNamePattern.Text, tbxFixDel.Text, outputPath, cbxSepDir.IsChecked.Value); pbCreate.Dispatcher.Invoke(new Action <System.Windows.DependencyProperty, object>(pbCreate.SetValue), System.Windows.Threading.DispatcherPriority.Background, ProgressBar.ValueProperty, value); tipCreate.Dispatcher.Invoke(new Action <System.Windows.DependencyProperty, object>(tipCreate.SetValue), System.Windows.Threading.DispatcherPriority.Background, TextBlock.TextProperty, "正在执行生成:" + table.Title + "[" + table.Name + "]..."); value++; } } } if (T4Engine.T4Creator.T4Errors != null && T4Engine.T4Creator.T4Errors.Count > 0) { string res = string.Empty; foreach (System.CodeDom.Compiler.CompilerError error in T4Engine.T4Creator.T4Errors) { res += "\r\n" + "行:" + error.Line + " 列:" + error.Column + " ... " + error.ErrorText; } _Owner.ShowInfo(res); } else { _Owner.ShowInfo("生成成功。"); this.Visibility = Visibility.Collapsed; } } else { MessageBox.Show("此目录不存在。", "错误", MessageBoxButton.OK, MessageBoxImage.Error); } }
/// <summary> /// 生成代码 /// </summary> /// <param name="source">数据源</param> /// <param name="ttFile">tt模板路径</param> /// <param name="outputFileName">生成的文件的名称</param> /// <param name="fileNamePattern">需要替换的文件名</param> /// <param name="fileNamesDel">需要去掉的文件名前缀</param> /// <param name="outputPath">输出路径</param> /// <param name="nameSpace">命名空间</param> public static void CreateCode(ZippyCoder.Entity.Project project, ZippyCoder.Entity.Table table, string ttFile, string outputFileName, string fileNamePattern, string fileNamesDel, string outputPath, bool multiDir) { if (T4Errors != null) { T4Errors.Clear(); } ZippyT4Host host = new ZippyT4Host(); host.Project = project; host.Table = table; Engine engine = new Engine(); host.TemplateFile = ttFile; string input = File.ReadAllText(ttFile); string output = engine.ProcessTemplate(input, host); string[] fileNameDels = fileNamesDel.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); foreach (string fileNameDel in fileNameDels) { if (outputFileName.StartsWith(fileNameDel)) { outputFileName = outputFileName.Remove(0, fileNameDel.Length); } } //大写文件名,java 会敏感,与类对应。 outputFileName = ZippyCoder.Helper.ToJavaClassName(outputFileName); if (multiDir) { if (table != null) { outputPath = Path.Combine(outputPath, table.Name); if (!System.IO.Directory.Exists(outputPath)) { System.IO.Directory.CreateDirectory(outputPath); } } } outputFileName = string.Format(fileNamePattern, outputFileName); var utf8WithoutBom = new System.Text.UTF8Encoding(false); File.WriteAllText(Path.Combine(outputPath, outputFileName + host.FileExtension), output, utf8WithoutBom); T4Errors = host.Errors; }
public void UpdateProjectNode(ZippyCoder.Entity.Project project) { _Modified = true; ChangeTitle(false); if (_CurrentTreeViewItem == null) //当前选中的item项目不存在,表示新创 { _CurrentTreeViewItem = new TreeViewItem(); treeProject.Items.Add(_CurrentTreeViewItem); _CurrentTreeViewItem.Focus(); _CurrentNode = Nodes.Project; } _CurrentTreeViewItem.Tag = project; _CurrentTreeViewItem.Header = project.Title + "[" + project.Namespace + "]"; this.Title = project.Title + "[" + project.Namespace + "]" + " - Zippy 代码生成器"; }
private void ImportOldProject_Click(object sender, RoutedEventArgs e) { Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); dlg.Filter = "Xml文件|*.xml"; Nullable <bool> result = dlg.ShowDialog(); if (result == true) { //projectPath = dlg.FileName; try { _Project = ZippyCoder.ProjectParser.LoadOldProject(dlg.FileName); //ZippyCoder.Entity.Project.Load(projectPath); projectPath = string.Empty; UpdateUI(); } catch (Exception ex) { ShowInfo("文件打开错误,请核对项目文件的格式。" + ex.Message); //MessageBox.Show("文件打开错误,请核对项目文件的格式。\r\n" + ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error); } } }
public PPOpenMySql(MainForm owner) { InitializeComponent(); _Owner = owner; project = new ZippyCoder.Entity.Project(); }
private void CreateProject() { _Project = new ZippyCoder.Entity.Project(); _uiProject.Project = _Project; Navigate(_uiProject); }