using CloudBuilder.CodeGenerator.Config; using CloudBuilder.CodeGenerator.Data; using CloudBuilder.CodeGenerator.Utility; using CloudBuilder.Core.Application.Builders; using CloudBuilder.Core.DatabaseAccessor.Entity; using CloudBuilder.Core.DependencyInjection.Form; using CloudBuilder.Core.Extensions; using CloudBuilder.Core.Service; using CloudBuilder.Core.Utility; using Krypton.Toolkit; using Microsoft.Extensions.DependencyInjection; using Microsoft.VisualStudio.OLE.Interop; using RazorEngineCore; using System.Data; using System.Linq; using System.Reflection; using System.Windows.Forms.Design; namespace CloudBuilder.CodeGenerator { public partial class OperationUserControl : UserControl, IUserControl { private readonly IApplicationService app; private readonly TableControl tableControl; private ConfigUserControl configUserControl; private readonly TemplateUserControl templateUserControl; private readonly OutputUserControl outputUserControl; private ToolInitConfig toolInitConfig; public OperationUserControl(IApplicationService app, [FromKeyedServices("TableControl")] TableControl tableControl, [FromKeyedServices("TemplateUserControl")] TemplateUserControl templateUserControl, [FromKeyedServices("OutputUserControl")] OutputUserControl outputUserControl) { InitializeComponent(); this.app = app; this.tableControl = tableControl; this.templateUserControl = templateUserControl; this.outputUserControl = outputUserControl; InitUserControl(); } public void InitUserControl() { assemblyComboBox.DisplayMember = "Text"; assemblyComboBox.ValueMember = "Value"; toolInitConfig = ToolInitConfig.GetInstance(); List l = new List(); List types = new List(); foreach (NamespaceObject nso in toolInitConfig.NamespaceObjects) { if (l.Select(x => x.Text).Contains(nso.Assembly)) continue; l.Add(CreateNewComboBoxItem(nso.Assembly, nso.Assembly)); } DataTable dtbl = DataTableConvertor.ToDataTable(l); assemblyComboBox.DataSource = dtbl; assemblyComboBox.SelectedValue = toolInitConfig.CurrentAssembly; InitkryptonCheckedListBox1(toolInitConfig.CurrentAssembly); } private void InitkryptonCheckedListBox1(string currentAssembly) { if (string.IsNullOrEmpty(currentAssembly)) return; int i = 0; kryptonCheckedListBox1.Items.Clear(); foreach (NamespaceObject nso in toolInitConfig.NamespaceObjects.Where(x => x.Assembly == currentAssembly).ToList()) { if (nso.TemplateType.Contains("Entity") && nso.TemplateType != "Entity") continue; if (nso.TemplateType.Contains("TypeScript")) continue; kryptonCheckedListBox1.Items.Add(CreateNewItem(nso.TemplateType)); if (!nso.TemplateType.Contains("Entity")) { i++; continue; } kryptonCheckedListBox1.SetItemCheckState(i++, CheckState.Checked); } } private void outputFileBut_Click(object sender, EventArgs e) { ModelData model = GetModelData(); if (model.sysTableSchemas == null || model.sysTableSchemas.Length == 0) { MessageBox.Show("请选择要生成的数据表", "提示"); return; } for (var i = 0; i < kryptonCheckedListBox1.Items.Count; i++) { if (kryptonCheckedListBox1.GetItemCheckState(i) == CheckState.Checked) { KryptonListItem a = (KryptonListItem)kryptonCheckedListBox1.Items[i]; WriteAllText(model, a.ShortText); } } MessageBox.Show("生成成功", "提示"); } private void WriteAllText(ModelData model, string templateType) { NamespaceObject namespaceObject = toolInitConfig.NamespaceObjects.Where(x => x.Assembly == assemblyComboBox.Text.Trim() && x.TemplateType == templateType).FirstOrDefault(); if (namespaceObject == null) return; string fileName = Path.Combine(toolInitConfig.TemplatePath, namespaceObject.TemplateFileName); if (!File.Exists(fileName)) return; if (!Path.Exists(namespaceObject.OutFilePath)) return; var tp = File.ReadAllText(fileName); string outputFileName = Path.Combine(namespaceObject.OutFilePath, namespaceObject.FileNamePrefix + model.sysTableSchemas[0].TableName.StringToProerty() + namespaceObject.FileNameSuffix); if (templateType == "RegistEntity") { outputFileName = Path.Combine(namespaceObject.OutFilePath, namespaceObject.Assembly.Replace(".", "") + namespaceObject.FileNameSuffix); //文件不存在,先初始化文件 if (!File.Exists(outputFileName)) File.WriteAllText(outputFileName, RazorEngineHelper.CompiledTemplate(tp, model)); string allTxt = File.ReadAllText(outputFileName); tp = File.ReadAllText(Path.Combine(toolInitConfig.TemplatePath, namespaceObject.TemplateFileName.Replace(".cshtml", ".Key.cshtml"))); var temp = RazorEngineHelper.CompiledTemplate(tp, model); if (allTxt.Contains("<" + model.sysTableSchemas[0].TableName.StringToProerty() + "Entity>()")) return; int end = allTxt.IndexOf("//***END***", StringComparison.OrdinalIgnoreCase); allTxt = allTxt.Substring(0, end) + temp + allTxt.Substring(end, allTxt.Length - end); File.WriteAllText(outputFileName, allTxt); return; } else if (templateType == "Permission") { outputFileName = Path.Combine(namespaceObject.OutFilePath, namespaceObject.Assembly.Split('.')[namespaceObject.Assembly.Split('.').Length - 1] + namespaceObject.FileNameSuffix); //文件存在不覆盖,插入 if (File.Exists(outputFileName)) { string allTxt = File.ReadAllText(outputFileName); tp = File.ReadAllText(Path.Combine(toolInitConfig.TemplatePath, namespaceObject.TemplateFileName)); var temp = RazorEngineHelper.CompiledTemplate(tp, model); if (allTxt.Contains(model.sysTableSchemas[0].TableName.StringToProerty())) return; int end = allTxt.IndexOf("//***END***", StringComparison.OrdinalIgnoreCase); allTxt = allTxt.Substring(0, end) + temp + allTxt.Substring(end, allTxt.Length - end); File.WriteAllText(outputFileName, allTxt); return; } else { tp = File.ReadAllText(Path.Combine(toolInitConfig.TemplatePath, namespaceObject.TemplateFileName.Replace(".cshtml", ".auto.cshtml"))); var temp = RazorEngineHelper.CompiledTemplate(tp, model); File.WriteAllText(outputFileName, temp); } } //Entity可以直接輸出,其他要文件不存在才生成 if (templateType == "Entity" || templateType == "IEntityKey" || !File.Exists(outputFileName)) File.WriteAllText(outputFileName, RazorEngineHelper.CompiledTemplate(tp, model)); if ((model.KeyCount >= 0) && templateType == "Entity") { if (model.KeyCount > 1) WriteAllText(model, "IEntityKey"); WriteAllText(model, "RegistEntity"); } else if (templateType == "Service") { WriteAllText(model, "Permission"); } } private void templateBut_Click(object sender, EventArgs e) { try { ModelData modelData = GetModelData(); ((OutputUserControl)outputUserControl).contextTxt.Text = RazorEngineHelper.CompiledTemplate(((TemplateUserControl)templateUserControl).contextTxt.Text, modelData); MessageBox.Show("生成成功", "提示"); } catch (Exception ex) { MessageBox.Show(ex.Message, "出错"); } } private ModelData GetModelData() { configUserControl = app.ServiceProvider.GetKeyedService("ConfigUserControl"); ModelData modelData = new ModelData(); modelData.sysTableSchemas = ((TableControl)tableControl).GetDataSource(); modelData.NamespaceObject = toolInitConfig.NamespaceObjects.Where(x => x.TemplateType == configUserControl.GetTemplateType() && x.Assembly == configUserControl.GetAssembly()).FirstOrDefault(); if (modelData.sysTableSchemas != null && modelData.sysTableSchemas.Length > 0) { modelData.KeyCount = modelData.sysTableSchemas.Where(x => x.Pk > 0).Count(); if (modelData.KeyCount == 1) { modelData.KeyType = modelData.sysTableSchemas.Where(x => x.Pk > 0).FirstOrDefault().TypeOfName; } else if (modelData.KeyCount > 1) { modelData.KeyType = $"{modelData.sysTableSchemas[0].TableName.StringToProerty()}EntityKey"; } else { modelData.KeyType = "string"; } } List assemblies = new List(); if (toolInitConfig.AssemblyReferences != null) { foreach (var reference in toolInitConfig.AssemblyReferences) { if (string.IsNullOrEmpty(reference.AssemblyFileName)) continue; if (!File.Exists(reference.AssemblyFileName)) continue; assemblies.Add(Assembly.LoadFrom(reference.AssemblyFileName)); } } if (configUserControl.GetTemplateType() == "Controller" || configUserControl.GetTemplateType() == "TypeScriptService") modelData.ClassInfos = RazorEngineHelper.PackIServiceClassInfo(assemblies.Where(x => Path.GetFileNameWithoutExtension(x.ManifestModule.Name) == modelData.NamespaceObject.Assembly).FirstOrDefault()); else if (configUserControl.GetTemplateType() == "TypeScript") modelData.ClassInfos = RazorEngineHelper.PackTypeScriptClassInfo(assemblies.Where(x => Path.GetFileNameWithoutExtension(x.ManifestModule.Name) == modelData.NamespaceObject.Assembly).FirstOrDefault()); return modelData; } public class ComboBoxItem { public string Text { get; set; } public string Value { get; set; } } private ComboBoxItem CreateNewComboBoxItem(string text, string value) { ComboBoxItem item = new ComboBoxItem(); item.Text = text; item.Value = value; return item; } private object CreateNewItem(string text) { KryptonListItem item = new KryptonListItem(); item.ShortText = text; return item; } private void assemblyComboBox_SelectionChangeCommitted(object sender, EventArgs e) { ((MainFunctionView)(this.ParentForm)).SetConfigUserControl(assemblyComboBox.SelectedValue); } private void assemblyComboBox_SelectedValueChanged(object sender, EventArgs e) { InitkryptonCheckedListBox1((string)assemblyComboBox.SelectedValue); } } }