feat: Complete the logic of the basic file conversion method.

This commit is contained in:
real-zony 2021-05-30 22:14:45 +08:00
parent 232c36de3b
commit 730d71afc0
2 changed files with 50 additions and 5 deletions

View File

@ -10,13 +10,11 @@ using Serilog;
using Serilog.Events;
using ZonyLrcTools.Cli.Infrastructure.DependencyInject;
using ZonyLrcTools.Cli.Infrastructure.Exceptions;
using ZonyLrcTools.Cli.Infrastructure.Extensions;
namespace ZonyLrcTools.Cli.Commands
{
[Command("lyric-tool")]
[Subcommand(typeof(ScanCommand),
typeof(DownloadCommand),
[Subcommand(typeof(DownloadCommand),
typeof(UtilityCommand))]
public class ToolCommand : ToolCommandBase
{

View File

@ -1,17 +1,64 @@
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using McMaster.Extensions.CommandLineUtils;
using ZonyLrcTools.Cli.Infrastructure.IO;
using ZonyLrcTools.Cli.Infrastructure.Threading;
namespace ZonyLrcTools.Cli.Commands
{
public enum SupportFileType
{
Ncm = 1,
Qcm = 2
}
/// <summary>
/// 工具类相关命令。
/// </summary>
[Command("util", Description = "提供常用的工具类功能。")]
public class UtilityCommand : ToolCommandBase
{
protected override Task<int> OnExecuteAsync(CommandLineApplication app)
[Required(ErrorMessage = "音乐格式为必须参数,请指定 -t 参数。")]
[Option("-t|--type", CommandOptionType.SingleValue, Description = "需要转换的文件格式,参数[Ncm、Qcm]", ShowInHelpText = true)]
public SupportFileType Type { get; set; }
[Required(ErrorMessage = "文件路径为必须按参数,请传入有效路径。")]
[Argument(0, "Path", "指定需要转换的音乐文件路径,支持目录和文件路径。")]
public string Path { get; set; }
private readonly IFileScanner _fileScanner;
public UtilityCommand(IFileScanner fileScanner)
{
return base.OnExecuteAsync(app);
_fileScanner = fileScanner;
}
protected override async Task<int> OnExecuteAsync(CommandLineApplication app)
{
if (Directory.Exists(Path))
{
var files = await _fileScanner.ScanAsync(Path, new[] {"*.ncm"});
var wrapTask = new WarpTask(4);
var tasks = files
.SelectMany(f => f.FilePaths)
.Select(path => wrapTask.RunAsync(() => Convert(path)));
await Task.WhenAll(tasks);
}
else if (File.Exists(Path))
{
await Convert(Path);
}
return 0;
}
private async Task Convert(string filePath)
{
await Task.CompletedTask;
}
}
}