FilesAspect.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using Quadarax.Foundation.Core.QConsole.Argument;
  2. using Quadarax.Foundation.Core.QConsole.Command.Base;
  3. using System.IO.Abstractions;
  4. using System.Text;
  5. namespace qdr.app.tools.qfu.Commands.Aspects
  6. {
  7. internal class FilesAspect
  8. {
  9. #region *** Constants ***
  10. private const string ARG_FILES_SEP = "|";
  11. private const string ARG_FILES_NAME = "files";
  12. private const string ARG_FILES_HINT = "files";
  13. private const string ARG_FILES_DESCR = "One or more files separated by character pipe (" + ARG_FILES_SEP + ")";
  14. private const int ARG_SRC_ORD = 1;
  15. public const string ARG_PATHENS_NAME = "pe";
  16. private const string ARG_PATHENS_HINT = "path_ensure";
  17. private const string ARG_PATHENS_DESCR = "Flag if set, then path of the files will be ensured (created if not exists). Otherwise fails.";
  18. #endregion
  19. #region *** Public Operations ***
  20. public static IEnumerable<AbstractArgument> SetupArguments()
  21. {
  22. return
  23. [
  24. new OrdinalArgument(ARG_SRC_ORD, false, ARG_FILES_NAME, ARG_FILES_DESCR, ARG_FILES_HINT,Quadarax.Foundation.Core.QConsole.Value.TypeValuesEnum.String, string.Empty, true),
  25. new FlagArgument(ARG_PATHENS_NAME, ARG_PATHENS_DESCR, ARG_PATHENS_HINT,false)
  26. ];
  27. }
  28. public static bool EnsureFilePath(IFileSystem fs, string filePath)
  29. {
  30. if (string.IsNullOrEmpty(filePath))
  31. throw new ArgumentException("File path cannot be null or empty.", nameof(filePath));
  32. var directory = Path.GetDirectoryName(filePath);
  33. if (string.IsNullOrEmpty(directory) || fs.Directory.Exists(directory))
  34. return true;
  35. fs.Directory.CreateDirectory(directory);
  36. return false;
  37. }
  38. public static string[] GetFiles(AbstractCommand context, IFileSystem fs, bool checkValidity = false)
  39. {
  40. if (context == null)
  41. throw new ArgumentNullException(nameof(context), "Command context cannot be null.");
  42. var filesArgument = context.Arguments.First(x => string.Equals(x.Code, ARG_FILES_NAME));
  43. if (filesArgument.Value.Get() == null)
  44. throw new ArgumentException($"Argument '{ARG_FILES_NAME}' is not set.", ARG_FILES_NAME);
  45. var files = ParseFiles(filesArgument.Value.Get()?.ToString()!);
  46. if (checkValidity)
  47. {
  48. var sb = new StringBuilder();
  49. foreach (var file in files)
  50. {
  51. if (string.IsNullOrEmpty(file))
  52. continue;
  53. if (!fs.File.Exists(file) && !fs.Directory.Exists(file))
  54. sb.AppendLine(file);
  55. }
  56. if (sb.Length > 0)
  57. throw new FileNotFoundException($"The following files do not exist:\n{sb.ToString()}");
  58. }
  59. return files;
  60. }
  61. public static string[] ParseFiles(string filesArgument)
  62. {
  63. if (string.IsNullOrEmpty(filesArgument))
  64. throw new ArgumentException($"Argument '{ARG_FILES_NAME}' is not defined or empty.", ARG_FILES_NAME);
  65. return filesArgument.Split(new[] { ARG_FILES_SEP }, StringSplitOptions.RemoveEmptyEntries)
  66. .Select(f => f.Trim())
  67. .Where(f => !string.IsNullOrEmpty(f))
  68. .ToArray();
  69. }
  70. #endregion
  71. }
  72. }