256 lines
11 KiB
C#
256 lines
11 KiB
C#
using CloudBuilder.CodeGenerator.Config;
|
|
using CloudBuilder.Core.DatabaseAccessor;
|
|
using CloudBuilder.Core.DatabaseAccessor.Entity;
|
|
using CloudBuilder.Core.DependencyInjection.Form;
|
|
using CloudBuilder.Core.Utility;
|
|
using Microsoft.AspNetCore.Connections;
|
|
using Microsoft.AspNetCore.Routing.Template;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace CloudBuilder.CodeGenerator
|
|
{
|
|
public partial class ConfigUserControl : UserControl, IUserControl
|
|
{
|
|
private readonly IApplicationService app;
|
|
private readonly OperationUserControl operationUserControl;
|
|
public ToolInitConfig toolInitConfig;
|
|
public ConfigUserControl(IApplicationService app,
|
|
[FromKeyedServices("OperationUserControl")] OperationUserControl operationUserControl)
|
|
{
|
|
InitializeComponent();
|
|
this.app = app;
|
|
this.operationUserControl = operationUserControl;
|
|
InitUserControl();
|
|
}
|
|
|
|
public void InitUserControl()
|
|
{
|
|
dbConnTxt.Text = app.CloudBuilder.CloudBuilderOptions.DbContexts[MasterDbContext.MASTER_DB_CONTEXT].ConnectionString;
|
|
|
|
toolInitConfig = ToolInitConfig.GetInstance();
|
|
|
|
app.CloudBuilder.Services.AddSingleton(toolInitConfig);
|
|
usingTxt.Text = toolInitConfig.Using;
|
|
|
|
assemblyComboBox.DisplayMember = "Text";
|
|
assemblyComboBox.ValueMember = "Value";
|
|
|
|
List<ComboBoxItem> l = new List<ComboBoxItem>();
|
|
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<ComboBoxItem>(l);
|
|
assemblyComboBox.DataSource = dtbl;
|
|
|
|
assemblyComboBox.SelectedValue = toolInitConfig.CurrentAssembly;
|
|
|
|
templateTypeCombox.DisplayMember = "Text";
|
|
templateTypeCombox.ValueMember = "Value";
|
|
|
|
NamespaceObject namespaceObject = toolInitConfig.NamespaceObjects.Where(x => x.Assembly == toolInitConfig.CurrentAssembly).FirstOrDefault();
|
|
|
|
l = new List<ComboBoxItem>();
|
|
foreach (NamespaceObject nso in toolInitConfig.NamespaceObjects.Where(x => x.Assembly == toolInitConfig.CurrentAssembly).ToArray())
|
|
{
|
|
if (l.Select(x => x.Text).Contains(nso.TemplateType)) continue;
|
|
l.Add(CreateNewComboBoxItem(nso.TemplateType, nso.TemplateType));
|
|
}
|
|
|
|
dtbl = DataTableConvertor.ToDataTable<ComboBoxItem>(l);
|
|
templateTypeCombox.DataSource = dtbl;
|
|
|
|
|
|
templateTypeCombox.SelectedValue = namespaceObject.TemplateType;
|
|
|
|
templatePathTxt.Text = toolInitConfig.TemplatePath;
|
|
|
|
BindData(namespaceObject);
|
|
}
|
|
|
|
public string GetTemplateType()
|
|
{
|
|
return (string)templateTypeCombox.SelectedValue;
|
|
}
|
|
|
|
public string GetAssembly()
|
|
{
|
|
return (string)assemblyComboBox.SelectedValue;
|
|
}
|
|
|
|
private void BindData(NamespaceObject namespaceObject)
|
|
{
|
|
if (namespaceObject == null)
|
|
{
|
|
templateTypeCombox.SelectedIndex = -1;
|
|
fileNamePrefixTxt.Text = fileNameSuffixTxt.Text = templateTypeTxt.Text = outFilePathTxt.Text = templateFileNameTxt.Text = assemblyTxt.Text = namespaceTxt.Text = null;
|
|
return;
|
|
}
|
|
templateTypeCombox.SelectedValue= namespaceObject.TemplateType;
|
|
templateTypeTxt.Text = namespaceObject.TemplateType;
|
|
outFilePathTxt.Text = namespaceObject.OutFilePath;
|
|
templateFileNameTxt.Text = namespaceObject.TemplateFileName;
|
|
fileNameSuffixTxt.Text = namespaceObject.FileNameSuffix;
|
|
fileNamePrefixTxt.Text = namespaceObject.FileNamePrefix;
|
|
assemblyTxt.Text = namespaceObject.Assembly;
|
|
namespaceTxt.Text = namespaceObject.Namespace;
|
|
}
|
|
|
|
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 void assemblyComboBox_SelectionChangeCommitted(object sender, EventArgs e)
|
|
{
|
|
if (assemblyComboBox.SelectedValue == null) return;
|
|
NamespaceObject namespaceObject = toolInitConfig.NamespaceObjects.Where(x => x.Assembly == (string)assemblyComboBox.SelectedValue).FirstOrDefault();
|
|
|
|
List<ComboBoxItem> l = new List<ComboBoxItem>();
|
|
foreach (NamespaceObject nso in toolInitConfig.NamespaceObjects.Where(x => x.Assembly == (string)assemblyComboBox.SelectedValue).ToArray())
|
|
{
|
|
if (l.Select(x => x.Text).Contains(nso.TemplateType)) continue;
|
|
l.Add(CreateNewComboBoxItem(nso.TemplateType, nso.TemplateType));
|
|
}
|
|
|
|
DataTable dtbl = DataTableConvertor.ToDataTable<ComboBoxItem>(l);
|
|
templateTypeCombox.DataSource = dtbl;
|
|
|
|
BindData(namespaceObject);
|
|
|
|
((MainFunctionView)(this.ParentForm)).SetOperationUserControl(assemblyComboBox.SelectedValue);
|
|
}
|
|
|
|
private void templateTypeCombox_SelectionChangeCommitted(object sender, EventArgs e)
|
|
{
|
|
if (templateTypeCombox.SelectedValue == null) return;
|
|
NamespaceObject namespaceObject = toolInitConfig.NamespaceObjects.Where(x => x.Assembly == (string)assemblyComboBox.SelectedValue && x.TemplateType == (string)templateTypeCombox.SelectedValue).FirstOrDefault();
|
|
|
|
BindData(namespaceObject);
|
|
}
|
|
|
|
private void saveConfigBut_Click(object sender, EventArgs e)
|
|
{
|
|
toolInitConfig.TemplatePath = templatePathTxt.Text.Trim();
|
|
toolInitConfig.CurrentAssembly = assemblyTxt.Text.Trim();
|
|
toolInitConfig.Using = usingTxt.Text.Trim();
|
|
|
|
NamespaceObject namespaceObject = new NamespaceObject();
|
|
namespaceObject.TemplateType = templateTypeTxt.Text.Trim();
|
|
namespaceObject.TemplateFileName = templateFileNameTxt.Text.Trim();
|
|
namespaceObject.FileNameSuffix = fileNameSuffixTxt.Text.Trim();
|
|
namespaceObject.FileNamePrefix = fileNamePrefixTxt.Text.Trim();
|
|
namespaceObject.OutFilePath = outFilePathTxt.Text.Trim();
|
|
|
|
namespaceObject.Assembly = assemblyTxt.Text.Trim();
|
|
namespaceObject.Namespace = namespaceTxt.Text.Trim();
|
|
|
|
|
|
NamespaceObject obj = toolInitConfig.NamespaceObjects.Where(x => x.Assembly == assemblyTxt.Text.Trim()).FirstOrDefault();
|
|
|
|
//如果不存在,插入
|
|
if (obj == null)
|
|
{
|
|
List<NamespaceObject> l = new List<NamespaceObject>();
|
|
l.Add(namespaceObject);
|
|
if (toolInitConfig.NamespaceObjects != null && toolInitConfig.NamespaceObjects.Length > 0)
|
|
l.AddRange(toolInitConfig.NamespaceObjects);
|
|
toolInitConfig.NamespaceObjects = l.ToArray();
|
|
}
|
|
else
|
|
{
|
|
obj = toolInitConfig.NamespaceObjects.Where(x => x.Assembly == assemblyTxt.Text.Trim() && x.TemplateType == templateTypeTxt.Text.Trim()).FirstOrDefault();
|
|
if (obj == null)
|
|
{
|
|
List<NamespaceObject> ls = new List<NamespaceObject>();
|
|
ls.AddRange(toolInitConfig.NamespaceObjects);
|
|
ls.Add(namespaceObject);
|
|
toolInitConfig.NamespaceObjects = ls.OrderBy(x => x.Assembly).ThenBy(x => x.TemplateType).ToArray();
|
|
}
|
|
else
|
|
{
|
|
foreach (NamespaceObject t in toolInitConfig.NamespaceObjects)
|
|
{
|
|
if (t.Assembly != assemblyTxt.Text.Trim()) continue;
|
|
if (t.TemplateType != templateTypeTxt.Text.Trim()) continue;
|
|
t.Assembly = namespaceObject.Assembly;
|
|
t.Namespace = namespaceObject.Namespace;
|
|
t.OutFilePath = namespaceObject.OutFilePath;
|
|
t.TemplateType = namespaceObject.TemplateType;
|
|
t.TemplateFileName = namespaceObject.TemplateFileName;
|
|
t.FileNameSuffix = namespaceObject.FileNameSuffix;
|
|
t.FileNamePrefix = namespaceObject.FileNamePrefix;
|
|
}
|
|
}
|
|
}
|
|
ToolInitConfig.Save(toolInitConfig);
|
|
InitUserControl();
|
|
((OperationUserControl)operationUserControl).InitUserControl();
|
|
}
|
|
|
|
private void deleteConfigBut_Click(object sender, EventArgs e)
|
|
{
|
|
NamespaceObject obj = toolInitConfig.NamespaceObjects.Where(x => x.Assembly == assemblyTxt.Text.Trim() && x.TemplateType == templateTypeTxt.Text.Trim()).FirstOrDefault();
|
|
|
|
//如果存在,删除,如果只有一个,不能删除
|
|
if (obj != null && toolInitConfig.NamespaceObjects != null && toolInitConfig.NamespaceObjects.Length > 1)
|
|
{
|
|
toolInitConfig.NamespaceObjects = toolInitConfig.NamespaceObjects.Where(x => !(x.Assembly == obj.Assembly && x.TemplateType == obj.TemplateType)).ToArray();
|
|
|
|
var current = toolInitConfig.NamespaceObjects.Where(x => x.Assembly == assemblyComboBox.Text.Trim()).FirstOrDefault();
|
|
|
|
if (current == null)
|
|
{
|
|
toolInitConfig.CurrentAssembly = toolInitConfig.NamespaceObjects.FirstOrDefault().Assembly;
|
|
}
|
|
|
|
ToolInitConfig.Save(toolInitConfig);
|
|
|
|
InitUserControl();
|
|
((OperationUserControl)operationUserControl).InitUserControl();
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("最后一个选项不能删除", "提示");
|
|
}
|
|
|
|
}
|
|
|
|
private void templatePathBut_Click(object sender, EventArgs e)
|
|
{
|
|
if (folderBrowserDialog1.ShowDialog() != DialogResult.OK) return;
|
|
|
|
templatePathTxt.Text = folderBrowserDialog1.SelectedPath;
|
|
}
|
|
|
|
private void outFilePathBut_Click(object sender, EventArgs e)
|
|
{
|
|
if (folderBrowserDialog1.ShowDialog() != DialogResult.OK) return;
|
|
|
|
outFilePathTxt.Text = folderBrowserDialog1.SelectedPath;
|
|
}
|
|
}
|
|
}
|