BaseConverter.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using ArcToolkitCLI.Commands.Options;
  6. using ArcToolkitCLI.Util;
  7. using CommandLine;
  8. namespace ArcToolkitCLI.Commands.Converters
  9. {
  10. public class BaseConverter : IInputOptions, IOutputOptions
  11. {
  12. protected Dictionary<string, Func<string, int>> Converters { get; set; }
  13. protected BaseConverter() { }
  14. public int Run()
  15. {
  16. var verb = GetType().GetCustomAttributes(typeof(VerbAttribute), true).Cast<VerbAttribute>().FirstOrDefault();
  17. if(verb == null)
  18. throw new NotImplementedException("The converter is not properly implemented yet, sorry!");
  19. var convertCommand = verb.Name;
  20. var files = Glob.EnumerateFiles(Input).ToArray();
  21. Directory.CreateDirectory(Output);
  22. if (files.Length == 0)
  23. {
  24. Console.WriteLine($"No files specified. Run `convert help {convertCommand}` for help.");
  25. return 0;
  26. }
  27. foreach (var file in files)
  28. {
  29. if (Converters.TryGetValue(file.Extension.ToLowerInvariant(), out var converter))
  30. {
  31. var result = 0;
  32. if ((result = converter(file.FullName)) != 0)
  33. return result;
  34. }
  35. else
  36. Console.WriteLine($"File {file.FullName} is none of the supported file formats ({string.Join(", ", Converters.Keys)}). Skipping...");
  37. }
  38. return 0;
  39. }
  40. [Value(0, HelpText = "Input files to convert")]
  41. public IEnumerable<string> Input { get; set; }
  42. public string Output { get; set; }
  43. }
  44. }