File.vala 4.2 KB

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