public void GenerateCode() { VelocityEngine velocity = new VelocityEngine(); TextReader reader = new StreamReader(NvelocityUtil.GetTemplateFolderPath() + "\\CustomItem.base.vm"); string template = reader.ReadToEnd(); //Setup the template with the needed code, and then do the merge VelocityContext baseContext = new VelocityContext(); baseContext.Put("Usings", CustomItemInformation.Usings); baseContext.Put("BaseTemplates", CustomItemInformation.BaseTemplates); baseContext.Put("CustomItemFields", CustomItemInformation.Fields); baseContext.Put("CustomItemInformation", CustomItemInformation); //Get the full file path to the .base.cs file string filePath = FileUtil.GetClassFilePath(CustomItemInformation.ClassName, CustomItemInformation.FolderPathProvider.GetFolderPath( CustomItemInformation.Template, CustomItemInformation.BaseFileRoot)); //Build the folder strucutre so that we have a place to put the .base.cs file BuildFolderStructure(CustomItemInformation); //Write the .base.cs file if (GenerateBaseFile) { using (StreamWriter sw = new StreamWriter(filePath)) { //TODO add error checking Velocity.Init(); sw.Write(Sitecore.Text.NVelocity.VelocityHelper.Evaluate(baseContext, template, "base-custom-item")); GenerationMessage += filePath + " successfully written\n\n"; GeneratedFilePaths.Add(filePath); } } //Write out the other partial files OuputPartialFiles(velocity); }
/// <summary> /// Ouputs the partial class files for a custom item. /// </summary> /// <param name="velocity">The velocity.</param> private void OuputPartialFiles(VelocityEngine velocity) { StringWriter writer; TextReader reader = new StreamReader(NvelocityUtil.GetTemplateFolderPath() + "\\CustomItem.partial.vm"); string template = reader.ReadToEnd(); string folderPath = CustomItemInformation.FolderPathProvider.GetFolderPath(CustomItemInformation.Template, CustomItemInformation.BaseFileRoot); VelocityContext partialContext = new VelocityContext(); partialContext.Put("CustomItemInformation", CustomItemInformation); writer = new StringWriter(); writer.Write(Sitecore.Text.NVelocity.VelocityHelper.Evaluate(partialContext, template, "base-custom-item")); //.instance.cs if (GenerateInstanceFile) { //Only create the file if it does not already exist string instanceFilePath = folderPath + "\\" + CustomItemInformation.ClassName + ".instance.cs"; OutputFileIfDoesNotExist(instanceFilePath, writer); } //.interface.cs if (GenerateInterfaceFile) { //Only create the file if it does not already exist string interfaceFilePath = folderPath + "\\" + CustomItemInformation.ClassName + ".interface.cs"; OutputFileIfDoesNotExist(interfaceFilePath, writer); } //.static.cs if (GenerateStaticFile) { //Only create the file if it does not already exist string staticFilePath = folderPath + "\\" + CustomItemInformation.ClassName + ".static.cs"; OutputFileIfDoesNotExist(staticFilePath, writer); } }
private void SaveBtn_Click(object sender, EventArgs e) { try { string temSql = $"select * from CODE_PROJECT_TEMPLATE_CONFIG_INFO where PROJECT_ID ='{codeProjectInfo.ID}'"; DataTable dataTable1 = createBLL.SelectData(temSql); List <CodeProjectTemplateConfigInfo> templateConfigInfos = DbConvert.TableToList <CodeProjectTemplateConfigInfo>(dataTable1); if (string.IsNullOrEmpty(this.tableNames.Text)) { MessageHelper.ShowWarn("表名不能为空,至少选择一个表"); return; } if (string.IsNullOrEmpty(this.TOP_LEVEL.Text)) { MessageHelper.ShowWarn("顶级包名不能为空"); return; } if (templateConfigInfos == null || templateConfigInfos.Count <= 0) { MessageHelper.ShowWarn("未检出到任何模板"); return; } string[] tableArr = this.tableNames.Text.Split(','); List <CsProjectCode> projectCodes = new List <CsProjectCode>(); List <TableFieldInfo> lists = null; CsProjectCode csProjectCode = null; CodeProjectInfo codeProjectInfoEdit = null; if (this.TopLevelControl is AutoCodeConfigForm) { codeProjectInfoEdit = ((AutoCodeConfigForm)this.TopLevelControl).GetTopTextBoxInfo(); } string outFileFolder = "", outFilePath, projectCodePath = ""; Dictionary <string, object> dicKv; StringBuilder sbf = new StringBuilder(); string projectSite = null; for (int i = 0; i < tableArr.Length; i++) { csProjectCode = new CsProjectCode(tableArr[i]) { Author = AUTHOR.Text, TopLevel = TOP_LEVEL.Text, Version = codeProjectInfo.VERSION }; lists = dbBLL.SelectTableFields(codeProjectDbConfigInfo, tableArr[i]); csProjectCode.TableFieldInfos = lists; projectCodes.Add(csProjectCode); dicKv = new Dictionary <string, object>(); dicKv.Add("tableName", csProjectCode.TableName);//表名 if (tablesDicts.ContainsKey(csProjectCode.TableName)) { dicKv.Add("tableComment", tablesDicts[csProjectCode.TableName].TableComment); //表备注 } dicKv.Add("entityName", csProjectCode.EntityName); //实体名 dicKv.Add("projectCode", csProjectCode); //项目表信息 if (codeProjectInfoEdit != null) { dicKv.Add("projectName", codeProjectInfoEdit.PRO_NAME);//项目名称 projectSite = codeProjectInfoEdit.PRO_SITE; } for (int k = 0; k < templateConfigInfos.Count; k++) { projectCodePath = projectSite ?? codeProjectInfo.PRO_SITE; //templateConfigInfos[k].TemplatePath.Replace("{tableName}", ""); outFileFolder = templateConfigInfos[k].TemplatePath.Replace("{projectSite}", projectCodePath).Replace("{tableName}", csProjectCode.EntityName); outFilePath = Path.Combine(outFileFolder, $"{csProjectCode.EntityName}{templateConfigInfos[k].OverlayName}{templateConfigInfos[k].Suffix}"); NvelocityUtil.WriteTemplateByString(outFilePath, templateConfigInfos[k].TemplateContent, dicKv); sbf.Append($"表:{csProjectCode.TableName} ,模板【{templateConfigInfos[k].TemplateName}{templateConfigInfos[k].Suffix}】生成成功\n"); } } MessageHelper.ShowSuccess(sbf.ToString()); //MessageHelper.ShowSuccess("生成完毕"); this.codeFolder.Text = projectCodePath; } catch (Exception ex) { MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } }