GeneratorOptions.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System.Collections.Generic;
  2. using System.IO;
  3. namespace Google.ProtocolBuffers.ProtoGen {
  4. /// <summary>
  5. /// All the configuration required for the generator - where to generate
  6. /// output files, the location of input files etc. While this isn't immutable
  7. /// in practice, the contents shouldn't be changed after being passed to
  8. /// the generator.
  9. /// </summary>
  10. public sealed class GeneratorOptions {
  11. public string OutputDirectory { get; set; }
  12. public IList<string> InputFiles { get; set; }
  13. /// <summary>
  14. /// Attempts to validate the options, but doesn't throw an exception if they're invalid.
  15. /// Instead, when this method returns false, the output variable will contain a collection
  16. /// of reasons for the validation failure.
  17. /// </summary>
  18. /// <param name="reasons">Variable to receive a list of reasons in case of validation failure.</param>
  19. /// <returns>true if the options are valid; false otherwise</returns>
  20. public bool TryValidate(out IList<string> reasons) {
  21. List<string> tmpReasons = new List<string>();
  22. // Output directory validation
  23. if (string.IsNullOrEmpty(OutputDirectory)) {
  24. tmpReasons.Add("No output directory specified");
  25. } else {
  26. if (!Directory.Exists(OutputDirectory)) {
  27. tmpReasons.Add("Specified output directory (" + OutputDirectory + " doesn't exist.");
  28. }
  29. }
  30. // Input file validation (just in terms of presence)
  31. if (InputFiles == null || InputFiles.Count == 0) {
  32. tmpReasons.Add("No input files specified");
  33. } else {
  34. foreach (string input in InputFiles) {
  35. FileInfo fi = new FileInfo(input);
  36. if (!fi.Exists) {
  37. tmpReasons.Add("Input file " + input + " doesn't exist.");
  38. }
  39. }
  40. }
  41. if (tmpReasons.Count != 0) {
  42. reasons = tmpReasons;
  43. return false;
  44. }
  45. reasons = null;
  46. return true;
  47. }
  48. /// <summary>
  49. /// Validates that all the options have been set and are valid,
  50. /// throwing an exception if they haven't.
  51. /// </summary>
  52. /// <exception cref="InvalidOptionsException">The options are invalid.</exception>
  53. public void Validate() {
  54. IList<string> reasons;
  55. if (!TryValidate(out reasons)) {
  56. throw new InvalidOptionsException(reasons);
  57. }
  58. }
  59. }
  60. }