使用System.CommandLine重构Program.cs
This commit is contained in:
@@ -13,6 +13,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="NPOI" Version="2.7.6" />
|
<PackageReference Include="NPOI" Version="2.7.6" />
|
||||||
<PackageReference Include="Spire.XLS" Version="12.7.1" />
|
<PackageReference Include="Spire.XLS" Version="12.7.1" />
|
||||||
|
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
|
||||||
<PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
|
<PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
+49
-56
@@ -1,51 +1,60 @@
|
|||||||
using System;
|
#nullable enable
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.CommandLine;
|
||||||
using ExcelTool.Parser;
|
using ExcelTool.Parser;
|
||||||
|
|
||||||
namespace ExcelTool
|
namespace ExcelTool
|
||||||
{
|
{
|
||||||
class Program
|
class Program
|
||||||
{
|
{
|
||||||
static void Main(string[] args)
|
static async Task<int> Main(string[] args)
|
||||||
{
|
{
|
||||||
string path = AppDomain.CurrentDomain.BaseDirectory;
|
Option<DirectoryInfo> inputOption = new(
|
||||||
string outputCodeDir = "";
|
name: "--input",
|
||||||
string outputDataDir = "";
|
description: "Excel 文件所在目录",
|
||||||
string nameSpace = "";
|
getDefaultValue: () => new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory)
|
||||||
|
);
|
||||||
|
|
||||||
foreach (string arg in args)
|
Option<DirectoryInfo> outputCodeDirOption = new(
|
||||||
{
|
name: "--outputCodeDir",
|
||||||
if (arg.StartsWith("--input="))
|
description: "CS 模板代码输出目录"
|
||||||
{
|
);
|
||||||
path = arg["--input=".Length..];
|
|
||||||
}
|
|
||||||
else if (arg.StartsWith("--outputCodeDir="))
|
|
||||||
{
|
|
||||||
outputCodeDir = arg["--outputCodeDir=".Length..];
|
|
||||||
|
|
||||||
}
|
Option<DirectoryInfo> outputDataDirOption = new(
|
||||||
else if (arg.StartsWith("--outputDataDir="))
|
name: "--outputDataDir",
|
||||||
{
|
description: "二进制数据输出目录"
|
||||||
outputDataDir = arg["--outputDataDir=".Length..];
|
);
|
||||||
}
|
|
||||||
else if (arg.StartsWith("--namespace="))
|
|
||||||
{
|
|
||||||
nameSpace = arg["--namespace=".Length..];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(outputCodeDir) && string.IsNullOrEmpty(outputDataDir))
|
Option<string> namespaceOption = new(
|
||||||
{
|
name: "--namespace",
|
||||||
outputDataDir = outputCodeDir = path;
|
description: "生成代码的命名空间",
|
||||||
}else if (string.IsNullOrEmpty(outputCodeDir))
|
getDefaultValue: () => ""
|
||||||
{
|
);
|
||||||
outputCodeDir = outputDataDir;
|
|
||||||
}else if (string.IsNullOrEmpty(outputDataDir))
|
RootCommand rootCommand = new();
|
||||||
{
|
rootCommand.AddOption(inputOption);
|
||||||
outputDataDir = outputCodeDir;
|
rootCommand.AddOption(outputCodeDirOption);
|
||||||
}
|
rootCommand.AddOption(outputDataDirOption);
|
||||||
// TODO 使用System.CommandLine重构参数解析
|
rootCommand.AddOption(namespaceOption);
|
||||||
|
|
||||||
|
rootCommand.SetHandler(ExcelProcess.Run, inputOption, outputCodeDirOption, outputDataDirOption, namespaceOption);
|
||||||
|
// TODO 单元测试
|
||||||
|
|
||||||
|
return await rootCommand.InvokeAsync(args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class ExcelProcess
|
||||||
|
{
|
||||||
|
public static void Run(DirectoryInfo inputDir, DirectoryInfo? outputCodeDir, DirectoryInfo? outputDataDir,
|
||||||
|
string nameSpace)
|
||||||
|
{
|
||||||
|
string path = inputDir.FullName;
|
||||||
|
string codeDir = outputCodeDir?.FullName ?? outputDataDir?.FullName ?? path;
|
||||||
|
string dataDir = outputDataDir?.FullName ?? outputCodeDir?.FullName ?? path;
|
||||||
|
|
||||||
DirectoryInfo dirInfo = new(path);
|
DirectoryInfo dirInfo = new(path);
|
||||||
FileInfo[] excels = dirInfo.GetFiles("*.xlsx", SearchOption.AllDirectories);
|
FileInfo[] excels = dirInfo.GetFiles("*.xlsx", SearchOption.AllDirectories);
|
||||||
@@ -56,8 +65,8 @@ namespace ExcelTool
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
"==========================================================".WriteSuccessLine();
|
"==========================================================".WriteSuccessLine();
|
||||||
"== 根据xlsx生成模板代码和二进制文件工具 ==".WriteSuccessLine();
|
"== 根据xlsx生成模板代码和二进制文件工具 ==".WriteSuccessLine();
|
||||||
"== 说明:将exe放在xlsx目录中或者exe或者传入根目录 ==".WriteSuccessLine();
|
"== 说明:将exe放在xlsx目录中或者exe或者传入根目录 ==".WriteSuccessLine();
|
||||||
"==========================================================".WriteSuccessLine();
|
"==========================================================".WriteSuccessLine();
|
||||||
|
|
||||||
excels = dirInfo.GetFiles("*.xlsx", SearchOption.AllDirectories);
|
excels = dirInfo.GetFiles("*.xlsx", SearchOption.AllDirectories);
|
||||||
@@ -70,7 +79,7 @@ namespace ExcelTool
|
|||||||
List<ParsedSheet> sheets = ExcelHelper.ParseAllSheets(file.FullName);
|
List<ParsedSheet> sheets = ExcelHelper.ParseAllSheets(file.FullName);
|
||||||
|
|
||||||
//生成CS文件
|
//生成CS文件
|
||||||
bool res = GenModels.GenCSharpModel(sheets, outputCodeDir, nameSpace);
|
bool res = GenModels.GenCSharpModel(sheets, codeDir, nameSpace);
|
||||||
if (res)
|
if (res)
|
||||||
{
|
{
|
||||||
$"{file.Name}CS模板生成成功".WriteSuccessLine();
|
$"{file.Name}CS模板生成成功".WriteSuccessLine();
|
||||||
@@ -81,7 +90,7 @@ namespace ExcelTool
|
|||||||
}
|
}
|
||||||
|
|
||||||
//生成二进制文件,如果list或者vector数据为空则写入0,要根据类型来读取csv的字段数据强转成对应的数据类型然后写入
|
//生成二进制文件,如果list或者vector数据为空则写入0,要根据类型来读取csv的字段数据强转成对应的数据类型然后写入
|
||||||
res = TableExcelExportBytes.ExportToFile(sheets, outputDataDir);
|
res = TableExcelExportBytes.ExportToFile(sheets, dataDir);
|
||||||
if (res)
|
if (res)
|
||||||
{
|
{
|
||||||
$"{file.Name}二进制数据生成成功".WriteSuccessLine();
|
$"{file.Name}二进制数据生成成功".WriteSuccessLine();
|
||||||
@@ -91,22 +100,6 @@ namespace ExcelTool
|
|||||||
$"{file.Name}二进制数据生成失败".WriteErrorLine();
|
$"{file.Name}二进制数据生成失败".WriteErrorLine();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO 抽象 ExcelProcess 类 处理相关流程
|
|
||||||
|
|
||||||
// TODO 单元测试
|
|
||||||
//IBinarySerializable newavList = new avatarguideTestConfig();
|
|
||||||
//var readOK = FileManager.ReadBinaryDataFromFile(Path.Combine(path, "avatarguideTest.bytes"), ref newavList);
|
|
||||||
//if (readOK)
|
|
||||||
//{
|
|
||||||
// ConsoleHelper.WriteInfoLine(newavList.ToString());
|
|
||||||
// var d = (newavList as avatarguideTestConfig).QueryById(1).ToList();
|
|
||||||
// ConsoleHelper.WriteInfoLine(d[0].gender);
|
|
||||||
//}
|
|
||||||
//else
|
|
||||||
//{
|
|
||||||
// ConsoleHelper.WriteErrorLine("读取失败");
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user