You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
2.5 KiB

  1. using System;
  2. using System.ComponentModel.DataAnnotations;
  3. using System.IO;
  4. using McMaster.Extensions.CommandLineUtils.Abstractions;
  5. using McMaster.Extensions.CommandLineUtils.Validation;
  6. namespace ICSharpCode.ILSpyCmd
  7. {
  8. [AttributeUsage(AttributeTargets.Class)]
  9. public sealed class ProjectOptionRequiresOutputDirectoryValidationAttribute : ValidationAttribute
  10. {
  11. public ProjectOptionRequiresOutputDirectoryValidationAttribute()
  12. {
  13. }
  14. protected override ValidationResult IsValid(object value, ValidationContext context)
  15. {
  16. if (value is ILSpyCmdProgram obj)
  17. {
  18. if (obj.CreateCompilableProjectFlag && string.IsNullOrEmpty(obj.OutputDirectory))
  19. {
  20. return new ValidationResult("--project cannot be used unless --outputdir is also specified");
  21. }
  22. }
  23. return ValidationResult.Success;
  24. }
  25. }
  26. [AttributeUsage(AttributeTargets.Property)]
  27. public sealed class FileExistsOrNullAttribute : ValidationAttribute
  28. {
  29. protected override ValidationResult IsValid(object value, ValidationContext validationContext)
  30. {
  31. var path = value as string;
  32. if (string.IsNullOrEmpty(path))
  33. {
  34. return ValidationResult.Success;
  35. }
  36. if (!Path.IsPathRooted(path) && validationContext.GetService(typeof(CommandLineContext)) is CommandLineContext context)
  37. {
  38. path = Path.Combine(context.WorkingDirectory, path);
  39. }
  40. if (File.Exists(path))
  41. {
  42. return ValidationResult.Success;
  43. }
  44. return new ValidationResult($"File '{path}' does not exist!");
  45. }
  46. }
  47. [AttributeUsage(AttributeTargets.Property)]
  48. public sealed class FilesExistAttribute : ValidationAttribute
  49. {
  50. protected override ValidationResult IsValid(object value, ValidationContext validationContext)
  51. {
  52. switch (value)
  53. {
  54. case string path:
  55. return ValidatePath(path);
  56. case string[] paths:
  57. {
  58. foreach (string path in paths)
  59. {
  60. ValidationResult result = ValidatePath(path);
  61. if (result != ValidationResult.Success)
  62. return result;
  63. }
  64. return ValidationResult.Success;
  65. }
  66. default:
  67. return new ValidationResult($"File '{value}' does not exist!");
  68. }
  69. ValidationResult ValidatePath(string path)
  70. {
  71. if (!string.IsNullOrWhiteSpace(path))
  72. {
  73. if (!Path.IsPathRooted(path) && validationContext.GetService(typeof(CommandLineContext)) is CommandLineContext context)
  74. {
  75. path = Path.Combine(context.WorkingDirectory, path);
  76. }
  77. if (File.Exists(path))
  78. {
  79. return ValidationResult.Success;
  80. }
  81. }
  82. return new ValidationResult($"File '{path}' does not exist!");
  83. }
  84. }
  85. }
  86. }