diff --git a/src/ZonyLrcTools.Cli/Commands/ToolCommand.cs b/src/ZonyLrcTools.Cli/Commands/ToolCommand.cs index bc8d318..80ec0ae 100644 --- a/src/ZonyLrcTools.Cli/Commands/ToolCommand.cs +++ b/src/ZonyLrcTools.Cli/Commands/ToolCommand.cs @@ -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 { diff --git a/src/ZonyLrcTools.Cli/Commands/UtilityCommand.cs b/src/ZonyLrcTools.Cli/Commands/UtilityCommand.cs index b4562c0..335b46a 100644 --- a/src/ZonyLrcTools.Cli/Commands/UtilityCommand.cs +++ b/src/ZonyLrcTools.Cli/Commands/UtilityCommand.cs @@ -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 + } + /// /// 工具类相关命令。 /// [Command("util", Description = "提供常用的工具类功能。")] public class UtilityCommand : ToolCommandBase { - protected override Task 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 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; } } } \ No newline at end of file