static async Task <int> Main(string[] args) { if (args.Length == 0) { args = new string[] { "--help" }; } else if (args.Length == 1 && args[0].Equals("example") == false) // support drag & drop { ToCodeVerb toCode = new ToCodeVerb(); toCode.inputPaths = args; toCode.fileName = Path.GetFileName(args[0]) + "_gen"; var result = await ToCodeVerb.Verb(toCode); return((int)result); } // var commandLineParser = new Parser(settings => settings.CaseSensitive = false); var commandLineParser = Parser.Default; var mainResult = await commandLineParser .ParseArguments <ToCodeVerb, ExampleVerb>(args) .MapResult( (ToCodeVerb toCode) => ToCodeVerb.Verb(toCode), (ExampleVerb example) => ExampleVerb.Verb(example), err => Task.FromResult(ErrorCode.NoVerb)); if (mainResult == ErrorCode.NoVerb) { Console.WriteLine($"wait input for exit.."); Console.ReadKey(); } return((int)mainResult); }
public static async Task <ErrorCode> Verb(ToCodeVerb command) { string[] paths = command.inputPaths .Where(path => command.excludePaths.Any(excludePath => path.StartsWith(excludePath)) == false) .ToArray(); Console.WriteLine($"path: {string.Join(',', paths)}"); var errorPath = paths.Where(path => (File.Exists(path) || Directory.Exists(path)) == false); if (errorPath.Any()) { Console.WriteLine($"not exist file:{string.Join(',', errorPath)}"); return(ErrorCode.Code_Input_Validate); } List <FileInfo> existFiles = new List <FileInfo>(); foreach (string path in paths) { bool isDirectory = Directory.Exists(path); if (isDirectory) { DirectoryInfo directoryInfo = new DirectoryInfo(path); existFiles.AddRange(directoryInfo.GetFiles()); } else { existFiles.Add(new FileInfo(path)); } } var supportedFile = existFiles.Where(fileInfo => _serializer.ContainsKey(fileInfo.Extension.ToLower())); if (supportedFile.Count() == 0) { Console.WriteLine($"not contain supported files.. checked fileList:{string.Join(',', existFiles.Select(fileInfo => fileInfo.FullName))}"); return(ErrorCode.Success); } List <Task <(string path, bool success, ClassFormat[] formats)> > taskList = new List <Task <(string, bool, ClassFormat[])> >(); foreach (FileInfo fileInfo in supportedFile) { var serializer = _serializer[fileInfo.Extension.ToLower()]; StreamReader reader = fileInfo.OpenText(); taskList.Add(reader .ReadToEndAsync() .ContinueWith(task => { reader.Dispose(); bool result = serializer.TryDeserialize(task.Result, out ClassFormat[] formats); return(fileInfo.FullName, result, formats); })); } Task.WaitAll(taskList.ToArray()); ClassFormat[] formats = taskList .Where(task => task.Result.success) .SelectMany(task => task.Result.formats) .ToArray(); CodeGenerator codeGen = new CodeGenerator() .AddImportNameSpace(command.importNameSpaces.ToArray()) .AddClass(formats); codeGen.GenerateCSharpCode(command.OutputFilePath(), command.nameSpace); Console.WriteLine($"has fail path, use '-exclude':{string.Join(',', taskList.Where(task => task.Result.success == false).Select(task => task.Result.path))}"); return(ErrorCode.Success); }