-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandLineProcessor.cs
More file actions
258 lines (210 loc) · 11.9 KB
/
CommandLineProcessor.cs
File metadata and controls
258 lines (210 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
using CSExcelConverter.Models;
using CSExcelConverter.Services;
using OfficeOpenXml;
namespace CSExcelConverter
{
public static class CommandLineProcessor
{
public static void ProcessCommandLine(string[] args)
{
if (args.Length < 1)
{
Console.WriteLine("用法: CSExcelConverter <Excel文件路径> [输出目录] [客户端子目录] [服务端子目录]");
Console.WriteLine("示例: CSExcelConverter config.xlsx ./output client server");
return;
}
string excelPath = args[0];
var globalConfig = new Models.GlobalConfig();
if (args.Length > 1) globalConfig.BaseOutputPath = args[1];
if (args.Length > 2) globalConfig.ClientSubDirectory = args[2];
if (args.Length > 3) globalConfig.ServerSubDirectory = args[3];
if (!File.Exists(excelPath))
{
Console.WriteLine($"错误: Excel文件 '{excelPath}' 不存在");
return;
}
try
{
Console.WriteLine($"开始处理Excel文件: {excelPath}");
var excelReader = new ExcelReader();
var codeGenerator = new CodeGenerator();
// Setup text ID manager for localization
var textIdManager = new TextIdManager(globalConfig.BaseOutputPath);
codeGenerator.SetTextIdManager(textIdManager);
var configs = excelReader.ReadExportConfigs(excelPath);
Console.WriteLine($"找到 {configs.Count} 个导出配置");
using var package = new ExcelPackage(new FileInfo(excelPath));
foreach (var config in configs)
{
Console.WriteLine($"处理配置: {config.Mode} 模式, 目标: {config.Target}, 文件: {config.FileName}");
var mappings = excelReader.ParseSheetMappings(config.SheetMappings, config.Mode);
if (config.Mode == ExportMode.Dict)
{
ProcessDictMode(package, excelReader, codeGenerator, config, mappings, globalConfig);
}
else if (config.Mode == ExportMode.Const)
{
ProcessConstMode(package, excelReader, codeGenerator, config, mappings, globalConfig);
}
else if (config.Mode == ExportMode.MulDict)
{
ProcessMulDictMode(package, excelReader, codeGenerator, config, mappings, globalConfig);
}
}
// 报告TextId统计信息
textIdManager.ReportHashStatistics();
Console.WriteLine("Excel导出完成!");
}
catch (Exception ex)
{
Console.WriteLine($"错误: {ex.Message}");
Console.WriteLine($"堆栈跟踪: {ex.StackTrace}");
}
}
private static void ProcessDictMode(ExcelPackage package, ExcelReader excelReader,
CodeGenerator codeGenerator, ExportConfig config, List<SheetMapping> mappings, Models.GlobalConfig globalConfig)
{
var sheetColumns = new Dictionary<string, List<DictColumn>>();
var sheetData = new Dictionary<string, List<Dictionary<string, object?>>>();
// 检查是否使用多源文件模式
var sourceFiles = excelReader.ParseSources(config.Sources);
foreach (var mapping in mappings)
{
List<DictColumn> columns;
List<Dictionary<string, object?>> data;
if (sourceFiles.Any())
{
// 多源文件模式:从第一个源文件读取列定义,然后从所有源文件(包括当前文件)读取数据
Console.WriteLine($"使用多源文件模式读取Dict工作表: {mapping.SheetName}");
var firstSourcePath = Path.Combine(Path.GetDirectoryName(package.File.FullName) ?? "", sourceFiles.First());
using var firstSourcePackage = new ExcelPackage(new FileInfo(firstSourcePath));
var firstSheet = firstSourcePackage.Workbook.Worksheets[mapping.SheetName];
if (firstSheet == null)
{
Console.WriteLine($"警告: 在第一个源文件中找不到工作表 '{mapping.SheetName}'");
continue;
}
columns = excelReader.ReadDictColumns(firstSheet);
// 获取当前文件的对应sheet
var currentSheet = package.Workbook.Worksheets[mapping.SheetName];
data = excelReader.ReadMultipleSourcesDictData(package.File.FullName, sourceFiles, mapping.SheetName, columns, currentSheet);
}
else
{
// 单文件模式:从当前Excel文件读取
var sheet = package.Workbook.Worksheets[mapping.SheetName];
if (sheet == null)
{
Console.WriteLine($"警告: 找不到工作表 '{mapping.SheetName}'");
continue;
}
Console.WriteLine($"读取Dict工作表: {mapping.SheetName}");
columns = excelReader.ReadDictColumns(sheet);
data = excelReader.ReadDictData(sheet, columns, package.File.FullName, globalConfig.ExcelRootDirectory);
}
sheetColumns[mapping.SheetName] = columns;
sheetData[mapping.SheetName] = data;
Console.WriteLine($" - 列数: {columns.Count}, 数据行数: {data.Count}");
}
if (sheetColumns.Any())
{
codeGenerator.GenerateDictCode(config.FileName, mappings, sheetColumns,
sheetData, config.Target, globalConfig.BaseOutputPath,
globalConfig.ClientSubDirectory, globalConfig.ServerSubDirectory, config, globalConfig);
Console.WriteLine($"Dict代码已生成: {config.FileName}");
}
}
private static void ProcessConstMode(ExcelPackage package, ExcelReader excelReader,
CodeGenerator codeGenerator, ExportConfig config, List<SheetMapping> mappings, Models.GlobalConfig globalConfig)
{
var allConstants = new List<ConstData>();
// 检查是否使用多源文件模式
var sourceFiles = excelReader.ParseSources(config.Sources);
foreach (var mapping in mappings)
{
List<ConstData> constants;
if (sourceFiles.Any())
{
// 多源文件模式
Console.WriteLine($"使用多源文件模式读取Const工作表: {mapping.SheetName}");
// 获取当前文件的对应sheet
var currentSheet = package.Workbook.Worksheets[mapping.SheetName];
constants = excelReader.ReadMultipleSourcesConstData(package.File.FullName, sourceFiles, mapping.SheetName, currentSheet);
}
else
{
// 单文件模式
var sheet = package.Workbook.Worksheets[mapping.SheetName];
if (sheet == null)
{
Console.WriteLine($"警告: 找不到工作表 '{mapping.SheetName}'");
continue;
}
Console.WriteLine($"读取Const工作表: {mapping.SheetName}");
constants = excelReader.ReadConstData(sheet);
}
allConstants.AddRange(constants);
Console.WriteLine($" - 常量数: {constants.Count}");
}
if (allConstants.Any())
{
codeGenerator.GenerateConstCode(config.FileName, allConstants, config.Target, globalConfig.BaseOutputPath,
globalConfig.ClientSubDirectory, globalConfig.ServerSubDirectory);
Console.WriteLine($"Const代码已生成: {config.FileName}");
}
}
private static void ProcessMulDictMode(ExcelPackage package, ExcelReader excelReader,
CodeGenerator codeGenerator, ExportConfig config, List<SheetMapping> mappings, Models.GlobalConfig globalConfig)
{
var sheetColumns = new Dictionary<string, List<DictColumn>>();
var sheetData = new Dictionary<string, Dictionary<object, Dictionary<object, Dictionary<string, object?>>>>();
// 检查是否使用多源文件模式
var sourceFiles = excelReader.ParseSources(config.Sources);
foreach (var mapping in mappings)
{
List<DictColumn> columns;
Dictionary<object, Dictionary<object, Dictionary<string, object?>>> data;
if (sourceFiles.Any())
{
// 多源文件模式:从第一个源文件读取列定义,然后从所有源文件(包括当前文件)读取数据
Console.WriteLine($"使用多源文件模式读取MulDict工作表: {mapping.SheetName}");
var firstSourcePath = Path.Combine(Path.GetDirectoryName(package.File.FullName) ?? "", sourceFiles.First());
using var firstSourcePackage = new ExcelPackage(new FileInfo(firstSourcePath));
var firstSheet = firstSourcePackage.Workbook.Worksheets[mapping.SheetName];
if (firstSheet == null)
{
Console.WriteLine($"警告: 在第一个源文件中找不到工作表 '{mapping.SheetName}'");
continue;
}
columns = excelReader.ReadDictColumns(firstSheet);
// 获取当前文件的对应sheet
var currentSheet = package.Workbook.Worksheets[mapping.SheetName];
data = excelReader.ReadMultipleSourcesMulDictData(package.File.FullName, sourceFiles, mapping.SheetName, columns, currentSheet);
}
else
{
// 单文件模式:从当前Excel文件读取
var sheet = package.Workbook.Worksheets[mapping.SheetName];
if (sheet == null)
{
Console.WriteLine($"警告: 找不到工作表 '{mapping.SheetName}'");
continue;
}
Console.WriteLine($"读取MulDict工作表: {mapping.SheetName}");
columns = excelReader.ReadDictColumns(sheet);
data = excelReader.ReadMulDictData(sheet, columns);
}
sheetColumns[mapping.SheetName] = columns;
sheetData[mapping.SheetName] = data;
Console.WriteLine($" - 列数: {columns.Count}, 主键数: {data.Count}");
}
if (sheetColumns.Any())
{
codeGenerator.GenerateMulDictCode(config.FileName, mappings, sheetColumns,
sheetData, config.Target, globalConfig.BaseOutputPath,
globalConfig.ClientSubDirectory, globalConfig.ServerSubDirectory);
Console.WriteLine($"MulDict代码已生成: {config.FileName}");
}
}
}
}