|
|
@@ -24,44 +24,47 @@ namespace ET
|
|
|
|
|
|
class Program
|
|
|
{
|
|
|
+ private static string template;
|
|
|
+
|
|
|
static void Main(string[] args)
|
|
|
{
|
|
|
+ template = File.ReadAllText("Template.txt");
|
|
|
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
|
|
|
- ExportExcel("/Users/tanghai/Documents/ET/Excel/StartMachineConfig.xlsx", new StringBuilder());
|
|
|
+ ExportExcel("/Users/tanghai/Documents/ET/Excel/StartZoneConfig.xlsx", new StringBuilder());
|
|
|
}
|
|
|
|
|
|
static void ExportExcel(string path, StringBuilder stringBuilder)
|
|
|
{
|
|
|
- FileInfo fileInfo = new FileInfo(path);
|
|
|
- using ExcelPackage p = new ExcelPackage(fileInfo);
|
|
|
+ using Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
|
|
+ using ExcelPackage p = new ExcelPackage(stream);
|
|
|
|
|
|
List<HeadInfo> classField = new List<HeadInfo>();
|
|
|
+ HashSet<string> uniqeField = new HashSet<string>();
|
|
|
foreach (ExcelWorksheet worksheet in p.Workbook.Worksheets)
|
|
|
{
|
|
|
- ExportSheet(worksheet, classField);
|
|
|
+ ExportSheet(worksheet, classField, uniqeField);
|
|
|
}
|
|
|
|
|
|
- ExportClass(Path.GetFileNameWithoutExtension(path), classField, "");
|
|
|
-
|
|
|
- foreach (HeadInfo headInfo in classField)
|
|
|
- {
|
|
|
- Console.WriteLine($"{headInfo.FieldName} {headInfo.FieldType} {headInfo.FieldDesc}");
|
|
|
- }
|
|
|
+ ExportClass(Path.GetFileNameWithoutExtension(path), classField, "./");
|
|
|
}
|
|
|
|
|
|
- static void ExportSheet(ExcelWorksheet worksheet, List<HeadInfo> classField)
|
|
|
+ static void ExportSheet(ExcelWorksheet worksheet, List<HeadInfo> classField, HashSet<string> uniqeField)
|
|
|
{
|
|
|
const int row = 2;
|
|
|
for (int col = 3; col < worksheet.Dimension.Columns; ++col)
|
|
|
{
|
|
|
- string fieldCS = worksheet.Cells[row, col].Text.Trim();
|
|
|
- string fieldDesc = worksheet.Cells[row + 1, col].Text.Trim();
|
|
|
string fieldName = worksheet.Cells[row + 2, col].Text.Trim();
|
|
|
- string fieldType = worksheet.Cells[row + 3, col].Text.Trim();
|
|
|
if (fieldName == "")
|
|
|
{
|
|
|
continue;
|
|
|
}
|
|
|
+ if (!uniqeField.Add(fieldName))
|
|
|
+ {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ string fieldCS = worksheet.Cells[row, col].Text.Trim();
|
|
|
+ string fieldDesc = worksheet.Cells[row + 1, col].Text.Trim();
|
|
|
+ string fieldType = worksheet.Cells[row + 3, col].Text.Trim();
|
|
|
|
|
|
classField.Add(new HeadInfo(fieldCS, fieldDesc, fieldName, fieldType));
|
|
|
}
|
|
|
@@ -70,41 +73,23 @@ namespace ET
|
|
|
static void ExportClass(string protoName, List<HeadInfo> classField, string exportDir)
|
|
|
{
|
|
|
string exportPath = Path.Combine(exportDir, $"{protoName}.cs");
|
|
|
- using (FileStream txt = new FileStream(exportPath, FileMode.Create))
|
|
|
- using (StreamWriter sw = new StreamWriter(txt))
|
|
|
+
|
|
|
+ using FileStream txt = new FileStream(exportPath, FileMode.Create);
|
|
|
+ using StreamWriter sw = new StreamWriter(txt);
|
|
|
+
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for (int i = 0; i < classField.Count; i++)
|
|
|
{
|
|
|
- StringBuilder sb = new StringBuilder();
|
|
|
- sb.Append("namespace ET\n{");
|
|
|
- sb.Append($"\t[Config]\n");
|
|
|
- sb.Append($"\tpublic partial class {protoName}Category : ACategory<{protoName}>\n");
|
|
|
- sb.Append("\t{\n");
|
|
|
- sb.Append($"\t\tpublic static {protoName}Category Instance;\n");
|
|
|
- sb.Append($"\t\tpublic {protoName}Category()\n");
|
|
|
- sb.Append("\t\t{\n");
|
|
|
- sb.Append($"\t\t\tInstance = this;\n");
|
|
|
- sb.Append("\t\t}\n");
|
|
|
- sb.Append("\t}\n\n");
|
|
|
-
|
|
|
- sb.Append($"\tpublic partial class {protoName}: IConfig\n");
|
|
|
- sb.Append("\t{\n");
|
|
|
- sb.Append("\t\t[BsonId]\n");
|
|
|
- sb.Append("\t\tpublic long Id { get; set; }\n");
|
|
|
-
|
|
|
- for (int i = 0; i < classField.Count; i++)
|
|
|
+ HeadInfo headInfo = classField[i];
|
|
|
+ if (headInfo.ClientServer.StartsWith("#"))
|
|
|
{
|
|
|
- HeadInfo headInfo = classField[i];
|
|
|
- if (headInfo.ClientServer.StartsWith("#"))
|
|
|
- {
|
|
|
- continue;
|
|
|
- }
|
|
|
- sb.Append($"\t\tpublic {headInfo.FieldType} {headInfo.FieldName} {{ get; set;}};\n");
|
|
|
+ continue;
|
|
|
}
|
|
|
-
|
|
|
- sb.Append("\t}\n");
|
|
|
- sb.Append("}\n");
|
|
|
-
|
|
|
- sw.Write(sb.ToString());
|
|
|
+ sb.Append($"\t\t[ProtoMember({i + 1}, IsRequired = true)]\n");
|
|
|
+ sb.Append($"\t\tpublic {headInfo.FieldType} {headInfo.FieldName} {{ get; set; }}\n");
|
|
|
}
|
|
|
+ string content = template.Replace("(ConfigName)", protoName).Replace(("(Fields)"), sb.ToString());
|
|
|
+ sw.Write(content);
|
|
|
}
|
|
|
}
|
|
|
}
|