新建一个txt文件模板(直接在代码里面拼接字符串也行,只不过那样修改起来麻烦),取名IndexJsTemplate.txt,添加如下代码
(function () { $(function () { var _${{entity_Name_Plural_Here}}Table = $('#MainTable'); var _{{entity_Name_Plural_Here}}Service = abp.services.app.{{entity_Name_Plural_Here}}; var _permissions = { create: abp.auth.hasPermission('{{Permission_Value_Here}}.Create'), edit: abp.auth.hasPermission('{{Permission_Value_Here}}.Edit'), 'delete': abp.auth.hasPermission('{{Permission_Value_Here}}.Delete') }; var _createOrEditModal = new app.ModalManager({ viewUrl: abp.appPath + '{{App_Area_Name_Here}}/{{Entity_Name_Plural_Here}}/CreateOrEditModal', scriptUrl: abp.appPath + 'view-resources/Areas/{{App_Area_Name_Here}}/Views/{{Entity_Name_Plural_Here}}/_CreateOrEditModal.js', modalClass: 'CreateOrEdit{{Entity_Name_Here}}Modal' }); var dataTable = _${{entity_Name_Plural_Here}}Table.DataTable({ paging: true, serverSide: true, processing: true, listAction: { ajaxFunction: _{{entity_Name_Plural_Here}}Service.getAll, inputFilter: function () { return { filter: $('#{{Entity_Name_Plural_Here}}TableFilter').val() }; } }, columnDefs: [ { width: 120, targets: 0, data: null, orderable: false, autoWidth: false, defaultContent: '', rowAction: { cssClass: 'btn btn-brand dropdown-toggle', text: '<i></i> ' + app.localize('Actions') + ' <span></span>', items: [ { text: app.localize('Edit'), visible: function () { return _permissions.edit; }, action: function (data) { _createOrEditModal.open({ id: data.record.id }); } }, { text: app.localize('Delete'), visible: function () { return _permissions.delete; }, action: function (data) { delete{{Entity_Name_Here}}(data.record); } }] } }{{Property_Looped_Template_Here}} ] }); function get{{Entity_Name_Plural_Here}}() { dataTable.ajax.reload(); } function delete{{Entity_Name_Here}}({{entity_Name_Here}}) { abp.message.confirm( '', function (isConfirmed) { if (isConfirmed) { _{{entity_Name_Plural_Here}}Service.delete({ id: {{entity_Name_Here}}.id }).done(function () { get{{Entity_Name_Plural_Here}}(true); abp.notify.success(app.localize('SuccessfullyDeleted')); }); } } ); } $('#Export{{Entity_Name_Here}}ToExcelButton').click(function () { _{{entity_Name_Here}}Service .get{{Entity_Name_Here}}ToExcel({}) .done(function (result) { app.downloadTempFile(result); }); }); $('#CreateNew{{Entity_Name_Here}}Button').click(function () { _createOrEditModal.open(); }); abp.event.on('app.createOrEdit{{Entity_Name_Here}}ModalSaved', function () { get{{Entity_Name_Plural_Here}}(); }); $('#Get{{Entity_Name_Plural_Here}}Button').click(function (e) { e.preventDefault(); get{{Entity_Name_Plural_Here}}(); }); $(document).keypress(function(e) { if(e.which === 13) { get{{Entity_Name_Plural_Here}}(); } }); }); })();
首先读取文件模板,然后去替换掉“{{}}” 这种类似占位符的变量,然后再输出一个新的文件,代码如下
static void Main(string[] args) { //反射程序集的方式生成相应代码 string className = "User";//跟类名保持一致 var metaTableInfoList = MetaTableInfo.GetMetaTableInfoListForAssembly(className); SetIndexJsTemplate(className, metaTableInfoList); } /// <summary> /// 生成IndexJsTemplate /// </summary> /// <param></param> public static void SetIndexJsTemplate(string className, List<MetaTableInfo> metaTableInfoList) { var rootPath = (Directory.GetCurrentDirectory().Split(new string[] { "bin" }, StringSplitOptions.RemoveEmptyEntries))[0]; string templateContentDirectory = rootPath + "IndexJsTemplate.txt"; var templateContent = Read(templateContentDirectory); StringBuilder sb = new StringBuilder(); var i = 1; foreach (var item in metaTableInfoList) { sb.AppendLine(", {"); sb.AppendLine("targets: " + i + ","); sb.AppendLine("data: \"" + GetFirstToLowerStr(item.Name) + "\""); sb.AppendLine("}"); i++; } var property_Looped_Template_Here = sb.ToString(); templateContent = templateContent .Replace("{{Entity_Name_Plural_Here}}", className) .Replace("{{Entity_Name_Here}}", className) .Replace("{{entity_Name_Here}}", GetFirstToLowerStr(className)) .Replace("{{entity_Name_Plural_Here}}", GetFirstToLowerStr(className)) .Replace("{{App_Area_Name_Here}}", "App_Area_Name") .Replace("{{Property_Looped_Template_Here}}", property_Looped_Template_Here) .Replace("{{Permission_Value_Here}}", "Pages.Administration." + className + "") ; Write(rootPath, "Index.js", templateContent); } #region 文件读取 public static string Read(string path) { using (StreamReader sr = new StreamReader(path, Encoding.Default)) { StringBuilder sb = new StringBuilder(); String line; while ((line = sr.ReadLine()) != null) { sb.AppendLine(line.ToString()); } return sb.ToString(); } } /// <summary> /// /// </summary> /// <param>文件保存路径</param> /// <param>文件名</param> /// <param>模板内容</param> public static void Write(string filePath, string fileName, string templateContent) { if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } using (FileStream fs = new FileStream(filePath + fileName, FileMode.Create)) { //获得字节数组 byte[] data = Encoding.Default.GetBytes(templateContent); //开始写入 fs.Write(data, 0, data.Length); } } /// <summary> /// /// </summary> /// <param>文件保存路径</param> /// <param>模板内容</param> public static void Write(string filePath, string templateContent) { using (FileStream fs = new FileStream(filePath, FileMode.Create)) { //获得字节数组 byte[] data = Encoding.Default.GetBytes(templateContent); //开始写入 fs.Write(data, 0, data.Length); } } #endregion #region 首字母小写 /// <summary> /// 首字母小写 /// </summary> /// <param></param> /// <returns></returns> public static string GetFirstToLowerStr(string str) { if (!string.IsNullOrEmpty(str)) { if (str.Length > 1) { return char.ToLower(str[0]) + str.Substring(1); } return char.ToLower(str[0]).ToString(); } return null; } #endregion }
启动控制台,即在当前项目下生成相应代码,这里分享一个我之前写好的基于abp.zero core 5.3.0的
https://github.com/HisKingdom/CodeGenerator