File.vala 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using Invercargill;
  2. using Invercargill.Mapping;
  3. using Invercargill.DataStructures;
  4. namespace Usm {
  5. public class ManifestFile {
  6. public ManifestFilePathBase path_base { get; set; }
  7. public string path { get; set; }
  8. public ManifestFileType file_type { get; set; }
  9. public string? destination { get; set; }
  10. public Vector<RemoveType>? keep_on { get; set; }
  11. public Vector<InstallType>? skip_for { get; set; }
  12. public static PropertyMapper<ManifestFile> get_mapper() {
  13. return PropertyMapper.build_for<ManifestFile>(cfg => {
  14. cfg.map<string>("path", o => o.path, (o, v) => o.path = v); // TODO make nullable in some cases, add validtion (i.e. type != reg, and pathBase == as-expected)
  15. cfg.map<string>("pathBase", o => o.path_base.to_string(), (o, v) => o.path_base = ManifestFilePathBase.from_string(v));
  16. cfg.map<string>("type", o => o.file_type.to_string(), (o, v) => o.file_type = ManifestFileType.from_string(v));
  17. cfg.map<string>("dest", o => o.destination, (o, v) => o.destination = v)
  18. .undefined_when(o => o.destination == null)
  19. .when_undefined(o => o.destination = null);
  20. cfg.map_many<string>("keepOn", o => o.keep_on.select<string>(i => i.to_string()), (o, v) => o.keep_on = v.attempt_select<RemoveType>(i => RemoveType.from_string(i)).to_vector())
  21. .undefined_when(o => o.keep_on == null)
  22. .when_undefined(o => o.keep_on = null);
  23. cfg.map_many<string>("skipFor", o => o.skip_for.select<string>(i => i.to_string()), (o, v) => o.skip_for = v.attempt_select<InstallType>(i => InstallType.from_string(i)).to_vector())
  24. .undefined_when(o => o.skip_for == null)
  25. .when_undefined(o => o.skip_for = null);
  26. cfg.set_constructor(() => new ManifestFile());
  27. });
  28. }
  29. public ManifestFile.from_string(string str) throws ManifestError {
  30. var parts = str.split(":", 2);
  31. path_base = ManifestFilePathBase.from_string(parts[0]);
  32. file_type = ManifestFileType.REGULAR;
  33. if(path_base == ManifestFilePathBase.AS_EXPECTED) {
  34. path = "";
  35. return;
  36. }
  37. if(parts.length != 2) {
  38. throw new ManifestError.INVALID_FILE_PATH("No file path provided");
  39. }
  40. path = parts[1];
  41. }
  42. }
  43. public enum ManifestFileType {
  44. REGULAR,
  45. DIRECTORY,
  46. SYMBOLIC_LINK;
  47. public string to_string() {
  48. switch(this) {
  49. case ManifestFileType.REGULAR:
  50. return "reg";
  51. case ManifestFileType.DIRECTORY:
  52. return "dir";
  53. case ManifestFileType.SYMBOLIC_LINK:
  54. return "lnk";
  55. default:
  56. assert_not_reached();
  57. }
  58. }
  59. public static ManifestFileType from_string(string str) throws ManifestError {
  60. switch(str) {
  61. case "reg":
  62. return ManifestFileType.REGULAR;
  63. case "dir":
  64. return ManifestFileType.DIRECTORY;
  65. case "lnk":
  66. return ManifestFileType.SYMBOLIC_LINK;
  67. default:
  68. throw new ManifestError.INVALID_FILE_TYPE(@"Unknown file type \"$str\".");
  69. }
  70. }
  71. }
  72. public enum ManifestFilePathBase {
  73. SOURCE,
  74. BUILD,
  75. INSTALL,
  76. AS_EXPECTED;
  77. public string to_string() {
  78. switch(this) {
  79. case ManifestFilePathBase.SOURCE:
  80. return "source";
  81. case ManifestFilePathBase.BUILD:
  82. return "build";
  83. case ManifestFilePathBase.INSTALL:
  84. return "install";
  85. case ManifestFilePathBase.AS_EXPECTED:
  86. return "as-expected";
  87. default:
  88. assert_not_reached();
  89. }
  90. }
  91. public static ManifestFilePathBase from_string(string str) throws ManifestError {
  92. switch(str) {
  93. case "source":
  94. return ManifestFilePathBase.SOURCE;
  95. case "build":
  96. return ManifestFilePathBase.BUILD;
  97. case "install":
  98. return ManifestFilePathBase.INSTALL;
  99. case "as-expected":
  100. return ManifestFilePathBase.AS_EXPECTED;
  101. default:
  102. throw new ManifestError.INVALID_PATH_BASE(@"Unknown path base \"$str\".");
  103. }
  104. }
  105. }
  106. }